KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > LauncherBootstrap


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 import java.io.File JavaDoc;
18 import java.io.FileInputStream JavaDoc;
19 import java.io.FileNotFoundException JavaDoc;
20 import java.lang.reflect.Method JavaDoc;
21 import java.net.MalformedURLException JavaDoc;
22 import java.net.URL JavaDoc;
23 import java.net.URLClassLoader JavaDoc;
24 import java.net.URLDecoder JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Properties JavaDoc;
27 import java.util.StringTokenizer JavaDoc;
28
29 /**
30  * This class is used as a wrapper for loading the
31  * org.apache.commons.launcher.Launcher class and invoking its
32  * <code>main(String[])</code> method. This particular
33  * class is primary used by the Windows 95, 98, ME, and 2000 platforms to
34  * overcome the difficulty of putting a jar file directly into the JVM's
35  * classpath when using batch scripts on these platforms.
36  * <p>
37  * Specifically, the problem on thse platforms is when Windows uses the PATH
38  * environment variable to find and run a batch script, %0 will resolve
39  * incorrectly in that batch script.
40  * <p>
41  * The way to work around this Windows limitation is to do the following:
42  * <ol>
43  * <li>Put this class' class file - LauncherBootstrap.class - in the same
44  * directory as the batch script. Do not put this class file in a jar file.
45  * <li>Put the jar file containing the launcher's classes in the same
46  * directory as the batch script and this class' class file. Be sure that
47  * that the jar file is named "commons-launcher.jar".
48  * <li>Make the Java command in the batch script invoke Java use the following
49  * classpath arguments. Be sure to include the quotes to ensure that paths
50  * containing spaces are handled properly:
51  * <code>-classpath %0\..;"%PATH%"</code>
52  * </ol>
53  *
54  * @author Patrick Luby
55  */

56 public class LauncherBootstrap {
57
58     //---------------------------------------------------------- Static Fields
59

60     /**
61      * Ant classpath property name
62      */

63     public final static String JavaDoc ANT_CLASSPATH_PROP_NAME = "ant.class.path";
64
65     /**
66      * Jar file name
67      */

68     public final static String JavaDoc LAUNCHER_JAR_FILE_NAME = "commons-launcher.jar";
69
70     /**
71      * Properties file name
72      */

73     public final static String JavaDoc LAUNCHER_PROPS_FILE_NAME = "launcher.properties";
74
75     /**
76      * Class name to load
77      */

78     public final static String JavaDoc LAUNCHER_MAIN_CLASS_NAME = "org.apache.commons.launcher.Launcher";
79
80     /**
81      * Cached Laucher class.
82      */

83     private static Class JavaDoc launcherClass = null;
84
85     //---------------------------------------------------------- Static Methods
86

87     /**
88      * The main method.
89      *
90      * @param args command line arguments
91      */

92     public static void main(String JavaDoc[] args) {
93
94         try {
95
96             // Try to find the LAUNCHER_JAR_FILE_NAME file in the class
97
// loader's and JVM's classpath.
98
URL JavaDoc coreURL = LauncherBootstrap.class.getResource("/" + LauncherBootstrap.LAUNCHER_JAR_FILE_NAME);
99             if (coreURL == null)
100                 throw new FileNotFoundException JavaDoc(LauncherBootstrap.LAUNCHER_JAR_FILE_NAME);
101
102             // Coerce the coreURL's directory into a file
103
File JavaDoc coreDir = new File JavaDoc(URLDecoder.decode(coreURL.getFile())).getCanonicalFile().getParentFile();
104
105             // Try to find the LAUNCHER_PROPS_FILE_NAME file in the same
106
// directory as this class
107
File JavaDoc propsFile = new File JavaDoc(coreDir, LauncherBootstrap.LAUNCHER_PROPS_FILE_NAME);
108             if (!propsFile.canRead())
109                 throw new FileNotFoundException JavaDoc(propsFile.getPath());
110
111             // Load the properties in the LAUNCHER_PROPS_FILE_NAME
112
Properties JavaDoc props = new Properties JavaDoc();
113             FileInputStream JavaDoc fis = new FileInputStream JavaDoc(propsFile);
114             props.load(fis);
115             fis.close();
116
117             // Create a class loader that contains the Launcher, Ant, and
118
// JAXP classes.
119
URL JavaDoc[] antURLs = LauncherBootstrap.fileListToURLs((String JavaDoc)props.get(LauncherBootstrap.ANT_CLASSPATH_PROP_NAME));
120             URL JavaDoc[] urls = new URL JavaDoc[1 + antURLs.length];
121             urls[0] = coreURL;
122             for (int i = 0; i < antURLs.length; i++)
123                 urls[i + 1] = antURLs[i];
124             ClassLoader JavaDoc parentLoader = Thread.currentThread().getContextClassLoader();
125             URLClassLoader JavaDoc loader = null;
126             if (parentLoader != null)
127                 loader = new URLClassLoader JavaDoc(urls, parentLoader);
128             else
129                 loader = new URLClassLoader JavaDoc(urls);
130
131             // Load the LAUNCHER_MAIN_CLASS_NAME class
132
launcherClass = loader.loadClass(LAUNCHER_MAIN_CLASS_NAME);
133
134             // Get the LAUNCHER_MAIN_CLASS_NAME class' getLocalizedString()
135
// method as we need it for printing the usage statement
136
Method JavaDoc getLocalizedStringMethod = launcherClass.getDeclaredMethod("getLocalizedString", new Class JavaDoc[]{ String JavaDoc.class });
137
138             // Invoke the LAUNCHER_MAIN_CLASS_NAME class' start() method.
139
// If the ant.class.path property is not set correctly in the
140
// LAUNCHER_PROPS_FILE_NAME, this will throw an exception.
141
Method JavaDoc startMethod = launcherClass.getDeclaredMethod("start", new Class JavaDoc[]{ String JavaDoc[].class });
142             int returnValue = ((Integer JavaDoc)startMethod.invoke(null, new Object JavaDoc[]{ args })).intValue();
143             // Always exit cleanly after invoking the start() method
144
System.exit(returnValue);
145
146        } catch (Throwable JavaDoc t) {
147
148            t.printStackTrace();
149            System.exit(1);
150
151         }
152
153     }
154
155     /**
156      * Convert a ":" separated list of URL file fragments into an array of URL
157      * objects. Note that any all URL file fragments must conform to the format
158      * required by the "file" parameter in the
159      * {@link URL(String, String, String)} constructor.
160      *
161      * @param fileList the ":" delimited list of URL file fragments to be
162      * converted
163      * @return an array of URL objects
164      * @throws MalformedURLException if the fileList parameter contains any
165      * malformed URLs
166      */

167     private static URL JavaDoc[] fileListToURLs(String JavaDoc fileList)
168         throws MalformedURLException JavaDoc
169     {
170
171         if (fileList == null || "".equals(fileList))
172             return new URL JavaDoc[0];
173
174         // Parse the path string
175
ArrayList JavaDoc list = new ArrayList JavaDoc();
176         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(fileList, ":");
177         URL JavaDoc bootstrapURL = LauncherBootstrap.class.getResource("/" + LauncherBootstrap.class.getName() + ".class");
178         while (tokenizer.hasMoreTokens())
179             list.add(new URL JavaDoc(bootstrapURL, tokenizer.nextToken()));
180
181         return (URL JavaDoc[])list.toArray(new URL JavaDoc[list.size()]);
182
183     }
184
185 }
186
Popular Tags