27
27
import java .util .ArrayList ;
28
28
import java .util .Enumeration ;
29
29
import java .util .HashMap ;
30
+ import java .util .HashSet ;
30
31
import java .util .List ;
31
32
import java .util .Map ;
32
33
import java .util .NoSuchElementException ;
34
+ import java .util .Set ;
33
35
34
36
import org .osgi .framework .Bundle ;
35
37
41
43
* @author Tim Ward
42
44
*/
43
45
public class OsgiClassLoader extends ClassLoader {
46
+ // Leave these as Sets -- addClassLoader or addBundle may be called more
47
+ // than once if a SF or EMF is closed and re-created.
48
+ private Set <ClassLoader > classLoaders = new HashSet <ClassLoader >();
49
+ private Set <Bundle > bundles = new HashSet <Bundle >();
44
50
45
- private List <ClassLoader > classLoaders = new ArrayList <ClassLoader >();
46
-
47
- private List <Bundle > bundles = new ArrayList <Bundle >();
48
-
49
51
private Map <String , Class <?>> classCache = new HashMap <String , Class <?>>();
50
52
51
53
private Map <String , URL > resourceCache = new HashMap <String , URL >();
52
-
53
- private Map <String , Enumeration <URL >> resourceListCache = new HashMap <String , Enumeration <URL >>();
54
54
55
55
/**
56
56
* Load the class and break on first found match.
@@ -132,16 +132,15 @@ protected URL findResource(String name) {
132
132
133
133
/**
134
134
* Load the class and break on first found match.
135
+ *
136
+ * Note: Since they're Enumerations, do not cache these results!
137
+ *
135
138
* TODO: Should this throw a different exception or warn if multiple
136
139
* classes were found? Naming collisions can and do happen in OSGi...
137
140
*/
138
141
@ SuppressWarnings ("unchecked" )
139
142
@ Override
140
143
protected Enumeration <URL > findResources (String name ) {
141
- if ( resourceListCache .containsKey ( name ) ) {
142
- return resourceListCache .get ( name );
143
- }
144
-
145
144
final List <Enumeration <URL >> enumerations = new ArrayList <Enumeration <URL >>();
146
145
147
146
for ( Bundle bundle : bundles ) {
@@ -188,8 +187,6 @@ public URL nextElement() {
188
187
}
189
188
};
190
189
191
- resourceListCache .put ( name , aggEnumeration );
192
-
193
190
return aggEnumeration ;
194
191
}
195
192
@@ -204,7 +201,6 @@ public void addBundle( Bundle bundle ) {
204
201
public void clear () {
205
202
classCache .clear ();
206
203
resourceCache .clear ();
207
- resourceListCache .clear ();
208
204
}
209
205
210
206
}
0 commit comments