Package org.jboss.fresh.shell.commands

Source Code of org.jboss.fresh.shell.commands.RunExe

package org.jboss.fresh.shell.commands;


import org.jboss.fresh.io.*;
import org.jboss.fresh.shell.AbstractExecutable;
import org.jboss.fresh.shell.ProcessInfo;
import org.jboss.fresh.shell.ShellException;
import org.jboss.fresh.shell.impl.ShellImpl;
import org.jboss.fresh.vfs.FileName;
import org.jboss.fresh.vfs.impl.SecureVFS;
import org.jboss.fresh.vfs.impl.VFSInputStream;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;


// EX OK


public class RunExe extends AbstractExecutable {

    //private static transient org.jboss.fresh.parsek.logging.Logger log = org.jboss.fresh.parsek.logging.Logger.getLogger(RunExe.class);

    private static org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(RunExe.class);


    /**
     * Runs the specified batch file.
     */


    public void process(String path, String[] params) throws Exception {

        boolean debug = log.isDebugEnabled();

        if (debug) {
            log.debug(exePath + " entered");
        }

        // read first parameter which is filename

        if (helpRequested()) {

            PrintWriter2 out = new PrintWriter2(new BufferWriter(getStdOut()));

            out.println("Usage:  run [--help] <vfs:path-to-batch-file>");

            out.println("      run [--help] < inputstream");
            out.println("      --ex       : (as first param) If exception occurs throw it without dump to stdout");
            out.println("      --out      : Output everything to stdout");
            out.println("      --exout    : If exception occurs - dump it to stdout and rethrow");
            out.println("      --failfast : If exception occurs stop further process (the default is to continue with next command)");
            out.println("    --help     : this help");

            out.close();

            log.debug(exePath + " done");

            return;

        }

        boolean wasactive = false;

        /*if (TxSupport.isActive()) {
              wasactive = true;
              TxSupport.commit();
          }*/

        BufferedReader in = null;

        boolean outThrough = false;
        boolean exDump = !throwsException();
        boolean failfast = false;

        if (params.length > 0) {
            for (int i = 0; i < params.length; i++) {
                String tmp = params[i];

                if (tmp.startsWith("--")) {
                    if ("--out".equals(tmp)) {
                        outThrough = true;
                    } else if ("--exout".equals(tmp)) {
                        exDump = true;
                    } else if ("--failfast".equals(tmp)) {
                        failfast = true;
                    } else {
                        error("Unknown parameter: " + tmp);
                        return;
                    }
                } else {
                    // open file - use VFSInputStream
                    FileName fname = new FileName(tmp);
                    if (fname.isRelative())
                        fname = new FileName(getShell().getEnvProperty("PWD")).absolutize(fname);

                    in = new BufferedReader(new InputStreamReader(new VFSInputStream(
                            new SecureVFS(getShell().getVFS(), getShell().getUserCtx()),
                            fname.toString())));
                    break;
                }
            }
        }

        if (in == null) {
            in = new BufferedReader(new BufferReader(getStdIn()));
        }

        BufferObjectWriter oout = new BufferObjectWriter(getStdOut());
        PrintWriter pwout = new PrintWriter(new BufferWriter(getStdOut()));

        String line = in.readLine();
        while (line != null) {
            String ln = line.trim();
            if (ln.length() != 0 && ln.charAt(0) != '#') {
                if (debug) {
                    log.debug(exePath + " - Executing " + line);
                }

                ShellImpl sh = (ShellImpl) getShell();
                try {
                    ProcessInfo pinfo = sh.execute(line);

                    InBuffer buf = null;
                    try {
                        buf = sh.getBuffer(pinfo.procid, 1);
                    } catch (org.jboss.fresh.shell.NoSuchProcessException ex) {
                        continue;
                    }

                    BufferObjectReader ein = new BufferObjectReader(buf); // get out buffer
                    //ein.setTimeout((int) 120000);

                    Object retObj = null;
                    while (!ein.isFinished()) { // �e ho�emo, da sa izvede do konca moramo prebrati vse  -  dokler se ne zapre.
                        try {
                            retObj = ein.readObject()// read it
                            if (outThrough)
                                oout.writeObject(retObj);
                        } catch (IOException ex) {
                            //log.error(ex.getMessage(), ex);
                            if (exDump) {
                                ex.printStackTrace(pwout);
                                pwout.flush();
                            }

                            if (failfast) {
                                if (canThrowEx()) {
                                    throw ex;
                                }
                                break;
                            }
                        }
                    }

                } catch (ShellException ex) {
                    //log.error(ex.getMessage(), ex);
                    if (canThrowEx()) {
                        throw ex;
                    } else {
                        ex.printStackTrace(pwout);
                        return;
                    }
                } catch (Throwable t) {
                    //log.error(t.getMessage(), t);
                    ShellException ex = new ShellException(t);

                    if (canThrowEx()) {
                        throw ex;
                    } else {
                        ex.printStackTrace(pwout);
                        return;
                    }
                }
            }

            line = in.readLine();
        }

        // read line by line - execute each line
        /*if (wasactive)
            TxSupport.begin();
        */
        if (debug) {
            log.debug(exePath + " done");
        }
    }
}

TOP

Related Classes of org.jboss.fresh.shell.commands.RunExe

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.