View Javadoc
1 /*** 2 * Redistribution and use of this software and associated documentation 3 * ("Software"), with or without modification, are permitted provided 4 * that the following conditions are met: 5 * 6 * 1. Redistributions of source code must retain copyright 7 * statements and notices. Redistributions must also contain a 8 * copy of this document. 9 * 10 * 2. Redistributions in binary form must reproduce the 11 * above copyright notice, this list of conditions and the 12 * following disclaimer in the documentation and/or other 13 * materials provided with the distribution. 14 * 15 * 3. The name "Exolab" must not be used to endorse or promote 16 * products derived from this Software without prior written 17 * permission of Exoffice Technologies. For written permission, 18 * please contact tma@netspace.net.au. 19 * 20 * 4. Products derived from this Software may not be called "Exolab" 21 * nor may "Exolab" appear in their names without prior written 22 * permission of Exoffice Technologies. Exolab is a registered 23 * trademark of Exoffice Technologies. 24 * 25 * 5. Due credit should be given to the Exolab Project 26 * (http://www.exolab.org/). 27 * 28 * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS 29 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT 30 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 31 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 32 * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 39 * OF THE POSSIBILITY OF SUCH DAMAGE. 40 * 41 * Copyright 2001-2004 (C) Exoffice Technologies Inc. All Rights Reserved. 42 * 43 * $Id: AsyncExecutor.java,v 1.4 2004/02/03 21:52:06 tanderson Exp $ 44 */ 45 package org.exolab.jmscts.core; 46 47 import java.io.OutputStream; 48 49 import org.apache.log4j.Category; 50 51 import org.exolab.jmscts.core.service.ExecutionMonitorService; 52 53 54 /*** 55 * This class enables java applications to be executed asynchronously, 56 * with the output being captured, or echoed to System.out and System.err 57 * Clients can register a listener to be notified when the application 58 * starts and stops. 59 * 60 * @version $Revision: 1.4 $ $Date: 2004/02/03 21:52:06 $ 61 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a> 62 * @see Executor 63 * @see ExecutionListener 64 */ 65 public class AsyncExecutor { 66 67 /*** 68 * The command to execute 69 */ 70 private String _command = null; 71 72 /*** 73 * The command executor 74 */ 75 private Executor _executor = null; 76 77 /*** 78 * The identifier for this executor 79 */ 80 private String _id = null; 81 82 /*** 83 * The logger 84 */ 85 private static final Category log = 86 Category.getInstance(AsyncExecutor.class.getName()); 87 88 89 /*** 90 * Constructor to execute a command, with output going to System.out and 91 * System.err 92 * 93 * @param application the class of the application to execute 94 * @param arguments any arguments to pass to the application 95 * @param port the RMI registry port 96 */ 97 public AsyncExecutor(Class application, String arguments, int port) { 98 this(application, arguments, System.out, System.err, port); 99 } 100 101 /*** 102 * Constructor to execute a command, with all output going to a stream 103 * 104 * @param application the class of the application to execute 105 * @param arguments any arguments to pass to the application 106 * @param out the stream to log output to 107 * @param port the RMI registry port 108 */ 109 public AsyncExecutor(Class application, String arguments, 110 OutputStream out, int port) { 111 this(application, arguments, out, out, port); 112 } 113 114 /*** 115 * Constructor to execute a command with standard output and error output 116 * going to two separate streams 117 * 118 * @param application the class of the application to execute 119 * @param arguments any arguments to pass to the application 120 * @param out the stream to direct standard output to 121 * @param err the stream to direct standard error to 122 * @param port the RMI registry port 123 */ 124 public AsyncExecutor(Class application, String arguments, OutputStream out, 125 OutputStream err, int port) { 126 if (application == null) { 127 throw new IllegalArgumentException("Argument application is null"); 128 } 129 if (out == null) { 130 throw new IllegalArgumentException("Argument out is null"); 131 } 132 if (err == null) { 133 throw new IllegalArgumentException("Argument err is null"); 134 } 135 _id = ExecutionMonitorService.instance().allocateId(); 136 137 StringBuffer command = new StringBuffer(); 138 command.append(System.getProperty("java.home")); 139 command.append("/bin/java "); 140 String policy = System.getProperty("java.security.policy"); 141 if (policy != null) { 142 command.append(" -Djava.security.policy="); 143 command.append(policy); 144 command.append(" "); 145 } 146 command.append(" -classpath "); 147 command.append(System.getProperty("java.class.path")); 148 command.append(" "); 149 command.append(ApplicationRunner.class.getName()); 150 command.append(" "); 151 command.append(_id); 152 command.append(" "); 153 command.append(port); 154 command.append(" "); 155 command.append(application.getName()); 156 if (arguments != null) { 157 command.append(" "); 158 command.append(arguments); 159 } 160 _command = command.toString(); 161 } 162 163 /*** 164 * Add a listener to monitor for execution events 165 * 166 * @param listener the listener to add 167 */ 168 public void addListener(ExecutionListener listener) { 169 ExecutionMonitorService.instance().addListener(_id, listener); 170 } 171 172 /*** 173 * Remove a listener 174 * 175 * @param listener the listener to remove 176 */ 177 public void removeListener(ExecutionListener listener) { 178 ExecutionMonitorService.instance().removeListener(_id, listener); 179 } 180 181 /*** 182 * Execute the command in a separate thread. This method returns 183 * immediately 184 */ 185 public synchronized void run() { 186 if (_executor == null) { 187 _executor = new Executor(_command); 188 } 189 Thread thread = new Thread() { 190 public void run() { 191 try { 192 int status = _executor.run(); 193 ExecutionMonitorService.instance().stopped(_id, status); 194 } catch (Exception exception) { 195 ExecutionMonitorService.instance().failed(_id, exception); 196 } 197 } 198 }; 199 log.debug("running command=" + _command); 200 thread.start(); 201 202 } 203 204 /*** 205 * Stop the process 206 */ 207 public void stop() { 208 if (_executor != null) { 209 _executor.stop(); 210 } 211 } 212 213 }

This page was automatically generated by Maven