1 package org.sapia.magnet.domain.java; 2 3 import java.util.ArrayList ; 6 import java.util.Collection ; 7 import java.util.Iterator ; 8 import java.util.List ; 9 10 import org.apache.log4j.Logger; 13 14 import org.sapia.console.CmdLine; 17 import org.sapia.magnet.MagnetException; 18 import org.sapia.magnet.Log; 19 import org.sapia.magnet.domain.DefaultLaunchHandler; 20 import org.sapia.magnet.domain.Magnet; 21 import org.sapia.magnet.domain.Param; 22 import org.sapia.magnet.domain.Profile; 23 import org.sapia.magnet.render.MagnetContext; 24 import org.sapia.magnet.render.RenderingException; 25 26 37 public class JavaLauncher extends DefaultLaunchHandler { 38 39 43 44 private static final Logger _theLogger = Logger.getLogger(JavaLauncher.class); 45 46 50 51 private String _theMainClass; 52 53 54 private String _theArguments; 55 56 57 private String _isDaemon; 58 59 63 66 public JavaLauncher() { 67 } 68 69 73 78 public String getMainClass() { 79 return _theMainClass; 80 } 81 82 87 public String getArgs() { 88 return _theArguments; 89 } 90 91 96 public boolean isDaemon() { 97 return _isDaemon != null && _isDaemon.equals("true"); 98 } 99 100 104 109 public void setMainClass(String aMainClass) { 110 _theMainClass = aMainClass; 111 } 112 113 118 public void setArgs(String anArguments) { 119 _theArguments = anArguments; 120 } 121 122 127 public void setIsDaemon(boolean isDaemon) { 128 if (isDaemon) { 129 _isDaemon = "true"; 130 } else { 131 _isDaemon = "false"; 132 } 133 } 134 135 139 147 protected Classpath findClasspath(String anId, Collection someMagnets) { 148 if (anId == null) { 150 throw new IllegalArgumentException ("The classpath identifer passed in is null"); 151 } else if (someMagnets == null) { 152 throw new IllegalArgumentException ("The collection of magnet passed in is null"); 153 } 154 155 Classpath aResult = null; 156 for (Iterator it = someMagnets.iterator(); aResult == null && it.hasNext(); ) { 157 Magnet aParentMagnet = (Magnet) it.next(); 158 aResult = findClasspath(anId, aParentMagnet); 159 } 160 161 return aResult; 162 } 163 164 172 protected Classpath findClasspath(String anId, Magnet aMagnet) { 173 if (anId == null) { 175 throw new IllegalArgumentException ("The classpath identifer passed in is null"); 176 } else if (aMagnet == null) { 177 throw new IllegalArgumentException ("The magnet passed in is null"); 178 } 179 180 boolean isFound = false; 182 Classpath aResult = null; 183 184 for (Iterator it = aMagnet.getObjectsFor("classpath").iterator(); !isFound && it.hasNext(); ) { 185 Classpath aClasspath = (Classpath) it.next(); 186 if (anId.equals(aClasspath.getId())) { 187 isFound = true; 188 aResult = aClasspath; 189 } 190 } 191 192 if (aResult == null && aMagnet.getParents().size() != 0) { 193 return findClasspath(anId, aMagnet.getParents()); 194 } else { 195 return aResult; 196 } 197 } 198 199 205 protected ClassLoader getParentClassloader(Classpath aClasspath) throws MagnetException { 206 if (aClasspath == null) { 208 throw new RuntimeException ("The classpath passed in is null"); 209 } 210 211 ArrayList someIdentifiers = new ArrayList (); 212 return getParentClassloaderIter(aClasspath, someIdentifiers); 213 } 214 215 218 private ClassLoader getParentClassloaderIter(Classpath aClasspath, List someIdentifiers) throws MagnetException { 219 if (someIdentifiers.contains(aClasspath.getId())) { 221 throw new IllegalStateException ("Circular referenced classpath objects detected: " + aClasspath.getId()); 222 } else { 223 someIdentifiers.add(aClasspath.getId()); 224 } 225 226 if (aClasspath.getParent() == null) { 227 return ClassLoader.getSystemClassLoader(); 228 } else { 229 Classpath aParentClasspath = findClasspath(aClasspath.getParent(), getMagnet()); 230 if (aParentClasspath == null) { 231 String aMessage = "Unable to find the parent classpath: " + aClasspath.getParent(); 232 _theLogger.error(aMessage); 233 throw new MagnetException(aMessage); 234 } 235 236 ClassLoader aParentClassloader = getParentClassloaderIter(aParentClasspath, someIdentifiers); 237 return aParentClasspath.createClassLoader(aParentClassloader); 238 } 239 } 240 241 246 protected String [] parseCommandString() { 247 if (_theArguments != null && _theArguments.length() > 0) { 248 CmdLine aCommandLine = CmdLine.parse(_theArguments); 249 return aCommandLine.toArray(); 250 } else { 251 return new String [0]; 252 } 253 } 254 255 258 private void logClassLoader(ClassLoader aClassLoader) { 259 StringBuffer aBuffer = new StringBuffer (); 260 aBuffer.append("Context class loader is: ").append(aClassLoader).append("\n"); 261 if (aClassLoader instanceof java.net.URLClassLoader ) { 262 java.net.URL [] someURLs = ((java.net.URLClassLoader ) aClassLoader).getURLs(); 263 for (int i = 0; i < someURLs.length; i++) { 264 aBuffer.append("\t[").append(i+1).append("] ").append(someURLs[i]).append("\n"); 265 } 266 } 267 while (aClassLoader.getParent() != null) { 268 aClassLoader = aClassLoader.getParent(); 269 aBuffer.append("Parent class loader is: ").append(aClassLoader).append("\n"); 270 if (aClassLoader instanceof java.net.URLClassLoader ) { 271 java.net.URL [] someURLs = ((java.net.URLClassLoader ) aClassLoader).getURLs(); 272 for (int i = 0; i < someURLs.length; i++) { 273 aBuffer.append("\t[").append(i+1).append("] ").append(someURLs[i]).append("\n"); 274 } 275 } 276 } 277 _theLogger.info(aBuffer.toString()); 278 } 279 280 284 290 public void render(MagnetContext aContext) throws RenderingException { 291 super.render(aContext); 293 294 Profile aProfile = findProfile(aContext.getProfile()); 296 297 if (aProfile != null) { 298 aProfile.render(aContext); 300 MagnetContext aSubContext = new MagnetContext(aContext); 301 if (aProfile.getParameters() != null) { 302 for (Iterator it = aProfile.getParameters().getParams().iterator(); it.hasNext(); ) { 303 Param aParam = (Param) it.next(); 304 aSubContext.addParameter(aParam, false); 305 } 306 } 307 308 _theMainClass = resolveValue(aSubContext, _theMainClass); 310 _theArguments = resolveValue(aSubContext, _theArguments); 311 _isDaemon = resolveValue(aSubContext, _isDaemon); 312 } else { 313 if (aContext.getProfile() == null || aContext.getProfile().length() == 0) { 315 Log.warn("Unable to find a default profile in this system launcher", this); 316 } else { 317 Log.warn("Unable to find the profile " + aContext.getProfile() + " in this system launcher", this); 318 } 319 } 320 } 321 322 327 public void execute(String aProfileName) { 328 try { 329 Profile aProfile = findProfile(aProfileName); 331 if (aProfile == null) { 332 if (aProfileName == null || aProfileName.length() == 0) { 333 Log.warn("Skipping this java launcher --> no default profile defined", this); 334 } else { 335 Log.warn("Skipping this java launcher --> no profile found for the name " + aProfileName, this); 336 } 337 return; 338 } 339 340 Log.info("Executing profile " + aProfile.getName(), this); 341 342 ClassLoader aClassloader = ClassLoader.getSystemClassLoader(); 344 Collection someClasspath = aProfile.getObjectsFor("classpath"); 345 if (!someClasspath.isEmpty()) { 346 Classpath aClasspath = (Classpath) someClasspath.iterator().next(); 348 ClassLoader aParentClassloader = getParentClassloader(aClasspath); 349 aClassloader = aClasspath.createClassLoader(aParentClassloader); 350 } 351 352 String [] someArguments = parseCommandString(); 354 355 logClassLoader(aClassloader); 357 Log.info("Calling the main() method on the class " + _theMainClass, this); 358 359 if (isDaemon()) { 360 Log.info("Executing the main() method using a daemon thread", this); 361 } 362 363 if (someArguments.length > 0) { 364 StringBuffer aBuffer = new StringBuffer (); 365 Log.info("Applicatiom arguments passed to the main() method:", this); 366 for (int i = 0; i < someArguments.length; i++) { 367 aBuffer.setLength(0); 368 aBuffer.append("\targs[").append(i).append("] = ").append(someArguments[i]); 369 Log.info(aBuffer.toString(), this); 370 } 371 } 372 373 Class aClass = aClassloader.loadClass(_theMainClass); 375 JavaTask aTask = new JavaTask(aClass, someArguments); 376 Thread aThread = new Thread (aTask); 377 aThread.setName(getName() + "-" + aProfile.getName()); 378 aThread.setContextClassLoader(aClassloader); 379 aThread.setDaemon(isDaemon()); 380 aThread.start(); 381 382 } catch (MagnetException de) { 383 String aMessage = "Application error spawning this java process"; 384 _theLogger.error(aMessage, de); 385 Log.error(aMessage + " - " + Log.extactMessage(de), this); 386 387 } catch (ClassNotFoundException cnfe) { 388 String aMessage = "Unable to spawn a java process\n\t---> the class specified is not found"; 389 _theLogger.error(aMessage, cnfe); 390 Log.error(aMessage + " - " + Log.extactMessage(cnfe), this); 391 392 } catch (RuntimeException re) { 393 String aMessage = "System error spawning this java process"; 394 _theLogger.error(aMessage, re); 395 Log.error(aMessage + " - " + Log.extactMessage(re), this); 396 } 397 } 398 399 404 public String toString() { 405 StringBuffer aBuffer = new StringBuffer (super.toString()); 406 aBuffer.append("[mainClass=").append(_theMainClass). 407 append(" arguments=").append(_theArguments). 408 append(" isDaemon=").append(_isDaemon). 409 append("]"); 410 411 return aBuffer.toString(); 412 } 413 } 414 415 | Popular Tags |