Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2012, Red Hat, Inc., and individual contributors
   * as indicated by the @author tags. See the copyright.txt file in the
   * distribution for a full listing of individual contributors.
   *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
 
 package org.jboss.as.test.integration.domain.management.util;
 
 
 import java.util.Map;

Basic process test wrapper.

Author(s):
Emanuel Muckenhuber
 
 class ProcessWrapper {
 
     private final String processName;
     private final CommandBuilder commandBuilder;
     private final Map<StringStringenv;
     private final String workingDirectory;
 
     private Process process;
     private volatile boolean stopped;
 
     ProcessWrapper(final String processNamefinal CommandBuilder commandBuilderfinal Map<StringStringenvfinal String workingDirectory) {
         assert processName != null;
         assert commandBuilder != null;
         assert env != null;
         assert workingDirectory != null;
         this. = processName;
         this. = commandBuilder;
         this. = env;
         this. = workingDirectory;
         Runtime.getRuntime().addShutdownHook(new Thread() {
             @Override
             public void run() {
                 ProcessWrapper.this.stop();
             }
         });
     }
 
     int getExitValue() {
         synchronized (this) {
             try {
                 return .exitValue();
             } catch (IllegalThreadStateException e) {
                 return -1;
             }
         }
     }
 
     void start() throws Exception {
         synchronized (this) {
             if() {
                 throw new IllegalStateException();
             }
              = Launcher.of()
                     .setRedirectErrorStream(true)
                     .addEnvironmentVariables()
                     .setDirectory()
                     .launch();
             final Runnable consoleConsumer = new ConsoleConsumer(.getInputStream(), .);
             new Thread(consoleConsumer"Console consumer " + ).start();
         }
     }
 
     int waitFor() throws InterruptedException {
         final Process process;
         synchronized (this) {
             process = this.;
         }
         if(process != null) {
             return process.waitFor();
        }
        return 0;
    }
    void stop() {
        synchronized (this) {
            boolean stopped = this.;
            if(! stopped) {
                this. = true;
                final Process process = this.;
                if(process != null) {
                    process.destroy();
                }
            }
        }
    }
    @Override
    public String toString() {
        return "ProcessWrapper [processName=" +  + ", command=" +  + ", env=" +  + ", workingDirectory="
                +  + ", stopped=" +  + "]";
    }
    
Runnable that consumes the output of the process. If nothing consumes the output the AS will hang on some platforms

Author(s):
Stuart Douglas
    private class ConsoleConsumer implements Runnable {
        private final InputStream source;
        private final PrintStream target;
        private final boolean writeOutput;
        {
             = AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
                @Override
                public Boolean run() {
                    // this needs a better name
                    String val = System.getProperty("org.jboss.as.writeconsole");
                    return val == null || !"false".equals(val);
                }
            });
        }
        private ConsoleConsumer(final InputStream sourcefinal PrintStream target) {
            this. = source;
            this. = target;
        }
        public void run() {
            final InputStream source = this.;
            try {
                byte[] buf = new byte[32];
                int num;
                // Do not try reading a line cos it considers '\r' end of line
                while((num = source.read(buf)) != -1){
                    if ()
                        ..write(buf, 0, num);
                }
            } catch (IOException e) {
                if(! ProcessWrapper.this.) {
                    e.printStackTrace();
                }
            } finally {
                StreamUtils.safeClose(source);
            }
        }
    }
New to GrepCode? Check out our FAQ X