KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > openccm > task > LauncherApplication


1 /*====================================================================
2
3 OpenCCM: The Open CORBA Component Model Platform
4 Copyright (C) 2000-2005 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): Christophe Contreras, Philippe Merle.
24
25 ====================================================================
26 $Id: LauncherApplication.java,v 1.9 2005/06/01 18:42:49 merle Exp $
27 ====================================================================*/

28
29 package org.objectweb.openccm.task;
30
31 //Package dependencies
32
import java.io.File JavaDoc;
33 import java.util.ArrayList JavaDoc;
34 import java.util.Enumeration JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.util.List JavaDoc;
37 import java.util.Map JavaDoc;
38 import java.util.Properties JavaDoc;
39 import org.apache.tools.ant.BuildException;
40
41 /**
42  * This Class is an abstraction for Launcher configuration.
43  *
44  * @author <a HREF="mailto:Christophe.Demarey@lifl.fr">Christophe Demarey</a>
45  *
46  * @version 0.1
47  */

48 public abstract class LauncherApplication
49 {
50     // ==================================================================
51
//
52
// Internal states.
53
//
54
// ==================================================================
55

56     /** Properties of the Ant Project. */
57     protected Properties JavaDoc project_properties_;
58
59     /** List of properties to add to the JVM. */
60     private Properties JavaDoc properties_;
61
62     /** Pathes to add in the classpath. */
63     private List JavaDoc pathes_;
64
65     /** Application arguments. */
66     private List JavaDoc arguments_;
67
68     /** The launcher xml configuration file. */
69     private String JavaDoc xml_file_;
70
71     /** The run_id to launch. */
72     private String JavaDoc run_id_;
73
74     /** The Launcher Application Process. */
75     private Process JavaDoc process_;
76
77     /** Arguments delimiter. */
78     private String JavaDoc arg_delimiter_;
79
80     /** Launcher working directory. */
81     private File JavaDoc working_directory_;
82
83     // ==================================================================
84
//
85
// Constructor.
86
//
87
// ==================================================================
88

89     /**
90      * The default constructor.
91      */

92     public LauncherApplication()
93     {
94         project_properties_ = new Properties JavaDoc();
95         properties_ = new Properties JavaDoc();
96         pathes_ = new ArrayList JavaDoc();
97         arguments_ = new ArrayList JavaDoc();
98         run_id_ = null;
99         xml_file_ = null;
100         process_ = null;
101         if ( System.getProperty("file.separator").compareTo("\\") == 0 )
102             arg_delimiter_ = "\"";
103         else
104             arg_delimiter_ = "";
105         working_directory_ = null;
106     }
107     
108     /**
109      * The constructor with project properties parameter.
110      *
111      * @param project_properties - Properties of the Ant Project.
112      */

113     public LauncherApplication(Map JavaDoc project_properties)
114     {
115         this();
116         project_properties_.putAll(project_properties);
117     }
118     
119     // ==================================================================
120
//
121
// Internal methods.
122
//
123
// ==================================================================
124

125     /**
126      * Get the JDK to run a 'java' command.
127      *
128      * @return The full path to the java command.
129      */

130     private String JavaDoc
131     getJDK()
132     {
133         return System.getProperty("java.home") + "/bin/java";
134     }
135     
136     /**
137      * Get the JDK bootclasspath.
138      *
139      * @return A bootclasspath declaration string.
140      */

141     private String JavaDoc
142     getBootClasspath()
143     {
144         String JavaDoc cp = null;
145         
146         if (project_properties_.getProperty("CORBA.launcher.bootclasspath")
147             != null)
148             cp = project_properties_.getProperty("CORBA.launcher.bootclasspath");
149         if ( cp == null)
150             return "";
151         else
152             return "-Xbootclasspath/p:"
153                 + cp ;
154     }
155     
156     /**
157      * Get the JDK properties declaration.
158      *
159      * @return A properties declaration string.
160      */

161     private String JavaDoc
162     getProperties()
163     {
164         String JavaDoc props = "";
165         
166         for (Enumeration JavaDoc e = properties_.keys() ; e.hasMoreElements() ;)
167         {
168             String JavaDoc key = (String JavaDoc) e.nextElement();
169             props += arg_delimiter_+ "-D" + key + "=" + properties_.getProperty(key) + arg_delimiter_ + " ";
170         }
171
172         /**
173          * Retrieves any OpenCCM_* and ORB_HOMEDIR property to propagate it to
174          * the Java System Properties of the launched application.
175          *
176          * [ analog to retrieving the shell environment variables and
177          * passing them to the Launcher with -D , as done in the
178          * openccm_launcher scripts ]
179          */

180         for (
181             Enumeration JavaDoc openccm_envi = project_properties_.propertyNames();
182             openccm_envi.hasMoreElements();
183             )
184         {
185             String JavaDoc key = (String JavaDoc) openccm_envi.nextElement();
186             if (key.startsWith("OpenCCM_") || key.equals("ORB_HOMEDIR"))
187                 props += arg_delimiter_+ "-D" + key + "=" + project_properties_.getProperty(key) + arg_delimiter_ + " ";
188         }
189         /** */
190
191         return props;
192     }
193     
194     /**
195      * Get the Launcher classpath declaration.
196      *
197      * @return The Launcher classpath declaration string.
198      */

199     private String JavaDoc
200     getClasspath()
201     {
202         String JavaDoc classpath = "-classpath " + arg_delimiter_;
203         String JavaDoc separator = System.getProperty("path.separator");
204
205         // Add pathes for Launcher
206
if (project_properties_.getProperty("CORBA.launcher.classpath")
207             !=null)
208             classpath += project_properties_.getProperty("CORBA.launcher.classpath")+ separator;
209
210         // Add user pathes
211
for (Iterator JavaDoc it = pathes_.iterator(); it.hasNext();)
212         {
213             classpath += it.next() + separator;
214         }
215         
216         // Remove the last separator
217
return classpath.substring(0, classpath.length()-1) + arg_delimiter_;
218     }
219     
220     /**
221      * Get Launcher arguments declaration.
222      *
223      * @return Launcher arguments declaration string.
224      */

225     private String JavaDoc
226     getArguments()
227     {
228         String JavaDoc args = "";
229         
230         for (Iterator JavaDoc it = arguments_.iterator(); it.hasNext();)
231         {
232             args += arg_delimiter_ + it.next() + arg_delimiter_ + " ";
233         }
234         
235         return args;
236     }
237     
238     /**
239      * Get Launcher run_id declaration.
240      *
241      * @return Launcher run_id declaration string.
242      */

243     private String JavaDoc
244     getRunId()
245     {
246         if (run_id_ != null)
247             return "--runid " + run_id_;
248         else
249             return "";
250     }
251     
252     /**
253      * Get the Launcher xml configuration file.
254      *
255      * @return The Launcher xml configuration file.
256      */

257     private String JavaDoc
258     getXmlFile()
259     {
260         return xml_file_;
261     }
262     
263     // ==================================================================
264
//
265
// Public methods.
266
//
267
// ==================================================================
268

269     /**
270      * Set ant project properties.
271      *
272      * @param project_properties - List of project properties.
273      */

274     public void
275     setProjectProperties(Map JavaDoc project_properties)
276     {
277         project_properties_.putAll(project_properties);
278     }
279     
280     /**
281      * Set the Working directory.
282      *
283      * @param dir - A directory.
284      */

285     public void
286     setWorkingDirectory(File JavaDoc dir)
287     {
288         working_directory_ = dir;
289     }
290     
291     /**
292      * Add a property to Launcher.
293      *
294      * @param name - The property name.
295      * @param value - The property value.
296      */

297     public void
298     addProperty(String JavaDoc name, String JavaDoc value)
299     {
300         properties_.setProperty(name, value);
301     }
302
303     /**
304      * Add a path to Launcher ClassLoader.
305      *
306      * @param path - The path to add.
307      */

308     public void
309     addPath(String JavaDoc path)
310     {
311         pathes_.add(path);
312     }
313
314     /**
315      * Add an argument to the application.
316      *
317      * @param arg - The argument to add.
318      */

319     public void
320     addArgument(String JavaDoc arg)
321     {
322         arguments_.add(arg);
323     }
324
325     /**
326      * Set the Launcher xml configuration file.
327      *
328      * @param file - The xml configuration file.
329      */

330     public void
331     setXmlFile(String JavaDoc file)
332     {
333         this.xml_file_ = file;
334     }
335
336     /**
337      * Set the run_id to launch.
338      *
339      * @param run_id - the run_id to launch.
340      */

341     public void
342     setRunId(String JavaDoc run_id)
343     {
344         this.run_id_ = run_id;
345     }
346
347     /**
348      * Get the command line to execute the launcher
349      * with its configuration.
350      *
351      * @return The ommand line to execute.
352      */

353     public String JavaDoc
354     getCommandLine()
355     {
356         String JavaDoc result = null;
357
358         result = getJDK() + " ";
359         result += getBootClasspath() + " ";
360         result += getProperties() + " ";
361         result += getClasspath() + " ";
362         result += "org.objectweb.util.launcher.Launcher ";
363         result += arg_delimiter_ + "file:" + getXmlFile() + arg_delimiter_ + " ";
364         result += getRunId() + " ";
365         result += getArguments();
366               
367         return result;
368     }
369
370     /**
371      * Configure the application.
372      * Sub-classes must implement this method.
373      */

374     abstract public void
375     configure();
376
377     /**
378      * Callback method to insert code after
379      * application launched.
380      */

381     public void
382     post_exec() {}
383
384     /**
385      * Run the launcher application as an external command
386      * and wait for process termination.
387      *
388      * @throws BuildException if an error occurs.
389      */

390     public void
391     run()
392     throws BuildException
393     {
394         run(false);
395     }
396
397     /**
398      * Run the launcher application as an external command.
399      *
400      * @param standalone - If true the process is run in a standalone mode
401      * else, this method waits for process termination.
402      *
403      * @throws BuildException if an error occurs.
404      */

405     public void
406     run(boolean standalone)
407     throws BuildException
408     {
409         Redirector redirector = null;
410
411         configure();
412         try
413         {
414             // System.out.println("exec="+ getCommandLine());
415
process_ = Runtime.getRuntime().exec( getCommandLine(), null, working_directory_ );
416             
417             redirector = new Redirector(process_);
418             redirector.redirectOutput();
419
420             post_exec();
421         }catch(Exception JavaDoc e){
422             e.printStackTrace();
423         }
424
425         if (!standalone)
426         {
427             try
428             {
429                 process_.waitFor();
430             }catch (InterruptedException JavaDoc e) {
431                 e.printStackTrace();
432             }
433             if (process_.exitValue() == -1)
434             {
435                 throw new BuildException("OpenCCM build failed!");
436             }
437         }
438     }
439     
440     /**
441      * Stop the launcher application process.
442      */

443     public void
444     stop()
445     {
446         if (process_ != null)
447         {
448             process_.destroy();
449         }
450     }
451 }
452
Popular Tags