aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/util/JavaEnvUtils.java
blob: df7782086dec0bfd566d4844147df024fedd87ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package org.apache.tools.ant.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Vector;

import org.apache.tools.ant.taskdefs.condition.Os;

/**
 * A set of helper methods related to locating executables or checking
 * conditions of a given Java installation.
 *
 * @since Ant 1.5
 */
public final class JavaEnvUtils {

    private JavaEnvUtils() {
    }

    /** Are we on a DOS-based system */
    private static final boolean IS_DOS = Os.isFamily("dos");
    /** Are we on Novell NetWare */
    private static final boolean IS_NETWARE = Os.isName("netware");
    /** Are we on AIX */
    private static final boolean IS_AIX = Os.isName("aix");

    /** shortcut for System.getProperty("java.home") */
    private static final String JAVA_HOME = System.getProperty("java.home");

    /** FileUtils instance for path normalization */
    private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();

    /** Version of currently running VM. */
    private static String javaVersion;

    /** floating version of the JVM */
    private static int javaVersionNumber;

    /** Version constant for Java 1.0 */
    public static final String JAVA_1_0 = "1.0";
    /** Number Version constant for Java 1.0 */
    public static final int VERSION_1_0 = 10;

    /** Version constant for Java 1.1 */
    public static final String JAVA_1_1 = "1.1";
    /** Number Version constant for Java 1.1 */
    public static final int VERSION_1_1 = 11;

    /** Version constant for Java 1.2 */
    public static final String JAVA_1_2 = "1.2";
    /** Number Version constant for Java 1.2 */
    public static final int VERSION_1_2 = 12;

    /** Version constant for Java 1.3 */
    public static final String JAVA_1_3 = "1.3";
    /** Number Version constant for Java 1.3 */
    public static final int VERSION_1_3 = 13;

    /** Version constant for Java 1.4 */
    public static final String JAVA_1_4 = "1.4";
    /** Number Version constant for Java 1.4 */
    public static final int VERSION_1_4 = 14;

    /** Version constant for Java 1.5 */
    public static final String JAVA_1_5 = "1.5";
    /** Number Version constant for Java 1.5 */
    public static final int VERSION_1_5 = 15;

    /** Version constant for Java 1.6 */
    public static final String JAVA_1_6 = "1.6";
    /** Number Version constant for Java 1.6 */
    public static final int VERSION_1_6 = 16;

    /** Version constant for Java 1.7 */
    public static final String JAVA_1_7 = "1.7";
    /** Number Version constant for Java 1.7 */
    public static final int VERSION_1_7 = 17;

    /** Version constant for Java 1.8 */
    public static final String JAVA_1_8 = "1.8";
    /** Number Version constant for Java 1.8 */
    public static final int VERSION_1_8 = 18;

    /** Version constant for Java 1.9 */
    public static final String JAVA_1_9 = "1.9";
    /** Number Version constant for Java 1.9 */
    public static final int VERSION_1_9 = 19;

    /** Whether this is the Kaffe VM */
    private static boolean kaffeDetected;

    /** Wheter this is a GNU Classpath based VM */
    private static boolean classpathDetected;

    /** Whether this is the GNU VM (gcj/gij) */
    private static boolean gijDetected;

    /** Whether this is Apache Harmony */
    private static boolean harmonyDetected;

    /** array of packages in the runtime */
    private static Vector<String> jrePackages;


    static {

        // Determine the Java version by looking at available classes
        // java.net.Proxy was introduced in JDK 1.5
        // java.lang.CharSequence was introduced in JDK 1.4
        // java.lang.StrictMath was introduced in JDK 1.3
        // java.lang.ThreadLocal was introduced in JDK 1.2
        // java.lang.Void was introduced in JDK 1.1
        // Count up version until a NoClassDefFoundError ends the try

        try {
            javaVersion = JAVA_1_0;
            javaVersionNumber = VERSION_1_0;
            Class.forName("java.lang.Void");
            javaVersion = JAVA_1_1;
            javaVersionNumber++;
            Class.forName("java.lang.ThreadLocal");
            javaVersion = JAVA_1_2;
            javaVersionNumber++;
            Class.forName("java.lang.StrictMath");
            javaVersion = JAVA_1_3;
            javaVersionNumber++;
            Class.forName("java.lang.CharSequence");
            javaVersion = JAVA_1_4;
            javaVersionNumber++;
            Class.forName("java.net.Proxy");
            javaVersion = JAVA_1_5;
            javaVersionNumber++;
            Class.forName("java.net.CookieStore");
            javaVersion = JAVA_1_6;
            javaVersionNumber++;
            Class.forName("java.nio.file.FileSystem");
            javaVersion = JAVA_1_7;
            javaVersionNumber++;
            Class.forName("java.lang.reflect.Executable");
            javaVersion = JAVA_1_8;
            javaVersionNumber++;
            checkForJava9();
            javaVersion = JAVA_1_9;
            javaVersionNumber++;
        } catch (Throwable t) {
            // swallow as we've hit the max class version that
            // we have
        }
        kaffeDetected = false;
        try {
            Class.forName("kaffe.util.NotImplemented");
            kaffeDetected = true;
        } catch (Throwable t) {
            // swallow as this simply doesn't seem to be Kaffe
        }
        classpathDetected = false;
        try {
            Class.forName("gnu.classpath.Configuration");
            classpathDetected = true;
        } catch (Throwable t) {
            // swallow as this simply doesn't seem to be GNU classpath based.
        }
        gijDetected = false;
        try {
            Class.forName("gnu.gcj.Core");
            gijDetected = true;
        } catch (Throwable t) {
            // swallow as this simply doesn't seem to be gcj/gij
        }
        harmonyDetected = false;
        try {
            Class.forName("org.apache.harmony.luni.util.Base64");
            harmonyDetected = true;
        } catch (Throwable t) {
            // swallow as this simply doesn't seem to be Apache Harmony
        }
    }

    /**
     * Returns the version of Java this class is running under.
     * @return the version of Java as a String, e.g. "1.6"
     */
    public static String getJavaVersion() {
        return javaVersion;
    }


    /**
     * Checks for a give Java 9 runtime.
     * At the time of writing the actual version of the JDK was 1.9.0_b06.
     * Searching for new classes gave no hits, so we need another aproach.
     * Searching for changes (grep -r -i -n "@since 1.9" .) in the sources gave
     * only one hit: a new constant in the class SourceVersion.
     * So we have to check that ...
     *
     * @throws Exception if we can't load the class or don't find the new constant.
     *    This is the behavior when searching for new features on older versions.
     * @since Ant 1.9.4
     */
    private static void checkForJava9() throws Exception {
    	Class<?> clazz = Class.forName("javax.lang.model.SourceVersion");
    	clazz.getDeclaredField("RELEASE_9");
    }


    /**
     * Returns the version of Java this class is running under.
     * This number can be used for comparisons; it will always be
     * @return the version of Java as a number 10x the major/minor,
     * e.g Java1.5 has a value of 15
     */
    public static int getJavaVersionNumber() {
        return javaVersionNumber;
    }

    /**
     * Compares the current Java version to the passed in String -
     * assumes the argument is one of the constants defined in this
     * class.
     * Note that Ant now requires JDK 1.5+ so {@link #JAVA_1_0} through
     * {@link #JAVA_1_4} need no longer be tested for.
     * @param version the version to check against the current version.
     * @return true if the version of Java is the same as the given version.
     * @since Ant 1.5
     */
    public static boolean isJavaVersion(String version) {
        return javaVersion.equals(version);
    }

    /**
     * Compares the current Java version to the passed in String -
     * assumes the argument is one of the constants defined in this
     * class.
     * Note that Ant now requires JDK 1.5+ so {@link #JAVA_1_0} through
     * {@link #JAVA_1_4} need no longer be tested for.
     * @param version the version to check against the current version.
     * @return true if the version of Java is the same or higher than the
     * given version.
     * @since Ant 1.7
     */
    public static boolean isAtLeastJavaVersion(String version) {
        return javaVersion.compareTo(version) >= 0;
    }

    /**
     * Checks whether the current Java VM is Kaffe.
     * @return true if the current Java VM is Kaffe.
     * @since Ant 1.6.3
     * @see <a href="http://www.kaffe.org/">http://www.kaffe.org/</a>
     */
    public static boolean isKaffe() {
        return kaffeDetected;
    }

    /**
     * Checks whether the current Java VM is GNU Classpath
     * @since Ant 1.9.1
     * @return true if the version of Java is GNU Classpath
     */
    public static boolean isClasspathBased() {
        return classpathDetected;
    }

    /**
     * Checks whether the current Java VM is the GNU interpreter gij
     * or we are running in a gcj precompiled binary.
     * @since Ant 1.8.2
     * @return true if the current Java VM is gcj/gij.
     */
    public static boolean isGij() {
        return gijDetected;
    }

    /**
     * Checks whether the current VM is Apache Harmony.
     * @since Ant 1.8.2
     * @return true if the current VM is Apache Harmony.
     */
    public static boolean isApacheHarmony() {
        return harmonyDetected;
    }

    /**
     * Finds an executable that is part of a JRE installation based on
     * the java.home system property.
     *
     * <p><code>java</code>, <code>keytool</code>,
     * <code>policytool</code>, <code>orbd</code>, <code>rmid</code>,
     * <code>rmiregistry</code>, <code>servertool</code> and
     * <code>tnameserv</code> are JRE executables on Sun based
     * JRE's.</p>
     *
     * <p>You typically find them in <code>JAVA_HOME/jre/bin</code> if
     * <code>JAVA_HOME</code> points to your JDK installation.  JDK
     * &lt; 1.2 has them in the same directory as the JDK
     * executables.</p>
     * @param command the java executable to find.
     * @return the path to the command.
     * @since Ant 1.5
     */
    public static String getJreExecutable(String command) {
        if (IS_NETWARE) {
            // Extrapolating from:
            // "NetWare may have a "java" in that directory, but 99% of
            // the time, you don't want to execute it" -- Jeff Tulley
            // <JTULLEY@novell.com>
            return command;
        }

        File jExecutable = null;

        if (IS_AIX) {
            // On IBM's JDK 1.2 the directory layout is different, 1.3 follows
            // Sun's layout.
            jExecutable = findInDir(JAVA_HOME + "/sh", command);
        }

        if (jExecutable == null) {
            jExecutable = findInDir(JAVA_HOME + "/bin", command);
        }

        if (jExecutable != null) {
            return jExecutable.getAbsolutePath();
        } else {
            // Unfortunately on Windows java.home doesn't always refer
            // to the correct location, so we need to fall back to
            // assuming java is somewhere on the PATH.
            return addExtension(command);
        }
    }

    /**
     * Finds an executable that is part of a JDK installation based on
     * the java.home system property.
     *
     * <p>You typically find them in <code>JAVA_HOME/bin</code> if
     * <code>JAVA_HOME</code> points to your JDK installation.</p>
     * @param command the java executable to find.
     * @return the path to the command.
     * @since Ant 1.5
     */
    public static String getJdkExecutable(String command) {
        if (IS_NETWARE) {
            // Extrapolating from:
            // "NetWare may have a "java" in that directory, but 99% of
            // the time, you don't want to execute it" -- Jeff Tulley
            // <JTULLEY@novell.com>
            return command;
        }

        File jExecutable = null;

        if (IS_AIX) {
            // On IBM's JDK 1.2 the directory layout is different, 1.3 follows
            // Sun's layout.
            jExecutable = findInDir(JAVA_HOME + "/../sh", command);
        }

        if (jExecutable == null) {
            jExecutable = findInDir(JAVA_HOME + "/../bin", command);
        }

        if (jExecutable != null) {
            return jExecutable.getAbsolutePath();
        } else {
            // fall back to JRE bin directory, also catches JDK 1.0 and 1.1
            // where java.home points to the root of the JDK and Mac OS X where
            // the whole directory layout is different from Sun's
            // and also catches JDK 1.9 (and probably later) which
            // merged JDK and JRE dirs
            return getJreExecutable(command);
        }
    }

    /**
     * Adds a system specific extension to the name of an executable.
     *
     * @since Ant 1.5
     */
    private static String addExtension(String command) {
        // This is the most common extension case - exe for windows and OS/2,
        // nothing for *nix.
        return command + (IS_DOS ? ".exe" : "");
    }

    /**
     * Look for an executable in a given directory.
     *
     * @return null if the executable cannot be found.
     */
    private static File findInDir(String dirName, String commandName) {
        File dir = FILE_UTILS.normalize(dirName);
        File executable = null;
        if (dir.exists()) {
            executable = new File(dir, addExtension(commandName));
            if (!executable.exists()) {
                executable = null;
            }
        }
        return executable;
    }

    /**
     * demand creation of the package list.
     * When you add a new package, add a new test below.
     */

    private static void buildJrePackages() {
        jrePackages = new Vector<String>();
        switch(javaVersionNumber) {
            case VERSION_1_9:
            case VERSION_1_8:
            case VERSION_1_7:
            case VERSION_1_6:
            case VERSION_1_5:
                //In Java1.5, the apache stuff moved.
                jrePackages.addElement("com.sun.org.apache");
                //fall through.
            case VERSION_1_4:
                if (javaVersionNumber == VERSION_1_4) {
                    jrePackages.addElement("org.apache.crimson");
                    jrePackages.addElement("org.apache.xalan");
                    jrePackages.addElement("org.apache.xml");
                    jrePackages.addElement("org.apache.xpath");
                }
                jrePackages.addElement("org.ietf.jgss");
                jrePackages.addElement("org.w3c.dom");
                jrePackages.addElement("org.xml.sax");
                // fall through
            case VERSION_1_3:
                jrePackages.addElement("org.omg");
                jrePackages.addElement("com.sun.corba");
                jrePackages.addElement("com.sun.jndi");
                jrePackages.addElement("com.sun.media");
                jrePackages.addElement("com.sun.naming");
                jrePackages.addElement("com.sun.org.omg");
                jrePackages.addElement("com.sun.rmi");
                jrePackages.addElement("sunw.io");
                jrePackages.addElement("sunw.util");
                // fall through
            case VERSION_1_2:
                jrePackages.addElement("com.sun.java");
                jrePackages.addElement("com.sun.image");
                // are there any here that we forgot?
                // fall through
            case VERSION_1_1:
            default:
                //things like sun.reflection, sun.misc, sun.net
                jrePackages.addElement("sun");
                jrePackages.addElement("java");
                jrePackages.addElement("javax");
                break;
        }
    }

    /**
     * Testing helper method; kept here for unification of changes.
     * @return a list of test classes depending on the java version.
     */
    public static Vector<String> getJrePackageTestCases() {
        Vector<String> tests = new Vector<String>();
        tests.addElement("java.lang.Object");
        switch(javaVersionNumber) {
            case VERSION_1_9:
            case VERSION_1_8:
            case VERSION_1_7:
            case VERSION_1_6:
            case VERSION_1_5:
                tests.addElement(
                    "com.sun.org.apache.xerces.internal.jaxp.datatype.DatatypeFactoryImpl ");
                // Fall through
            case VERSION_1_4:
                tests.addElement("sun.audio.AudioPlayer");
                if (javaVersionNumber == VERSION_1_4) {
                	// only for 1.4, not for higher versions which fall through
                    tests.addElement("org.apache.crimson.parser.ContentModel");
                    tests.addElement("org.apache.xalan.processor.ProcessorImport");
                    tests.addElement("org.apache.xml.utils.URI");
                    tests.addElement("org.apache.xpath.XPathFactory");
                }
                tests.addElement("org.ietf.jgss.Oid");
                tests.addElement("org.w3c.dom.Attr");
                tests.addElement("org.xml.sax.XMLReader");
                // fall through
            case VERSION_1_3:
                tests.addElement("org.omg.CORBA.Any");
                tests.addElement("com.sun.corba.se.internal.corba.AnyImpl");
                tests.addElement("com.sun.jndi.ldap.LdapURL");
                tests.addElement("com.sun.media.sound.Printer");
                tests.addElement("com.sun.naming.internal.VersionHelper");
                tests.addElement("com.sun.org.omg.CORBA.Initializer");
                tests.addElement("sunw.io.Serializable");
                tests.addElement("sunw.util.EventListener");
                // fall through
            case VERSION_1_2:
                tests.addElement("javax.accessibility.Accessible");
                tests.addElement("sun.misc.BASE64Encoder");
                tests.addElement("com.sun.image.codec.jpeg.JPEGCodec");
                // fall through
            case VERSION_1_1:
            default:
                //things like sun.reflection, sun.misc, sun.net
                tests.addElement("sun.reflect.SerializationConstructorAccessorImpl");
                tests.addElement("sun.net.www.http.HttpClient");
                tests.addElement("sun.audio.AudioPlayer");
                break;
        }
        return tests;
    }
    /**
     * get a vector of strings of packages built into
     * that platforms runtime jar(s)
     * @return list of packages.
     */
    public static Vector<String> getJrePackages() {
        if (jrePackages == null) {
            buildJrePackages();
        }
        return jrePackages;
    }

    /**
     *
     * Writes the command into a temporary DCL script and returns the
     * corresponding File object.
     * It is the job of the caller to delete the file on exit.
     * @param cmd the command.
     * @return the file containing the command.
     * @throws IOException if there is an error writing to the file.
     */
    public static File createVmsJavaOptionFile(String[] cmd)
            throws IOException {
        File script = FILE_UTILS.createTempFile("ANT", ".JAVA_OPTS", null, false, true);
        BufferedWriter out = null;
        try {
            out = new BufferedWriter(new FileWriter(script));
            for (int i = 0; i < cmd.length; i++) {
                out.write(cmd[i]);
                out.newLine();
            }
        } finally {
            FileUtils.close(out);
        }
        return script;
    }

    /**
     * Return the value of ${java.home}
     * @return the java home value.
     */
    public static String getJavaHome() {
        return JAVA_HOME;
    }
}