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 */ 017package org.apache.xbean.classpath; 018 019import java.io.File; 020import java.lang.reflect.Method; 021import java.net.URL; 022import java.net.URLClassLoader; 023 024public class TomcatClassPath extends SunURLClassPath { 025 026 /** 027 * The Tomcat Common ClassLoader 028 */ 029 private final ClassLoader classLoader; 030 031 /** 032 * The addRepository(String jar) method of the Tomcat Common ClassLoader 033 */ 034 private Method addRepositoryMethod; 035 private Method addURLMethod; 036 037 038 public TomcatClassPath() { 039 this(getCommonLoader(getContextClassLoader()).getParent()); 040 } 041 042 public TomcatClassPath(ClassLoader classLoader){ 043 this.classLoader = classLoader; 044 try { 045 addRepositoryMethod = getAddRepositoryMethod(); 046 } catch (Exception tomcat4Exception) { 047 // Must be tomcat 5 048 try { 049 addURLMethod = getAddURLMethod(); 050 } catch (Exception tomcat5Exception) { 051 throw new RuntimeException("Failed accessing classloader for Tomcat 4 or 5", tomcat5Exception); 052 } 053 } 054 } 055 056 private static ClassLoader getCommonLoader(ClassLoader loader) { 057 if (loader.getClass().getName().equals("org.apache.catalina.loader.StandardClassLoader")) { 058 return loader; 059 } else { 060 return getCommonLoader(loader.getParent()); 061 } 062 } 063 064 public ClassLoader getClassLoader() { 065 return classLoader; 066 } 067 068 public void addJarsToPath(File dir) throws Exception { 069 String[] jarNames = dir.list(new java.io.FilenameFilter() { 070 public boolean accept(File dir, String name) { 071 return (name.endsWith(".jar") || name.endsWith(".zip")); 072 } 073 }); 074 075 if (jarNames == null) { 076 return; 077 } 078 079 for (int j = 0; j < jarNames.length; j++) { 080 this.addJarToPath(new File(dir, jarNames[j]).toURI().toURL()); 081 } 082 rebuild(); 083 } 084 085 public void addJarToPath(URL jar) throws Exception { 086 this._addJarToPath(jar); 087 rebuild(); 088 } 089 090 public void _addJarToPath(URL jar) throws Exception { 091 String path = jar.toExternalForm(); 092 this.addRepository(path); 093 } 094 095 public void addRepository(String path) throws Exception { 096 if (addRepositoryMethod != null){ 097 addRepositoryMethod.invoke(getClassLoader(), new Object[]{path}); 098 } else { 099 addURLMethod.invoke(getClassLoader(), new Object[]{new File(path).toURI().toURL()}); 100 } 101 } 102 103 protected void rebuild() { 104 try { 105 sun.misc.URLClassPath cp = getURLClassPath((URLClassLoader) getClassLoader()); 106 URL[] urls = cp.getURLs(); 107 //for (int i=0; i < urls.length; i++){ 108 // System.out.println(urls[i].toExternalForm()); 109 //} 110 if (urls.length < 1) 111 return; 112 113 StringBuffer path = new StringBuffer(urls.length * 32); 114 115 File s = new File(urls[0].getFile()); 116 path.append(s.getPath()); 117 //System.out.println(s.getPath()); 118 119 for (int i = 1; i < urls.length; i++) { 120 path.append(File.pathSeparator); 121 122 s = new File(urls[i].getFile()); 123 //System.out.println(s.getPath()); 124 path.append(s.getPath()); 125 } 126 System.setProperty("java.class.path", path.toString()); 127 } catch (Exception e) { 128 } 129 130 } 131 132 /** 133 * This method gets the Tomcat StandardClassLoader.addRepository method 134 * via reflection. This allows us to call the addRepository method for 135 * Tomcat integration, but doesn't require us to include or ship any 136 * Tomcat libraries. 137 * 138 * @return URLClassLoader.addURL method instance 139 */ 140 private Method getAddURLMethod() throws Exception { 141 Method method = null; 142 Class clazz = URLClassLoader.class; 143 method = clazz.getDeclaredMethod("addURL", new Class[]{URL.class}); 144 method.setAccessible(true); 145 return method; 146 } 147 148 private Method getAddRepositoryMethod() throws Exception { 149 Method method = null; 150 Class clazz = getClassLoader().getClass(); 151 method = clazz.getDeclaredMethod("addRepository", new Class[]{String.class}); 152 method.setAccessible(true); 153 return method; 154 } 155 156}