Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
LauncherBootstrap |
|
| 6.5;6.5 |
1 | /* | |
2 | * Licensed to the Apache Software Foundation (ASF) under one or more | |
3 | * contributor license agreements. See the NOTICE file distributed with | |
4 | * this work for additional information regarding copyright ownership. | |
5 | * The ASF licenses this file to You under the Apache License, Version 2.0 | |
6 | * (the "License"); you may not use this file except in compliance with | |
7 | * the License. You may obtain a copy of the License at | |
8 | * | |
9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
10 | * | |
11 | * Unless required by applicable law or agreed to in writing, software | |
12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 | * See the License for the specific language governing permissions and | |
15 | * limitations under the License. | |
16 | */ | |
17 | ||
18 | import java.io.File; | |
19 | import java.io.FileInputStream; | |
20 | import java.io.FileNotFoundException; | |
21 | import java.lang.reflect.Method; | |
22 | import java.net.MalformedURLException; | |
23 | import java.net.URL; | |
24 | import java.net.URLClassLoader; | |
25 | import java.net.URLDecoder; | |
26 | import java.util.ArrayList; | |
27 | import java.util.Properties; | |
28 | import java.util.StringTokenizer; | |
29 | ||
30 | /** | |
31 | * This class is used as a wrapper for loading the | |
32 | * org.apache.commons.launcher.Launcher class and invoking its | |
33 | * <code>main(String[])</code> method. This particular | |
34 | * class is primary used by the Windows 95, 98, ME, and 2000 platforms to | |
35 | * overcome the difficulty of putting a jar file directly into the JVM's | |
36 | * classpath when using batch scripts on these platforms. | |
37 | * <p> | |
38 | * Specifically, the problem on thse platforms is when Windows uses the PATH | |
39 | * environment variable to find and run a batch script, %0 will resolve | |
40 | * incorrectly in that batch script. | |
41 | * <p> | |
42 | * The way to work around this Windows limitation is to do the following: | |
43 | * <ol> | |
44 | * <li>Put this class' class file - LauncherBootstrap.class - in the same | |
45 | * directory as the batch script. Do not put this class file in a jar file. | |
46 | * <li>Put the jar file containing the launcher's classes in the same | |
47 | * directory as the batch script and this class' class file. Be sure that | |
48 | * that the jar file is named "commons-launcher.jar". | |
49 | * <li>Make the Java command in the batch script invoke Java use the following | |
50 | * classpath arguments. Be sure to include the quotes to ensure that paths | |
51 | * containing spaces are handled properly: | |
52 | * <code>-classpath %0\..;"%PATH%"</code> | |
53 | * </ol> | |
54 | * | |
55 | * @author Patrick Luby | |
56 | */ | |
57 | 0 | public class LauncherBootstrap { |
58 | ||
59 | //---------------------------------------------------------- Static Fields | |
60 | ||
61 | /** | |
62 | * Ant classpath property name | |
63 | */ | |
64 | public final static String ANT_CLASSPATH_PROP_NAME = "ant.class.path"; | |
65 | ||
66 | /** | |
67 | * Jar file name | |
68 | */ | |
69 | public final static String LAUNCHER_JAR_FILE_NAME = "commons-launcher.jar"; | |
70 | ||
71 | /** | |
72 | * Properties file name | |
73 | */ | |
74 | public final static String LAUNCHER_PROPS_FILE_NAME = "launcher.properties"; | |
75 | ||
76 | /** | |
77 | * Class name to load | |
78 | */ | |
79 | public final static String LAUNCHER_MAIN_CLASS_NAME = "org.apache.commons.launcher.Launcher"; | |
80 | ||
81 | /** | |
82 | * Cached Laucher class. | |
83 | */ | |
84 | 0 | private static Class launcherClass = null; |
85 | ||
86 | //---------------------------------------------------------- Static Methods | |
87 | ||
88 | /** | |
89 | * The main method. | |
90 | * | |
91 | * @param args command line arguments | |
92 | */ | |
93 | public static void main(String[] args) { | |
94 | ||
95 | try { | |
96 | ||
97 | // Try to find the LAUNCHER_JAR_FILE_NAME file in the class | |
98 | // loader's and JVM's classpath. | |
99 | 0 | URL coreURL = LauncherBootstrap.class.getResource("/" + LauncherBootstrap.LAUNCHER_JAR_FILE_NAME); |
100 | 0 | if (coreURL == null) |
101 | 0 | throw new FileNotFoundException(LauncherBootstrap.LAUNCHER_JAR_FILE_NAME); |
102 | ||
103 | // Coerce the coreURL's directory into a file | |
104 | 0 | File coreDir = new File(URLDecoder.decode(coreURL.getFile())).getCanonicalFile().getParentFile(); |
105 | ||
106 | // Try to find the LAUNCHER_PROPS_FILE_NAME file in the same | |
107 | // directory as this class | |
108 | 0 | File propsFile = new File(coreDir, LauncherBootstrap.LAUNCHER_PROPS_FILE_NAME); |
109 | 0 | if (!propsFile.canRead()) |
110 | 0 | throw new FileNotFoundException(propsFile.getPath()); |
111 | ||
112 | // Load the properties in the LAUNCHER_PROPS_FILE_NAME | |
113 | 0 | Properties props = new Properties(); |
114 | 0 | FileInputStream fis = new FileInputStream(propsFile); |
115 | 0 | props.load(fis); |
116 | 0 | fis.close(); |
117 | ||
118 | // Create a class loader that contains the Launcher, Ant, and | |
119 | // JAXP classes. | |
120 | 0 | URL[] antURLs = LauncherBootstrap.fileListToURLs((String)props.get(LauncherBootstrap.ANT_CLASSPATH_PROP_NAME)); |
121 | 0 | URL[] urls = new URL[1 + antURLs.length]; |
122 | 0 | urls[0] = coreURL; |
123 | 0 | for (int i = 0; i < antURLs.length; i++) |
124 | 0 | urls[i + 1] = antURLs[i]; |
125 | 0 | ClassLoader parentLoader = Thread.currentThread().getContextClassLoader(); |
126 | 0 | URLClassLoader loader = null; |
127 | 0 | if (parentLoader != null) |
128 | 0 | loader = new URLClassLoader(urls, parentLoader); |
129 | else | |
130 | 0 | loader = new URLClassLoader(urls); |
131 | ||
132 | // Load the LAUNCHER_MAIN_CLASS_NAME class | |
133 | 0 | launcherClass = loader.loadClass(LAUNCHER_MAIN_CLASS_NAME); |
134 | ||
135 | // Get the LAUNCHER_MAIN_CLASS_NAME class' getLocalizedString() | |
136 | // method as we need it for printing the usage statement | |
137 | 0 | Method getLocalizedStringMethod = launcherClass.getDeclaredMethod("getLocalizedString", new Class[]{ String.class }); |
138 | ||
139 | // Invoke the LAUNCHER_MAIN_CLASS_NAME class' start() method. | |
140 | // If the ant.class.path property is not set correctly in the | |
141 | // LAUNCHER_PROPS_FILE_NAME, this will throw an exception. | |
142 | 0 | Method startMethod = launcherClass.getDeclaredMethod("start", new Class[]{ String[].class }); |
143 | 0 | int returnValue = ((Integer)startMethod.invoke(null, new Object[]{ args })).intValue(); |
144 | // Always exit cleanly after invoking the start() method | |
145 | 0 | System.exit(returnValue); |
146 | ||
147 | 0 | } catch (Throwable t) { |
148 | ||
149 | 0 | t.printStackTrace(); |
150 | 0 | System.exit(1); |
151 | ||
152 | 0 | } |
153 | ||
154 | 0 | } |
155 | ||
156 | /** | |
157 | * Convert a ":" separated list of URL file fragments into an array of URL | |
158 | * objects. Note that any all URL file fragments must conform to the format | |
159 | * required by the "file" parameter in the | |
160 | * {@link URL(String, String, String)} constructor. | |
161 | * | |
162 | * @param fileList the ":" delimited list of URL file fragments to be | |
163 | * converted | |
164 | * @return an array of URL objects | |
165 | * @throws MalformedURLException if the fileList parameter contains any | |
166 | * malformed URLs | |
167 | */ | |
168 | private static URL[] fileListToURLs(String fileList) | |
169 | throws MalformedURLException | |
170 | { | |
171 | ||
172 | 0 | if (fileList == null || "".equals(fileList)) |
173 | 0 | return new URL[0]; |
174 | ||
175 | // Parse the path string | |
176 | 0 | ArrayList list = new ArrayList(); |
177 | 0 | StringTokenizer tokenizer = new StringTokenizer(fileList, ":"); |
178 | 0 | URL bootstrapURL = LauncherBootstrap.class.getResource("/" + LauncherBootstrap.class.getName() + ".class"); |
179 | 0 | while (tokenizer.hasMoreTokens()) |
180 | 0 | list.add(new URL(bootstrapURL, tokenizer.nextToken())); |
181 | ||
182 | 0 | return (URL[])list.toArray(new URL[list.size()]); |
183 | ||
184 | } | |
185 | ||
186 | } |