KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > applications > types > ServerType


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.applications.types;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Enumeration JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import javax.servlet.http.HttpServletRequest JavaDoc;
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 /**
52  * Implementation of an
53  * {@link com.sslexplorer.applications.ApplicationLauncherType} that allows
54  * launching of native applications installed on the server.
55  *
56  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
57  * @author Sebastien Belin <a HREF="mailto: seb@3sp.com">&lt;seb@3sp.com&gt;</a>
58  */

59
60 public class ServerType implements ApplicationLauncherType,
61         ApplicationServerType {
62
63     /**
64      * Type name
65      */

66     public final static String JavaDoc TYPE = "server";
67
68     private ServerLauncher launcher;
69
70     private String JavaDoc program;
71
72     private File JavaDoc workingDir;
73
74     private List JavaDoc<String JavaDoc> programArgs = new ArrayList JavaDoc<String JavaDoc>();
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 JavaDoc getType() {
97         return TYPE;
98     }
99
100     public void prepare(ServerLauncher launcher, ServerLauncherEvents events,
101             XMLElement element) throws IOException JavaDoc {
102         this.launcher = launcher;
103
104         if (element.getName().equalsIgnoreCase(getType())) {
105             program = launcher.replaceTokens((String JavaDoc) element
106                     .getAttribute("program"));
107             String JavaDoc dir = (String JavaDoc) element.getAttribute("dir");
108             if (dir != null) {
109                 workingDir = new File JavaDoc(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 JavaDoc,
122             IllegalArgumentException JavaDoc {
123         throw new IllegalArgumentException JavaDoc(
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 JavaDoc<String JavaDoc, String JavaDoc> parameters,
135             ExtensionDescriptor descriptor, ApplicationShortcut shortcut,
136             ActionMapping mapping, LaunchSession launchSession,
137             String JavaDoc returnTo, HttpServletRequest JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc {
177         if (e.getName().equalsIgnoreCase("arg")) {
178
179             String JavaDoc arg = launcher.replaceTokens(e.getContent());
180             if (arg.indexOf(' ') > -1)
181                 arg = "\"" + arg + "\"";
182             programArgs.add(arg);
183         } else {
184             throw new IOException JavaDoc("Unexpected element <" + e.getName()
185                     + "> found");
186         }
187     }
188
189     private void buildProgramArguments(XMLElement element) throws IOException JavaDoc {
190
191         Enumeration JavaDoc 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 JavaDoc iae) {
205
206                     String JavaDoc parameter = (String JavaDoc) e.getAttribute("parameter");
207                     boolean not = "true".equalsIgnoreCase(((String JavaDoc) e
208                             .getAttribute("not")));
209
210                     if (parameter != null) {
211                         String JavaDoc requiredValue = (String JavaDoc) e.getAttribute("value");
212
213                         String JavaDoc value = (String JavaDoc) 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 JavaDoc(
224                                 "<if> element requires parameter attribute");
225                 }
226
227             } else
228                 throw new IOException JavaDoc("Unexpected element <" + e.getName()
229                         + "> found in <executable>");
230         }
231
232     }
233
234     private void execute(String JavaDoc program, File JavaDoc workingDir) {
235         
236         List JavaDoc<String JavaDoc> fullArgs = new ArrayList JavaDoc<String JavaDoc>(programArgs);
237
238         // Add the program to execute
239

240         File JavaDoc tmp = new File JavaDoc(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         // Let windows executables work on Linux using Wine
249

250         if(program.toLowerCase().endsWith(".exe") && System.getProperty("os.name").toLowerCase().startsWith("linux")) {
251             fullArgs.add(0, "wine");
252         }
253         
254         // To the array
255

256         String JavaDoc[] args = new String JavaDoc[fullArgs.size()];
257         fullArgs.toArray(args);
258
259         // Build up the command line (for debug only)
260

261         String JavaDoc 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 JavaDoc prc = Runtime.getRuntime().exec(args, null, workingDir);
270             process = new ProcessMonitor(launcher.getName(), prc);
271         } catch (IOException JavaDoc 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     /*
285      * (non-Javadoc)
286      *
287      * @see com.sslexplorer.extensions.ExtensionType#descriptorCreated(org.jdom.Element)
288      */

289     public void descriptorCreated(Element element, SessionInfo session)
290             throws IOException JavaDoc {
291     }
292
293     /*
294      * (non-Javadoc)
295      *
296      * @see com.sslexplorer.extensions.ExtensionType#getTypeBundle()
297      */

298     public String JavaDoc getTypeBundle() {
299         return "applications";
300     }
301
302     /*
303      * (non-Javadoc)
304      *
305      * @see com.sslexplorer.applications.ApplicationLauncherType#isServiceSide()
306      */

307     public boolean isServerSide() {
308         return true;
309     }
310 }
Popular Tags