KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > extensions > ApplicationLauncher


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.extensions;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.Set JavaDoc;
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 JavaDoc files;
43   HashMap JavaDoc parameters;
44   ExtensionType launcherType;
45   String JavaDoc typeName;
46   MessageResources messageResources;
47   Map JavaDoc tunnels;
48   ExtensionDescriptor descriptor;
49   String JavaDoc description;
50   String JavaDoc id;
51   String JavaDoc name;
52
53   public static final String JavaDoc UNDEFINED_PARAMETER = "UNDEFINED";
54
55   public ApplicationLauncher(ExtensionDescriptor descriptor) {
56     this.descriptor = descriptor;
57     this.files = new HashSet JavaDoc();
58     this.parameters = new HashMap JavaDoc();
59     this.tunnels = new HashMap JavaDoc();
60   }
61
62   public boolean isOptions() {
63     return parameters.size() > 0;
64   }
65
66   public void load(Element element) throws JDOMException, IOException JavaDoc {
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     //
81
try {
82       String JavaDoc 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 JavaDoc 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 JavaDoc 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 // launcherType.load(this, e);
130
}
131     }
132     
133     if(messageResources == null) {
134       throw new JDOMException("No <messages> element supplied.");
135     }
136   }
137   
138   public TunnelDescriptor getTunnel(String JavaDoc 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 JavaDoc {
147     ApplicationParameterDefinition definition = new ApplicationParameterDefinition(e);
148     parameters.put(definition.getName(),
149                     definition);
150   }
151
152   private void verifyTunnel(Element e) throws JDOMException, IOException JavaDoc {
153     String JavaDoc name = e.getAttributeValue("name");
154     if (name == null || name.equals("")) {
155       throw new JDOMException("name attribute required for <tunnel> element");
156     }
157     String JavaDoc hostname = e.getAttributeValue("hostname");
158     if (hostname == null || hostname.equals("")) {
159       throw new JDOMException(
160           "hostname attribute required for <tunnel> element");
161     }
162     String JavaDoc 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 JavaDoc {
173     return descriptor;
174   }
175
176   public Set JavaDoc getParameters() {
177     return parameters.keySet();
178   }
179
180   public Map JavaDoc getParametersAndDefaults() {
181     return parameters;
182   }
183
184   public ApplicationParameterDefinition getParameterDefinition(String JavaDoc parameter) {
185     return (ApplicationParameterDefinition) parameters.get(parameter);
186   }
187
188   private void verifyFiles(Element element) throws JDOMException, IOException JavaDoc {
189
190     for (Iterator JavaDoc 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 JavaDoc {
208
209     String JavaDoc filename = e.getText();
210     
211     File JavaDoc entry = new File JavaDoc(descriptor.getApplicationBundle().getBaseDir(), filename);
212
213     if (!entry.exists()) {
214       throw new IOException JavaDoc(
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 JavaDoc filename) {
226     return files.contains(filename);
227   }
228
229   public File JavaDoc getFile(String JavaDoc filename) throws IOException JavaDoc {
230     if (!containsFile(filename)) {
231       throw new IOException JavaDoc(filename + " is not a valid application file");
232     }
233     return new File JavaDoc(descriptor.getApplicationBundle().getBaseDir(), filename);
234   }
235
236   /**
237    * @return
238    */

239   public MessageResources getMessageResources() {
240     return messageResources;
241   }
242   
243   public class TunnelDescriptor {
244     private String JavaDoc name;
245     private String JavaDoc hostname;
246     private String JavaDoc port;
247     private boolean usePreferredPort;
248     
249     public TunnelDescriptor(String JavaDoc name, String JavaDoc hostname, String JavaDoc port, boolean usePreferredPort) {
250       this.name = name;
251       this.hostname = hostname;
252       this.port = port;
253       this.usePreferredPort = usePreferredPort;
254     }
255     
256     public String JavaDoc getName() {
257       return name;
258     }
259     
260     public String JavaDoc getHostname() {
261       return hostname;
262     }
263     
264     public String JavaDoc getPort() {
265       return port;
266     }
267     
268     public boolean isUsePreferredPort() {
269       return usePreferredPort;
270     }
271   }
272
273 }
274
Popular Tags