1 | /*
|
---|
2 | * Copyright 2002-2005 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 | package org.apache.tools.ant.util;
|
---|
18 |
|
---|
19 | import java.io.File;
|
---|
20 | import java.util.Vector;
|
---|
21 | import org.apache.tools.ant.taskdefs.condition.Os;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * A set of helper methods related to locating executables or checking
|
---|
25 | * conditons of a given Java installation.
|
---|
26 | *
|
---|
27 | * @since Ant 1.5
|
---|
28 | */
|
---|
29 | public final class JavaEnvUtils {
|
---|
30 |
|
---|
31 | private JavaEnvUtils() {
|
---|
32 | }
|
---|
33 |
|
---|
34 | /** Are we on a DOS-based system */
|
---|
35 | private static final boolean isDos = Os.isFamily("dos");
|
---|
36 | /** Are we on Novell NetWare */
|
---|
37 | private static final boolean isNetware = Os.isName("netware");
|
---|
38 | /** Are we on AIX */
|
---|
39 | private static final boolean isAix = Os.isName("aix");
|
---|
40 |
|
---|
41 | /** shortcut for System.getProperty("java.home") */
|
---|
42 | private static final String javaHome = System.getProperty("java.home");
|
---|
43 |
|
---|
44 | /** FileUtils instance for path normalization */
|
---|
45 | private static final FileUtils fileUtils = FileUtils.newFileUtils();
|
---|
46 |
|
---|
47 | /** Version of currently running VM. */
|
---|
48 | private static String javaVersion;
|
---|
49 |
|
---|
50 | /** floating version of the JVM */
|
---|
51 | private static int javaVersionNumber;
|
---|
52 |
|
---|
53 | /** Version constant for Java 1.0 */
|
---|
54 | public static final String JAVA_1_0 = "1.0";
|
---|
55 | /** Version constant for Java 1.1 */
|
---|
56 | public static final String JAVA_1_1 = "1.1";
|
---|
57 | /** Version constant for Java 1.2 */
|
---|
58 | public static final String JAVA_1_2 = "1.2";
|
---|
59 | /** Version constant for Java 1.3 */
|
---|
60 | public static final String JAVA_1_3 = "1.3";
|
---|
61 | /** Version constant for Java 1.4 */
|
---|
62 | public static final String JAVA_1_4 = "1.4";
|
---|
63 | /** Version constant for Java 1.5 */
|
---|
64 | public static final String JAVA_1_5 = "1.5";
|
---|
65 |
|
---|
66 | /** Whether this is the Kaffe VM */
|
---|
67 | private static boolean kaffeDetected;
|
---|
68 |
|
---|
69 | /** array of packages in the runtime */
|
---|
70 | private static Vector jrePackages;
|
---|
71 |
|
---|
72 |
|
---|
73 | static {
|
---|
74 |
|
---|
75 | // Determine the Java version by looking at available classes
|
---|
76 | // java.lang.Readable was introduced in JDK 1.5
|
---|
77 | // java.lang.CharSequence was introduced in JDK 1.4
|
---|
78 | // java.lang.StrictMath was introduced in JDK 1.3
|
---|
79 | // java.lang.ThreadLocal was introduced in JDK 1.2
|
---|
80 | // java.lang.Void was introduced in JDK 1.1
|
---|
81 | // Count up version until a NoClassDefFoundError ends the try
|
---|
82 |
|
---|
83 | try {
|
---|
84 | javaVersion = JAVA_1_0;
|
---|
85 | javaVersionNumber = 10;
|
---|
86 | Class.forName("java.lang.Void");
|
---|
87 | javaVersion = JAVA_1_1;
|
---|
88 | javaVersionNumber++;
|
---|
89 | Class.forName("java.lang.ThreadLocal");
|
---|
90 | javaVersion = JAVA_1_2;
|
---|
91 | javaVersionNumber++;
|
---|
92 | Class.forName("java.lang.StrictMath");
|
---|
93 | javaVersion = JAVA_1_3;
|
---|
94 | javaVersionNumber++;
|
---|
95 | Class.forName("java.lang.CharSequence");
|
---|
96 | javaVersion = JAVA_1_4;
|
---|
97 | javaVersionNumber++;
|
---|
98 | Class.forName("java.lang.Readable");
|
---|
99 | javaVersion = JAVA_1_5;
|
---|
100 | javaVersionNumber++;
|
---|
101 | } catch (Throwable t) {
|
---|
102 | // swallow as we've hit the max class version that
|
---|
103 | // we have
|
---|
104 | }
|
---|
105 | kaffeDetected = false;
|
---|
106 | try {
|
---|
107 | Class.forName("kaffe.util.NotImplemented");
|
---|
108 | kaffeDetected = true;
|
---|
109 | } catch (Throwable t) {
|
---|
110 | // swallow as this simply doesn't seem to be Kaffe
|
---|
111 | }
|
---|
112 | }
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * Returns the version of Java this class is running under.
|
---|
116 | * @return the version of Java as a String, e.g. "1.1"
|
---|
117 | */
|
---|
118 | public static String getJavaVersion() {
|
---|
119 | return javaVersion;
|
---|
120 | }
|
---|
121 |
|
---|
122 | /**
|
---|
123 | * Compares the current Java version to the passed in String -
|
---|
124 | * assumes the argument is one of the constants defined in this
|
---|
125 | * class.
|
---|
126 | * @param version the version to check against the current version.
|
---|
127 | * @return true if the version of Java is the same as the given version.
|
---|
128 | * @since Ant 1.5
|
---|
129 | */
|
---|
130 | public static boolean isJavaVersion(String version) {
|
---|
131 | return javaVersion.equals(version);
|
---|
132 | }
|
---|
133 |
|
---|
134 | /**
|
---|
135 | * Checks whether the current Java VM is Kaffe.
|
---|
136 | * @return true if the current Java VM is Kaffe.
|
---|
137 | * @since Ant 1.6.3
|
---|
138 | * @see http://www.kaffe.org/
|
---|
139 | */
|
---|
140 | public static boolean isKaffe() {
|
---|
141 | return kaffeDetected;
|
---|
142 | }
|
---|
143 |
|
---|
144 | /**
|
---|
145 | * Finds an executable that is part of a JRE installation based on
|
---|
146 | * the java.home system property.
|
---|
147 | *
|
---|
148 | * <p><code>java</code>, <code>keytool</code>,
|
---|
149 | * <code>policytool</code>, <code>orbd</code>, <code>rmid</code>,
|
---|
150 | * <code>rmiregistry</code>, <code>servertool</code> and
|
---|
151 | * <code>tnameserv</code> are JRE executables on Sun based
|
---|
152 | * JRE's.</p>
|
---|
153 | *
|
---|
154 | * <p>You typically find them in <code>JAVA_HOME/jre/bin</code> if
|
---|
155 | * <code>JAVA_HOME</code> points to your JDK installation. JDK
|
---|
156 | * < 1.2 has them in the same directory as the JDK
|
---|
157 | * executables.</p>
|
---|
158 | * @param command the java executable to find.
|
---|
159 | * @return the path to the command.
|
---|
160 | * @since Ant 1.5
|
---|
161 | */
|
---|
162 | public static String getJreExecutable(String command) {
|
---|
163 | if (isNetware) {
|
---|
164 | // Extrapolating from:
|
---|
165 | // "NetWare may have a "java" in that directory, but 99% of
|
---|
166 | // the time, you don't want to execute it" -- Jeff Tulley
|
---|
167 | // <[email protected]>
|
---|
168 | return command;
|
---|
169 | }
|
---|
170 |
|
---|
171 | File jExecutable = null;
|
---|
172 |
|
---|
173 | if (isAix) {
|
---|
174 | // On IBM's JDK 1.2 the directory layout is different, 1.3 follows
|
---|
175 | // Sun's layout.
|
---|
176 | jExecutable = findInDir(javaHome + "/sh", command);
|
---|
177 | }
|
---|
178 |
|
---|
179 | if (jExecutable == null) {
|
---|
180 | jExecutable = findInDir(javaHome + "/bin", command);
|
---|
181 | }
|
---|
182 |
|
---|
183 | if (jExecutable != null) {
|
---|
184 | return jExecutable.getAbsolutePath();
|
---|
185 | } else {
|
---|
186 | // Unfortunately on Windows java.home doesn't always refer
|
---|
187 | // to the correct location, so we need to fall back to
|
---|
188 | // assuming java is somewhere on the PATH.
|
---|
189 | return addExtension(command);
|
---|
190 | }
|
---|
191 | }
|
---|
192 |
|
---|
193 | /**
|
---|
194 | * Finds an executable that is part of a JDK installation based on
|
---|
195 | * the java.home system property.
|
---|
196 | *
|
---|
197 | * <p>You typically find them in <code>JAVA_HOME/bin</code> if
|
---|
198 | * <code>JAVA_HOME</code> points to your JDK installation.</p>
|
---|
199 | * @param command the java executable to find.
|
---|
200 | * @return the path to the command.
|
---|
201 | * @since Ant 1.5
|
---|
202 | */
|
---|
203 | public static String getJdkExecutable(String command) {
|
---|
204 | if (isNetware) {
|
---|
205 | // Extrapolating from:
|
---|
206 | // "NetWare may have a "java" in that directory, but 99% of
|
---|
207 | // the time, you don't want to execute it" -- Jeff Tulley
|
---|
208 | // <[email protected]>
|
---|
209 | return command;
|
---|
210 | }
|
---|
211 |
|
---|
212 | File jExecutable = null;
|
---|
213 |
|
---|
214 | if (isAix) {
|
---|
215 | // On IBM's JDK 1.2 the directory layout is different, 1.3 follows
|
---|
216 | // Sun's layout.
|
---|
217 | jExecutable = findInDir(javaHome + "/../sh", command);
|
---|
218 | }
|
---|
219 |
|
---|
220 | if (jExecutable == null) {
|
---|
221 | jExecutable = findInDir(javaHome + "/../bin", command);
|
---|
222 | }
|
---|
223 |
|
---|
224 | if (jExecutable != null) {
|
---|
225 | return jExecutable.getAbsolutePath();
|
---|
226 | } else {
|
---|
227 | // fall back to JRE bin directory, also catches JDK 1.0 and 1.1
|
---|
228 | // where java.home points to the root of the JDK and Mac OS X where
|
---|
229 | // the whole directory layout is different from Sun's
|
---|
230 | return getJreExecutable(command);
|
---|
231 | }
|
---|
232 | }
|
---|
233 |
|
---|
234 | /**
|
---|
235 | * Adds a system specific extension to the name of an executable.
|
---|
236 | *
|
---|
237 | * @since Ant 1.5
|
---|
238 | */
|
---|
239 | private static String addExtension(String command) {
|
---|
240 | // This is the most common extension case - exe for windows and OS/2,
|
---|
241 | // nothing for *nix.
|
---|
242 | return command + (isDos ? ".exe" : "");
|
---|
243 | }
|
---|
244 |
|
---|
245 | /**
|
---|
246 | * Look for an executable in a given directory.
|
---|
247 | *
|
---|
248 | * @return null if the executable cannot be found.
|
---|
249 | */
|
---|
250 | private static File findInDir(String dirName, String commandName) {
|
---|
251 | File dir = fileUtils.normalize(dirName);
|
---|
252 | File executable = null;
|
---|
253 | if (dir.exists()) {
|
---|
254 | executable = new File(dir, addExtension(commandName));
|
---|
255 | if (!executable.exists()) {
|
---|
256 | executable = null;
|
---|
257 | }
|
---|
258 | }
|
---|
259 | return executable;
|
---|
260 | }
|
---|
261 |
|
---|
262 | /**
|
---|
263 | * demand creation of the package list.
|
---|
264 | * When you add a new package, add a new test below.
|
---|
265 | */
|
---|
266 |
|
---|
267 | private static void buildJrePackages() {
|
---|
268 | jrePackages = new Vector();
|
---|
269 | switch(javaVersionNumber) {
|
---|
270 | case 15:
|
---|
271 | case 14:
|
---|
272 | jrePackages.addElement("org.apache.crimson");
|
---|
273 | jrePackages.addElement("org.apache.xalan");
|
---|
274 | jrePackages.addElement("org.apache.xml");
|
---|
275 | jrePackages.addElement("org.apache.xpath");
|
---|
276 | jrePackages.addElement("org.ietf.jgss");
|
---|
277 | jrePackages.addElement("org.w3c.dom");
|
---|
278 | jrePackages.addElement("org.xml.sax");
|
---|
279 | // fall through
|
---|
280 | case 13:
|
---|
281 | jrePackages.addElement("org.omg");
|
---|
282 | jrePackages.addElement("com.sun.corba");
|
---|
283 | jrePackages.addElement("com.sun.jndi");
|
---|
284 | jrePackages.addElement("com.sun.media");
|
---|
285 | jrePackages.addElement("com.sun.naming");
|
---|
286 | jrePackages.addElement("com.sun.org.omg");
|
---|
287 | jrePackages.addElement("com.sun.rmi");
|
---|
288 | jrePackages.addElement("sunw.io");
|
---|
289 | jrePackages.addElement("sunw.util");
|
---|
290 | // fall through
|
---|
291 | case 12:
|
---|
292 | jrePackages.addElement("com.sun.java");
|
---|
293 | jrePackages.addElement("com.sun.image");
|
---|
294 | // are there any here that we forgot?
|
---|
295 | // fall through
|
---|
296 | case 11:
|
---|
297 | default:
|
---|
298 | //things like sun.reflection, sun.misc, sun.net
|
---|
299 | jrePackages.addElement("sun");
|
---|
300 | jrePackages.addElement("java");
|
---|
301 | jrePackages.addElement("javax");
|
---|
302 | break;
|
---|
303 | }
|
---|
304 | }
|
---|
305 |
|
---|
306 | /**
|
---|
307 | * Testing helper method; kept here for unification of changes.
|
---|
308 | * @return a list of test classes depending on the java version.
|
---|
309 | */
|
---|
310 | public static Vector getJrePackageTestCases() {
|
---|
311 | Vector tests = new Vector();
|
---|
312 | tests.addElement("java.lang.Object");
|
---|
313 | switch(javaVersionNumber) {
|
---|
314 | case 15:
|
---|
315 | case 14:
|
---|
316 | tests.addElement("sun.audio.AudioPlayer");
|
---|
317 | tests.addElement("org.apache.crimson.parser.ContentModel");
|
---|
318 | tests.addElement("org.apache.xalan.processor.ProcessorImport");
|
---|
319 | tests.addElement("org.apache.xml.utils.URI");
|
---|
320 | tests.addElement("org.apache.xpath.XPathFactory");
|
---|
321 | tests.addElement("org.ietf.jgss.Oid");
|
---|
322 | tests.addElement("org.w3c.dom.Attr");
|
---|
323 | tests.addElement("org.xml.sax.XMLReader");
|
---|
324 | // fall through
|
---|
325 | case 13:
|
---|
326 | tests.addElement("org.omg.CORBA.Any");
|
---|
327 | tests.addElement("com.sun.corba.se.internal.corba.AnyImpl");
|
---|
328 | tests.addElement("com.sun.jndi.ldap.LdapURL");
|
---|
329 | tests.addElement("com.sun.media.sound.Printer");
|
---|
330 | tests.addElement("com.sun.naming.internal.VersionHelper");
|
---|
331 | tests.addElement("com.sun.org.omg.CORBA.Initializer");
|
---|
332 | tests.addElement("sunw.io.Serializable");
|
---|
333 | tests.addElement("sunw.util.EventListener");
|
---|
334 | // fall through
|
---|
335 | case 12:
|
---|
336 | tests.addElement("javax.accessibility.Accessible");
|
---|
337 | tests.addElement("sun.misc.BASE64Encoder");
|
---|
338 | tests.addElement("com.sun.image.codec.jpeg.JPEGCodec");
|
---|
339 | // fall through
|
---|
340 | case 11:
|
---|
341 | default:
|
---|
342 | //things like sun.reflection, sun.misc, sun.net
|
---|
343 | tests.addElement("sun.reflect.SerializationConstructorAccessorImpl");
|
---|
344 | tests.addElement("sun.net.www.http.HttpClient");
|
---|
345 | tests.addElement("sun.audio.AudioPlayer");
|
---|
346 | break;
|
---|
347 | }
|
---|
348 | return tests;
|
---|
349 | }
|
---|
350 | /**
|
---|
351 | * get a vector of strings of packages built into
|
---|
352 | * that platforms runtime jar(s)
|
---|
353 | * @return list of packages.
|
---|
354 | */
|
---|
355 | public static Vector getJrePackages() {
|
---|
356 | if (jrePackages == null) {
|
---|
357 | buildJrePackages();
|
---|
358 | }
|
---|
359 | return jrePackages;
|
---|
360 | }
|
---|
361 | }
|
---|