1 19 20 package com.sslexplorer.agent.client.util.types; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.lang.reflect.Method ; 25 import java.text.MessageFormat ; 26 import java.util.Enumeration ; 27 import java.util.Vector ; 28 29 import com.sslexplorer.agent.client.util.AbstractApplicationLauncher; 30 import com.sslexplorer.agent.client.util.ApplicationLauncherEvents; 31 import com.sslexplorer.agent.client.util.ApplicationType; 32 import com.sslexplorer.agent.client.util.ProcessMonitor; 33 import com.sslexplorer.agent.client.util.Utils; 34 import com.sslexplorer.agent.client.util.XMLElement; 35 36 42 public class ExecutableApplicationType implements ApplicationType { 43 44 46 private ApplicationLauncherEvents events; 47 private AbstractApplicationLauncher launcher; 48 private String program; 49 private File workingDir; 50 private Vector programArgs = new Vector (); 51 private ProcessMonitor process; 52 53 59 public void prepare(AbstractApplicationLauncher launcher, ApplicationLauncherEvents events, XMLElement element) throws IOException { 60 this.launcher = launcher; 61 this.events = events; 62 63 if (element.getName().equalsIgnoreCase("executable")) { String programName = (String ) element.getAttribute("program"); if(programName == null) { 66 programName = findProgram(element); 67 } 68 program = launcher.replaceTokens(programName); 69 program = launcher.replaceAllTokens(program, "/", File.separator); 70 String dir = (String ) element.getAttribute("dir"); if (dir != null) { 72 workingDir = new File (launcher.replaceTokens(dir)); 73 } else { 74 workingDir = null; 75 } 76 buildProgramArguments(element); 77 } 78 } 79 80 83 public void start() { 84 execute(program, workingDir); 85 } 86 87 private void addArgument(XMLElement e) throws IOException { 88 if (e.getName().equalsIgnoreCase("arg")) { 90 String arg = launcher.replaceTokens(Utils.trimmedBothOrBlank(e.getContent())); 91 if (arg.indexOf(' ') > -1) 92 arg = "\"" + arg + "\""; programArgs.addElement(arg); 94 } else { 95 throw new IOException (MessageFormat.format( 96 Messages.getString("ExecutableApplicationType.unexpectedElementFound"), new Object [] { e.getName() } ) ); 98 } 99 } 100 101 private void buildProgramArguments(XMLElement element) throws IOException { 102 103 Enumeration en = element.enumerateChildren(); 104 105 while (en.hasMoreElements()) { 106 107 XMLElement e = (XMLElement) en.nextElement(); 108 if (e.getName().equalsIgnoreCase("arg")) addArgument(e); 110 else if (e.getName().equalsIgnoreCase("program")) continue; 112 else if (e.getName().equalsIgnoreCase("if")) { if (AbstractApplicationLauncher.checkCondition(this, e, launcher.getDescriptorParams())) { 114 buildProgramArguments(e); 115 } 116 } else 117 throw new IOException (MessageFormat.format(Messages.getString("ExecutableApplicationType.unexpectedElementFoundInExecutable"), new Object [] { e.getName() } ) ) ; } 119 120 } 121 122 private String findProgram(XMLElement element) throws IOException { 123 Enumeration en = element.enumerateChildren(); 124 125 while (en.hasMoreElements()) { 126 127 XMLElement e = (XMLElement) en.nextElement(); 128 if (e.getName().equalsIgnoreCase("program")) return Utils.trimmedBothOrBlank(e.getContent()); 130 else if (e.getName().equalsIgnoreCase("if")) { if (AbstractApplicationLauncher.checkCondition(this, e, launcher.getDescriptorParams())) { 132 String program = findProgram(e); 133 if(program != null) { 134 return program; 135 } 136 } 137 } 138 } 139 throw new IOException ("No valid program found for conditions."); 140 141 } 142 143 private void execute(String program, File workingDir) { 144 145 String [] args = new String [programArgs.size()]; 146 programArgs.copyInto(args); 147 148 File tmp = new File (workingDir != null ? workingDir.getAbsolutePath() : launcher.getInstallDir().getAbsolutePath(), program); 154 if (tmp.exists()) 155 program = tmp.getAbsolutePath(); 156 157 String [] cmdargs = new String [args.length + 1]; 158 System.arraycopy(args, 0, cmdargs, 1, args.length); 159 if (program.indexOf(' ') > -1) 160 program = "\"" + program + "\""; cmdargs[0] = program; 162 163 String cmdline = ""; for (int i = 0; i < cmdargs.length; i++) 165 cmdline += " " + cmdargs[i]; 167 if (events != null) 168 events.debug(MessageFormat.format(Messages.getString("ExecutableApplicationType.executingCommand"), new Object [] { cmdline } ) ); 170 try { 171 172 if (events != null) 173 events.executingApplication(launcher.getName(), cmdline.trim()); 174 175 try { 177 Method m = Runtime .class.getMethod("exec", new Class [] { String [].class, String [].class, File .class }); process = new ProcessMonitor(launcher.getName(), (Process ) m.invoke(Runtime.getRuntime(), new Object [] { cmdargs, 179 null, 180 workingDir })); 181 } catch (Throwable t) { 182 if (workingDir != null) { 183 t.printStackTrace(); 184 185 187 throw new IOException (Messages.getString("ExecutableApplicationType.applicationRequestsThatTheWorkingDirectoryIsChange") + ""); } 189 process = new ProcessMonitor(launcher.getName(), Runtime.getRuntime().exec(cmdargs)); 190 } 191 } catch (IOException ex) { 192 if (events != null) 193 events.debug(MessageFormat.format(Messages.getString("ExecutableApplicationType.processExecutionFailed"), new Object [] { ex.getMessage() } ) ); } 195 196 } 197 198 203 public ProcessMonitor getProcessMonitor() { 204 return process; 205 } 206 207 212 public String getRedirectParameters() { 213 return null; 214 } 215 216 219 public String getTypeName() { 220 return "executable"; 221 } 222 223 } 224 | Popular Tags |