001 /* 002 * Copyright 2005 Stephen J. McConnell. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 013 * implied. 014 * 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 019 package net.dpml.lang; 020 021 import java.io.File; 022 import java.io.IOException; 023 import java.net.URI; 024 import java.net.URL; 025 import java.util.List; 026 import java.util.Arrays; 027 028 /** 029 * The SystemClassLoader is a URLClassLoader that supports late binding of 030 * URLs. This class may be configured as the system classloader when loading plugins 031 * that declare system category plugin entries. 032 * 033 * @author <a href="http://www.dpml.net">Digital Product Meta Library</a> 034 * @version 1.1.0 035 */ 036 public final class SystemClassLoader extends StandardClassLoader 037 { 038 private final ClassLoader m_parent; 039 040 /** 041 * Creation of a new system classloader. 042 * 043 * @param parent the parent classloader 044 */ 045 public SystemClassLoader( ClassLoader parent ) 046 { 047 super( "system", Category.SYSTEM, new URL[0], parent ); 048 049 m_parent = parent; 050 051 if( "true".equals( System.getProperty( "dpml.transit.include.tools" ) ) ) 052 { 053 String jrePath = System.getProperty( "java.home" ); 054 try 055 { 056 File jre = new File( jrePath ); 057 File jdk = jre.getParentFile(); 058 File lib = new File( jdk, "lib" ); 059 File jar = new File( lib, "tools.jar" ); 060 URI uri = jar.toURI(); 061 URL url = uri.toURL(); 062 addURL( url ); 063 } 064 catch( Throwable e ) 065 { 066 final String error = 067 "Internal error while attempting to establish tools.jar in the system classloader."; 068 throw new PartError( error, e ); 069 } 070 } 071 } 072 073 synchronized void addDelegates( final URI[] uris ) throws IOException 074 { 075 URL[] urls = toURLs( uris ); 076 URL[] local = super.getURLs(); 077 List list = Arrays.asList( local ); 078 for( int i=0; i<urls.length; i++ ) 079 { 080 URL url = urls[i]; 081 if( !list.contains( url ) ) 082 { 083 try 084 { 085 url.getContent(); 086 addURL( url ); 087 } 088 catch( Exception e ) 089 { 090 final String error = 091 "Failed to resolve url " + url; 092 System.err.println( error ); 093 } 094 } 095 } 096 } 097 } 098