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