KickJava   Java API By Example, From Geeks To Geeks.

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


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): Romain Rouvoy.
23 Contributor(s): Christophe Contreras.
24
25 --------------------------------------------------------------------
26 $Id: CommandJava.java,v 1.2 2004/09/28 15:19:47 contrera Exp $
27 ====================================================================*/

28
29 package org.objectweb.util.launcher ;
30
31
32 import java.lang.reflect.Method JavaDoc ;
33
34 import java.util.Properties JavaDoc ;
35
36 import org.objectweb.util.trace.TraceSystem;
37
38
39
40 /**
41  * Implementation of a particular Java Command which takes into account.
42  * <ul>
43  * <li>an Identifier for the command</li>
44  * <li>a bootstrap class name for executing the command</li>
45  * <li>a specific classloader where class are stored</li>
46  * <li>a list of java properties to associate to the command</li>
47  * <li>a list of arguments to give to the bootstrap class</li>
48  * <li>a particular mode of execution</li>
49  * </ul>
50  *
51  * <p>This command represent most of the command which could be executed
52  * in the context of Java developements.</p>
53  *
54  * @author <a HREF="mailto:Romain.Rouvoy@lifl.fr">Romain Rouvoy</a>
55  * @version 0.2
56  */

57 public class CommandJava
58 {
59     /** Identifier of the command */
60     protected String JavaDoc name_ ;
61     /** name of the bootstrap class */
62     protected String JavaDoc class_ ;
63     /** classloader used */
64     protected XBootClassLoader loader_ ;
65     /** set of java properties */
66     protected Properties JavaDoc properties_ ;
67     /** set of arguments for the bootstrap method */
68     protected StringList args_ ;
69     /** mode of execution */
70     protected Mode mode_;
71
72     /**
73      * Default constructor
74      */

75     public CommandJava() {
76         this(null);
77     }
78
79     /**
80      * Configurable constructor
81      * @param loader
82      */

83     public CommandJava(XBootClassLoader loader) {
84         name_ = "default" ;
85         class_ = "";
86         args_ = new StringList();
87         properties_ = new Properties JavaDoc();
88         mode_ = new ModeThread();
89         loader_ = loader;
90     }
91
92     /**
93      * Defines the identifier of the command
94      *
95      * @param value - identifier of the command ("default" by default)
96      */

97     public void setName(String JavaDoc value) {
98         TraceSystem.get("launcher").debug(getName()+": Setting Command name to "+value);
99         this.name_ = value ;
100     }
101
102     /**
103      * Provides the identifier of the command
104      *
105      * @return the identifier of the command ("default" by default)
106      */

107     public String JavaDoc getName() {
108         return this.name_ ;
109     }
110
111     /**
112      * Defines the Java class to execute
113      *
114      * @param value - full name of the java class
115      */

116     public void setClassname(String JavaDoc value) {
117         TraceSystem.get("launcher").debug(getName()+": Setting classname to "+value);
118         this.class_ = value ;
119     }
120
121     /**
122      * Retrieves the name of the Java class to run
123      *
124      * @return full name of the java class
125      */

126     public String JavaDoc getClassname() {
127         return this.class_ ;
128     }
129
130
131     /**
132      * Defines the classloader to use when executing the application
133      *
134      * @param value - the classloader
135      */

136     public void setLoader(XBootClassLoader value) {
137         this.loader_ = value ;
138     }
139
140     /**
141      * Retrieves the classloader used when executing the application
142      *
143      * @return the classloader used
144      */

145     public XBootClassLoader getLoader() {
146         return this.loader_ ;
147     }
148
149
150     /**
151      * Defines the Java properties to load in the Java Virtual Machine
152      *
153      * @param value - the properties to set
154      */

155     public void addProperties(Properties JavaDoc value) {
156         TraceSystem.get("launcher").debug(getName()+": Adding properties list"+value);
157         this.properties_.putAll(value) ;
158     }
159
160     /**
161      * Retrieves the properties defined for this application
162      *
163      * @return the properties defined
164      */

165     public Properties JavaDoc getProperties() {
166         return this.properties_ ;
167     }
168
169
170     /**
171      * Defines the command line contexts for the main method of the application
172      *
173      * @param value - the contexts to specify
174      */

175     public void addArguments(StringList value) {
176         TraceSystem.get("launcher").debug(getName()+": Adding argument list "+value);
177         this.args_.addAll(value) ;
178     }
179
180     /**
181      * Retrieves the command line contexts for the main method of the application
182      *
183      * @return the contexts specified
184      */

185     public StringList getArguments() {
186         return this.args_ ;
187     }
188
189
190     /**
191      * Defines the mode of execution of the command
192      *
193      * @param value - the mode of execution to use (ModeNormal by default)
194      */

195     public void setMode(String JavaDoc value) {
196         TraceSystem.get("launcher").debug(getName()+": Setting mode to "+value);
197         this.mode_ = org.objectweb.util.launcher.ModeFactory.create(value);
198     }
199
200     /**
201      * Provides the mode of execution of the command
202      *
203      * @return the mode of execution used (ModeNormal by default)
204      */

205     public Mode getMode() {
206         return this.mode_ ;
207     }
208
209     /** the initial ClassLoader used */
210     protected ClassLoader JavaDoc systemLoader_ ;
211
212     /**
213      * Provides the type "String[]"
214      *
215      * @return an instance of the String[] type
216      */

217     private Class JavaDoc getStringArrayType() {
218         return (Class JavaDoc)(new String JavaDoc[0]).getClass();
219     }
220
221     /**
222      * Run the application (must not be called)
223      *
224      * @throws LauncherException is raised when the Launcher wasn't able to launch
225      * the application
226      */

227     protected void execute()
228     { // Retrieve the public static void main(String[]) method of the class
229
Class JavaDoc class_instance = null;
230         TraceSystem.get("launcher").info(getName()+": Launching the "+getClassname()+" class");
231         try {
232             class_instance = Class.forName(getClassname(), false, getLoader()) ;
233         } catch (java.lang.ClassNotFoundException JavaDoc ex) {
234             throw new LauncherException(ex);
235         }
236         Method JavaDoc method = null;
237         try {
238             Class JavaDoc[] params = {getStringArrayType()} ;
239             method = class_instance.getMethod("main", params);
240         } catch (java.lang.NoSuchMethodException JavaDoc ex) {
241             throw new LauncherException(ex);
242         }
243
244         // Sets loader_ as Current Class Loader for the application
245
this.systemLoader_ = Thread.currentThread().getContextClassLoader();
246         Thread.currentThread().setContextClassLoader(getLoader());
247         System.getProperties().putAll(getProperties());
248
249         try { // invoke the method main
250
Object JavaDoc[] params = {getArguments().toStringArray()} ;
251             TraceSystem.get("launcher").debug("Arguments used: "+getArguments());
252             method.invoke(null, params);
253         } catch (java.lang.IllegalAccessException JavaDoc ex) {
254             throw new LauncherException(ex);
255         } catch (java.lang.reflect.InvocationTargetException JavaDoc ex) {
256             throw new LauncherException(ex);
257         }
258     }
259
260     /**
261      * Run the application
262      */

263     public void run() {
264         this.mode_.execute(this);
265     }
266
267     /**
268      * Rollback the ClassLoader
269      */

270     protected void finalize() {
271          Thread.currentThread().setContextClassLoader(this.systemLoader_);
272     }
273
274     /**
275      * Returns true if the two command are using the same parameters
276      *
277      * @param obj - the CommandJava to compare
278      */

279     public boolean equals(Object JavaDoc obj) {
280         if (obj instanceof CommandJava) {
281             CommandJava cmd = (CommandJava) obj ;
282             return ((getName().equals(cmd.getName()))
283                     && (getClassname().equals(cmd.getClassname()))
284                     && (getLoader().equals(cmd.getLoader()))
285                     && (getProperties().equals(cmd.getProperties()))
286                     && (getArguments().equals(cmd.getArguments())));
287         }
288         return false ;
289     }
290 }
291
Popular Tags