View Javadoc

1   /*
2    *  jDTAUS Banking RI CurrencyDirectory
3    *  Copyright (C) 2005 Christian Schulte
4    *  <cs@schulte.it>
5    *
6    *  This library is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU Lesser General Public
8    *  License as published by the Free Software Foundation; either
9    *  version 2.1 of the License, or any later version.
10   *
11   *  This library is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   *  Lesser General Public License for more details.
15   *
16   *  You should have received a copy of the GNU Lesser General Public
17   *  License along with this library; if not, write to the Free Software
18   *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19   *
20   */
21  package org.jdtaus.banking.ri.currencydir.test;
22  
23  import java.io.IOException;
24  import java.net.URL;
25  import java.util.Collections;
26  import java.util.Enumeration;
27  import java.util.HashMap;
28  import java.util.HashSet;
29  import java.util.Map;
30  import java.util.Set;
31  
32  /**
33   * Class loader for providing classpath resources.
34   *
35   * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
36   * @version $JDTAUS: ResourceLoader.java 8661 2012-09-27 11:29:58Z schulte $
37   *
38   * @see #addResource(java.lang.String, java.net.URL)
39   * @see #addResources(java.lang.String, java.net.URL[])
40   */
41  public class ResourceLoader extends ClassLoader
42  {
43  
44      /** The class loader delegated to. */
45      private ClassLoader delegate;
46  
47      /** Mapping of classpath locations to a list of URLs to provide. */
48      private Map locations = new HashMap();
49  
50      /**
51       * Creates a new {@code ResourceLoader} instance taking a class loader classloading is delegated to.
52       *
53       * @param delegate class loader classloading is delegated to.
54       */
55      public ResourceLoader( final ClassLoader delegate )
56      {
57          super();
58          this.delegate = delegate;
59      }
60  
61      /** {@inheritDoc} */
62      protected Class findClass( final String name ) throws ClassNotFoundException
63      {
64          return this.delegate.loadClass( name );
65      }
66  
67      /**
68       * Finds the resource with the given name by searching the instance for any resources first, falling back to the
69       * class loader classloading is delegated to.
70       *
71       * @param name The name of the resource.
72       *
73       * @return A {@code URL} object for reading the resource, or {@code null} if the resource could not be found.
74       */
75      protected URL findResource( final String name )
76      {
77          URL thisResource = null;
78  
79          if ( this.locations.containsKey( name ) )
80          {
81              final Set resources = (Set) this.locations.get( name );
82              thisResource = (URL) resources.iterator().next();
83          }
84          else
85          {
86              thisResource = this.delegate.getResource( name );
87          }
88  
89          return thisResource;
90      }
91  
92      /**
93       * Returns an enumeration of {@link java.net.URL {@code URL}} objects representing all the resources with the given
94       * name by searching the instance for any resources first, falling back to the class loader classloading is
95       * delegated to.
96       *
97       * @param name The resource name
98       *
99       * @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 }