View Javadoc

1   /*
2    *  jDTAUS Core Container Mojo
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.mojo;
22  
23  import java.io.IOException;
24  import java.net.URL;
25  import java.net.URLClassLoader;
26  import java.util.Collections;
27  import java.util.Enumeration;
28  import java.util.HashMap;
29  import java.util.HashSet;
30  import java.util.Map;
31  import java.util.Set;
32  
33  /**
34   * Classloader for providing classpath resources.
35   *
36   * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
37   * @version $JDTAUS: ResourceLoader.java 8743 2012-10-07 03:06:20Z schulte $
38   */
39  final class ResourceLoader extends URLClassLoader
40  {
41  
42      /** The classloader delegated to. */
43      private ClassLoader delegate;
44  
45      /** Mapping of classpath locations to a list of URLs to provide. */
46      private Map locations = new HashMap();
47  
48      /**
49       * Creates a new {@code ResourceLoader} instance taking a classloader
50       * classloading is delegated to.
51       *
52       * @param delegate the classloader to delegate classloading to.
53       */
54      ResourceLoader( final ClassLoader delegate )
55      {
56          super( new URL[]
57              {
58              } );
59  
60          this.delegate = delegate;
61      }
62  
63      /**
64       * Creates a new {@code ResourceLoader} instance taking an array of
65       * classpath URLs to back the instance and a classloader classloading is
66       * delegated to.
67       *
68       * @param classpath array of classpath URLs to back the instance.
69       * @param delegate the classloader to delegate classloading to.
70       */
71      ResourceLoader( final URL[] classpath, final ClassLoader delegate )
72      {
73          super( classpath );
74          this.delegate = delegate;
75      }
76  
77      protected Class findClass( final String name ) throws ClassNotFoundException
78      {
79          Class thisClass = null;
80  
81          try
82          {
83              thisClass = Class.forName( name, true, this.delegate );
84          }
85          catch ( final ClassNotFoundException e )
86          {
87              thisClass = super.findClass( name );
88          }
89  
90          return thisClass;
91      }
92  
93      public URL findResource( final String name )
94      {
95          URL thisResource = null;
96  
97          if ( this.locations.containsKey( name ) )
98          {
99              final Set resources = (Set) this.locations.get( name );
100             thisResource = (URL) resources.iterator().next();
101         }
102         else if ( ( thisResource = super.findResource( name ) ) == null )
103         {
104             thisResource = this.delegate.getResource( name );
105         }
106 
107         return thisResource;
108     }
109 
110     public Enumeration findResources( final String name ) 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
120         {
121             thisResources = super.findResources( name );
122             if ( thisResources == null || !thisResources.hasMoreElements() )
123             {
124                 thisResources = this.delegate.getResources( name );
125             }
126         }
127 
128         return thisResources;
129     }
130 
131     /**
132      * Adds a resource to the instance.
133      *
134      * @param location the location of the resource to add.
135      * @param resource the resource to add.
136      */
137     public void addResource( final String location, final URL resource )
138     {
139         if ( location == null )
140         {
141             throw new NullPointerException( "location" );
142         }
143         if ( resource == null )
144         {
145             throw new NullPointerException( "resource" );
146         }
147 
148         Set resources = (Set) this.locations.get( location );
149         if ( resources == null )
150         {
151             resources = new HashSet();
152             this.locations.put( location, resources );
153         }
154 
155         resources.add( resource );
156     }
157 
158 }