KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > core > process > JVMProcessImpl


1 /*
2  * ################################################################
3  *
4  * ProActive: The Java(TM) library for Parallel, Distributed,
5  * Concurrent computing with Security and Mobility
6  *
7  * Copyright (C) 1997-2002 INRIA/University of Nice-Sophia Antipolis
8  * Contact: proactive-support@inria.fr
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23  * USA
24  *
25  * Initial developer(s): The ProActive Team
26  * http://www.inria.fr/oasis/ProActive/contacts.html
27  * Contributor(s):
28  *
29  * ################################################################
30  */

31 package org.objectweb.proactive.core.process;
32
33 import org.apache.log4j.Logger;
34
35 import org.objectweb.proactive.core.util.MessageLogger;
36
37 import java.io.File JavaDoc;
38 import java.io.IOException JavaDoc;
39 import java.io.Serializable JavaDoc;
40
41
42 /**
43  * <p>
44  * The JVMProcess class is able to start localy any class of the ProActive library by
45  * creating a Java Virtual Machine.
46  * </p><p>
47  * For instance:
48  * </p>
49  * <pre>
50  * .............
51  * JVMProcessImpl process = new JVMProcessImpl(new StandardOutputMessageLogger());
52  * process.setClassname("org.objectweb.proactive.StartNode");
53  * process.setParameters("nodeName");
54  * process.startProcess();
55  * .............
56  * </pre>
57  * <p>
58  * This piece of code launches the ProActive java class org.objectweb.proactive.StartNode
59  * with nodeName as parameter.
60  * </p>
61  * @author ProActive Team
62  * @version 1.0, 2002/09/20
63  * @since ProActive 0.9.4
64  */

65 public class JVMProcessImpl extends AbstractExternalProcess
66     implements JVMProcess, Serializable JavaDoc {
67     protected static Logger logger = Logger.getLogger(JVMProcessImpl.class.getName());
68     private static final String JavaDoc FILE_SEPARATOR = System.getProperty(
69             "file.separator");
70
71     //private final static String POLICY_FILE = "proactive.java.policy";
72
private final static String JavaDoc POLICY_OPTION = " -Djava.security.policy=";
73     private final static String JavaDoc LOG4J_OPTION = " -Dlog4j.configuration=file:";
74
75     //private final static String LOG4J_FILE = "proactive-log4j";
76
public final static String JavaDoc DEFAULT_CLASSPATH = convertClasspathToAbsolutePath(System.getProperty(
77                 "java.class.path"));
78     public final static String JavaDoc DEFAULT_JAVAPATH = System.getProperty(
79             "java.home") + FILE_SEPARATOR + "bin" + FILE_SEPARATOR + "java";
80     public static String JavaDoc DEFAULT_POLICY_FILE = System.getProperty(
81             "java.security.policy");
82     public static String JavaDoc DEFAULT_LOG4J_FILE = System.getProperty(
83             "log4j.configuration");
84
85     static {
86         if (DEFAULT_POLICY_FILE != null) {
87             DEFAULT_POLICY_FILE = getAbsolutePath(DEFAULT_POLICY_FILE);
88         }
89         if (DEFAULT_LOG4J_FILE != null) {
90             DEFAULT_LOG4J_FILE = getAbsolutePath(DEFAULT_LOG4J_FILE);
91         }
92     }
93
94     public final static String JavaDoc DEFAULT_CLASSNAME = "org.objectweb.proactive.StartNode";
95     public final static String JavaDoc DEFAULT_JVMPARAMETERS = "";
96     private final static String JavaDoc PROACTIVE_POLICYFILE_OPTION = " -Dproactive.runtime.security=";
97     protected String JavaDoc classpath = DEFAULT_CLASSPATH;
98     protected String JavaDoc bootClasspath;
99     protected String JavaDoc javaPath = DEFAULT_JAVAPATH;
100     protected String JavaDoc policyFile = DEFAULT_POLICY_FILE;
101     protected String JavaDoc log4jFile = DEFAULT_LOG4J_FILE;
102     protected String JavaDoc classname = DEFAULT_CLASSNAME;
103
104     // protected String parameters = DEFAULT_JVMPARAMETERS;
105
// protected String jvmParameters;
106
protected StringBuffer JavaDoc parameters = new StringBuffer JavaDoc();
107     protected StringBuffer JavaDoc jvmParameters = new StringBuffer JavaDoc();
108
109     //
110
// -- CONSTRUCTORS -----------------------------------------------
111
//
112

113     /**
114      * Creates a new JVMProcess
115      * Used with XML Descriptor
116      */

117     public JVMProcessImpl() {
118     }
119
120     /**
121      * Creates a new JVMProcess
122      * @param messageLogger The logger that handles input and error stream of this process
123      */

124     public JVMProcessImpl(MessageLogger messageLogger) {
125         super(messageLogger);
126     }
127
128     /**
129      * Creates a new JVMProcess
130      * @param inputMessageLogger The logger that handles input stream of this process
131      * @param errorMessageLogger The logger that handles error stream of this process
132      */

133     public JVMProcessImpl(MessageLogger inputMessageLogger,
134         MessageLogger errorMessageLogger) {
135         super(inputMessageLogger, errorMessageLogger);
136     }
137
138     //
139
// -- PUBLIC METHODS -----------------------------------------------
140
//
141
public static void main(String JavaDoc[] args) {
142         try {
143             JVMProcessImpl rsh = new JVMProcessImpl(new StandardOutputMessageLogger());
144             rsh.setClassname("org.objectweb.proactive.StartNode");
145             rsh.setParameters(args[0]);
146             rsh.startProcess();
147         } catch (Exception JavaDoc e) {
148             e.printStackTrace();
149         }
150     }
151
152     //
153
// -- implements JVMProcess -----------------------------------------------
154
//
155
public String JavaDoc getClasspath() {
156         return classpath;
157     }
158
159     public void setClasspath(String JavaDoc classpath) {
160         checkStarted();
161         this.classpath = classpath;
162     }
163
164     public void setBootClasspath(String JavaDoc bootClasspath) {
165         checkStarted();
166         this.bootClasspath = bootClasspath;
167     }
168
169     public String JavaDoc getBootClasspath() {
170         return bootClasspath;
171     }
172
173     public String JavaDoc getJavaPath() {
174         return javaPath;
175     }
176
177     public void setJavaPath(String JavaDoc javaPath) {
178         checkStarted();
179         if (javaPath == null) {
180             throw new NullPointerException JavaDoc();
181         }
182         this.javaPath = javaPath;
183     }
184
185     public String JavaDoc getPolicyFile() {
186         return policyFile;
187     }
188
189     public void setPolicyFile(String JavaDoc policyFile) {
190         checkStarted();
191         this.policyFile = policyFile;
192     }
193
194     public String JavaDoc getLog4jFile() {
195         return log4jFile;
196     }
197
198     public void setLog4jFile(String JavaDoc log4jFile) {
199         this.log4jFile = log4jFile;
200     }
201
202     public String JavaDoc getClassname() {
203         return classname;
204     }
205
206     public void setClassname(String JavaDoc classname) {
207         checkStarted();
208         this.classname = classname;
209     }
210
211     public String JavaDoc getParameters() {
212         return parameters.toString();
213     }
214
215     public void setParameters(String JavaDoc parameters) {
216         checkStarted();
217         this.parameters.append(parameters + " ");
218     }
219
220     public void setJvmOptions(String JavaDoc string) {
221         jvmParameters.append(string + " ");
222     }
223
224     //
225
// -- PROTECTED METHODS -----------------------------------------------
226
//
227
protected String JavaDoc buildCommand() {
228         return buildJavaCommand();
229     }
230
231     protected String JavaDoc buildJavaCommand() {
232         StringBuffer JavaDoc javaCommand = new StringBuffer JavaDoc();
233
234         // append java command
235
if (javaPath == null) {
236             javaCommand.append("java");
237         } else {
238             javaCommand.append(javaPath);
239         }
240
241         if (bootClasspath != null) {
242             javaCommand.append(" -Xbootclasspath:");
243             javaCommand.append(bootClasspath);
244             javaCommand.append(" ");
245         }
246
247         // append jvmParameters
248
if (jvmParameters != null) {
249             javaCommand.append(" " + jvmParameters);
250         }
251
252         // append classpath
253
if ((classpath != null) && (classpath.length() > 0)) {
254             javaCommand.append(" -cp ");
255             javaCommand.append(classpath);
256         }
257
258         // append policy option
259
if (policyFile != null) {
260             javaCommand.append(POLICY_OPTION);
261             javaCommand.append(policyFile);
262         }
263
264         // append log4j option
265
if (log4jFile != null) {
266             javaCommand.append(LOG4J_OPTION);
267             javaCommand.append(log4jFile);
268         }
269
270         // append proactive policy File
271
// if (securityFile != null) {
272
// javaCommand.append(PROACTIVE_POLICYFILE_OPTION);
273
// javaCommand.append(securityFile);
274
// }// else if (System.getProperty("proactive.runtime.security") != null) {
275
// javaCommand.append(PROACTIVE_POLICYFILE_OPTION);
276
// javaCommand.append(System.getProperty("proactive.runtime.security"));
277
// }
278
// append classname
279
javaCommand.append(" ");
280         javaCommand.append(classname);
281         if (logger.isDebugEnabled()) {
282             logger.debug("JVMProcessImpl.buildJavaCommand() Parameters " +
283                 parameters);
284         }
285         if (parameters != null) {
286             javaCommand.append(" ");
287             javaCommand.append(parameters);
288         }
289         if (logger.isDebugEnabled()) {
290             logger.debug(javaCommand.toString());
291         }
292         if (logger.isDebugEnabled()) {
293             logger.debug(javaCommand.toString() + "\n");
294         }
295         if (logger.isDebugEnabled()) {
296             logger.debug("JVMProcessImpl.buildJavaCommand() " + javaCommand);
297         }
298         return javaCommand.toString();
299     }
300
301     //
302
// -- PRIVATE METHODS -----------------------------------------------
303
//
304
private static String JavaDoc convertClasspathToAbsolutePath(String JavaDoc classpath) {
305         StringBuffer JavaDoc absoluteClasspath = new StringBuffer JavaDoc();
306         String JavaDoc pathSeparator = System.getProperty("path.separator");
307         java.util.StringTokenizer JavaDoc st = new java.util.StringTokenizer JavaDoc(classpath,
308                 pathSeparator);
309         while (st.hasMoreTokens()) {
310             absoluteClasspath.append(new java.io.File JavaDoc(st.nextToken()).getAbsolutePath());
311             absoluteClasspath.append(pathSeparator);
312         }
313         return absoluteClasspath.substring(0, absoluteClasspath.length() - 1);
314     }
315
316     private static String JavaDoc getAbsolutePath(String JavaDoc path) {
317         if (path.startsWith("file:")) {
318             //remove file part to build absolute path
319
path = path.substring(5);
320         }
321         try {
322             return new File JavaDoc(path).getCanonicalPath();
323         } catch (IOException JavaDoc e) {
324             logger.error(e.getMessage());
325             return path;
326         }
327     }
328 }
329
Popular Tags