001/* 002 * jDTAUS Banking RI CurrencyDirectory 003 * Copyright (C) 2005 Christian Schulte 004 * <cs@schulte.it> 005 * 006 * This library is free software; you can redistribute it and/or 007 * modify it under the terms of the GNU Lesser General Public 008 * License as published by the Free Software Foundation; either 009 * version 2.1 of the License, or any later version. 010 * 011 * This library is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 * Lesser General Public License for more details. 015 * 016 * You should have received a copy of the GNU Lesser General Public 017 * License along with this library; if not, write to the Free Software 018 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 019 * 020 */ 021package org.jdtaus.banking.ri.currencydir.test; 022 023import java.io.IOException; 024import java.net.URL; 025import java.util.Collections; 026import java.util.Enumeration; 027import java.util.HashMap; 028import java.util.HashSet; 029import java.util.Map; 030import java.util.Set; 031 032/** 033 * Class loader for providing classpath resources. 034 * 035 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a> 036 * @version $JDTAUS: ResourceLoader.java 8661 2012-09-27 11:29:58Z schulte $ 037 * 038 * @see #addResource(java.lang.String, java.net.URL) 039 * @see #addResources(java.lang.String, java.net.URL[]) 040 */ 041public class ResourceLoader extends ClassLoader 042{ 043 044 /** The class loader delegated to. */ 045 private ClassLoader delegate; 046 047 /** Mapping of classpath locations to a list of URLs to provide. */ 048 private Map locations = new HashMap(); 049 050 /** 051 * Creates a new {@code ResourceLoader} instance taking a class loader classloading is delegated to. 052 * 053 * @param delegate class loader classloading is delegated to. 054 */ 055 public ResourceLoader( final ClassLoader delegate ) 056 { 057 super(); 058 this.delegate = delegate; 059 } 060 061 /** {@inheritDoc} */ 062 protected Class findClass( final String name ) throws ClassNotFoundException 063 { 064 return this.delegate.loadClass( name ); 065 } 066 067 /** 068 * Finds the resource with the given name by searching the instance for any resources first, falling back to the 069 * class loader classloading is delegated to. 070 * 071 * @param name The name of the resource. 072 * 073 * @return A {@code URL} object for reading the resource, or {@code null} if the resource could not be found. 074 */ 075 protected URL findResource( final String name ) 076 { 077 URL thisResource = null; 078 079 if ( this.locations.containsKey( name ) ) 080 { 081 final Set resources = (Set) this.locations.get( name ); 082 thisResource = (URL) resources.iterator().next(); 083 } 084 else 085 { 086 thisResource = this.delegate.getResource( name ); 087 } 088 089 return thisResource; 090 } 091 092 /** 093 * Returns an enumeration of {@link java.net.URL {@code URL}} objects representing all the resources with the given 094 * name by searching the instance for any resources first, falling back to the class loader classloading is 095 * delegated to. 096 * 097 * @param name The resource name 098 * 099 * @return An enumeration of {@link java.net.URL {@code URL}} objects for the resources. 100 * 101 * @throws IOException if searching fails. 102 */ 103 protected Enumeration findResources( final String name ) throws IOException 104 { 105 Enumeration thisResources = null; 106 107 if ( this.locations.containsKey( name ) ) 108 { 109 final Set resources = (Set) this.locations.get( name ); 110 thisResources = Collections.enumeration( resources ); 111 } 112 else if ( thisResources == null || !thisResources.hasMoreElements() ) 113 { 114 thisResources = this.delegate.getResources( name ); 115 } 116 117 return thisResources; 118 } 119 120 /** 121 * Adds a resource to the instance. 122 * 123 * @param name The name of the resource to add. 124 * @param resource The resource to add to the instance. 125 * 126 * @throws NullPointerException if {@code name} or {@code resource} is {@code null}. 127 */ 128 public void addResource( final String name, final URL resource ) 129 { 130 if ( name == null ) 131 { 132 throw new NullPointerException( "name " ); 133 } 134 if ( resource == null ) 135 { 136 throw new NullPointerException( "resource" ); 137 } 138 139 Set resources = (Set) this.locations.get( name ); 140 if ( resources == null ) 141 { 142 resources = new HashSet(); 143 this.locations.put( name, resources ); 144 } 145 146 resources.add( resource ); 147 } 148 149 /** 150 * Adds an array of resources to the instance. 151 * 152 * @param name The name of the resources to add. 153 * @param resources The resources to add to the instance. 154 * 155 * @throws NullPointerException if {@code name} or {@code resources} is {@code null}. 156 */ 157 public void addResources( final String name, final URL[] resources ) 158 { 159 if ( name == null ) 160 { 161 throw new NullPointerException( "name" ); 162 } 163 if ( resources == null ) 164 { 165 throw new NullPointerException( "resources" ); 166 } 167 168 for ( int i = resources.length - 1; i >= 0; i-- ) 169 { 170 this.addResource( name, resources[i] ); 171 } 172 } 173 174}