1 package org.codehaus.plexus.classworlds;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import java.io.Closeable;
20 import java.io.IOException;
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.Collections;
24 import java.util.LinkedHashMap;
25 import java.util.List;
26 import java.util.Map;
27
28 import org.codehaus.plexus.classworlds.realm.ClassRealm;
29 import org.codehaus.plexus.classworlds.realm.DuplicateRealmException;
30 import org.codehaus.plexus.classworlds.realm.NoSuchRealmException;
31
32
33
34
35
36
37 public class ClassWorld
38 {
39 private Map<String, ClassRealm> realms;
40
41 private final List<ClassWorldListener> listeners = new ArrayList<ClassWorldListener>();
42
43 public ClassWorld( String realmId, ClassLoader classLoader )
44 {
45 this();
46
47 try
48 {
49 newRealm( realmId, classLoader );
50 }
51 catch ( DuplicateRealmException e )
52 {
53
54 }
55 }
56
57 public ClassWorld()
58 {
59 this.realms = new LinkedHashMap<String, ClassRealm>();
60 }
61
62 public ClassRealm newRealm( String id )
63 throws DuplicateRealmException
64 {
65 return newRealm( id, getClass().getClassLoader() );
66 }
67
68 public synchronized ClassRealm newRealm( String id, ClassLoader classLoader )
69 throws DuplicateRealmException
70 {
71 if ( realms.containsKey( id ) )
72 {
73 throw new DuplicateRealmException( this, id );
74 }
75
76 ClassRealm realm;
77
78 realm = new ClassRealm( this, id, classLoader );
79
80 realms.put( id, realm );
81
82 for ( ClassWorldListener listener : listeners )
83 {
84 listener.realmCreated( realm );
85 }
86
87 return realm;
88 }
89
90 public synchronized void disposeRealm( String id )
91 throws NoSuchRealmException
92 {
93 ClassRealm realm = (ClassRealm) realms.remove( id );
94
95 if ( realm != null )
96 {
97 closeIfJava7( realm );
98 for ( ClassWorldListener listener : listeners )
99 {
100 listener.realmDisposed( realm );
101 }
102 }
103 }
104
105 private void closeIfJava7( ClassRealm realm )
106 {
107 try
108 {
109
110 if ( realm instanceof Closeable )
111 {
112
113 ( (Closeable) realm ).close();
114 }
115 }
116 catch ( IOException ignore )
117 {
118 }
119 }
120
121 public synchronized ClassRealm getRealm( String id )
122 throws NoSuchRealmException
123 {
124 if ( realms.containsKey( id ) )
125 {
126 return (ClassRealm) realms.get( id );
127 }
128
129 throw new NoSuchRealmException( this, id );
130 }
131
132 public synchronized Collection<ClassRealm> getRealms()
133 {
134 return Collections.unmodifiableList( new ArrayList<ClassRealm>( realms.values() ) );
135 }
136
137
138 public synchronized ClassRealm getClassRealm( String id )
139 {
140 if ( realms.containsKey( id ) )
141 {
142 return realms.get( id );
143 }
144
145 return null;
146 }
147
148 public synchronized void addListener( ClassWorldListener listener )
149 {
150
151 if ( !listeners.contains( listener ) )
152 {
153 listeners.add( listener );
154 }
155 }
156
157 public synchronized void removeListener( ClassWorldListener listener )
158 {
159 listeners.remove( listener );
160 }
161 }