KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > util > classloader > ToolsJarClassLoader


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2005 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32  *END_COPYRIGHT_BLOCK*/

33
34 package edu.rice.cs.util.classloader;
35
36 import java.net.*;
37 import java.io.File JavaDoc;
38 import java.io.FileFilter JavaDoc;
39 import java.util.ArrayList JavaDoc;
40 import java.util.Arrays JavaDoc;
41 import java.util.LinkedHashSet JavaDoc;
42
43 import edu.rice.cs.plt.io.IOUtil;
44 import edu.rice.cs.util.FileOps;
45 import edu.rice.cs.util.swing.Utilities;
46
47 /** A class loader that tries to load classes from tools.jar. It will never delegate to the system loader.
48  * @version $Id: ToolsJarClassLoader.java 4075 2007-01-19 21:35:50Z dlsmith $
49  */

50 public class ToolsJarClassLoader extends URLClassLoader {
51   
52   /* Directory containing tools.jar, if known by caller */
53   
54   /** Standard constructors */
55   public ToolsJarClassLoader(File JavaDoc toolsJar) { super(getToolsJarURLs(toolsJar)); }
56   public ToolsJarClassLoader() { this(FileOps.NONEXISTENT_FILE); }
57
58   /** Returns an array of possible Files for the tools.jar file. */
59   public static File JavaDoc[] getToolsJarFiles(File JavaDoc toolsJar) {
60     File JavaDoc javaHome = IOUtil.attemptCanonicalFile(new File JavaDoc(System.getProperty("java.home")));
61     
62     /*
63      * javaHomeParents is a set of (attempted) canonical paths that may not exist.
64      * We must maintain insertion order, so that the first entries have priority;
65      * at the same time, we want to eliminate duplicates so that the same tools.jar file
66      * doesn't show up multiple times.
67      */

68     LinkedHashSet JavaDoc<File JavaDoc> javaHomeParents = new LinkedHashSet JavaDoc<File JavaDoc>();
69     javaHomeParents.add(IOUtil.attemptCanonicalFile(new File JavaDoc(javaHome, "..")));
70     javaHomeParents.add(IOUtil.attemptCanonicalFile(new File JavaDoc(javaHome, "../..")));
71     
72     final String JavaDoc jv = System.getProperty("java.version");
73     if (!jv.startsWith("1.3") && !jv.startsWith("1.4")) {
74       // in Java 1.3 and 1.4, getenv is deprecated and throws an exception,
75
// so we cannot use it
76
String JavaDoc winPrograms = System.getenv("ProgramFiles");
77       if (winPrograms != null) {
78         javaHomeParents.add(IOUtil.attemptCanonicalFile(new File JavaDoc(winPrograms, "Java")));
79         javaHomeParents.add(IOUtil.attemptCanonicalFile(new File JavaDoc(winPrograms)));
80       }
81       else { // in case the environment variables aren't set up properly
82
javaHomeParents.add(IOUtil.attemptCanonicalFile(new File JavaDoc("/C:/Program Files/Java/")));
83         javaHomeParents.add(IOUtil.attemptCanonicalFile(new File JavaDoc("/C:/Program Files/")));
84       }
85       
86       String JavaDoc winSystem = System.getenv("SystemDrive");
87       if (winSystem != null) {
88         javaHomeParents.add(IOUtil.attemptCanonicalFile(new File JavaDoc(winSystem, "Java")));
89         javaHomeParents.add(IOUtil.attemptCanonicalFile(new File JavaDoc(winSystem)));
90       }
91       else { // in case the environment variables aren't set up properly
92
javaHomeParents.add(IOUtil.attemptCanonicalFile(new File JavaDoc("/C:/Java/")));
93         javaHomeParents.add(IOUtil.attemptCanonicalFile(new File JavaDoc("/C:/")));
94       }
95     }
96
97     javaHomeParents.add(IOUtil.attemptCanonicalFile(new File JavaDoc("/usr/")));
98     javaHomeParents.add(IOUtil.attemptCanonicalFile(new File JavaDoc("/usr/java/")));
99     javaHomeParents.add(IOUtil.attemptCanonicalFile(new File JavaDoc("/usr/j2se/")));
100     javaHomeParents.add(IOUtil.attemptCanonicalFile(new File JavaDoc("/usr/local/")));
101     javaHomeParents.add(IOUtil.attemptCanonicalFile(new File JavaDoc("/usr/local/java/")));
102     javaHomeParents.add(IOUtil.attemptCanonicalFile(new File JavaDoc("/usr/local/j2se/")));
103     
104     
105     /* javaHomes is a set of potential Java installations. Each is an existing directory. */
106     LinkedHashSet JavaDoc<File JavaDoc> javaHomes = new LinkedHashSet JavaDoc<File JavaDoc>();
107     
108     try { if (javaHome.isDirectory()) { javaHomes.add(javaHome); } }
109     catch (SecurityException JavaDoc e) { /* ignore */ }
110     
111     String JavaDoc version = System.getProperty("java.specification.version");
112     final String JavaDoc prefix1 = "j2sdk" + version;
113     final String JavaDoc prefix2 = "jdk" + version;
114     FileFilter JavaDoc matchHomes = new FileFilter JavaDoc() {
115       public boolean accept(File JavaDoc f) {
116         return f.isDirectory() && (f.getName().startsWith(prefix1) || f.getName().startsWith(prefix2));
117       }
118     };
119     for (File JavaDoc parent : javaHomeParents) {
120       try {
121         File JavaDoc[] files = parent.listFiles(matchHomes);
122         if (files != null) { for (File JavaDoc f : files) javaHomes.add(f); }
123       }
124       catch (SecurityException JavaDoc e) { /* ignore */ }
125     }
126     
127     /* The result is a set of existing tools.jar files, (attempted) canonicalized */
128     LinkedHashSet JavaDoc<File JavaDoc> result = new LinkedHashSet JavaDoc<File JavaDoc>();
129     
130     try { if (toolsJar.isFile()) result.add(IOUtil.attemptCanonicalFile(toolsJar)); }
131     catch (SecurityException JavaDoc e) { /* ignore */ }
132     
133     for (File JavaDoc home : javaHomes) {
134       try {
135         File JavaDoc tools = new File JavaDoc(home, "lib/tools.jar");
136         if (tools.isFile()) { result.add(IOUtil.attemptCanonicalFile(tools)); }
137       }
138       catch (SecurityException JavaDoc e) { /* ignore */ }
139     }
140
141     return result.toArray(new File JavaDoc[0]);
142   }
143   
144   /** Returns an array of possible URLs for the tools.jar file. */
145   public static URL[] getToolsJarURLs() { return getToolsJarURLs(FileOps.NONEXISTENT_FILE); }
146   
147   /** Returns an array of possible URLs for the tools.jar file. */
148   public static URL[] getToolsJarURLs(File JavaDoc toolsJar) {
149     File JavaDoc[] files = getToolsJarFiles(toolsJar);
150     try {
151       URL[] urls = new URL[files.length];
152       for (int i=0; i < files.length; i++) {
153         urls[i] = FileOps.toURL(files[i]);
154       }
155       return urls;
156     }
157     catch (MalformedURLException e) {
158       return new URL[0];
159     }
160   }
161   
162    /** Returns a string containing all possible tools.jar locations, separated by the system's path separator. */
163   public static String JavaDoc getToolsJarClassPath() { return getToolsJarClassPath(FileOps.NONEXISTENT_FILE); }
164
165   /** Returns a string containing all possible tools.jar locations, separated by the system's path separator. */
166   public static String JavaDoc getToolsJarClassPath(File JavaDoc toolsJar) {
167     File JavaDoc[] files = getToolsJarFiles(toolsJar);
168     final StringBuilder JavaDoc classPath = new StringBuilder JavaDoc();
169     String JavaDoc pathSep = System.getProperty("path.separator");
170
171     for (int i=0; i < files.length; i++) {
172       if (i > 0) classPath.append(pathSep);
173       classPath.append(files[i].getAbsolutePath());
174     }
175     return classPath.toString();
176   }
177
178   /** Gets the requested resource, bypassing the parent classloader. */
179   public URL getResource(String JavaDoc name) { return findResource(name); }
180 }
181
Popular Tags