Source Code Cross Referenced for JavaEnvUtils.java in  » Build » ANT » org » apache » tools » ant » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Build » ANT » org.apache.tools.ant.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *  Licensed to the Apache Software Foundation (ASF) under one or more
003:         *  contributor license agreements.  See the NOTICE file distributed with
004:         *  this work for additional information regarding copyright ownership.
005:         *  The ASF licenses this file to You under the Apache License, Version 2.0
006:         *  (the "License"); you may not use this file except in compliance with
007:         *  the License.  You may obtain a copy of the License at
008:         *
009:         *      http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         *  Unless required by applicable law or agreed to in writing, software
012:         *  distributed under the License is distributed on an "AS IS" BASIS,
013:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         *  See the License for the specific language governing permissions and
015:         *  limitations under the License.
016:         *
017:         */
018:        package org.apache.tools.ant.util;
019:
020:        import java.io.File;
021:        import java.io.IOException;
022:        import java.io.PrintWriter;
023:        import java.io.FileWriter;
024:        import java.io.BufferedWriter;
025:        import java.util.Vector;
026:        import org.apache.tools.ant.taskdefs.condition.Os;
027:
028:        /**
029:         * A set of helper methods related to locating executables or checking
030:         * conditons of a given Java installation.
031:         *
032:         * @since Ant 1.5
033:         */
034:        public final class JavaEnvUtils {
035:
036:            private JavaEnvUtils() {
037:            }
038:
039:            /** Are we on a DOS-based system */
040:            private static final boolean IS_DOS = Os.isFamily("dos");
041:            /** Are we on Novell NetWare */
042:            private static final boolean IS_NETWARE = Os.isName("netware");
043:            /** Are we on AIX */
044:            private static final boolean IS_AIX = Os.isName("aix");
045:
046:            /** shortcut for System.getProperty("java.home") */
047:            private static final String JAVA_HOME = System
048:                    .getProperty("java.home");
049:
050:            /** FileUtils instance for path normalization */
051:            private static final FileUtils FILE_UTILS = FileUtils
052:                    .getFileUtils();
053:
054:            /** Version of currently running VM. */
055:            private static String javaVersion;
056:
057:            /** floating version of the JVM */
058:            private static int javaVersionNumber;
059:
060:            /** Version constant for Java 1.0 */
061:            public static final String JAVA_1_0 = "1.0";
062:            /** Version constant for Java 1.1 */
063:            public static final String JAVA_1_1 = "1.1";
064:            /** Version constant for Java 1.2 */
065:            public static final String JAVA_1_2 = "1.2";
066:            /** Version constant for Java 1.3 */
067:            public static final String JAVA_1_3 = "1.3";
068:            /** Version constant for Java 1.4 */
069:            public static final String JAVA_1_4 = "1.4";
070:            /** Version constant for Java 1.5 */
071:            public static final String JAVA_1_5 = "1.5";
072:            /** Version constant for Java 1.6 */
073:            public static final String JAVA_1_6 = "1.6";
074:
075:            /** Whether this is the Kaffe VM */
076:            private static boolean kaffeDetected;
077:
078:            /** array of packages in the runtime */
079:            private static Vector jrePackages;
080:
081:            static {
082:
083:                // Determine the Java version by looking at available classes
084:                // java.net.Proxy was introduced in JDK 1.5
085:                // java.lang.CharSequence was introduced in JDK 1.4
086:                // java.lang.StrictMath was introduced in JDK 1.3
087:                // java.lang.ThreadLocal was introduced in JDK 1.2
088:                // java.lang.Void was introduced in JDK 1.1
089:                // Count up version until a NoClassDefFoundError ends the try
090:
091:                try {
092:                    javaVersion = JAVA_1_0;
093:                    javaVersionNumber = 10;
094:                    Class.forName("java.lang.Void");
095:                    javaVersion = JAVA_1_1;
096:                    javaVersionNumber++;
097:                    Class.forName("java.lang.ThreadLocal");
098:                    javaVersion = JAVA_1_2;
099:                    javaVersionNumber++;
100:                    Class.forName("java.lang.StrictMath");
101:                    javaVersion = JAVA_1_3;
102:                    javaVersionNumber++;
103:                    Class.forName("java.lang.CharSequence");
104:                    javaVersion = JAVA_1_4;
105:                    javaVersionNumber++;
106:                    Class.forName("java.net.Proxy");
107:                    javaVersion = JAVA_1_5;
108:                    javaVersionNumber++;
109:                    Class.forName("java.util.ServiceLoader");
110:                    javaVersion = JAVA_1_6;
111:                    javaVersionNumber++;
112:                } catch (Throwable t) {
113:                    // swallow as we've hit the max class version that
114:                    // we have
115:                }
116:                kaffeDetected = false;
117:                try {
118:                    Class.forName("kaffe.util.NotImplemented");
119:                    kaffeDetected = true;
120:                } catch (Throwable t) {
121:                    // swallow as this simply doesn't seem to be Kaffe
122:                }
123:            }
124:
125:            /**
126:             * Returns the version of Java this class is running under.
127:             * @return the version of Java as a String, e.g. "1.1"
128:             */
129:            public static String getJavaVersion() {
130:                return javaVersion;
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 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 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 getJreExecutable(String 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 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 getJdkExecutable(String 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 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 addExtension(String 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 findInDir(String dirName, String commandName) {
289:                File dir = FILE_UTILS.normalize(dirName);
290:                File executable = null;
291:                if (dir.exists()) {
292:                    executable = new File(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();
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 getJrePackageTestCases() {
355:                Vector tests = new Vector();
356:                tests.addElement("java.lang.Object");
357:                switch (javaVersionNumber) {
358:                case 16:
359:                case 15:
360:                    tests
361:                            .addElement("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
367:                                .addElement("org.apache.crimson.parser.ContentModel");
368:                        tests
369:                                .addElement("org.apache.xalan.processor.ProcessorImport");
370:                        tests.addElement("org.apache.xml.utils.URI");
371:                        tests.addElement("org.apache.xpath.XPathFactory");
372:                    }
373:                    tests.addElement("org.ietf.jgss.Oid");
374:                    tests.addElement("org.w3c.dom.Attr");
375:                    tests.addElement("org.xml.sax.XMLReader");
376:                    // fall through
377:                case 13:
378:                    tests.addElement("org.omg.CORBA.Any");
379:                    tests.addElement("com.sun.corba.se.internal.corba.AnyImpl");
380:                    tests.addElement("com.sun.jndi.ldap.LdapURL");
381:                    tests.addElement("com.sun.media.sound.Printer");
382:                    tests.addElement("com.sun.naming.internal.VersionHelper");
383:                    tests.addElement("com.sun.org.omg.CORBA.Initializer");
384:                    tests.addElement("sunw.io.Serializable");
385:                    tests.addElement("sunw.util.EventListener");
386:                    // fall through
387:                case 12:
388:                    tests.addElement("javax.accessibility.Accessible");
389:                    tests.addElement("sun.misc.BASE64Encoder");
390:                    tests.addElement("com.sun.image.codec.jpeg.JPEGCodec");
391:                    // fall through
392:                case 11:
393:                default:
394:                    //things like sun.reflection, sun.misc, sun.net
395:                    tests
396:                            .addElement("sun.reflect.SerializationConstructorAccessorImpl");
397:                    tests.addElement("sun.net.www.http.HttpClient");
398:                    tests.addElement("sun.audio.AudioPlayer");
399:                    break;
400:                }
401:                return tests;
402:            }
403:
404:            /**
405:             * get a vector of strings of packages built into
406:             * that platforms runtime jar(s)
407:             * @return list of packages.
408:             */
409:            public static Vector getJrePackages() {
410:                if (jrePackages == null) {
411:                    buildJrePackages();
412:                }
413:                return jrePackages;
414:            }
415:
416:            /**
417:             *
418:             * Writes the command into a temporary DCL script and returns the
419:             * corresponding File object.
420:             * It is the job of the caller to delete the file on exit.
421:             * @param cmd the command.
422:             * @return the file containing the command.
423:             * @throws IOException if there is an error writing to the file.
424:             */
425:            public static File createVmsJavaOptionFile(String[] cmd)
426:                    throws IOException {
427:                File script = FILE_UTILS.createTempFile("ANT", ".JAVA_OPTS",
428:                        null);
429:                PrintWriter out = null;
430:                try {
431:                    out = new PrintWriter(new BufferedWriter(new FileWriter(
432:                            script)));
433:                    for (int i = 0; i < cmd.length; i++) {
434:                        out.println(cmd[i]);
435:                    }
436:                } finally {
437:                    FileUtils.close(out);
438:                }
439:                return script;
440:            }
441:
442:            /**
443:             * Return the value of ${java.home}
444:             * @return the java home value.
445:             */
446:            public static String getJavaHome() {
447:                return JAVA_HOME;
448:            }
449:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.