Package java.lang

Examples of java.lang.Process


     */
    Runtime rt = null;
    BufferedReader commandsStdOut = null;
    BufferedReader commandsStdErr = null;
    BufferedOutputStream commandsStdIn = null;
    Process proc = null;
    byte[] bBuf = new byte[1024];
    char[] cBuf = new char[1024];
    int bufRead = -1;

    //create query arguments
    Enumeration paramNames = params.keys();
    StringBuffer cmdAndArgs = new StringBuffer(command);
    if (paramNames != null && paramNames.hasMoreElements()) {
        cmdAndArgs.append(" ");
        while (paramNames.hasMoreElements()) {
            String k = (String) paramNames.nextElement();
            String v = params.get(k).toString();
            if ((k.indexOf("=") < 0) && (v.indexOf("=") < 0)) {
                cmdAndArgs.append(k);
                cmdAndArgs.append("=");
                v = java.net.URLEncoder.encode(v);
                cmdAndArgs.append(v);
                cmdAndArgs.append(" ");
            }
        }
    }

    String postIn = getPostInput(params);
    int contentLength = (postIn.length()
            + System.getProperty("line.separator").length());
    if ("POST".equals(env.get("REQUEST_METHOD"))) {
        env.put("CONTENT_LENGTH", Integer.valueOf(contentLength));
    }

    rt = Runtime.getRuntime();
    proc = rt.exec(cmdAndArgs.toString(), hashToStringArray(env), wd);


    /*
     * provide input to cgi
     * First  -- parameters
     * Second -- any remaining input
     */
    commandsStdIn = new BufferedOutputStream(proc.getOutputStream());
    if (debug >= 2 ) {
        log("runCGI stdin=[" + stdin + "], qs="
            + env.get("QUERY_STRING"));
    }
    if ("POST".equals(env.get("REQUEST_METHOD"))) {
        if (debug >= 2) {
            log("runCGI: writing ---------------\n");
            log(postIn);
            log("runCGI: new content_length=" + contentLength
                + "---------------\n");
        }
        commandsStdIn.write(postIn.getBytes());
    }
    if (stdin != null) {
        //REMIND: document this
        /* assume if nothing is available after a time, that nothing is
         * coming...
         */
        if (stdin.available() <= 0) {
            if (debug >= 2 ) {
                log("runCGI stdin is NOT available ["
                    + stdin.available() + "]");
            }
            try {
                Thread.currentThread().sleep(iClientInputTimeout);
            } catch (InterruptedException ignored) {
            }
        }
        if (stdin.available() > 0) {
            if (debug >= 2 ) {
                log("runCGI stdin IS available ["
                    + stdin.available() + "]");
            }
            bBuf = new byte[1024];
            bufRead = -1;
            try {
                while ((bufRead = stdin.read(bBuf)) != -1) {
                    if (debug >= 2 ) {
                        log("runCGI: read [" + bufRead
                            + "] bytes from stdin");
                    }
                    commandsStdIn.write(bBuf, 0, bufRead);
                }
                if (debug >= 2 ) {
                    log("runCGI: DONE READING from stdin");
                }
            } catch (IOException ioe) {
                //REMIND: replace with logging
                //REMIND: should I throw this exception?
                log("runCGI: couldn't write all bytes.");
                ioe.printStackTrace();
            }
        }
    }
    commandsStdIn.flush();
    commandsStdIn.close();

    /* we want to wait for the process to exit,  Process.waitFor()
     * is useless in our situation; see
     * http://developer.java.sun.com/developer/
     *                               bugParade/bugs/4223650.html
     */

    boolean isRunning = true;
    commandsStdOut = new BufferedReader
        (new InputStreamReader(proc.getInputStream()));
    commandsStdErr = new BufferedReader
        (new InputStreamReader(proc.getErrorStream()));
    BufferedWriter servletContainerStdout = null;

    try {
        if (response.getOutputStream() != null) {
            servletContainerStdout =
                new BufferedWriter(new OutputStreamWriter
                    (response.getOutputStream()));
        }
    } catch (IOException ignored) {
        //NOOP: no output will be written
    }

    while (isRunning) {

        try {
            //read stderr first
            cBuf = new char[1024];
            while ((bufRead = commandsStdErr.read(cBuf)) != -1) {
                if (servletContainerStdout != null) {
                    servletContainerStdout.write(cBuf, 0, bufRead);
                }
            }

            //set headers
            String line = null;
            while (((line = commandsStdOut.readLine()) != null)
                   && !("".equals(line))) {
                if (debug >= 2) {
                    log("runCGI: addHeader(\"" + line + "\")");
                }
                if (line.startsWith("HTTP")) {
                    //TODO: should set status codes (NPH support)
                    /*
                     * response.setStatus(getStatusCode(line));
                     */
                } else {
                    response.addHeader
                        (line.substring(0, line.indexOf(":")).trim(),
                         line.substring(line.indexOf(":") + 1).trim());
                }
            }

            //write output
            cBuf = new char[1024];
            while ((bufRead = commandsStdOut.read(cBuf)) != -1) {
                if (servletContainerStdout != null) {
                    if (debug >= 4) {
                        log("runCGI: write(\"" + cBuf + "\")");
                    }
                    servletContainerStdout.write(cBuf, 0, bufRead);
                }
            }

            if (servletContainerStdout != null) {
                servletContainerStdout.flush();
            }

            proc.exitValue(); // Throws exception if alive

            isRunning = false;

        } catch (IllegalThreadStateException e) {
            try {
View Full Code Here


             */
            Runtime rt = null;
            BufferedReader commandsStdOut = null;
            BufferedReader commandsStdErr = null;
            BufferedOutputStream commandsStdIn = null;
            Process proc = null;
            byte[] bBuf = new byte[1024];
            char[] cBuf = new char[1024];
            int bufRead = -1;

            //create query arguments
            Enumeration paramNames = params.keys();
            StringBuffer cmdAndArgs = new StringBuffer(command);
            if (paramNames != null && paramNames.hasMoreElements()) {
                cmdAndArgs.append(" ");
                while (paramNames.hasMoreElements()) {
                    String k = (String) paramNames.nextElement();
                    String v = params.get(k).toString();
                    if ((k.indexOf("=") < 0) && (v.indexOf("=") < 0)) {
                        cmdAndArgs.append(k);
                        cmdAndArgs.append("=");
                        v = java.net.URLEncoder.encode(v);
                        cmdAndArgs.append(v);
                        cmdAndArgs.append(" ");
                    }
                }
            }

            /*String postIn = getPostInput(params);
            int contentLength = (postIn.length()
                    + System.getProperty("line.separator").length());
            if ("POST".equals(env.get("REQUEST_METHOD"))) {
                env.put("CONTENT_LENGTH", new Integer(contentLength));
            }*/

        StringBuffer perlCommand = new StringBuffer("perl ");
        if (command.endsWith(".pl") || command.endsWith(".cgi")) {
            perlCommand.append(cmdAndArgs.toString());
            cmdAndArgs = perlCommand;
        }

            rt = Runtime.getRuntime();
            proc = rt.exec(cmdAndArgs.toString(), hashToStringArray(env), wd);

            /*
             * provide input to cgi
             * First  -- parameters
             * Second -- any remaining input
             */
            /*commandsStdIn = new BufferedOutputStream(proc.getOutputStream());
            if (debug >= 2 ) {
                log("runCGI stdin=[" + stdin + "], qs="
                    + env.get("QUERY_STRING"));
            }
            if ("POST".equals(env.get("REQUEST_METHOD"))) {
                if (debug >= 2) {
                    log("runCGI: writing ---------------\n");
                    log(postIn);
                    log("runCGI: new content_length=" + contentLength
                        + "---------------\n");
                }
                commandsStdIn.write(postIn.getBytes());
            }
            if (stdin != null) {
                //REMIND: document this
                /* assume if nothing is available after a time, that nothing is
                 * coming...
                 */
                /*if (stdin.available() <= 0) {
                    if (debug >= 2 ) {
                        log("runCGI stdin is NOT available ["
                            + stdin.available() + "]");
                    }
                    try {
                        Thread.currentThread().sleep(iClientInputTimeout);
                    } catch (InterruptedException ignored) {
                    }
                }
                if (stdin.available() > 0) {
                    if (debug >= 2 ) {
                        log("runCGI stdin IS available ["
                            + stdin.available() + "]");
                    }
                    bBuf = new byte[1024];
                    bufRead = -1;
                    try {
                        while ((bufRead = stdin.read(bBuf)) != -1) {
                            if (debug >= 2 ) {
                                log("runCGI: read [" + bufRead
                                    + "] bytes from stdin");
                            }
                            commandsStdIn.write(bBuf, 0, bufRead);
                        }
                        if (debug >= 2 ) {
                            log("runCGI: DONE READING from stdin");
                        }
                    } catch (IOException ioe) {
                        //REMIND: replace with logging
                        //REMIND: should I throw this exception?
                        log("runCGI: couldn't write all bytes.");
                        ioe.printStackTrace();
                    }
                }
            }
            commandsStdIn.flush();
            commandsStdIn.close();*/
      String sContentLength = (String) env.get("CONTENT_LENGTH");
      if(!"".equals(sContentLength)) {
          commandsStdIn = new BufferedOutputStream(proc.getOutputStream());
          byte[] content = new byte[Integer.parseInt(sContentLength)];
          stdin.read(content);
          commandsStdIn.write(content);
          commandsStdIn.flush();
          commandsStdIn.close();
      }

            /* we want to wait for the process to exit,  Process.waitFor()
             * is useless in our situation; see
             * http://developer.java.sun.com/developer/
             *                               bugParade/bugs/4223650.html
             */

            boolean isRunning = true;
            commandsStdOut = new BufferedReader
                (new InputStreamReader(proc.getInputStream()));
            commandsStdErr = new BufferedReader
                (new InputStreamReader(proc.getErrorStream()));
            BufferedWriter servletContainerStdout = null;

            try {
                if (response.getOutputStream() != null) {
                    servletContainerStdout =
                        new BufferedWriter(new OutputStreamWriter
                            (response.getOutputStream()));
                }
            } catch (IOException ignored) {
                //NOOP: no output will be written
            }

            while (isRunning) {

                try {
                    //read stderr first
                    cBuf = new char[1024];
                    while ((bufRead = commandsStdErr.read(cBuf)) != -1) {
                        if (servletContainerStdout != null) {
                            servletContainerStdout.write(cBuf, 0, bufRead);
                        }
                    }

                    //set headers
                    String line = null;
                    while (((line = commandsStdOut.readLine()) != null)
                           && !("".equals(line))) {
                        if (debug >= 2) {
                            log("runCGI: addHeader(\"" + line + "\")");
                        }
                        if (line.startsWith("HTTP")) {
                            //TODO: should set status codes (NPH support)
                            /*
                             * response.setStatus(getStatusCode(line));
                             */
                        } else {
                            response.addHeader
                                (line.substring(0, line.indexOf(":")).trim(),
                                 line.substring(line.indexOf(":") + 1).trim());
                        }
                    }

                    //write output
                    cBuf = new char[1024];
                    while ((bufRead = commandsStdOut.read(cBuf)) != -1) {
                        if (servletContainerStdout != null) {
                            if (debug >= 4) {
                                log("runCGI: write(\"" + cBuf + "\")");
                            }
                            servletContainerStdout.write(cBuf, 0, bufRead);
                        }
                    }

                    if (servletContainerStdout != null) {
                        servletContainerStdout.flush();
                    }

                    proc.exitValue(); // Throws exception if alive

                    isRunning = false;

                } catch (IllegalThreadStateException e) {
                    try {
View Full Code Here

             */
            Runtime rt = null;
            BufferedReader commandsStdOut = null;
            BufferedReader commandsStdErr = null;
            BufferedOutputStream commandsStdIn = null;
            Process proc = null;
            byte[] bBuf = new byte[1024];
            char[] cBuf = new char[1024];
            int bufRead = -1;

            //create query arguments
            Enumeration paramNames = params.keys();
            StringBuffer cmdAndArgs = new StringBuffer(command);
            if (paramNames != null && paramNames.hasMoreElements()) {
                cmdAndArgs.append(" ");
                while (paramNames.hasMoreElements()) {
                    String k = (String) paramNames.nextElement();
                    String v = params.get(k).toString();
                    if ((k.indexOf("=") < 0) && (v.indexOf("=") < 0)) {
                        cmdAndArgs.append(k);
                        cmdAndArgs.append("=");
                        v = java.net.URLEncoder.encode(v);
                        cmdAndArgs.append(v);
                        cmdAndArgs.append(" ");
                    }
                }
            }

            /*String postIn = getPostInput(params);
            int contentLength = (postIn.length()
                    + System.getProperty("line.separator").length());
            if ("POST".equals(env.get("REQUEST_METHOD"))) {
                env.put("CONTENT_LENGTH", new Integer(contentLength));
            }*/

        StringBuffer perlCommand = new StringBuffer("perl ");
        if (command.endsWith(".pl") || command.endsWith(".cgi")) {
            perlCommand.append(cmdAndArgs.toString());
            cmdAndArgs = perlCommand;
        }

            rt = Runtime.getRuntime();
            proc = rt.exec(cmdAndArgs.toString(), hashToStringArray(env), wd);

            /*
             * provide input to cgi
             * First  -- parameters
             * Second -- any remaining input
             */
            /*commandsStdIn = new BufferedOutputStream(proc.getOutputStream());
            if (debug >= 2 ) {
                log("runCGI stdin=[" + stdin + "], qs="
                    + env.get("QUERY_STRING"));
            }
            if ("POST".equals(env.get("REQUEST_METHOD"))) {
                if (debug >= 2) {
                    log("runCGI: writing ---------------\n");
                    log(postIn);
                    log("runCGI: new content_length=" + contentLength
                        + "---------------\n");
                }
                commandsStdIn.write(postIn.getBytes());
            }
            if (stdin != null) {
                //REMIND: document this
                /* assume if nothing is available after a time, that nothing is
                 * coming...
                 */
                /*if (stdin.available() <= 0) {
                    if (debug >= 2 ) {
                        log("runCGI stdin is NOT available ["
                            + stdin.available() + "]");
                    }
                    try {
                        Thread.currentThread().sleep(iClientInputTimeout);
                    } catch (InterruptedException ignored) {
                    }
                }
                if (stdin.available() > 0) {
                    if (debug >= 2 ) {
                        log("runCGI stdin IS available ["
                            + stdin.available() + "]");
                    }
                    bBuf = new byte[1024];
                    bufRead = -1;
                    try {
                        while ((bufRead = stdin.read(bBuf)) != -1) {
                            if (debug >= 2 ) {
                                log("runCGI: read [" + bufRead
                                    + "] bytes from stdin");
                            }
                            commandsStdIn.write(bBuf, 0, bufRead);
                        }
                        if (debug >= 2 ) {
                            log("runCGI: DONE READING from stdin");
                        }
                    } catch (IOException ioe) {
                        //REMIND: replace with logging
                        //REMIND: should I throw this exception?
                        log("runCGI: couldn't write all bytes.");
                        ioe.printStackTrace();
                    }
                }
            }
            commandsStdIn.flush();
            commandsStdIn.close();*/
      String sContentLength = (String) env.get("CONTENT_LENGTH");
      if(!"".equals(sContentLength)) {
          commandsStdIn = new BufferedOutputStream(proc.getOutputStream());
          byte[] content = new byte[Integer.parseInt(sContentLength)];
          stdin.read(content);
          commandsStdIn.write(content);
          commandsStdIn.flush();
          commandsStdIn.close();
      }

            /* we want to wait for the process to exit,  Process.waitFor()
             * is useless in our situation; see
             * http://developer.java.sun.com/developer/
             *                               bugParade/bugs/4223650.html
             */

            boolean isRunning = true;
            commandsStdOut = new BufferedReader
                (new InputStreamReader(proc.getInputStream()));
            commandsStdErr = new BufferedReader
                (new InputStreamReader(proc.getErrorStream()));
            BufferedWriter servletContainerStdout = null;

            try {
                if (response.getOutputStream() != null) {
                    servletContainerStdout =
                        new BufferedWriter(new OutputStreamWriter
                            (response.getOutputStream()));
                }
            } catch (IOException ignored) {
                //NOOP: no output will be written
            }

            while (isRunning) {

                try {
                    //read stderr first
                    cBuf = new char[1024];
                    while ((bufRead = commandsStdErr.read(cBuf)) != -1) {
                        if (servletContainerStdout != null) {
                            servletContainerStdout.write(cBuf, 0, bufRead);
                        }
                    }

                    //set headers
                    String line = null;
                    while (((line = commandsStdOut.readLine()) != null)
                           && !("".equals(line))) {
                        if (debug >= 2) {
                            log("runCGI: addHeader(\"" + line + "\")");
                        }
                        if (line.startsWith("HTTP")) {
                            //TODO: should set status codes (NPH support)
                            /*
                             * response.setStatus(getStatusCode(line));
                             */
                        } else {
                            response.addHeader
                                (line.substring(0, line.indexOf(":")).trim(),
                                 line.substring(line.indexOf(":") + 1).trim());
                        }
                    }

                    //write output
                    cBuf = new char[1024];
                    while ((bufRead = commandsStdOut.read(cBuf)) != -1) {
                        if (servletContainerStdout != null) {
                            if (debug >= 4) {
                                log("runCGI: write(\"" + cBuf + "\")");
                            }
                            servletContainerStdout.write(cBuf, 0, bufRead);
                        }
                    }

                    if (servletContainerStdout != null) {
                        servletContainerStdout.flush();
                    }

                    proc.exitValue(); // Throws exception if alive

                    isRunning = false;

                } catch (IllegalThreadStateException e) {
                    try {
View Full Code Here

             */
            Runtime rt = null;
            BufferedReader commandsStdOut = null;
            BufferedReader commandsStdErr = null;
            BufferedOutputStream commandsStdIn = null;
            Process proc = null;
            int bufRead = -1;

            //create query arguments
            Enumeration paramNames = params.keys();
            StringBuffer cmdAndArgs = new StringBuffer(command);
            if (paramNames != null && paramNames.hasMoreElements()) {
                cmdAndArgs.append(" ");
                while (paramNames.hasMoreElements()) {
                    String k = (String) paramNames.nextElement();
                    String v = params.get(k).toString();
                    if ((k.indexOf("=") < 0) && (v.indexOf("=") < 0)) {
                        cmdAndArgs.append(k);
                        cmdAndArgs.append("=");
                        v = java.net.URLEncoder.encode(v);
                        cmdAndArgs.append(v);
                        cmdAndArgs.append(" ");
                    }
                }
            }

            /*String postIn = getPostInput(params);
            int contentLength = (postIn.length()
                    + System.getProperty("line.separator").length());
            if ("POST".equals(env.get("REQUEST_METHOD"))) {
                env.put("CONTENT_LENGTH", new Integer(contentLength));
            }*/

        if (command.endsWith(".pl") || command.endsWith(".cgi")) {
            StringBuffer perlCommand = new StringBuffer("perl ");
            perlCommand.append(cmdAndArgs.toString());
            cmdAndArgs = perlCommand;
        }

            rt = Runtime.getRuntime();
            proc = rt.exec(cmdAndArgs.toString(), hashToStringArray(env), wd);

            /*
             * provide input to cgi
             * First  -- parameters
             * Second -- any remaining input
             */
            /*commandsStdIn = new BufferedOutputStream(proc.getOutputStream());
            if (debug >= 2 ) {
                log("runCGI stdin=[" + stdin + "], qs="
                    + env.get("QUERY_STRING"));
            }
            if ("POST".equals(env.get("REQUEST_METHOD"))) {
                if (debug >= 2) {
                    log("runCGI: writing ---------------\n");
                    log(postIn);
                    log("runCGI: new content_length=" + contentLength
                        + "---------------\n");
                }
                commandsStdIn.write(postIn.getBytes());
            }
            if (stdin != null) {
                //REMIND: document this
                /* assume if nothing is available after a time, that nothing is
                 * coming...
                 */
                /*if (stdin.available() <= 0) {
                    if (debug >= 2 ) {
                        log("runCGI stdin is NOT available ["
                            + stdin.available() + "]");
                    }
                    try {
                        Thread.currentThread().sleep(iClientInputTimeout);
                    } catch (InterruptedException ignored) {
                    }
                }
                if (stdin.available() > 0) {
                    if (debug >= 2 ) {
                        log("runCGI stdin IS available ["
                            + stdin.available() + "]");
                    }
                    byte[] bBuf = new byte[1024];
                    bufRead = -1;
                    try {
                        while ((bufRead = stdin.read(bBuf)) != -1) {
                            if (debug >= 2 ) {
                                log("runCGI: read [" + bufRead
                                    + "] bytes from stdin");
                            }
                            commandsStdIn.write(bBuf, 0, bufRead);
                        }
                        if (debug >= 2 ) {
                            log("runCGI: DONE READING from stdin");
                        }
                    } catch (IOException ioe) {
                        //REMIND: replace with logging
                        //REMIND: should I throw this exception?
                        log("runCGI: couldn't write all bytes.");
                        ioe.printStackTrace();
                    }
                }
            }
            commandsStdIn.flush();
            commandsStdIn.close();*/
      String sContentLength = (String) env.get("CONTENT_LENGTH");
      if(!"".equals(sContentLength)) {
          commandsStdIn = new BufferedOutputStream(proc.getOutputStream());
          byte[] content = new byte[Integer.parseInt(sContentLength)];

          int lenRead = stdin.read(content);

          if ("POST".equals(env.get("REQUEST_METHOD"))) {
              String paramStr = getPostInput(params);
              if (paramStr != null) {
                  byte[] paramBytes = paramStr.getBytes();
                  commandsStdIn.write(paramBytes);

                  int contentLength = paramBytes.length;
                  if (lenRead > 0) {
                      String lineSep = System.getProperty("line.separator");

                      commandsStdIn.write(lineSep.getBytes());

                      contentLength = lineSep.length() + lenRead;
                  }

                  env.put("CONTENT_LENGTH", new Integer(contentLength));
              }
          }

          if (lenRead > 0) {
              commandsStdIn.write(content, 0, lenRead);
          }


          commandsStdIn.flush();
          commandsStdIn.close();
      }

            /* we want to wait for the process to exit,  Process.waitFor()
             * is useless in our situation; see
             * http://developer.java.sun.com/developer/
             *                               bugParade/bugs/4223650.html
             */

            boolean isRunning = true;
            commandsStdOut = new BufferedReader
                (new InputStreamReader(proc.getInputStream()));
            commandsStdErr = new BufferedReader
                (new InputStreamReader(proc.getErrorStream()));
            BufferedWriter servletContainerStdout = null;

            try {
                if (response.getOutputStream() != null) {
                    servletContainerStdout =
                        new BufferedWriter(new OutputStreamWriter
                            (response.getOutputStream()));
                }
            } catch (IOException ignored) {
                //NOOP: no output will be written
            }
            final BufferedReader stdErrRdr = commandsStdErr ;

            new Thread() {
                public void run () {
                    sendToLog(stdErrRdr) ;
                } ;
            }.start() ;


            while (isRunning) {

                try {

                    //set headers
                    String line = null;
                    while (((line = commandsStdOut.readLine()) != null)
                           && !("".equals(line))) {
                        if (debug >= 2) {
                            log("runCGI: addHeader(\"" + line + "\")");
                        }
                        if (line.startsWith("HTTP")) {
                            //TODO: should set status codes (NPH support)
                            /*
                             * response.setStatus(getStatusCode(line));
                             */
                        } else if (line.indexOf(":") >= 0) {
                            response.addHeader
                                (line.substring(0, line.indexOf(":")).trim(),
                                line.substring(line.indexOf(":") + 1).trim());
                        } else {
                            log("runCGI: bad header line \"" + line + "\"");
                        }
                    }

                    //write output
                    char[] cBuf = new char[1024];
                    while ((bufRead = commandsStdOut.read(cBuf)) != -1) {
                        if (servletContainerStdout != null) {
                            if (debug >= 4) {
                                log("runCGI: write(\"" + new String(cBuf, 0, bufRead) + "\")");
                            }
                            servletContainerStdout.write(cBuf, 0, bufRead);
                        }
                    }

                    if (servletContainerStdout != null) {
                        servletContainerStdout.flush();
                    }

                    proc.exitValue(); // Throws exception if alive

                    isRunning = false;

                } catch (IllegalThreadStateException e) {
                    try {
View Full Code Here

      ieStr = "\"c:\\program files\\internet explorer\\iexplore.exe\" http://www.google.com";
    } else {
      System.out.println("Executing Firefox using string");
      ieStr = "firefox http://www.google.com";
    }
    Process ie = runtime.exec(ieStr);
   
    if(System.getProperty("os.name").equals("windows")){
      System.out.println("Executing firefox");
      firefox[0] = "c:\\program files\\mozilla firefox\\firefox.exe";
      firefox[1] = "http://www.google.com";
    } else {
      System.out.println("Executing Firefox using array");
      firefox[0] = "firefox";
      firefox[1] = "http://www.google.com";
    }
    Process ff = runtime.exec(firefox);

    boolean ffSuccess = false;
    boolean ieSuccess = false;
    while(!(ieSuccess && ffSuccess)){
      if(!ffSuccess){
        try{
          System.out.println("Exit value from string exec: " + ff.exitValue());
          ffSuccess = true;
        } catch(IllegalThreadStateException e) {}
      }
      if(!ieSuccess){
        try{
          System.out.println("Exit value from array exec: " + ie.exitValue());
          ieSuccess = true;
        } catch(IllegalThreadStateException e) {}
      }
    }
    if(System.getProperty("os.name").equals("windows")){
      System.out.println("Executing and waiting for charmap");
      charmapStr = "c:\\windows\\system32\\charmap.exe";
    } else {
      System.out.println("Executing and waiting for firefox");
      charmapStr = "firefox http://www.google.com";
    }
    Process cm = runtime.exec(charmapStr);
    System.out.println("Exit value: " + cm.waitFor());
  }
View Full Code Here

             */
            Runtime rt = null;
            BufferedReader commandsStdOut = null;
            BufferedReader commandsStdErr = null;
            BufferedOutputStream commandsStdIn = null;
            Process proc = null;
            byte[] bBuf = new byte[1024];
            char[] cBuf = new char[1024];
            int bufRead = -1;

            //create query arguments
            Enumeration paramNames = params.keys();
            StringBuffer cmdAndArgs = new StringBuffer(command);
            if (paramNames != null && paramNames.hasMoreElements()) {
                cmdAndArgs.append(" ");
                while (paramNames.hasMoreElements()) {
                    String k = (String) paramNames.nextElement();
                    String v = params.get(k).toString();
                    if ((k.indexOf("=") < 0) && (v.indexOf("=") < 0)) {
                        cmdAndArgs.append(k);
                        cmdAndArgs.append("=");
                        v = java.net.URLEncoder.encode(v);
                        cmdAndArgs.append(v);
                        cmdAndArgs.append(" ");
                    }
                }
            }

            /*String postIn = getPostInput(params);
            int contentLength = (postIn.length()
                    + System.getProperty("line.separator").length());
            if ("POST".equals(env.get("REQUEST_METHOD"))) {
                env.put("CONTENT_LENGTH", new Integer(contentLength));
            }*/

        StringBuffer perlCommand = new StringBuffer("perl ");
        if (command.endsWith(".pl") || command.endsWith(".cgi")) {
            perlCommand.append(cmdAndArgs.toString());
            cmdAndArgs = perlCommand;
        }

            rt = Runtime.getRuntime();
            proc = rt.exec(cmdAndArgs.toString(), hashToStringArray(env), wd);

            /*
             * provide input to cgi
             * First  -- parameters
             * Second -- any remaining input
             */
            /*commandsStdIn = new BufferedOutputStream(proc.getOutputStream());
            if (debug >= 2 ) {
                log("runCGI stdin=[" + stdin + "], qs="
                    + env.get("QUERY_STRING"));
            }
            if ("POST".equals(env.get("REQUEST_METHOD"))) {
                if (debug >= 2) {
                    log("runCGI: writing ---------------\n");
                    log(postIn);
                    log("runCGI: new content_length=" + contentLength
                        + "---------------\n");
                }
                commandsStdIn.write(postIn.getBytes());
            }
            if (stdin != null) {
                //REMIND: document this
                /* assume if nothing is available after a time, that nothing is
                 * coming...
                 */
                /*if (stdin.available() <= 0) {
                    if (debug >= 2 ) {
                        log("runCGI stdin is NOT available ["
                            + stdin.available() + "]");
                    }
                    try {
                        Thread.currentThread().sleep(iClientInputTimeout);
                    } catch (InterruptedException ignored) {
                    }
                }
                if (stdin.available() > 0) {
                    if (debug >= 2 ) {
                        log("runCGI stdin IS available ["
                            + stdin.available() + "]");
                    }
                    bBuf = new byte[1024];
                    bufRead = -1;
                    try {
                        while ((bufRead = stdin.read(bBuf)) != -1) {
                            if (debug >= 2 ) {
                                log("runCGI: read [" + bufRead
                                    + "] bytes from stdin");
                            }
                            commandsStdIn.write(bBuf, 0, bufRead);
                        }
                        if (debug >= 2 ) {
                            log("runCGI: DONE READING from stdin");
                        }
                    } catch (IOException ioe) {
                        //REMIND: replace with logging
                        //REMIND: should I throw this exception?
                        log("runCGI: couldn't write all bytes.");
                        ioe.printStackTrace();
                    }
                }
            }
            commandsStdIn.flush();
            commandsStdIn.close();*/
      String sContentLength = (String) env.get("CONTENT_LENGTH");
      if(!"".equals(sContentLength)) {
          commandsStdIn = new BufferedOutputStream(proc.getOutputStream());
          byte[] content = new byte[Integer.parseInt(sContentLength)];
          stdin.read(content);
          commandsStdIn.write(content);
          commandsStdIn.flush();
          commandsStdIn.close();
      }

            /* we want to wait for the process to exit,  Process.waitFor()
             * is useless in our situation; see
             * http://developer.java.sun.com/developer/
             *                               bugParade/bugs/4223650.html
             */

            boolean isRunning = true;
            commandsStdOut = new BufferedReader
                (new InputStreamReader(proc.getInputStream()));
            commandsStdErr = new BufferedReader
                (new InputStreamReader(proc.getErrorStream()));
            BufferedWriter servletContainerStdout = null;

            try {
                if (response.getOutputStream() != null) {
                    servletContainerStdout =
                        new BufferedWriter(new OutputStreamWriter
                            (response.getOutputStream()));
                }
            } catch (IOException ignored) {
                //NOOP: no output will be written
            }

            while (isRunning) {

                try {
                    //read stderr first
                    cBuf = new char[1024];
                    while ((bufRead = commandsStdErr.read(cBuf)) != -1) {
                        if (servletContainerStdout != null) {
                            servletContainerStdout.write(cBuf, 0, bufRead);
                        }
                    }

                    //set headers
                    String line = null;
                    while (((line = commandsStdOut.readLine()) != null)
                           && !("".equals(line))) {
                        if (debug >= 2) {
                            log("runCGI: addHeader(\"" + line + "\")");
                        }
                        if (line.startsWith("HTTP")) {
                            //TODO: should set status codes (NPH support)
                            /*
                             * response.setStatus(getStatusCode(line));
                             */
                        } else {
                            response.addHeader
                                (line.substring(0, line.indexOf(":")).trim(),
                                 line.substring(line.indexOf(":") + 1).trim());
                        }
                    }

                    //write output
                    cBuf = new char[1024];
                    while ((bufRead = commandsStdOut.read(cBuf)) != -1) {
                        if (servletContainerStdout != null) {
                            if (debug >= 4) {
                                log("runCGI: write(\"" + cBuf + "\")");
                            }
                            servletContainerStdout.write(cBuf, 0, bufRead);
                        }
                    }

                    if (servletContainerStdout != null) {
                        servletContainerStdout.flush();
                    }

                    proc.exitValue(); // Throws exception if alive

                    isRunning = false;

                } catch (IllegalThreadStateException e) {
                    try {
View Full Code Here

            }
        }

        // no synchronization to avoid delay wrt getInstantiator
        void shutdownFast() {
            Process p = child;
            if (p != null) {
                p.destroy();
            }
        }
View Full Code Here

        for (int i=0; i < args.length; i++) {
            cmd.addElement(args[i]);
        }
        String command = concatenate((String[]) cmd.toArray(new String[0]));

        Process pr = null;

        try
        {
            pr = Runtime.getRuntime().exec(command);
            BackgroundStreamSaver saver =
                        new BackgroundStreamSaver(pr.getInputStream(), System.out);
            saver.finish();
            pr.waitFor();
            pr.destroy();
        } catch(Throwable t) {
            System.out.println("Process exception: " + t.getMessage());
            if (pr != null)
            {
                pr.destroy();
                pr = null;
            }
        }
    }
View Full Code Here

             */
            Runtime rt = null;
            BufferedReader commandsStdOut = null;
            BufferedReader commandsStdErr = null;
            BufferedOutputStream commandsStdIn = null;
            Process proc = null;
            byte[] bBuf = new byte[1024];
            char[] cBuf = new char[1024];
            int bufRead = -1;

            //create query arguments
            Enumeration paramNames = params.keys();
            StringBuffer cmdAndArgs = new StringBuffer(command);
            if (paramNames != null && paramNames.hasMoreElements()) {
                cmdAndArgs.append(" ");
                while (paramNames.hasMoreElements()) {
                    String k = (String) paramNames.nextElement();
                    String v = params.get(k).toString();
                    if ((k.indexOf("=") < 0) && (v.indexOf("=") < 0)) {
                        cmdAndArgs.append(k);
                        cmdAndArgs.append("=");
                        v = java.net.URLEncoder.encode(v);
                        cmdAndArgs.append(v);
                        cmdAndArgs.append(" ");
                    }
                }
            }

            /*String postIn = getPostInput(params);
            int contentLength = (postIn.length()
                    + System.getProperty("line.separator").length());
            if ("POST".equals(env.get("REQUEST_METHOD"))) {
                env.put("CONTENT_LENGTH", new Integer(contentLength));
            }*/

        StringBuffer perlCommand = new StringBuffer("perl ");
        if (command.endsWith(".pl") || command.endsWith(".cgi")) {
            perlCommand.append(cmdAndArgs.toString());
            cmdAndArgs = perlCommand;
        }

            rt = Runtime.getRuntime();
            proc = rt.exec(cmdAndArgs.toString(), hashToStringArray(env), wd);

            /*
             * provide input to cgi
             * First  -- parameters
             * Second -- any remaining input
             */
            /*commandsStdIn = new BufferedOutputStream(proc.getOutputStream());
            if (debug >= 2 ) {
                log("runCGI stdin=[" + stdin + "], qs="
                    + env.get("QUERY_STRING"));
            }
            if ("POST".equals(env.get("REQUEST_METHOD"))) {
                if (debug >= 2) {
                    log("runCGI: writing ---------------\n");
                    log(postIn);
                    log("runCGI: new content_length=" + contentLength
                        + "---------------\n");
                }
                commandsStdIn.write(postIn.getBytes());
            }
            if (stdin != null) {
                //REMIND: document this
                /* assume if nothing is available after a time, that nothing is
                 * coming...
                 */
                /*if (stdin.available() <= 0) {
                    if (debug >= 2 ) {
                        log("runCGI stdin is NOT available ["
                            + stdin.available() + "]");
                    }
                    try {
                        Thread.currentThread().sleep(iClientInputTimeout);
                    } catch (InterruptedException ignored) {
                    }
                }
                if (stdin.available() > 0) {
                    if (debug >= 2 ) {
                        log("runCGI stdin IS available ["
                            + stdin.available() + "]");
                    }
                    bBuf = new byte[1024];
                    bufRead = -1;
                    try {
                        while ((bufRead = stdin.read(bBuf)) != -1) {
                            if (debug >= 2 ) {
                                log("runCGI: read [" + bufRead
                                    + "] bytes from stdin");
                            }
                            commandsStdIn.write(bBuf, 0, bufRead);
                        }
                        if (debug >= 2 ) {
                            log("runCGI: DONE READING from stdin");
                        }
                    } catch (IOException ioe) {
                        //REMIND: replace with logging
                        //REMIND: should I throw this exception?
                        log("runCGI: couldn't write all bytes.");
                        ioe.printStackTrace();
                    }
                }
            }
            commandsStdIn.flush();
            commandsStdIn.close();*/
      String sContentLength = (String) env.get("CONTENT_LENGTH");
      if(!"".equals(sContentLength)) {
          commandsStdIn = new BufferedOutputStream(proc.getOutputStream());
          byte[] content = new byte[Integer.parseInt(sContentLength)];
          stdin.read(content);
          commandsStdIn.write(content);
          commandsStdIn.flush();
          commandsStdIn.close();
      }

            /* we want to wait for the process to exit,  Process.waitFor()
             * is useless in our situation; see
             * http://developer.java.sun.com/developer/
             *                               bugParade/bugs/4223650.html
             */

            boolean isRunning = true;
            commandsStdOut = new BufferedReader
                (new InputStreamReader(proc.getInputStream()));
            commandsStdErr = new BufferedReader
                (new InputStreamReader(proc.getErrorStream()));
            BufferedWriter servletContainerStdout = null;

            try {
                if (response.getOutputStream() != null) {
                    servletContainerStdout =
                        new BufferedWriter(new OutputStreamWriter
                            (response.getOutputStream()));
                }
            } catch (IOException ignored) {
                //NOOP: no output will be written
            }

            while (isRunning) {

                try {
                    //read stderr first
                    cBuf = new char[1024];
                    while ((bufRead = commandsStdErr.read(cBuf)) != -1) {
                        if (servletContainerStdout != null) {
                            servletContainerStdout.write(cBuf, 0, bufRead);
                        }
                    }

                    //set headers
                    String line = null;
                    while (((line = commandsStdOut.readLine()) != null)
                           && !("".equals(line))) {
                        if (debug >= 2) {
                            log("runCGI: addHeader(\"" + line + "\")");
                        }
                        if (line.startsWith("HTTP")) {
                            //TODO: should set status codes (NPH support)
                            /*
                             * response.setStatus(getStatusCode(line));
                             */
                        } else {
                            response.addHeader
                                (line.substring(0, line.indexOf(":")).trim(),
                                 line.substring(line.indexOf(":") + 1).trim());
                        }
                    }

                    //write output
                    cBuf = new char[1024];
                    while ((bufRead = commandsStdOut.read(cBuf)) != -1) {
                        if (servletContainerStdout != null) {
                            if (debug >= 4) {
                                log("runCGI: write(\"" + cBuf + "\")");
                            }
                            servletContainerStdout.write(cBuf, 0, bufRead);
                        }
                    }

                    if (servletContainerStdout != null) {
                        servletContainerStdout.flush();
                    }

                    proc.exitValue(); // Throws exception if alive

                    isRunning = false;

                } catch (IllegalThreadStateException e) {
                    try {
View Full Code Here

     */
    Runtime rt = null;
    BufferedReader commandsStdOut = null;
    BufferedReader commandsStdErr = null;
    BufferedOutputStream commandsStdIn = null;
    Process proc = null;
    byte[] bBuf = new byte[1024];
    char[] cBuf = new char[1024];
    int bufRead = -1;

    //create query arguments
    Enumeration paramNames = params.keys();
    StringBuffer cmdAndArgs = new StringBuffer(command);
    if (paramNames != null && paramNames.hasMoreElements()) {
        cmdAndArgs.append(" ");
        while (paramNames.hasMoreElements()) {
            String k = (String) paramNames.nextElement();
            String v = params.get(k).toString();
            if ((k.indexOf("=") < 0) && (v.indexOf("=") < 0)) {
                cmdAndArgs.append(k);
                cmdAndArgs.append("=");
                v = java.net.URLEncoder.encode(v);
                cmdAndArgs.append(v);
                cmdAndArgs.append(" ");
            }
        }
    }

    String postIn = getPostInput(params);
    int contentLength = (postIn.length()
            + System.getProperty("line.separator").length());
    if ("POST".equals(env.get("REQUEST_METHOD"))) {
        env.put("CONTENT_LENGTH", new Integer(contentLength));
    }

    rt = Runtime.getRuntime();
    proc = rt.exec(cmdAndArgs.toString(), hashToStringArray(env), wd);


    /*
     * provide input to cgi
     * First  -- parameters
     * Second -- any remaining input
     */
    commandsStdIn = new BufferedOutputStream(proc.getOutputStream());
    if (debug >= 2 ) {
        log("runCGI stdin=[" + stdin + "], qs="
            + env.get("QUERY_STRING"));
    }
    if ("POST".equals(env.get("REQUEST_METHOD"))) {
        if (debug >= 2) {
            log("runCGI: writing ---------------\n");
            log(postIn);
            log("runCGI: new content_length=" + contentLength
                + "---------------\n");
        }
        commandsStdIn.write(postIn.getBytes());
    }
    if (stdin != null) {
        //REMIND: document this
        /* assume if nothing is available after a time, that nothing is
         * coming...
         */
        if (stdin.available() <= 0) {
            if (debug >= 2 ) {
                log("runCGI stdin is NOT available ["
                    + stdin.available() + "]");
            }
            try {
                Thread.currentThread().sleep(iClientInputTimeout);
            } catch (InterruptedException ignored) {
            }
        }
        if (stdin.available() > 0) {
            if (debug >= 2 ) {
                log("runCGI stdin IS available ["
                    + stdin.available() + "]");
            }
            bBuf = new byte[1024];
            bufRead = -1;
            try {
                while ((bufRead = stdin.read(bBuf)) != -1) {
                    if (debug >= 2 ) {
                        log("runCGI: read [" + bufRead
                            + "] bytes from stdin");
                    }
                    commandsStdIn.write(bBuf, 0, bufRead);
                }
                if (debug >= 2 ) {
                    log("runCGI: DONE READING from stdin");
                }
            } catch (IOException ioe) {
                //REMIND: replace with logging
                //REMIND: should I throw this exception?
                log("runCGI: couldn't write all bytes.");
                ioe.printStackTrace();
            }
        }
    }
    commandsStdIn.flush();
    commandsStdIn.close();

    /* we want to wait for the process to exit,  Process.waitFor()
     * is useless in our situation; see
     * http://developer.java.sun.com/developer/
     *                               bugParade/bugs/4223650.html
     */

    boolean isRunning = true;
    commandsStdOut = new BufferedReader
        (new InputStreamReader(proc.getInputStream()));
    commandsStdErr = new BufferedReader
        (new InputStreamReader(proc.getErrorStream()));
    BufferedWriter servletContainerStdout = null;

    try {
        if (response.getOutputStream() != null) {
            servletContainerStdout =
                new BufferedWriter(new OutputStreamWriter
                    (response.getOutputStream()));
        }
    } catch (IOException ignored) {
        //NOOP: no output will be written
    }

    while (isRunning) {

        try {
            //read stderr first
            cBuf = new char[1024];
            while ((bufRead = commandsStdErr.read(cBuf)) != -1) {
                if (servletContainerStdout != null) {
                    servletContainerStdout.write(cBuf, 0, bufRead);
                }
            }

            //set headers
            String line = null;
            while (((line = commandsStdOut.readLine()) != null)
                   && !("".equals(line))) {
                if (debug >= 2) {
                    log("runCGI: addHeader(\"" + line + "\")");
                }
                if (line.startsWith("HTTP")) {
                    //TODO: should set status codes (NPH support)
                    /*
                     * response.setStatus(getStatusCode(line));
                     */
                } else {
                    response.addHeader
                        (line.substring(0, line.indexOf(":")).trim(),
                         line.substring(line.indexOf(":") + 1).trim());
                }
            }

            //write output
            cBuf = new char[1024];
            while ((bufRead = commandsStdOut.read(cBuf)) != -1) {
                if (servletContainerStdout != null) {
                    if (debug >= 4) {
                        log("runCGI: write(\"" + cBuf + "\")");
                    }
                    servletContainerStdout.write(cBuf, 0, bufRead);
                }
            }

            if (servletContainerStdout != null) {
                servletContainerStdout.flush();
            }

            proc.exitValue(); // Throws exception if alive

            isRunning = false;

        } catch (IllegalThreadStateException e) {
            try {
View Full Code Here

TOP

Related Classes of java.lang.Process

Copyright © 2018 www.massapicom. 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.