KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > system > main > ToolsJarHack


1 /**
2  *
3  * Copyright 2004 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * 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 package org.apache.geronimo.system.main;
18
19 import java.io.File JavaDoc;
20 import java.lang.reflect.Field JavaDoc;
21 import java.net.MalformedURLException JavaDoc;
22 import java.net.URL JavaDoc;
23 import java.net.URLClassLoader JavaDoc;
24 import java.security.AccessController JavaDoc;
25 import java.security.PrivilegedAction JavaDoc;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import sun.misc.URLClassPath;
30
31 /**
32  * @version $Rev: 46019 $ $Date: 2004-09-14 02:56:06 -0700 (Tue, 14 Sep 2004) $
33  */

34 public class ToolsJarHack {
35     private static boolean installed = false;
36     private static Log log;
37
38     public static void install() {
39         if (installed) {
40             return;
41         }
42
43         if (log == null) {
44             log = LogFactory.getLog(ToolsJarHack.class);
45         }
46
47         // Is the compiler already available
48
ClassLoader JavaDoc myClassLoader = ToolsJarHack.class.getClassLoader();
49         Class JavaDoc compilerClass = null;
50         try {
51             compilerClass = myClassLoader.loadClass("sun.tools.javac.Main");
52         } catch (ClassNotFoundException JavaDoc ignored) {
53         }
54         if (compilerClass != null) {
55             installed = true;
56             return;
57         }
58
59         File JavaDoc toolsJarFile = findToolsJarFile();
60         if (toolsJarFile == null) {
61             // could not locate the tools jar file... message alredy logged
62
return;
63         }
64
65         URL JavaDoc toolsJarURL;
66         try {
67             toolsJarURL = toolsJarFile.toURL();
68         } catch (MalformedURLException JavaDoc e) {
69             log.warn("Could not all find java compiler: tools.jar file not a regular file: " + toolsJarFile.getAbsolutePath(), e);
70             return;
71         }
72         addJarToPath(toolsJarURL);
73         installed = true;
74     }
75
76     private static File JavaDoc findToolsJarFile() {
77         String JavaDoc javaHome = System.getProperty("java.home");
78         if (javaHome == null) {
79             return null;
80         }
81
82         File JavaDoc javaHomeDir = new File JavaDoc(javaHome);
83         if (!javaHomeDir.isDirectory()) {
84             return null;
85         }
86
87         File JavaDoc toolsJarFile = findToolsJarFile(javaHomeDir);
88         if (toolsJarFile != null) {
89             return toolsJarFile;
90         }
91
92         toolsJarFile = findToolsJarFile(javaHomeDir.getParentFile());
93         if (toolsJarFile != null) {
94             return toolsJarFile;
95         }
96
97         log.warn("Could not all find java compiler: lib" + File.separator + "tools.jar file not found in " +
98                 javaHomeDir.getAbsolutePath() + " or " + javaHomeDir.getParentFile().getAbsolutePath());
99         return null;
100     }
101
102     private static File JavaDoc findToolsJarFile(File JavaDoc javaHomeDir) {
103         File JavaDoc toolsJarFile;
104         toolsJarFile = new File JavaDoc(javaHomeDir, "lib" + File.separator + "tools.jar");
105         if (!toolsJarFile.exists()) {
106             return null;
107         }
108         if (!toolsJarFile.isFile()) {
109             return null;
110         }
111         return toolsJarFile;
112     }
113
114
115     private static void addJarToPath(URL JavaDoc jar) {
116         //System.out.println("[|] SYSTEM "+jar.toExternalForm());
117
URLClassLoader JavaDoc urlClassLoader = null;
118         try {
119             urlClassLoader = (URLClassLoader JavaDoc) ClassLoader.getSystemClassLoader();
120         } catch (Throwable JavaDoc e) {
121             log.warn("Could not install compiler: Could not obtain access to system class loader", e);
122             return;
123         }
124
125         URLClassPath urlClassPath = getURLClassPath(urlClassLoader);
126         if (urlClassPath == null) {
127             // couldn't get the class path... error was already logged
128
return;
129         }
130         urlClassPath.addURL(jar);
131
132         rebuildJavaClassPathVariable(urlClassPath);
133     }
134
135     private static URLClassPath getURLClassPath(URLClassLoader JavaDoc loader) {
136         Field JavaDoc ucpField = (Field JavaDoc) AccessController.doPrivileged(new PrivilegedAction JavaDoc() {
137             public Object JavaDoc run() {
138                 Field JavaDoc ucp = null;
139                 try {
140                     ucp = URLClassLoader JavaDoc.class.getDeclaredField("ucp");
141                     ucp.setAccessible(true);
142                 } catch (Exception JavaDoc e) {
143                     log.warn("Could not install compiler: Could not obtain access to ucp field of the URLClassLoader", e);
144                 }
145                 return ucp;
146             }
147         });
148
149         if (ucpField == null) {
150             return null;
151         }
152         try {
153             return (URLClassPath) ucpField.get(loader);
154         } catch (IllegalAccessException JavaDoc e) {
155             log.warn("Could not install compiler: Could not obtain access to ucp field of the URLClassLoader", e);
156             return null;
157         }
158     }
159
160     private static void rebuildJavaClassPathVariable(URLClassPath urlClassPath) {
161         URL JavaDoc[] urls = urlClassPath.getURLs();
162         if (urls.length < 1) {
163             return;
164         }
165
166         StringBuffer JavaDoc path = new StringBuffer JavaDoc(urls.length * 32);
167
168         for (int i = 0; i < urls.length; i++) {
169             if (i != 0) {
170                 path.append(File.pathSeparator);
171             }
172
173             path.append(new File JavaDoc(urls[i].getFile()).getPath());
174         }
175         try {
176             System.setProperty("java.class.path", path.toString());
177         } catch (Exception JavaDoc e) {
178             log.warn("Error installing compiler: Could not update java.class.path property which may cause compiler to not work correctly", e);
179         }
180     }
181 }
182
Popular Tags