KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > loader > TomcatClassPath


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "OpenEJB" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of The OpenEJB Group. For written permission,
18  * please contact info@openejb.org.
19  *
20  * 4. Products derived from this Software may not be called "OpenEJB"
21  * nor may "OpenEJB" appear in their names without prior written
22  * permission of The OpenEJB Group. OpenEJB is a registered
23  * trademark of The OpenEJB Group.
24  *
25  * 5. Due credit should be given to the OpenEJB Project
26  * (http://openejb.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE OPENEJB GROUP AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * THE OPENEJB GROUP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 2005 (C) The OpenEJB Group. All Rights Reserved.
42  *
43  * $Id: TomcatClassPath.java 2490 2006-02-23 03:32:23Z dblevins $
44  */

45
46 package org.openejb.loader;
47
48 import sun.misc.URLClassPath;
49
50 import java.io.File JavaDoc;
51 import java.lang.reflect.Method JavaDoc;
52 import java.net.URL JavaDoc;
53 import java.net.URLClassLoader JavaDoc;
54 import java.security.AccessController JavaDoc;
55 import java.security.PrivilegedAction JavaDoc;
56
57 /**
58  * @version $Revision: 2490 $ $Date: 2006-02-22 19:32:23 -0800 (Wed, 22 Feb 2006) $
59  */

60 /*-------------------------------------------------------*/
61 /* Tomcat ClassLoader Support */
62 /*-------------------------------------------------------*/
63 public class TomcatClassPath extends BasicURLClassPath {
64
65     /**
66      * The Tomcat Common ClassLoader
67      */

68     private final ClassLoader JavaDoc classLoader;
69
70     /**
71      * The addRepository(String jar) method of the Tomcat Common ClassLoader
72      */

73     private Method JavaDoc addRepositoryMethod;
74     private Method JavaDoc addURLMethod;
75     private Method JavaDoc getLoaderMethod;
76
77
78     public TomcatClassPath() {
79         this(getCommonLoader(getContextClassLoader()).getParent());
80     }
81
82     public TomcatClassPath(ClassLoader JavaDoc classLoader){
83         this.classLoader = classLoader;
84         try {
85             addRepositoryMethod = getAddRepositoryMethod();
86         } catch (Exception JavaDoc tomcat4Exception) {
87             // Must be tomcat 5
88
try {
89                 addURLMethod = getAddURLMethod();
90             } catch (Exception JavaDoc tomcat5Exception) {
91                 throw new RuntimeException JavaDoc("Failed accessing classloader for Tomcat 4 or 5", tomcat5Exception);
92             }
93             try {
94                 getLoaderMethod = getLoaderMethod();
95             } catch (Exception JavaDoc e) {
96             }
97         }
98     }
99
100     private static ClassLoader JavaDoc getCommonLoader(ClassLoader JavaDoc loader) {
101         if (loader.getClass().getName().equals("org.apache.catalina.loader.StandardClassLoader")) {
102             return loader;
103         } else {
104             return getCommonLoader(loader.getParent());
105         }
106     }
107     
108     public ClassLoader JavaDoc getClassLoader() {
109         return classLoader;
110     }
111
112     public void addJarsToPath(File JavaDoc dir) throws Exception JavaDoc {
113         String JavaDoc[] jarNames = dir.list(new java.io.FilenameFilter JavaDoc() {
114             public boolean accept(File JavaDoc dir, String JavaDoc name) {
115                 return (name.endsWith(".jar") || name.endsWith(".zip"));
116             }
117         });
118
119         if (jarNames == null) {
120             return;
121         }
122
123         for (int j = 0; j < jarNames.length; j++) {
124             this.addJarToPath(new File JavaDoc(dir, jarNames[j]).toURL());
125         }
126         rebuild();
127     }
128
129     public void addJarToPath(URL JavaDoc jar) throws Exception JavaDoc {
130         this._addJarToPath(jar);
131         rebuild();
132     }
133
134     public void _addJarToPath(URL JavaDoc jar) throws Exception JavaDoc {
135         if (addRepositoryMethod != null){
136             String JavaDoc path = jar.toExternalForm();
137             addRepositoryMethod.invoke(getClassLoader(), new Object JavaDoc[]{path});
138         } else {
139             addURLMethod.invoke(getClassLoader(), new Object JavaDoc[]{jar});
140             int index = 0;
141             while (getLoader(index++) != null);
142         }
143     }
144
145     private Object JavaDoc getLoader(int i) {
146         if (getLoaderMethod == null){
147             return null;
148         }
149
150         try {
151             sun.misc.URLClassPath cp = getURLClassPath((URLClassLoader JavaDoc) getClassLoader());
152             Object JavaDoc object = getLoaderMethod.invoke(cp, new Object JavaDoc[]{new Integer JavaDoc(i)});
153             return object;
154         } catch (Exception JavaDoc e) {
155             e.printStackTrace();
156             return null;
157         }
158     }
159
160     protected void rebuild() {
161         try {
162             sun.misc.URLClassPath cp = getURLClassPath((URLClassLoader JavaDoc) getClassLoader());
163             URL JavaDoc[] urls = cp.getURLs();
164             //for (int i=0; i < urls.length; i++){
165
// System.out.println(urls[i].toExternalForm());
166
//}
167
if (urls.length < 1)
168                 return;
169
170             StringBuffer JavaDoc path = new StringBuffer JavaDoc(urls.length * 32);
171
172             File JavaDoc s = new File JavaDoc(urls[0].getFile());
173             path.append(s.getPath());
174             //System.out.println(s.getPath());
175

176             for (int i = 1; i < urls.length; i++) {
177                 path.append(File.pathSeparator);
178
179                 s = new File JavaDoc(urls[i].getFile());
180                 //System.out.println(s.getPath());
181
path.append(s.getPath());
182             }
183             System.setProperty("java.class.path", path.toString());
184         } catch (Exception JavaDoc e) {
185         }
186
187     }
188
189     /**
190      * This method gets the Tomcat StandardClassLoader.addRepository method
191      * via reflection. This allows us to call the addRepository method for
192      * Tomcat integration, but doesn't require us to include or ship any
193      * Tomcat libraries.
194      *
195      * @return URLClassLoader.addURL method instance
196      */

197     private java.lang.reflect.Method JavaDoc getAddURLMethod() throws Exception JavaDoc {
198         return (java.lang.reflect.Method JavaDoc) AccessController.doPrivileged(new PrivilegedAction JavaDoc() {
199             public Object JavaDoc run() {
200                 java.lang.reflect.Method JavaDoc method = null;
201                 try {
202                     Class JavaDoc clazz = URLClassLoader JavaDoc.class;
203                     method = clazz.getDeclaredMethod("addURL", new Class JavaDoc[]{URL JavaDoc.class});
204                     method.setAccessible(true);
205                     return method;
206                 } catch (Exception JavaDoc e2) {
207                     e2.printStackTrace();
208                 }
209                 return method;
210             }
211         });
212     }
213
214     private java.lang.reflect.Method JavaDoc getLoaderMethod() throws Exception JavaDoc {
215         return (java.lang.reflect.Method JavaDoc) AccessController.doPrivileged(new PrivilegedAction JavaDoc() {
216             public Object JavaDoc run() {
217                 java.lang.reflect.Method JavaDoc method = null;
218                 try {
219                     Class JavaDoc clazz = URLClassPath.class;
220                     method = clazz.getDeclaredMethod("getLoader", new Class JavaDoc[]{Integer.TYPE});
221                     method.setAccessible(true);
222                     return method;
223                 } catch (Exception JavaDoc e2) {
224                     e2.printStackTrace();
225                 }
226                 return method;
227             }
228         });
229     }
230
231     private Method JavaDoc getAddRepositoryMethod() throws Exception JavaDoc {
232         return (Method JavaDoc) AccessController.doPrivileged(new PrivilegedAction JavaDoc() {
233             public Object JavaDoc run() {
234                 Method JavaDoc method = null;
235                 try {
236                     Class JavaDoc clazz = getClassLoader().getClass();
237                     method = clazz.getDeclaredMethod("addRepository", new Class JavaDoc[]{String JavaDoc.class});
238                     method.setAccessible(true);
239                     return method;
240                 } catch (Exception JavaDoc e2) {
241                     throw (IllegalStateException JavaDoc) new IllegalStateException JavaDoc("Unable to find or access the addRepository method in StandardClassLoader").initCause(e2);
242                 }
243             }
244         });
245     }
246
247 }
248
Popular Tags