KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > launcher > Launcher


1 /*====================================================================
2
3 ObjectWeb Util Launcher Package.
4 Copyright (C) 2004 INRIA & USTL - LIFL - GOAL
5 Contact: openccm@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Christophe Demarey.
23 Contributor(s): Frédéric Briclet, Romain Rouvoy.
24
25 --------------------------------------------------------------------
26 $Id: Launcher.java,v 1.4 2004/06/14 12:52:33 moroy Exp $
27 ====================================================================*/

28 package org.objectweb.util.launcher;
29
30
31 import org.objectweb.util.launcher.option.OptionContext;
32 import org.objectweb.util.launcher.option.OptionRun ;
33 import org.objectweb.util.trace.TraceSystem;
34
35 /**
36  * Launcher is used to run classes (call the "main" method).
37  * Properties are read from an xml file.
38  *
39  * @author <a HREF="mailto:Christophe.Demarey@lifl.fr">Christophe Demarey</a>
40  * @version 0.1
41  */

42 public class Launcher
43 {
44     // ==================================================================
45
//
46
// Internal state.
47
//
48
// ==================================================================
49

50     /** list of run to execute */
51     protected String JavaDoc runid_ ;
52     /** list of context to execute */
53     protected String JavaDoc opt_ ;
54     /** file containing the descriptions */
55     protected String JavaDoc file_ ;
56     /** list of arguments */
57     protected String JavaDoc args_ ;
58     /** path option */
59     protected String JavaDoc classPath_ = " -classpath ";
60     /* Handler on the process launched */
61     protected Process JavaDoc process;
62
63     // ==================================================================
64
//
65
// Internal class.
66
//
67
// ==================================================================
68

69     /**
70      * Internal class in charge to launch an new JVM for new program
71      * to launch
72      */

73     private class ForkThread extends Thread JavaDoc {
74         
75         /*String command to execute*/
76         private String JavaDoc toExecute ;
77         /*Private handler on the process launched*/
78         private Process JavaDoc p=null;
79                 
80         /**
81          * Main constructor
82          * @param toExecute String line to execute
83          */

84         public ForkThread(String JavaDoc toExecute ){
85             this.toExecute=toExecute;
86         }
87     
88         /**
89          * Start method of the thread.
90          */

91         public void run(){
92             try{
93             // launch the process
94
p=Runtime.getRuntime().exec(toExecute);
95                       
96             }
97             catch(Exception JavaDoc e){
98                 System.out.println("Error occured in Thread"+e.getMessage());
99                 e.printStackTrace();
100             }
101         }
102         
103         /**
104          * Accessor method to retrieve the launched process
105          **/

106         public Process JavaDoc getProcess(){
107             return p;
108         }
109     
110     }
111         
112     // ==================================================================
113
//
114
// Constructor.
115
//
116
// ==================================================================
117

118     /**
119      * Default constructor.
120      */

121     public Launcher() {
122         this.runid_ = "" ;
123         this.opt_ = "" ;
124         this.file_ = "" ;
125         this.args_ = "" ;
126     }
127
128     /**
129      * Special constructor to fork in a new JVM.
130      */

131     public Launcher(String JavaDoc targetXMLFile,
132                     String JavaDoc runID,
133                     String JavaDoc targetArgs[])
134     {
135         try{
136             // Build the command to execute
137
String JavaDoc toExecute=System.getProperty("java.home")+"/bin/java"+
138                 classPath_ +" "+System.getProperty("java.class.path")+
139                 " org.objectweb.util.launcher.Launcher ";
140   
141             //gather the parameter give in parameter
142
for(int i=0; i<targetArgs.length;i++)
143                 toExecute+=targetArgs[i]+" ";
144             
145             //tick the rund id to use
146
toExecute+="--runid "+runID+" "+targetXMLFile;
147             
148             // launch a thread to execute the command
149
ForkThread ft=new ForkThread(toExecute);
150             // start the thread
151
ft.start();
152             //wait for component starting
153
while(ft.getProcess()==null)
154                 Thread.sleep(1000);
155             //retrieve the launched process
156
this.process=ft.getProcess();
157         }
158         catch(Exception JavaDoc e){
159             System.err.println("Cannot launch: "+e.getMessage());
160             e.printStackTrace();
161         }
162     }
163     
164     // ==================================================================
165
//
166
// Internal class.
167
//
168
// ==================================================================
169
/**
170      * Internal class in charge to launch an new JVM for new program
171      * to launch
172      */

173     protected class ProcessStarter
174             extends Thread JavaDoc
175     {
176         /* String command to execute */
177         protected String JavaDoc toExecute ;
178         /* Private handler on the process launched */
179         protected Process JavaDoc p=null;
180         
181         /**
182          * Accessor method to retrieve the launched process
183          **/

184         public Process JavaDoc getProcess() {
185             return p;
186         }
187
188         /**
189          * Main constructor
190          * @param toLaunch String line to execute
191          */

192         public ProcessStarter(String JavaDoc toLaunch ) {
193             toExecute = System.getProperty("java.home") +
194                 "/bin/java -classpath " +
195                 System.getProperty("java.class.path") +
196                 " org.objectweb.util.launcher.Launcher " +
197                 toLaunch;
198         }
199
200         /**
201          * Start method of the thread.
202          */

203          public void run() {
204              try { // launch the process
205
p=Runtime.getRuntime().exec(toExecute);
206              } catch(Exception JavaDoc ex) {
207                  TraceSystem.get("launcher").error("Exception raised: "+ex);
208                  throw new LauncherException(ex);
209              }
210          }
211     }
212
213
214     /**
215      * Defines the run to execute.
216      * @param run identifier of the run.
217      */

218     public void setRunid(String JavaDoc run){
219         runid_ += OptionRun.create(run);
220     }
221
222     /**
223      * Specify a particular context to use.
224      * @param opt identifier of the context.
225      */

226     public void addContext(String JavaDoc opt) {
227         opt_ += OptionContext.create(opt);
228     }
229
230     /**
231      * Specify the configuration file to use.
232      * @param file the URL of the file.
233      */

234     public void setFile(String JavaDoc file){
235         file_ = file ;
236     }
237
238     /**
239      * Defines additional arguments for the application.
240      * @param args list of arguments
241      */

242     public void addArgs(String JavaDoc args) {
243         args_ += " " + args ;
244     }
245
246     /**
247      * Provides the command line to use.
248      * @return the full command line.
249      */

250     private String JavaDoc getCommandLine() {
251         return runid_ + " " + opt_ + " " + " " + file_ ;
252     }
253     
254     /**
255      * Starts the application.
256      * @return the process representing the launched application.
257      */

258     public Process JavaDoc start() {
259         try {
260             ProcessStarter ps = new ProcessStarter(getCommandLine());
261             // start the thread
262
ps.start();
263             // wait for application starting
264
while(ps.getProcess()==null)
265                 Thread.sleep(1000);
266             return ps.getProcess();
267         } catch(Exception JavaDoc ex) {
268             TraceSystem.get("launcher").error("Exception raised: " + ex);
269             throw new LauncherException(ex);
270         }
271     }
272
273     
274     // ==================================================================
275
//
276
// Public methods.
277
//
278
// ==================================================================
279

280     /**
281      * Accessor method to retrieve the launched process
282      */

283     public Process JavaDoc
284     getProcess()
285     {
286         return process;
287     }
288       
289     /**
290      * The main bootstrap method.
291      *
292      * @param args The command line arguments.
293      */

294     public static void
295     main(String JavaDoc[] args) {
296         LauncherApplication application = new LauncherApplication();
297         application.runMain(args);
298     }
299 }
300
Popular Tags