1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
35
36
37
38
39 final class ResourceLoader extends URLClassLoader
40 {
41
42
43 private ClassLoader delegate;
44
45
46 private Map locations = new HashMap();
47
48
49
50
51
52
53
54 ResourceLoader( final ClassLoader delegate )
55 {
56 super( new URL[]
57 {
58 } );
59
60 this.delegate = delegate;
61 }
62
63
64
65
66
67
68
69
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
133
134
135
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 }