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 */ 017 package org.apache.xbean.classpath; 018 019 import java.io.File; 020 import java.net.URL; 021 import java.net.URLClassLoader; 022 import java.security.AccessController; 023 import java.security.PrivilegedAction; 024 025 public abstract class SunURLClassPath implements ClassPath { 026 public static ClassLoader getContextClassLoader() { 027 return (ClassLoader) AccessController.doPrivileged(new PrivilegedAction() { 028 public Object run() { 029 return Thread.currentThread().getContextClassLoader(); 030 } 031 }); 032 } 033 034 private java.lang.reflect.Field ucpField; 035 036 protected void addJarToPath(final URL jar, final URLClassLoader loader) throws Exception { 037 this.getURLClassPath(loader).addURL(jar); 038 } 039 040 protected void addJarsToPath(final File dir, final URLClassLoader loader) throws Exception { 041 if (dir == null || !dir.exists()) return; 042 //System.out.println("DIR "+dir); 043 // Get the list of jars and zips 044 String[] jarNames = dir.list(new java.io.FilenameFilter() { 045 public boolean accept(File dir, String name) { 046 //System.out.println("FILE "+name); 047 return (name.endsWith(".jar") || name.endsWith(".zip")); 048 } 049 }); 050 051 // Create URLs from them 052 final URL[] jars = new URL[jarNames.length]; 053 for (int j = 0; j < jarNames.length; j++) { 054 jars[j] = new File(dir, jarNames[j]).toURL(); 055 } 056 057 sun.misc.URLClassPath path = getURLClassPath(loader); 058 for (int i = 0; i < jars.length; i++) { 059 //System.out.println("URL "+jars[i]); 060 path.addURL(jars[i]); 061 } 062 } 063 064 protected sun.misc.URLClassPath getURLClassPath(URLClassLoader loader) throws Exception { 065 return (sun.misc.URLClassPath) getUcpField().get(loader); 066 } 067 068 private java.lang.reflect.Field getUcpField() throws Exception { 069 if (ucpField == null) { 070 // Add them to the URLClassLoader's classpath 071 ucpField = (java.lang.reflect.Field) AccessController.doPrivileged(new PrivilegedAction() { 072 public Object run() { 073 java.lang.reflect.Field ucp = null; 074 try { 075 ucp = URLClassLoader.class.getDeclaredField("ucp"); 076 ucp.setAccessible(true); 077 } catch (Exception e2) { 078 e2.printStackTrace(); 079 } 080 return ucp; 081 } 082 }); 083 } 084 085 return ucpField; 086 } 087 088 }