View Javadoc

1   /*
2    *  jDTAUS Core Client Container
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.core.container.ri.client.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   * Classloader for providing classpath resources.
34   *
35   * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
36   * @version $JDTAUS: ResourceLoader.java 8641 2012-09-27 06:45:17Z 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 classloader 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 classloader
52       * classloading is delegated to.
53       *
54       * @param delegate classloader classloading is delegated to.
55       */
56      public ResourceLoader( final ClassLoader delegate )
57      {
58          super();
59          this.delegate = delegate;
60      }
61  
62      /** {@inheritDoc} */
63      protected Class findClass( final String name )
64          throws ClassNotFoundException
65      {
66          return this.delegate.loadClass( name );
67      }
68  
69      /**
70       * Finds the resource with the given name by searching the instance for any
71       * resources first, falling back to the classloader classloading is
72       * delegated to.
73       *
74       * @param  name the name of the resource.
75       *
76       * @return  a {@code URL} object for reading the resource, or {@code null}
77       * if the resource could not be found.
78       */
79      protected URL findResource( final String name )
80      {
81          URL thisResource = null;
82  
83          if ( this.locations.containsKey( name ) )
84          {
85              final Set resources = ( Set ) this.locations.get( name );
86              thisResource = ( URL ) resources.iterator().next();
87          }
88          else
89          {
90              thisResource = this.delegate.getResource( name );
91          }
92  
93          return thisResource;
94      }
95  
96      /**
97       * Returns an enumeration of {@link java.net.URL {@code URL}} objects
98       * representing all the resources with the given name by searching the
99       * instance for any resources first, falling back to the classloader
100      * classloading is delegated to.
101      *
102      * @param  name the resource name
103      *
104      * @return  an enumeration of {@link java.net.URL {@code URL}} objects for
105      * the resources.
106      *
107      * @throws  IOException if searching fails.
108      */
109     protected Enumeration findResources( final String name )
110         throws IOException
111     {
112         Enumeration thisResources = null;
113 
114         if ( this.locations.containsKey( name ) )
115         {
116             final Set resources = ( Set ) this.locations.get( name );
117             thisResources = Collections.enumeration( resources );
118         }
119         else if ( thisResources == null || !thisResources.hasMoreElements() )
120         {
121             thisResources = this.delegate.getResources( name );
122         }
123 
124         return thisResources;
125     }
126 
127     /**
128      * Adds a resource to the instance.
129      *
130      * @param name the name of the resource to add.
131      * @param resource the resource to add to the instance.
132      *
133      * @throws NullPointerException if {@code name} or {@code resource} is
134      * {@code null}.
135      */
136     public void addResource( final String name, final URL resource )
137     {
138         if ( name == null )
139         {
140             throw new NullPointerException( "name " );
141         }
142         if ( resource == null )
143         {
144             throw new NullPointerException( "resource" );
145         }
146 
147         Set resources = ( Set ) this.locations.get( name );
148         if ( resources == null )
149         {
150             resources = new HashSet();
151             this.locations.put( name, resources );
152         }
153 
154         resources.add( resource );
155     }
156 
157     /**
158      * Adds an array of resources to the instance.
159      *
160      * @param name the name of the resources to add.
161      * @param resources the resources to add to the instance.
162      *
163      * @throws NullPointerException if {@code name} or {@code resources} is
164      * {@code null}.
165      */
166     public void addResources( final String name, final URL[] resources )
167     {
168         if ( name == null )
169         {
170             throw new NullPointerException( "name" );
171         }
172 
173         if ( resources == null )
174         {
175             throw new NullPointerException( "resources" );
176         }
177 
178         for ( int i = resources.length - 1; i >= 0; i-- )
179         {
180             this.addResource( name, resources[i] );
181         }
182     }
183 
184 }