KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > syncclient > spap > launcher > JavaLauncher


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

18
19 package sync4j.syncclient.spap.launcher;
20
21 import java.lang.reflect.Method JavaDoc;
22
23
24 import java.io.*;
25 import java.security.*;
26 import sync4j.syncclient.common.logging.Logger;
27 import sync4j.syncclient.spap.Asset;
28 import sync4j.syncclient.spap.AssetInstallationException;
29 import sync4j.syncclient.spap.installer.InstallationContext;
30 import java.lang.reflect.*;
31
32 /**
33  * This class represents a <code>Laucher</code> for the execution of java class.
34  * It uses the <code>SimpleClassLoader</code> in order to load the class and
35  * executes the method <i>main(String[] args)</i>
36  * @see sync4j.syncclient.common.SimpleClassLoader
37  * @version $Id: JavaLauncher.java,v 1.5 2005/01/19 11:18:36 fabius Exp $
38  * @author Stefano Nichele
39  */

40
41 public class JavaLauncher implements Launcher {
42
43     // ---------------------------------------------------------- Private Data
44

45     // This is the security manager used to prevent the Sistem.exit call
46
private SimpleSecurityManager simpleSecurityManager = null;
47
48
49     Logger logger = new Logger();
50
51     // ---------------------------------------------------------- Constructor
52
public JavaLauncher() {
53         simpleSecurityManager = new SimpleSecurityManager();
54     }
55
56     // ---------------------------------------------------------- Public methods
57

58     /**
59      * Executes the class with given name.
60      *
61      * @param programName the program to execute.
62      * @param install <code>true</code> if the program is the installation program,
63      * <code>false</code> if the program is the uninstallation program
64      * @param ctx installation context information
65      *
66      * @return Returns the exit code of the program. Exit code is the value returned of the method
67      * called or, if the method calls System.exit(code), is the code used in the exit method.
68      *
69      *
70      * @throws AssetInstallationException if the java class is not found or an error occurs during execution.
71      */

72     public int execute(String JavaDoc programName, boolean install, InstallationContext ctx)
73     throws AssetInstallationException {
74         Integer JavaDoc exitState = null;
75         SecurityManager JavaDoc originalSecurityManager = System.getSecurityManager();
76
77         String JavaDoc workingDirectory = ctx.getWorkingDirectory();
78
79
80         try {
81             int indexClass = programName.lastIndexOf(".class");
82
83             programName = programName.substring(0, indexClass);
84
85             String JavaDoc msgLog = "JavaLauncher - Execute " +
86                             programName +
87                             " in " +
88                             workingDirectory ;
89
90             if (logger.isLoggable(Logger.DEBUG)) {
91                 logger.debug(msgLog);
92             }
93
94             String JavaDoc methodName = null;
95
96             // use ClassLoader in order to load the class
97
ClassLoader JavaDoc cl = this.getClass().getClassLoader();
98
99             Class JavaDoc programClass = cl.loadClass(programName);
100
101             if (install) {
102                 // launch install method
103
methodName = "install";
104
105             } else {
106                 // launch uninstall method
107
methodName = "uninstall";
108             }
109
110             // call method install(InstallationContext)
111
Class JavaDoc[] arg = { InstallationContext.class };
112             Method JavaDoc method = programClass.getMethod(methodName, arg);
113             Object JavaDoc[] objArg = { ctx };
114
115             System.setSecurityManager(simpleSecurityManager);
116
117             exitState = (Integer JavaDoc)method.invoke(null, objArg);
118         } catch (InvocationTargetException ex) {
119
120             Throwable JavaDoc cause = ex.getTargetException();
121
122             if (cause instanceof SimpleSecurityManagerException) {
123                 cause.printStackTrace();
124                 // the program has called System.exit
125
exitState = new Integer JavaDoc( ((SimpleSecurityManagerException)cause).getExitCode() );
126             } else if (cause instanceof AssetInstallationException) {
127                 throw (AssetInstallationException)cause;
128             } else {
129                 // the program has generated a exception
130
throw new AssetInstallationException("Error in launcher", cause);
131             }
132
133         } catch (Exception JavaDoc e) {
134             throw new AssetInstallationException("Error in launcher: " + e, e);
135         } finally {
136             // re-set the originale security manager
137
System.setSecurityManager(originalSecurityManager);
138         }
139
140         // the program has returned a null values
141
if (exitState == null) {
142
143             if (logger.isLoggable(Logger.DEBUG)) {
144                 logger.debug("Java class executed with error. Return state null (--> -1)!! ");
145             }
146
147             return -1;
148         }
149
150         if (logger.isLoggable(Logger.DEBUG)) {
151             logger.debug("Java class executed. Return state: " + exitState.intValue());
152         }
153         return exitState.intValue();
154     }
155 }
156
157
158 /**
159  *
160  * It's a simple security manager used for catch the <code>System.exit(int)</code> call.
161  *
162  */

163 class SimpleSecurityManager extends SecurityManager JavaDoc {
164
165     private final SecurityManager JavaDoc parent = System.getSecurityManager();
166
167     public void checkPermission(Permission perm) {
168         if (parent != null) {
169             parent.checkPermission(perm);
170         }
171     }
172
173     public void checkExit(int code) {
174         throw new SimpleSecurityManagerException("System.exit not allowed", code);
175     }
176
177 }
178
179 /**
180  *
181  * It's a simple exception throw from SimpleSecurityManager if the
182  * method System.exit is called
183  *
184  */

185 class SimpleSecurityManagerException extends SecurityException JavaDoc {
186
187     private int exitCode = -1;
188
189     public SimpleSecurityManagerException(String JavaDoc message, int code) {
190         super(message);
191         exitCode = code;
192     }
193
194     public int getExitCode() {
195         return exitCode;
196     }
197
198 }
Popular Tags