View Javadoc

1   /***
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  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.xbean.classpath;
18  
19  import java.io.File;
20  import java.lang.reflect.Method;
21  import java.net.URL;
22  import java.net.URLClassLoader;
23  import java.security.AccessController;
24  import java.security.PrivilegedAction;
25  
26  public class TomcatClassPath extends SunURLClassPath {
27  
28      /***
29       * The Tomcat Common ClassLoader
30       */
31      private final ClassLoader classLoader;
32  
33      /***
34       * The addRepository(String jar) method of the Tomcat Common ClassLoader
35       */
36      private Method addRepositoryMethod;
37      private Method addURLMethod;
38  
39  
40      public TomcatClassPath() {
41          this(getCommonLoader(getContextClassLoader()).getParent());
42      }
43  
44      public TomcatClassPath(ClassLoader classLoader){
45          this.classLoader = classLoader;
46          try {
47              addRepositoryMethod = getAddRepositoryMethod();
48          } catch (Exception tomcat4Exception) {
49              // Must be tomcat 5
50              try {
51                  addURLMethod = getAddURLMethod();
52              } catch (Exception tomcat5Exception) {
53                  throw new RuntimeException("Failed accessing classloader for Tomcat 4 or 5", tomcat5Exception);
54              }
55          }
56      }
57  
58      private static ClassLoader getCommonLoader(ClassLoader loader) {
59          if (loader.getClass().getName().equals("org.apache.catalina.loader.StandardClassLoader")) {
60              return loader;
61          } else {
62              return getCommonLoader(loader.getParent());
63          }
64      }
65      
66      public ClassLoader getClassLoader() {
67          return classLoader;
68      }
69  
70      public void addJarsToPath(File dir) throws Exception {
71          String[] jarNames = dir.list(new java.io.FilenameFilter() {
72              public boolean accept(File dir, String name) {
73                  return (name.endsWith(".jar") || name.endsWith(".zip"));
74              }
75          });
76  
77          if (jarNames == null) {
78              return;
79          }
80  
81          for (int j = 0; j < jarNames.length; j++) {
82              this.addJarToPath(new File(dir, jarNames[j]).toURL());
83          }
84          rebuild();
85      }
86  
87      public void addJarToPath(URL jar) throws Exception {
88          this._addJarToPath(jar);
89          rebuild();
90      }
91  
92      public void _addJarToPath(URL jar) throws Exception {
93          String path = jar.toExternalForm();
94          this.addRepository(path);
95      }
96  
97      public void addRepository(String path) throws Exception {
98          if (addRepositoryMethod != null){
99              addRepositoryMethod.invoke(getClassLoader(), new Object[]{path});
100         } else {
101             addURLMethod.invoke(getClassLoader(), new Object[]{new File(path).toURL()});
102         }
103     }
104 
105     protected void rebuild() {
106         try {
107             sun.misc.URLClassPath cp = getURLClassPath((URLClassLoader) getClassLoader());
108             URL[] urls = cp.getURLs();
109             //for (int i=0; i < urls.length; i++){
110             //    System.out.println(urls[i].toExternalForm());
111             //}
112             if (urls.length < 1)
113                 return;
114 
115             StringBuffer path = new StringBuffer(urls.length * 32);
116 
117             File s = new File(urls[0].getFile());
118             path.append(s.getPath());
119             //System.out.println(s.getPath());
120 
121             for (int i = 1; i < urls.length; i++) {
122                 path.append(File.pathSeparator);
123 
124                 s = new File(urls[i].getFile());
125                 //System.out.println(s.getPath());
126                 path.append(s.getPath());
127             }
128             System.setProperty("java.class.path", path.toString());
129         } catch (Exception e) {
130         }
131 
132     }
133 
134     /***
135      * This method gets the Tomcat StandardClassLoader.addRepository method
136      * via reflection. This allows us to call the addRepository method for
137      * Tomcat integration, but doesn't require us to include or ship any
138      * Tomcat libraries.
139      *
140      * @return URLClassLoader.addURL method instance
141      */
142     private Method getAddURLMethod() throws Exception {
143         return (Method) AccessController.doPrivileged(new PrivilegedAction() {
144             public Object run() {
145                 Method method = null;
146                 try {
147                     Class clazz = URLClassLoader.class;
148                     method = clazz.getDeclaredMethod("addURL", new Class[]{URL.class});
149                     method.setAccessible(true);
150                     return method;
151                 } catch (Exception e2) {
152                     e2.printStackTrace();
153                 }
154                 return method;
155             }
156         });
157     }
158 
159     private Method getAddRepositoryMethod() throws Exception {
160         return (Method) AccessController.doPrivileged(new PrivilegedAction() {
161             public Object run() {
162                 Method method = null;
163                 try {
164                     Class clazz = getClassLoader().getClass();
165                     method = clazz.getDeclaredMethod("addRepository", new Class[]{String.class});
166                     method.setAccessible(true);
167                     return method;
168                 } catch (Exception e2) {
169                     throw (IllegalStateException) new IllegalStateException("Unable to find or access the addRepository method in StandardClassLoader").initCause(e2);
170                 }
171             }
172         });
173     }
174 
175 }