View Javadoc

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