1 19 20 package com.sslexplorer.applications.types; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.util.ArrayList ; 25 import java.util.Enumeration ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import java.util.Map ; 29 30 import javax.servlet.http.HttpServletRequest ; 31 32 import org.apache.commons.logging.Log; 33 import org.apache.commons.logging.LogFactory; 34 import org.apache.struts.action.ActionForward; 35 import org.apache.struts.action.ActionMapping; 36 import org.jdom.Element; 37 38 import com.sslexplorer.applications.ApplicationLauncherType; 39 import com.sslexplorer.applications.ApplicationShortcut; 40 import com.sslexplorer.applications.server.ApplicationServerType; 41 import com.sslexplorer.applications.server.ProcessMonitor; 42 import com.sslexplorer.applications.server.ServerApplicationLauncher; 43 import com.sslexplorer.applications.server.ServerLauncher; 44 import com.sslexplorer.applications.server.ServerLauncherEvents; 45 import com.sslexplorer.boot.XMLElement; 46 import com.sslexplorer.extensions.ExtensionDescriptor; 47 import com.sslexplorer.extensions.ExtensionException; 48 import com.sslexplorer.policyframework.LaunchSession; 49 import com.sslexplorer.security.SessionInfo; 50 51 59 60 public class ServerType implements ApplicationLauncherType, 61 ApplicationServerType { 62 63 66 public final static String TYPE = "server"; 67 68 private ServerLauncher launcher; 69 70 private String program; 71 72 private File workingDir; 73 74 private List <String > programArgs = new ArrayList <String >(); 75 76 private ProcessMonitor process; 77 78 static Log log = LogFactory.getLog(ServerType.class); 79 80 public void start(ExtensionDescriptor descriptor, Element element) 81 throws ExtensionException { 82 83 if (element.getName().equals(TYPE)) { 84 verifyExecutable(element); 85 } 86 87 } 88 89 public void verifyRequiredElements() throws ExtensionException { 90 } 91 92 public boolean isHidden() { 93 return false; 94 } 95 96 public String getType() { 97 return TYPE; 98 } 99 100 public void prepare(ServerLauncher launcher, ServerLauncherEvents events, 101 XMLElement element) throws IOException { 102 this.launcher = launcher; 103 104 if (element.getName().equalsIgnoreCase(getType())) { 105 program = launcher.replaceTokens((String ) element 106 .getAttribute("program")); 107 String dir = (String ) element.getAttribute("dir"); 108 if (dir != null) { 109 workingDir = new File (launcher.replaceTokens(dir)); 110 } else { 111 workingDir = null; 112 } 113 buildProgramArguments(element); 114 } 115 } 116 117 public void start() { 118 execute(program, workingDir); 119 } 120 121 public boolean checkFileCondition(XMLElement el) throws IOException , 122 IllegalArgumentException { 123 throw new IllegalArgumentException ( 124 "No supported attributes in condition."); 125 } 126 127 public ProcessMonitor getProcessMonitor() { 128 return process; 129 } 130 131 public void stop() throws ExtensionException { 132 } 133 134 public ActionForward launch(Map <String , String > parameters, 135 ExtensionDescriptor descriptor, ApplicationShortcut shortcut, 136 ActionMapping mapping, LaunchSession launchSession, 137 String returnTo, HttpServletRequest request) 138 throws ExtensionException { 139 if (log.isInfoEnabled()) 140 log.info("Starting server application " 141 + shortcut.getResourceName()); 142 143 ServerApplicationLauncher app; 144 try { 145 app = new ServerApplicationLauncher(parameters, shortcut 146 .getApplication(), launchSession.getSession(), shortcut); 147 app.start(); 148 } catch (Exception e) { 149 throw new ExtensionException(ExtensionException.FAILED_TO_LAUNCH, e); 150 } 151 152 return null; 153 } 154 155 public boolean isAgentRequired(ApplicationShortcut shortcut, 156 ExtensionDescriptor descriptor) { 157 return false; 158 } 159 160 private void verifyExecutable(Element element) throws ExtensionException { 161 for (Iterator it = element.getChildren().iterator(); it.hasNext();) { 162 Element e = (Element) it.next(); 163 if (e.getName().equalsIgnoreCase("if")) { 164 verifyExecutable(e); 165 } else if (!e.getName().equalsIgnoreCase("arg") 166 && !e.getName().equalsIgnoreCase("jvm")) { 167 throw new ExtensionException( 168 ExtensionException.FAILED_TO_PROCESS_DESCRIPTOR, 169 "Unexpected element <" + e.getName() 170 + "> found in <executable>"); 171 } 172 } 173 174 } 175 176 private void addArgument(XMLElement e) throws IOException { 177 if (e.getName().equalsIgnoreCase("arg")) { 178 179 String arg = launcher.replaceTokens(e.getContent()); 180 if (arg.indexOf(' ') > -1) 181 arg = "\"" + arg + "\""; 182 programArgs.add(arg); 183 } else { 184 throw new IOException ("Unexpected element <" + e.getName() 185 + "> found"); 186 } 187 } 188 189 private void buildProgramArguments(XMLElement element) throws IOException { 190 191 Enumeration en = element.enumerateChildren(); 192 193 while (en.hasMoreElements()) { 194 195 XMLElement e = (XMLElement) en.nextElement(); 196 if (e.getName().equalsIgnoreCase("arg")) 197 addArgument(e); 198 else if (e.getName().equalsIgnoreCase("if")) { 199 200 try { 201 if (checkFileCondition(e)) { 202 buildProgramArguments(e); 203 } 204 } catch (IllegalArgumentException iae) { 205 206 String parameter = (String ) e.getAttribute("parameter"); 207 boolean not = "true".equalsIgnoreCase(((String ) e 208 .getAttribute("not"))); 209 210 if (parameter != null) { 211 String requiredValue = (String ) e.getAttribute("value"); 212 213 String value = (String ) launcher.getDescriptorParams() 214 .get(parameter); 215 216 if ((!not && requiredValue.equalsIgnoreCase(value)) 217 || (not && !requiredValue 218 .equalsIgnoreCase(value))) { 219 buildProgramArguments(e); 220 } 221 222 } else 223 throw new IOException ( 224 "<if> element requires parameter attribute"); 225 } 226 227 } else 228 throw new IOException ("Unexpected element <" + e.getName() 229 + "> found in <executable>"); 230 } 231 232 } 233 234 private void execute(String program, File workingDir) { 235 236 List <String > fullArgs = new ArrayList <String >(programArgs); 237 238 240 File tmp = new File (workingDir != null ? workingDir.getAbsolutePath() 241 : launcher.getInstallDir().getAbsolutePath(), program); 242 if (tmp.exists()) 243 program = tmp.getAbsolutePath(); 244 if (program.indexOf(' ') > -1) 245 program = "\"" + program + "\""; 246 fullArgs.add(0, program); 247 248 250 if(program.toLowerCase().endsWith(".exe") && System.getProperty("os.name").toLowerCase().startsWith("linux")) { 251 fullArgs.add(0, "wine"); 252 } 253 254 256 String [] args = new String [fullArgs.size()]; 257 fullArgs.toArray(args); 258 259 261 String cmdline = ""; 262 for (int i = 0; i < args.length; i++) 263 cmdline += " " + args[i]; 264 265 if (log.isDebugEnabled()) 266 log.debug("Executing command: " + cmdline); 267 268 try { 269 Process prc = Runtime.getRuntime().exec(args, null, workingDir); 270 process = new ProcessMonitor(launcher.getName(), prc); 271 } catch (IOException ex) { 272 log.error("Failed to launch server command", ex); 273 } 274 275 } 276 277 public void activate() throws ExtensionException { 278 } 279 280 public boolean canStop() throws ExtensionException { 281 return false; 282 } 283 284 289 public void descriptorCreated(Element element, SessionInfo session) 290 throws IOException { 291 } 292 293 298 public String getTypeBundle() { 299 return "applications"; 300 } 301 302 307 public boolean isServerSide() { 308 return true; 309 } 310 } | Popular Tags |