Save This Page
Home » activemq-parent-5.3.1-source-release » org.apache » activemq » systest » impl » [javadoc | source]
    1   /**
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    */
   17   package org.apache.activemq.systest.impl;
   18   
   19   import org.apache.activemq.systest.AgentStopper;
   20   import org.apache.activemq.systest.AgentSupport;
   21   
   22   import java.io.BufferedReader;
   23   import java.io.IOException;
   24   import java.io.InputStream;
   25   import java.io.InputStreamReader;
   26   
   27   /**
   28    * Starts a separate process on this machine until its asked to be killed.
   29    * 
   30    * @version $Revision: 1.1 $
   31    */
   32   public class SeparateProcessAgent extends AgentSupport {
   33   
   34       private String[] commands;
   35       private Process process;
   36       private long sleepTime = 10000;
   37   
   38       public SeparateProcessAgent() {
   39       }
   40   
   41       public String[] getCommands() {
   42           if (commands == null) {
   43               commands = createCommand();
   44           }
   45           return commands;
   46       }
   47   
   48       public void setCommands(String[] command) {
   49           this.commands = command;
   50       }
   51   
   52       public Process getProcess() {
   53           return process;
   54       }
   55   
   56       public void start() throws Exception {
   57           process = createProcess();
   58           Thread thread = new Thread(new Runnable() {
   59               public void run() {
   60                   readInputStream(process.getInputStream());
   61               }
   62           });
   63           thread.start();
   64   
   65           Thread thread2 = new Thread(new Runnable() {
   66               public void run() {
   67                   waitForProcessExit();
   68               }
   69           });
   70           thread2.start();
   71           
   72           // lets wait for the process to startup
   73           
   74           System.out.println("Waiting a little while to give the broker process to start");
   75           Thread.sleep(sleepTime);
   76       }
   77   
   78       public void stop(AgentStopper stopper) {
   79           if (process != null) {
   80               try {
   81                   process.destroy();
   82               }
   83               catch (Exception e) {
   84                   stopper.onException(this, e);
   85               }
   86               finally {
   87                   process = null;
   88               }
   89           }
   90       }
   91   
   92       protected Process createProcess() throws Exception {
   93           return Runtime.getRuntime().exec(commands);
   94       }
   95   
   96       protected String[] createCommand() {
   97           throw new IllegalArgumentException("You must configure the 'commands' property");
   98       }
   99   
  100       protected void readInputStream(InputStream inputStream) {
  101           try {
  102               BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
  103               while (true) {
  104                   String line = reader.readLine();
  105                   if (line == null) {
  106                       break;
  107                   }
  108                   System.out.println(line);
  109               }
  110           }
  111           catch (IOException e) {
  112               // ignore exceptions
  113               // probably end of file
  114           }
  115       }
  116   
  117       protected void waitForProcessExit() {
  118           Process p = process;
  119           try {
  120               p.waitFor();
  121           }
  122           catch (InterruptedException e) {
  123               System.out.println("Interrupted while waiting for process to complete: " + e);
  124               e.printStackTrace();
  125           }
  126           int value = p.exitValue();
  127           System.out.println("Process completed with exit value: " + value);
  128   
  129       }
  130   }

Save This Page
Home » activemq-parent-5.3.1-source-release » org.apache » activemq » systest » impl » [javadoc | source]