1 19 20 package com.sslexplorer.extensions; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.util.HashMap ; 25 import java.util.HashSet ; 26 import java.util.Iterator ; 27 import java.util.Map ; 28 import java.util.Set ; 29 30 import org.apache.commons.logging.Log; 31 import org.apache.commons.logging.LogFactory; 32 import org.apache.struts.util.MessageResources; 33 import org.jdom.Element; 34 import org.jdom.JDOMException; 35 36 import com.sslexplorer.boot.PropertyClassManager; 37 import com.sslexplorer.core.CoreUtil; 38 39 public class ApplicationLauncher { 40 final static Log log = LogFactory.getLog(ApplicationLauncher.class); 41 42 HashSet files; 43 HashMap parameters; 44 ExtensionType launcherType; 45 String typeName; 46 MessageResources messageResources; 47 Map tunnels; 48 ExtensionDescriptor descriptor; 49 String description; 50 String id; 51 String name; 52 53 public static final String UNDEFINED_PARAMETER = "UNDEFINED"; 54 55 public ApplicationLauncher(ExtensionDescriptor descriptor) { 56 this.descriptor = descriptor; 57 this.files = new HashSet (); 58 this.parameters = new HashMap (); 59 this.tunnels = new HashMap (); 60 } 61 62 public boolean isOptions() { 63 return parameters.size() > 0; 64 } 65 66 public void load(Element element) throws JDOMException, IOException { 67 parameters.clear(); 68 tunnels.clear(); 69 files.clear(); 70 71 72 if (element.getName().equalsIgnoreCase("launcher")) { 73 throw new JDOMException("Launcher element must be <launcher>"); 74 } 75 76 typeName = element.getAttribute("type").getValue(); 77 if (typeName == null) { 78 throw new JDOMException("<launcher> element requires attribute 'type'"); 79 } 80 try { 82 String clazz = "com.sslexplorer.applications.types." + ( 83 String.valueOf(typeName.charAt(0)).toUpperCase() + typeName.substring(1) ) + "ApplicationType"; 84 if (log.isDebugEnabled()) 85 log.debug("Loading type class " + clazz); 86 launcherType = (ExtensionType)Class.forName(clazz).newInstance(); 87 } 88 catch(Throwable t) { 89 throw new JDOMException("Failed to load launcher extension for type " + typeName + "."); 90 } 91 92 id = (element.getAttribute("launcher").getValue()); 93 94 if (id == null) { 95 throw new JDOMException("<launcher> element requires attribute 'launcher'"); 96 } 97 98 name = element.getAttribute("name").getValue(); 99 if (log.isDebugEnabled()) 100 log.debug("Launcher name is " + name); 101 102 if (name == null) { 103 throw new JDOMException("<launcher> element requires the attribute 'name'"); 104 } 105 106 for (Iterator it = element.getChildren().iterator(); 107 it.hasNext(); ) { 108 Element e = (Element) it.next(); 109 if (e.getName().equalsIgnoreCase("description")) { 110 description = e.getText(); 111 } 112 else if (e.getName().equalsIgnoreCase("parameter")) { 113 addParameter(e); 114 } 115 else if (e.getName().equalsIgnoreCase("messages")) { 116 ExtensionDescriptorMessageResourcesFactory factory = new ExtensionDescriptorMessageResourcesFactory(e); 117 messageResources = factory.createResources("dummy"); 118 if(messageResources == null) { 119 throw new JDOMException("Failed to create message resources."); 120 } 121 } 122 else if (e.getName().equalsIgnoreCase("tunnel")) { 123 verifyTunnel(e); 124 } 125 else if (e.getName().equalsIgnoreCase("files")) { 126 verifyFiles(e); 127 } 128 else { 129 } 131 } 132 133 if(messageResources == null) { 134 throw new JDOMException("No <messages> element supplied."); 135 } 136 } 137 138 public TunnelDescriptor getTunnel(String name) { 139 return (TunnelDescriptor)tunnels.get(name); 140 } 141 142 public ExtensionType getLauncherType() { 143 return launcherType; 144 } 145 146 private void addParameter(Element e) throws JDOMException, IOException { 147 ApplicationParameterDefinition definition = new ApplicationParameterDefinition(e); 148 parameters.put(definition.getName(), 149 definition); 150 } 151 152 private void verifyTunnel(Element e) throws JDOMException, IOException { 153 String name = e.getAttributeValue("name"); 154 if (name == null || name.equals("")) { 155 throw new JDOMException("name attribute required for <tunnel> element"); 156 } 157 String hostname = e.getAttributeValue("hostname"); 158 if (hostname == null || hostname.equals("")) { 159 throw new JDOMException( 160 "hostname attribute required for <tunnel> element"); 161 } 162 String port = e.getAttributeValue("port"); 163 if (port == null || port.equals("")) { 164 throw new JDOMException( 165 "port attribute required for <tunnel> element"); 166 } 167 boolean usePreferredPort = !("false".equals(e.getAttributeValue("usePreferredPort"))); 168 tunnels.put(name, new TunnelDescriptor(name, hostname, port, usePreferredPort)); 169 170 } 171 172 public ExtensionDescriptor getApplicationDescriptor() throws IOException { 173 return descriptor; 174 } 175 176 public Set getParameters() { 177 return parameters.keySet(); 178 } 179 180 public Map getParametersAndDefaults() { 181 return parameters; 182 } 183 184 public ApplicationParameterDefinition getParameterDefinition(String parameter) { 185 return (ApplicationParameterDefinition) parameters.get(parameter); 186 } 187 188 private void verifyFiles(Element element) throws JDOMException, IOException { 189 190 for (Iterator it = element.getChildren().iterator(); 191 it.hasNext(); ) { 192 Element e = (Element) it.next(); 193 if (e.getName().equalsIgnoreCase("if")) { 194 verifyFiles(e); 195 } 196 else if (!e.getName().equalsIgnoreCase("file")) { 197 throw new JDOMException("Unexpected element <" + e.getName() + 198 "> found in <files>"); 199 } 200 else { 201 processFile(e); 202 } 203 } 204 205 } 206 207 public void processFile(Element e) throws IOException { 208 209 String filename = e.getText(); 210 211 File entry = new File (descriptor.getApplicationBundle().getBaseDir(), filename); 212 213 if (!entry.exists()) { 214 throw new IOException ( 215 "File specified in extension.xml does not exist! " 216 + entry.getAbsolutePath()); 217 } 218 219 e.setAttribute("checksum", String.valueOf(CoreUtil.generateChecksum(entry))); 220 e.setAttribute("size", String.valueOf(entry.length())); 221 222 files.add(e.getText()); 223 } 224 225 public boolean containsFile(String filename) { 226 return files.contains(filename); 227 } 228 229 public File getFile(String filename) throws IOException { 230 if (!containsFile(filename)) { 231 throw new IOException (filename + " is not a valid application file"); 232 } 233 return new File (descriptor.getApplicationBundle().getBaseDir(), filename); 234 } 235 236 239 public MessageResources getMessageResources() { 240 return messageResources; 241 } 242 243 public class TunnelDescriptor { 244 private String name; 245 private String hostname; 246 private String port; 247 private boolean usePreferredPort; 248 249 public TunnelDescriptor(String name, String hostname, String port, boolean usePreferredPort) { 250 this.name = name; 251 this.hostname = hostname; 252 this.port = port; 253 this.usePreferredPort = usePreferredPort; 254 } 255 256 public String getName() { 257 return name; 258 } 259 260 public String getHostname() { 261 return hostname; 262 } 263 264 public String getPort() { 265 return port; 266 } 267 268 public boolean isUsePreferredPort() { 269 return usePreferredPort; 270 } 271 } 272 273 } 274 | Popular Tags |