View Javadoc

1   package org.codehaus.plexus.classworlds;
2   
3   /*
4    * Copyright 2001-2006 Codehaus Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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   * A collection of <code>ClassRealm</code>s, indexed by id.
34   *
35   * @author <a href="mailto:bob@eng.werken.com">bob mcwhirter</a>
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              // Will never happen as we are just creating the world.
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             //noinspection ConstantConditions
110             if ( realm instanceof Closeable )
111             {
112                 //noinspection RedundantCast
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     // from exports branch
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         // TODO ideally, use object identity, not equals
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 }