1 2 package com.sslexplorer.agent.client.util.types; 3 4 import java.io.File ; 5 import java.io.IOException ; 6 import java.lang.reflect.Method ; 7 import java.text.MessageFormat ; 8 import java.util.Enumeration ; 9 import java.util.Vector ; 10 11 import com.sslexplorer.agent.client.util.AbstractApplicationLauncher; 12 import com.sslexplorer.agent.client.util.ApplicationLauncherEvents; 13 import com.sslexplorer.agent.client.util.ApplicationType; 14 import com.sslexplorer.agent.client.util.ProcessMonitor; 15 import com.sslexplorer.agent.client.util.Utils; 16 import com.sslexplorer.agent.client.util.XMLElement; 17 18 23 public class JavaApplicationType implements ApplicationType { 24 25 27 protected ApplicationLauncherEvents events; 28 protected AbstractApplicationLauncher launcher; 29 30 32 private String classpath = ""; private String mainclass; 34 private File workingDir; 35 private String [] jvm; 36 private Vector programArgs = new Vector (); 37 private Vector jvmArgs = new Vector (); 38 private Vector envVars = new Vector (); 39 private ProcessMonitor process; 40 private String javaLibraryPath = ""; 42 48 public void prepare(AbstractApplicationLauncher launcher, ApplicationLauncherEvents events, XMLElement element) 49 throws IOException { 50 51 if (events != null) 52 events.debug(MessageFormat.format(Messages.getString("JavaApplicationType.processingForJavaType"), new Object [] { element.getName() })); 54 this.launcher = launcher; 55 this.events = events; 56 57 if (element.getName().equals(getTypeName())) { 59 String jre = (String ) element.getAttribute("jre"); 61 if (events != null) 62 events.debug(MessageFormat.format(Messages.getString("JavaApplicationType.checkingJRE"), new Object [] { jre })); 64 65 if (!Utils.checkVersion(jre)) { 66 String mesage = Messages.getString("JavaApplicationType.applicationRequires", new Object [] { jre }); if (events != null) { 68 events.error(mesage); 69 } 70 return; 71 } 72 73 78 if (System.getProperty("java.version").startsWith("1.1") && !System.getProperty("java.vendor").startsWith( "Microsoft")) classpath = System.getProperty("java.home") + File.pathSeparator 85 + "lib" + File.pathSeparator + "classes.zip"; 88 Enumeration e = element.enumerateChildren(); 89 90 while (e.hasMoreElements()) { 91 XMLElement el = (XMLElement) e.nextElement(); 92 93 if (el.getName().equalsIgnoreCase("classpath")) { buildClassPath(el); 95 } else if (el.getName().equalsIgnoreCase("main")) { mainclass = (String ) el.getAttribute("class"); if (events != null) 98 events.debug(MessageFormat.format(Messages.getString("JavaApplicationType.mainClass"), new Object [] { mainclass })); String dir = (String ) el.getAttribute("dir"); if (events != null) 101 events.debug(MessageFormat.format(Messages.getString("JavaApplicationType.directoryIs"), new Object [] { dir })); if (dir != null) { 103 workingDir = new File (launcher.replaceTokens(dir)); 104 } else { 105 workingDir = null; 106 } 107 buildProgramArguments(el); 108 } 109 } 110 111 if (events != null) 112 events.debug(Messages.getString("JavaApplicationType.finishedPreparingDescriptor")); } else { 114 if (events != null) 115 events.debug(MessageFormat.format(Messages.getString("JavaApplicationType.ignoringTagAsNotJavaTag"), new Object [] { element.getName() })); } 117 118 } 119 120 125 public void start() { 126 execute(classpath, mainclass, workingDir); 127 } 128 129 135 public void addClasspathEntry(XMLElement e) throws IOException { 136 addClasspathEntry(e, null); 137 } 138 139 146 public void addClasspathEntry(XMLElement e, String app) throws IOException { 147 148 String shared = (String ) e.getAttribute("shared"); File entry; 150 if (shared != null && shared.equalsIgnoreCase("true")) { entry = launcher.addShared(e); 152 } else { 153 entry = launcher.addFile(e, app); 154 } 155 156 if (entry != null && events != null) 157 events.debug(MessageFormat.format(Messages.getString("JavaApplicationType.addingToClasspath"), new Object [] { entry.getAbsolutePath() })); 159 if (entry != null) 161 classpath += (!classpath.equals("") ? File.pathSeparator : "") + entry.getAbsolutePath(); 163 } 164 165 protected void buildClassPath(XMLElement element) throws IOException { 166 buildClassPath(element, null); 167 } 168 169 protected void buildClassPath(XMLElement element, String app) throws IOException { 170 171 if (events != null) 172 events.debug(Messages.getString("JavaApplicationType.buildingClasspath")); Enumeration en = element.enumerateChildren(); 174 XMLElement e; 175 176 while (en.hasMoreElements()) { 177 e = (XMLElement) en.nextElement(); 178 if (e.getName().equalsIgnoreCase("jar")) { if (AbstractApplicationLauncher.checkCondition(this, e, launcher.getDescriptorParams())) { 180 addClasspathEntry(e, app); 181 } 182 } else if (e.getName().equals("if")) { if (AbstractApplicationLauncher.checkCondition(this, e, launcher.getDescriptorParams())) { 184 buildClassPath(e, app); 185 } 186 } else 187 throw new IOException (MessageFormat.format(Messages.getString("JavaApplicationType.invalidElementInClasspath"), new Object [] { e.getName() })); } 189 190 } 191 192 protected void addArgument(String arg) { 193 if (arg != null) 194 programArgs.addElement(launcher.replaceTokens(arg)); 195 } 196 197 protected void addEnvVar(String envVar) { 198 if (envVar != null) 199 envVars.addElement(launcher.replaceTokens(envVar)); 200 } 201 202 protected void addJVMArgument(String arg) { 203 if (arg != null) { 204 205 if (arg.startsWith("java.library.path")) { int idx = arg.indexOf('='); 207 208 if (idx > -1) { 209 String val = arg.substring(idx + 1).replace('/', File.separatorChar); 210 javaLibraryPath += (javaLibraryPath.equals("") ? val : System.getProperty("path.separator") + val); 212 if (events != null) 213 events.debug(MessageFormat.format(Messages.getString("JavaApplicationType.valueAppendedToJavaLibraryPath"), new Object [] { val })); } else if (events != null) 215 events.debug(MessageFormat.format(Messages.getString("JavaApplicationType.invalidJavaLibraryPath"), new Object [] { arg })); 217 } else 218 jvmArgs.addElement(launcher.replaceTokens(arg)); 219 } 220 } 221 222 private void addArgument(XMLElement e) throws IOException { 223 if (e.getName().equalsIgnoreCase("arg")) addArgument(Utils.trimmedBothOrBlank(e.getContent())); 225 else if (e.getName().equalsIgnoreCase("env")) addEnvVar(Utils.trimmedBothOrBlank(e.getContent())); 227 else if (e.getName().equalsIgnoreCase("jvm")) { addJVMArgument(Utils.trimmedBothOrBlank(e.getContent())); 229 } else { 230 throw new IOException (MessageFormat.format(Messages.getString("JavaApplicationType.unexpectedElementFound"), new Object [] { e.getName() })); } 232 } 233 234 private void buildProgramArguments(XMLElement element) throws IOException { 235 236 Enumeration en = element.enumerateChildren(); 237 238 while (en.hasMoreElements()) { 239 240 XMLElement e = (XMLElement) en.nextElement(); 241 if (e.getName().equalsIgnoreCase("arg")) addArgument(e); 243 else if (e.getName().equalsIgnoreCase("jvm")) { addArgument(e); 245 } else if (e.getName().equalsIgnoreCase("env")) { addArgument(e); 247 } else if (e.getName().equalsIgnoreCase("if")) { 249 String jre = (String ) e.getAttribute("jre"); if (jre == null) { 251 String parameter = (String ) e.getAttribute("parameter"); boolean not = "true".equalsIgnoreCase((String ) e .getAttribute("not")); 255 if (parameter != null) { 256 String requiredValue = (String ) e.getAttribute("value"); 258 String value = (String ) launcher.getDescriptorParams().get(parameter); 260 261 if ((!not && requiredValue.equalsIgnoreCase(value)) || (not && !requiredValue.equalsIgnoreCase(value))) { 262 buildProgramArguments(e); 263 } 264 265 } else 266 throw new IOException (Messages.getString("JavaApplicationType.ifElementRequiresJreOrParameterAttribute")); } else { 268 if (Utils.isSupportedJRE(jre)) { 270 buildProgramArguments(e); 271 } 272 273 } 274 275 } else 276 throw new IOException (MessageFormat.format(Messages.getString("JavaApplicationType.unexpectedElementFoundInMain"), new Object [] { e.getName() })); } 278 279 } 280 281 private void execute(String classpath, String mainclass, File workingDir) { 282 283 String [] args = new String [programArgs.size()]; 284 programArgs.copyInto(args); 285 addDefaultEnvironment(); 286 String [] envp = new String [envVars.size()]; 287 envVars.copyInto(envp); 288 289 if (!javaLibraryPath.equals("")) jvmArgs.addElement("java.library.path=" + launcher.replaceTokens(javaLibraryPath)); 292 jvm = new String [jvmArgs.size()]; 293 jvmArgs.copyInto(jvm); 294 295 String [] cmdargs = new String [jvm.length + args.length + 4]; 296 297 if (!System.getProperty("java.vendor").startsWith("Microsoft")) { 302 cmdargs[0] = Utils.getJavaHome() + File.separator + "bin" + File.separator + "java"; cmdargs[1] = "-classpath"; cmdargs[2] = classpath; 306 307 for (int i = 0; i < jvm.length; i++) { 308 cmdargs[3 + i] = "-D" + jvm[i]; } 310 311 cmdargs[jvm.length + 3] = mainclass; 312 313 System.arraycopy(args, 0, cmdargs, jvm.length + 4, args.length); 314 315 } else { 316 320 cmdargs[0] = "jview.exe"; cmdargs[1] = "/cp"; cmdargs[2] = "\"" + classpath + "\""; 324 for (int i = 0; i < jvm.length; i++) { 325 cmdargs[3 + i] = "\"/d:" + jvm[i] + "\""; } 327 328 cmdargs[jvm.length + 3] = mainclass; 329 330 for (int i = 0; i < args.length; i++) { 331 if (args[i].indexOf(' ') > -1) 332 cmdargs[4 + i + jvm.length] = "\"" + args[i] + "\""; else 334 cmdargs[4 + i + jvm.length] = args[i]; 335 } 336 337 } 338 339 String cmdline = ""; for (int i = 0; i < cmdargs.length; i++) 341 cmdline += " " + cmdargs[i]; 343 if (events != null) 344 events.debug(MessageFormat.format(Messages.getString("JavaApplicationType.executingCommand"), new Object [] { cmdline })); 346 try { 347 348 if (events != null) 349 events.executingApplication(launcher.getName(), cmdline.trim()); 350 351 352 358 360 382 try { 384 Method m = Runtime .class.getMethod("exec", new Class [] { String [].class, String [].class, File .class }); 386 Process prc = (Process ) m.invoke(Runtime.getRuntime(), new Object [] { cmdargs, envp, workingDir }); 387 process = new ProcessMonitor(launcher.getName(), prc); 388 } catch (Throwable t) { 389 if (workingDir != null) { 390 throw new IOException (Messages.getString("JavaApplicationType.failedToChangeWorkingDirectory") + "changed, but this Java Virtual Machine does not support that."); } 393 process = new ProcessMonitor(launcher.getName(), Runtime.getRuntime().exec(cmdargs)); 394 } 395 } catch (IOException ex) { 396 if (events != null) 397 events.debug(MessageFormat.format(Messages.getString("JavaApplicationType.processExecutionFailed"), new Object [] { ex.getMessage() })); } 399 400 } 401 402 void addDefaultEnvironment() { 403 406 try { 407 Class iteratorClass = getClass().getClassLoader().loadClass("java.util.Iterator"); 408 Class mapClass = getClass().getClassLoader().loadClass("java.util.Map"); 409 Object mapObject = System .class.getMethod("getenv", new Class [] { }).invoke(null, new Object [] { }); 410 Object setObject = mapClass.getMethod("keySet", new Class [] { }).invoke(mapObject, new Object [] { }); 411 Object iteratorObject = getClass().getClassLoader().loadClass("java.util.Set").getMethod("iterator", new Class [] { }).invoke(setObject, new Object [] { }); 412 while( ((Boolean )iteratorClass.getMethod("hasNext", new Class [] { }).invoke(iteratorObject, new Object [] { })).booleanValue()) { 413 String name = (String )iteratorClass.getMethod("next", new Class [] { }).invoke(iteratorObject, new Object [] { }); 414 if(!envVarExists(name)) { 415 envVars.addElement(name + "=" + ( mapClass.getMethod("get", new Class [] { Object .class }).invoke(mapObject, new Object [] { name }))); 416 } 417 } 418 } catch (Exception e) { 419 } 420 421 } 422 423 boolean envVarExists(String name) { 424 for(Enumeration e = envVars.elements(); e.hasMoreElements(); ) { 425 if(((String )e.nextElement()).startsWith(name)) { 426 return true; 427 } 428 } 429 return false; 430 } 431 432 437 public ProcessMonitor getProcessMonitor() { 438 return process; 439 } 440 441 446 public String getRedirectParameters() { 447 return null; 448 } 449 450 455 public String getTypeName() { 456 return "java"; 457 } 458 } 459 | Popular Tags |