KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > util > JavaEnvUtils


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 package org.apache.tools.ant.util;
19
20 import java.io.File JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.PrintWriter JavaDoc;
23 import java.io.FileWriter JavaDoc;
24 import java.io.BufferedWriter JavaDoc;
25 import java.util.Vector JavaDoc;
26 import org.apache.tools.ant.taskdefs.condition.Os;
27
28 /**
29  * A set of helper methods related to locating executables or checking
30  * conditons of a given Java installation.
31  *
32  * @since Ant 1.5
33  */

34 public final class JavaEnvUtils {
35
36     private JavaEnvUtils() {
37     }
38
39     /** Are we on a DOS-based system */
40     private static final boolean IS_DOS = Os.isFamily("dos");
41     /** Are we on Novell NetWare */
42     private static final boolean IS_NETWARE = Os.isName("netware");
43     /** Are we on AIX */
44     private static final boolean IS_AIX = Os.isName("aix");
45
46     /** shortcut for System.getProperty("java.home") */
47     private static final String JavaDoc JAVA_HOME = System.getProperty("java.home");
48
49     /** FileUtils instance for path normalization */
50     private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();
51
52     /** Version of currently running VM. */
53     private static String JavaDoc javaVersion;
54
55     /** floating version of the JVM */
56     private static int javaVersionNumber;
57
58     /** Version constant for Java 1.0 */
59     public static final String JavaDoc JAVA_1_0 = "1.0";
60     /** Version constant for Java 1.1 */
61     public static final String JavaDoc JAVA_1_1 = "1.1";
62     /** Version constant for Java 1.2 */
63     public static final String JavaDoc JAVA_1_2 = "1.2";
64     /** Version constant for Java 1.3 */
65     public static final String JavaDoc JAVA_1_3 = "1.3";
66     /** Version constant for Java 1.4 */
67     public static final String JavaDoc JAVA_1_4 = "1.4";
68     /** Version constant for Java 1.5 */
69     public static final String JavaDoc JAVA_1_5 = "1.5";
70     /** Version constant for Java 1.6 */
71     public static final String JavaDoc JAVA_1_6 = "1.6";
72
73     /** Whether this is the Kaffe VM */
74     private static boolean kaffeDetected;
75
76     /** array of packages in the runtime */
77     private static Vector JavaDoc jrePackages;
78
79
80     static {
81
82         // Determine the Java version by looking at available classes
83
// java.net.Proxy was introduced in JDK 1.5
84
// java.lang.CharSequence was introduced in JDK 1.4
85
// java.lang.StrictMath was introduced in JDK 1.3
86
// java.lang.ThreadLocal was introduced in JDK 1.2
87
// java.lang.Void was introduced in JDK 1.1
88
// Count up version until a NoClassDefFoundError ends the try
89

90         try {
91             javaVersion = JAVA_1_0;
92             javaVersionNumber = 10;
93             Class.forName("java.lang.Void");
94             javaVersion = JAVA_1_1;
95             javaVersionNumber++;
96             Class.forName("java.lang.ThreadLocal");
97             javaVersion = JAVA_1_2;
98             javaVersionNumber++;
99             Class.forName("java.lang.StrictMath");
100             javaVersion = JAVA_1_3;
101             javaVersionNumber++;
102             Class.forName("java.lang.CharSequence");
103             javaVersion = JAVA_1_4;
104             javaVersionNumber++;
105             Class.forName("java.net.Proxy");
106             javaVersion = JAVA_1_5;
107             javaVersionNumber++;
108             Class.forName("java.util.ServiceLoader");
109             javaVersion = JAVA_1_6;
110             javaVersionNumber++;
111         } catch (Throwable JavaDoc t) {
112             // swallow as we've hit the max class version that
113
// we have
114
}
115         kaffeDetected = false;
116         try {
117             Class.forName("kaffe.util.NotImplemented");
118             kaffeDetected = true;
119         } catch (Throwable JavaDoc t) {
120             // swallow as this simply doesn't seem to be Kaffe
121
}
122     }
123
124     /**
125      * Returns the version of Java this class is running under.
126      * @return the version of Java as a String, e.g. "1.1"
127      */

128     public static String JavaDoc getJavaVersion() {
129         return javaVersion;
130     }
131
132
133     /**
134      * Returns the version of Java this class is running under.
135      * This number can be used for comparisions; it will always be
136      * @return the version of Java as a number 10x the major/minor,
137      * e.g Java1.5 has a value of 15
138      */

139     public static int getJavaVersionNumber() {
140         return javaVersionNumber;
141     }
142
143     /**
144      * Compares the current Java version to the passed in String -
145      * assumes the argument is one of the constants defined in this
146      * class.
147      * Note that Ant now requires JDK 1.2+ so {@link #JAVA_1_0} and
148      * {@link #JAVA_1_1} need no longer be tested for.
149      * @param version the version to check against the current version.
150      * @return true if the version of Java is the same as the given version.
151      * @since Ant 1.5
152      */

153     public static boolean isJavaVersion(String JavaDoc version) {
154         return javaVersion.equals(version);
155     }
156
157     /**
158      * Compares the current Java version to the passed in String -
159      * assumes the argument is one of the constants defined in this
160      * class.
161      * Note that Ant now requires JDK 1.2+ so {@link #JAVA_1_0} and
162      * {@link #JAVA_1_1} need no longer be tested for.
163      * @param version the version to check against the current version.
164      * @return true if the version of Java is the same or higher than the
165      * given version.
166      * @since Ant 1.7
167      */

168     public static boolean isAtLeastJavaVersion(String JavaDoc version) {
169         return javaVersion.compareTo(version) >= 0;
170     }
171
172     /**
173      * Checks whether the current Java VM is Kaffe.
174      * @return true if the current Java VM is Kaffe.
175      * @since Ant 1.6.3
176      * @see <a HREF="http://www.kaffe.org/">http://www.kaffe.org/</a>
177      */

178     public static boolean isKaffe() {
179         return kaffeDetected;
180     }
181
182     /**
183      * Finds an executable that is part of a JRE installation based on
184      * the java.home system property.
185      *
186      * <p><code>java</code>, <code>keytool</code>,
187      * <code>policytool</code>, <code>orbd</code>, <code>rmid</code>,
188      * <code>rmiregistry</code>, <code>servertool</code> and
189      * <code>tnameserv</code> are JRE executables on Sun based
190      * JRE's.</p>
191      *
192      * <p>You typically find them in <code>JAVA_HOME/jre/bin</code> if
193      * <code>JAVA_HOME</code> points to your JDK installation. JDK
194      * &lt; 1.2 has them in the same directory as the JDK
195      * executables.</p>
196      * @param command the java executable to find.
197      * @return the path to the command.
198      * @since Ant 1.5
199      */

200     public static String JavaDoc getJreExecutable(String JavaDoc command) {
201         if (IS_NETWARE) {
202             // Extrapolating from:
203
// "NetWare may have a "java" in that directory, but 99% of
204
// the time, you don't want to execute it" -- Jeff Tulley
205
// <JTULLEY@novell.com>
206
return command;
207         }
208
209         File JavaDoc jExecutable = null;
210
211         if (IS_AIX) {
212             // On IBM's JDK 1.2 the directory layout is different, 1.3 follows
213
// Sun's layout.
214
jExecutable = findInDir(JAVA_HOME + "/sh", command);
215         }
216
217         if (jExecutable == null) {
218             jExecutable = findInDir(JAVA_HOME + "/bin", command);
219         }
220
221         if (jExecutable != null) {
222             return jExecutable.getAbsolutePath();
223         } else {
224             // Unfortunately on Windows java.home doesn't always refer
225
// to the correct location, so we need to fall back to
226
// assuming java is somewhere on the PATH.
227
return addExtension(command);
228         }
229     }
230
231     /**
232      * Finds an executable that is part of a JDK installation based on
233      * the java.home system property.
234      *
235      * <p>You typically find them in <code>JAVA_HOME/bin</code> if
236      * <code>JAVA_HOME</code> points to your JDK installation.</p>
237      * @param command the java executable to find.
238      * @return the path to the command.
239      * @since Ant 1.5
240      */

241     public static String JavaDoc getJdkExecutable(String JavaDoc command) {
242         if (IS_NETWARE) {
243             // Extrapolating from:
244
// "NetWare may have a "java" in that directory, but 99% of
245
// the time, you don't want to execute it" -- Jeff Tulley
246
// <JTULLEY@novell.com>
247
return command;
248         }
249
250         File JavaDoc jExecutable = null;
251
252         if (IS_AIX) {
253             // On IBM's JDK 1.2 the directory layout is different, 1.3 follows
254
// Sun's layout.
255
jExecutable = findInDir(JAVA_HOME + "/../sh", command);
256         }
257
258         if (jExecutable == null) {
259             jExecutable = findInDir(JAVA_HOME + "/../bin", command);
260         }
261
262         if (jExecutable != null) {
263             return jExecutable.getAbsolutePath();
264         } else {
265             // fall back to JRE bin directory, also catches JDK 1.0 and 1.1
266
// where java.home points to the root of the JDK and Mac OS X where
267
// the whole directory layout is different from Sun's
268
return getJreExecutable(command);
269         }
270     }
271
272     /**
273      * Adds a system specific extension to the name of an executable.
274      *
275      * @since Ant 1.5
276      */

277     private static String JavaDoc addExtension(String JavaDoc command) {
278         // This is the most common extension case - exe for windows and OS/2,
279
// nothing for *nix.
280
return command + (IS_DOS ? ".exe" : "");
281     }
282
283     /**
284      * Look for an executable in a given directory.
285      *
286      * @return null if the executable cannot be found.
287      */

288     private static File JavaDoc findInDir(String JavaDoc dirName, String JavaDoc commandName) {
289         File JavaDoc dir = FILE_UTILS.normalize(dirName);
290         File JavaDoc executable = null;
291         if (dir.exists()) {
292             executable = new File JavaDoc(dir, addExtension(commandName));
293             if (!executable.exists()) {
294                 executable = null;
295             }
296         }
297         return executable;
298     }
299
300     /**
301      * demand creation of the package list.
302      * When you add a new package, add a new test below.
303      */

304
305     private static void buildJrePackages() {
306         jrePackages = new Vector JavaDoc();
307         switch(javaVersionNumber) {
308             case 16:
309             case 15:
310                 //In Java1.5, the apache stuff moved.
311
jrePackages.addElement("com.sun.org.apache");
312                 //fall through.
313
case 14:
314                 if (javaVersionNumber == 14) {
315                     jrePackages.addElement("org.apache.crimson");
316                     jrePackages.addElement("org.apache.xalan");
317                     jrePackages.addElement("org.apache.xml");
318                     jrePackages.addElement("org.apache.xpath");
319                 }
320                 jrePackages.addElement("org.ietf.jgss");
321                 jrePackages.addElement("org.w3c.dom");
322                 jrePackages.addElement("org.xml.sax");
323                 // fall through
324
case 13:
325                 jrePackages.addElement("org.omg");
326                 jrePackages.addElement("com.sun.corba");
327                 jrePackages.addElement("com.sun.jndi");
328                 jrePackages.addElement("com.sun.media");
329                 jrePackages.addElement("com.sun.naming");
330                 jrePackages.addElement("com.sun.org.omg");
331                 jrePackages.addElement("com.sun.rmi");
332                 jrePackages.addElement("sunw.io");
333                 jrePackages.addElement("sunw.util");
334                 // fall through
335
case 12:
336                 jrePackages.addElement("com.sun.java");
337                 jrePackages.addElement("com.sun.image");
338                 // are there any here that we forgot?
339
// fall through
340
case 11:
341             default:
342                 //things like sun.reflection, sun.misc, sun.net
343
jrePackages.addElement("sun");
344                 jrePackages.addElement("java");
345                 jrePackages.addElement("javax");
346                 break;
347         }
348     }
349
350     /**
351      * Testing helper method; kept here for unification of changes.
352      * @return a list of test classes depending on the java version.
353      */

354     public static Vector JavaDoc getJrePackageTestCases() {
355         Vector JavaDoc tests = new Vector JavaDoc();
356         tests.addElement("java.lang.Object");
357         switch(javaVersionNumber) {
358             case 16:
359             case 15:
360                 tests.addElement(
361                     "com.sun.org.apache.xerces.internal.jaxp.datatype.DatatypeFactoryImpl ");
362                 // Fall tru
363
case 14:
364                 tests.addElement("sun.audio.AudioPlayer");
365                 if (javaVersionNumber == 14) {
366                     tests.addElement("org.apache.crimson.parser.ContentModel");
367                     tests.addElement("org.apache.xalan.processor.ProcessorImport");
368                     tests.addElement("org.apache.xml.utils.URI");
369                     tests.addElement("org.apache.xpath.XPathFactory");
370                 }
371                 tests.addElement("org.ietf.jgss.Oid");
372                 tests.addElement("org.w3c.dom.Attr");
373                 tests.addElement("org.xml.sax.XMLReader");
374                 // fall through
375
case 13:
376                 tests.addElement("org.omg.CORBA.Any");
377                 tests.addElement("com.sun.corba.se.internal.corba.AnyImpl");
378                 tests.addElement("com.sun.jndi.ldap.LdapURL");
379                 tests.addElement("com.sun.media.sound.Printer");
380                 tests.addElement("com.sun.naming.internal.VersionHelper");
381                 tests.addElement("com.sun.org.omg.CORBA.Initializer");
382                 tests.addElement("sunw.io.Serializable");
383                 tests.addElement("sunw.util.EventListener");
384                 // fall through
385
case 12:
386                 tests.addElement("javax.accessibility.Accessible");
387                 tests.addElement("sun.misc.BASE64Encoder");
388                 tests.addElement("com.sun.image.codec.jpeg.JPEGCodec");
389                 // fall through
390
case 11:
391             default:
392                 //things like sun.reflection, sun.misc, sun.net
393
tests.addElement("sun.reflect.SerializationConstructorAccessorImpl");
394                 tests.addElement("sun.net.www.http.HttpClient");
395                 tests.addElement("sun.audio.AudioPlayer");
396                 break;
397         }
398         return tests;
399     }
400     /**
401      * get a vector of strings of packages built into
402      * that platforms runtime jar(s)
403      * @return list of packages.
404      */

405     public static Vector JavaDoc getJrePackages() {
406         if (jrePackages == null) {
407             buildJrePackages();
408         }
409         return jrePackages;
410     }
411
412     /**
413      *
414      * Writes the command into a temporary DCL script and returns the
415      * corresponding File object.
416      * It is the job of the caller to delete the file on exit.
417      * @param cmd the command.
418      * @return the file containing the command.
419      * @throws IOException if there is an error writing to the file.
420      */

421     public static File JavaDoc createVmsJavaOptionFile(String JavaDoc[] cmd)
422             throws IOException JavaDoc {
423         File JavaDoc script = FILE_UTILS.createTempFile("ANT", ".JAVA_OPTS", null);
424         PrintWriter JavaDoc out = null;
425         try {
426             out = new PrintWriter JavaDoc(new BufferedWriter JavaDoc(new FileWriter JavaDoc(script)));
427             for (int i = 0; i < cmd.length; i++) {
428                 out.println(cmd[i]);
429             }
430         } finally {
431             FileUtils.close(out);
432         }
433         return script;
434     }
435
436     /**
437      * Return the value of ${java.home}
438      * @return the java home value.
439      */

440     public static String JavaDoc getJavaHome() {
441         return JAVA_HOME;
442     }
443 }
444
Popular Tags