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.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
34
35
36
37
38
39
40
41 public class ResourceLoader extends ClassLoader
42 {
43
44
45 private ClassLoader delegate;
46
47
48 private Map locations = new HashMap();
49
50
51
52
53
54
55
56 public ResourceLoader( final ClassLoader delegate )
57 {
58 super();
59 this.delegate = delegate;
60 }
61
62
63 protected Class findClass( final String name )
64 throws ClassNotFoundException
65 {
66 return this.delegate.loadClass( name );
67 }
68
69
70
71
72
73
74
75
76
77
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
98
99
100
101
102
103
104
105
106
107
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
129
130
131
132
133
134
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
159
160
161
162
163
164
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 }