1 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 ; 38 import java.io.IOException ; 39 import java.io.Serializable ; 40 41 42 65 public class JVMProcessImpl extends AbstractExternalProcess 66 implements JVMProcess, Serializable { 67 protected static Logger logger = Logger.getLogger(JVMProcessImpl.class.getName()); 68 private static final String FILE_SEPARATOR = System.getProperty( 69 "file.separator"); 70 71 private final static String POLICY_OPTION = " -Djava.security.policy="; 73 private final static String LOG4J_OPTION = " -Dlog4j.configuration=file:"; 74 75 public final static String DEFAULT_CLASSPATH = convertClasspathToAbsolutePath(System.getProperty( 77 "java.class.path")); 78 public final static String DEFAULT_JAVAPATH = System.getProperty( 79 "java.home") + FILE_SEPARATOR + "bin" + FILE_SEPARATOR + "java"; 80 public static String DEFAULT_POLICY_FILE = System.getProperty( 81 "java.security.policy"); 82 public static String 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 DEFAULT_CLASSNAME = "org.objectweb.proactive.StartNode"; 95 public final static String DEFAULT_JVMPARAMETERS = ""; 96 private final static String PROACTIVE_POLICYFILE_OPTION = " -Dproactive.runtime.security="; 97 protected String classpath = DEFAULT_CLASSPATH; 98 protected String bootClasspath; 99 protected String javaPath = DEFAULT_JAVAPATH; 100 protected String policyFile = DEFAULT_POLICY_FILE; 101 protected String log4jFile = DEFAULT_LOG4J_FILE; 102 protected String classname = DEFAULT_CLASSNAME; 103 104 protected StringBuffer parameters = new StringBuffer (); 107 protected StringBuffer jvmParameters = new StringBuffer (); 108 109 113 117 public JVMProcessImpl() { 118 } 119 120 124 public JVMProcessImpl(MessageLogger messageLogger) { 125 super(messageLogger); 126 } 127 128 133 public JVMProcessImpl(MessageLogger inputMessageLogger, 134 MessageLogger errorMessageLogger) { 135 super(inputMessageLogger, errorMessageLogger); 136 } 137 138 public static void main(String [] 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 e) { 148 e.printStackTrace(); 149 } 150 } 151 152 public String getClasspath() { 156 return classpath; 157 } 158 159 public void setClasspath(String classpath) { 160 checkStarted(); 161 this.classpath = classpath; 162 } 163 164 public void setBootClasspath(String bootClasspath) { 165 checkStarted(); 166 this.bootClasspath = bootClasspath; 167 } 168 169 public String getBootClasspath() { 170 return bootClasspath; 171 } 172 173 public String getJavaPath() { 174 return javaPath; 175 } 176 177 public void setJavaPath(String javaPath) { 178 checkStarted(); 179 if (javaPath == null) { 180 throw new NullPointerException (); 181 } 182 this.javaPath = javaPath; 183 } 184 185 public String getPolicyFile() { 186 return policyFile; 187 } 188 189 public void setPolicyFile(String policyFile) { 190 checkStarted(); 191 this.policyFile = policyFile; 192 } 193 194 public String getLog4jFile() { 195 return log4jFile; 196 } 197 198 public void setLog4jFile(String log4jFile) { 199 this.log4jFile = log4jFile; 200 } 201 202 public String getClassname() { 203 return classname; 204 } 205 206 public void setClassname(String classname) { 207 checkStarted(); 208 this.classname = classname; 209 } 210 211 public String getParameters() { 212 return parameters.toString(); 213 } 214 215 public void setParameters(String parameters) { 216 checkStarted(); 217 this.parameters.append(parameters + " "); 218 } 219 220 public void setJvmOptions(String string) { 221 jvmParameters.append(string + " "); 222 } 223 224 protected String buildCommand() { 228 return buildJavaCommand(); 229 } 230 231 protected String buildJavaCommand() { 232 StringBuffer javaCommand = new StringBuffer (); 233 234 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 if (jvmParameters != null) { 249 javaCommand.append(" " + jvmParameters); 250 } 251 252 if ((classpath != null) && (classpath.length() > 0)) { 254 javaCommand.append(" -cp "); 255 javaCommand.append(classpath); 256 } 257 258 if (policyFile != null) { 260 javaCommand.append(POLICY_OPTION); 261 javaCommand.append(policyFile); 262 } 263 264 if (log4jFile != null) { 266 javaCommand.append(LOG4J_OPTION); 267 javaCommand.append(log4jFile); 268 } 269 270 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 private static String convertClasspathToAbsolutePath(String classpath) { 305 StringBuffer absoluteClasspath = new StringBuffer (); 306 String pathSeparator = System.getProperty("path.separator"); 307 java.util.StringTokenizer st = new java.util.StringTokenizer (classpath, 308 pathSeparator); 309 while (st.hasMoreTokens()) { 310 absoluteClasspath.append(new java.io.File (st.nextToken()).getAbsolutePath()); 311 absoluteClasspath.append(pathSeparator); 312 } 313 return absoluteClasspath.substring(0, absoluteClasspath.length() - 1); 314 } 315 316 private static String getAbsolutePath(String path) { 317 if (path.startsWith("file:")) { 318 path = path.substring(5); 320 } 321 try { 322 return new File (path).getCanonicalPath(); 323 } catch (IOException e) { 324 logger.error(e.getMessage()); 325 return path; 326 } 327 } 328 } 329 | Popular Tags |