KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > agent > client > util > types > ExecutableApplicationType


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.agent.client.util.types;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.lang.reflect.Method JavaDoc;
25 import java.text.MessageFormat JavaDoc;
26 import java.util.Enumeration JavaDoc;
27 import java.util.Vector JavaDoc;
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 /**
37  * Application type that launchs a native application, either one that is in the
38  * path or in a specified directory.
39  *
40  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
41  */

42 public class ExecutableApplicationType implements ApplicationType {
43     
44     // Private instance variables
45

46     private ApplicationLauncherEvents events;
47     private AbstractApplicationLauncher launcher;
48     private String JavaDoc program;
49     private File JavaDoc workingDir;
50     private Vector JavaDoc programArgs = new Vector JavaDoc();
51     private ProcessMonitor process;
52
53     /*
54      * (non-Javadoc)
55      *
56      * @see com.sslexplorer.vpn.util.ApplicationType#prepare(com.sslexplorer.vpn.util.ApplicationLauncher,
57      * com.sslexplorer.vpn.util.XMLElement)
58      */

59     public void prepare(AbstractApplicationLauncher launcher, ApplicationLauncherEvents events, XMLElement element) throws IOException JavaDoc {
60         this.launcher = launcher;
61         this.events = events;
62
63         if (element.getName().equalsIgnoreCase("executable")) { //$NON-NLS-1$
64
String JavaDoc programName = (String JavaDoc) element.getAttribute("program"); //$NON-NLS-1$
65
if(programName == null) {
66                 programName = findProgram(element);
67             }
68             program = launcher.replaceTokens(programName);
69             program = launcher.replaceAllTokens(program, "/", File.separator);
70             String JavaDoc dir = (String JavaDoc) element.getAttribute("dir"); //$NON-NLS-1$
71
if (dir != null) {
72                 workingDir = new File JavaDoc(launcher.replaceTokens(dir));
73             } else {
74                 workingDir = null;
75             }
76             buildProgramArguments(element);
77         }
78     }
79
80     /* (non-Javadoc)
81      * @see com.sslexplorer.vpn.util.ApplicationType#start()
82      */

83     public void start() {
84         execute(program, workingDir);
85     }
86
87     private void addArgument(XMLElement e) throws IOException JavaDoc {
88         if (e.getName().equalsIgnoreCase("arg")) { //$NON-NLS-1$
89

90             String JavaDoc arg = launcher.replaceTokens(Utils.trimmedBothOrBlank(e.getContent()));
91             if (arg.indexOf(' ') > -1)
92                 arg = "\"" + arg + "\""; //$NON-NLS-1$ //$NON-NLS-2$
93
programArgs.addElement(arg);
94         } else {
95             throw new IOException JavaDoc(MessageFormat.format(
96                 Messages.getString("ExecutableApplicationType.unexpectedElementFound"), //$NON-NLS-1$
97
new Object JavaDoc[] { e.getName() } ) );
98         }
99     }
100
101     private void buildProgramArguments(XMLElement element) throws IOException JavaDoc {
102
103         Enumeration JavaDoc en = element.enumerateChildren();
104
105         while (en.hasMoreElements()) {
106
107             XMLElement e = (XMLElement) en.nextElement();
108             if (e.getName().equalsIgnoreCase("arg")) //$NON-NLS-1$
109
addArgument(e);
110             else if (e.getName().equalsIgnoreCase("program")) //$NON-NLS-1$
111
continue;
112             else if (e.getName().equalsIgnoreCase("if")) { //$NON-NLS-1$
113
if (AbstractApplicationLauncher.checkCondition(this, e, launcher.getDescriptorParams())) {
114                     buildProgramArguments(e);
115                 }
116             } else
117                 throw new IOException JavaDoc(MessageFormat.format(Messages.getString("ExecutableApplicationType.unexpectedElementFoundInExecutable"), new Object JavaDoc[] { e.getName() } ) ) ;//$NON-NLS-1$
118
}
119
120     }
121
122     private String JavaDoc findProgram(XMLElement element) throws IOException JavaDoc {
123         Enumeration JavaDoc en = element.enumerateChildren();
124
125         while (en.hasMoreElements()) {
126
127             XMLElement e = (XMLElement) en.nextElement();
128             if (e.getName().equalsIgnoreCase("program")) //$NON-NLS-1$
129
return Utils.trimmedBothOrBlank(e.getContent());
130             else if (e.getName().equalsIgnoreCase("if")) { //$NON-NLS-1$
131
if (AbstractApplicationLauncher.checkCondition(this, e, launcher.getDescriptorParams())) {
132                     String JavaDoc program = findProgram(e);
133                     if(program != null) {
134                         return program;
135                     }
136                 }
137             }
138         }
139         throw new IOException JavaDoc("No valid program found for conditions.");
140
141     }
142
143     private void execute(String JavaDoc program, File JavaDoc workingDir) {
144
145         String JavaDoc[] args = new String JavaDoc[programArgs.size()];
146         programArgs.copyInto(args);
147
148         // LDP - Look for the program in our working dir.. looks like we need to
149
// specify
150
// this fully in order for it to be executed properly. Windows will not
151
// search
152
// the working directory for the executable file!!
153
File JavaDoc tmp = new File JavaDoc(workingDir != null ? workingDir.getAbsolutePath() : launcher.getInstallDir().getAbsolutePath(), program);
154         if (tmp.exists())
155             program = tmp.getAbsolutePath();
156
157         String JavaDoc[] cmdargs = new String JavaDoc[args.length + 1];
158         System.arraycopy(args, 0, cmdargs, 1, args.length);
159         if (program.indexOf(' ') > -1)
160             program = "\"" + program + "\""; //$NON-NLS-1$ //$NON-NLS-2$
161
cmdargs[0] = program;
162
163         String JavaDoc cmdline = ""; //$NON-NLS-1$
164
for (int i = 0; i < cmdargs.length; i++)
165             cmdline += " " + cmdargs[i]; //$NON-NLS-1$
166

167         if (events != null)
168             events.debug(MessageFormat.format(Messages.getString("ExecutableApplicationType.executingCommand"), new Object JavaDoc[] { cmdline } ) ); //$NON-NLS-1$
169

170         try {
171
172             if (events != null)
173                 events.executingApplication(launcher.getName(), cmdline.trim());
174
175             // Can we change the working directory of the process?
176
try {
177                 Method JavaDoc m = Runtime JavaDoc.class.getMethod("exec", new Class JavaDoc[] { String JavaDoc[].class, String JavaDoc[].class, File JavaDoc.class }); //$NON-NLS-1$
178
process = new ProcessMonitor(launcher.getName(), (Process JavaDoc) m.invoke(Runtime.getRuntime(), new Object JavaDoc[] { cmdargs,
179                     null,
180                     workingDir }));
181             } catch (Throwable JavaDoc t) {
182                 if (workingDir != null) {
183                     t.printStackTrace();
184
185                     // Try cmd.exe on windows
186

187                     throw new IOException JavaDoc(Messages.getString("ExecutableApplicationType.applicationRequestsThatTheWorkingDirectoryIsChange") + ""); //$NON-NLS-1$ //$NON-NLS-2$
188
}
189                 process = new ProcessMonitor(launcher.getName(), Runtime.getRuntime().exec(cmdargs));
190             }
191         } catch (IOException JavaDoc ex) {
192             if (events != null)
193                 events.debug(MessageFormat.format(Messages.getString("ExecutableApplicationType.processExecutionFailed"), new Object JavaDoc[] { ex.getMessage() } ) ); //$NON-NLS-1$
194
}
195
196     }
197
198     /*
199      * (non-Javadoc)
200      *
201      * @see com.sslexplorer.vpn.util.ApplicationType#getProcessMonitor()
202      */

203     public ProcessMonitor getProcessMonitor() {
204         return process;
205     }
206
207     /*
208      * (non-Javadoc)
209      *
210      * @see com.sslexplorer.vpn.util.ApplicationType#getRedirectParameters()
211      */

212     public String JavaDoc getRedirectParameters() {
213         return null;
214     }
215
216     /* (non-Javadoc)
217      * @see com.sslexplorer.agent.client.util.ApplicationType#getTypeName()
218      */

219     public String JavaDoc getTypeName() {
220         return "executable";
221     }
222
223 }
224
Popular Tags