Coverage Report - org.codehaus.plexus.classworlds.ClassWorld
 
Classes in this File Line Coverage Branch Coverage Complexity
ClassWorld
35 %
15/42
31 %
5/16
2,273
 
 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  277
     private final List<ClassWorldListener> listeners = new ArrayList<ClassWorldListener>();
 42  
 
 43  
     public ClassWorld( String realmId, ClassLoader classLoader )
 44  
     {
 45  0
         this();
 46  
 
 47  
         try
 48  
         {
 49  0
             newRealm( realmId, classLoader );
 50  
         }
 51  0
         catch ( DuplicateRealmException e )
 52  
         {
 53  
             // Will never happen as we are just creating the world.
 54  0
         }
 55  0
     }
 56  
 
 57  
     public ClassWorld()
 58  277
     {
 59  277
         this.realms = new LinkedHashMap<String, ClassRealm>();
 60  277
     }
 61  
 
 62  
     public ClassRealm newRealm( String id )
 63  
         throws DuplicateRealmException
 64  
     {
 65  58
         return newRealm( id, getClass().getClassLoader() );
 66  
     }
 67  
 
 68  
     public synchronized ClassRealm newRealm( String id, ClassLoader classLoader )
 69  
         throws DuplicateRealmException
 70  
     {
 71  94
         if ( realms.containsKey( id ) )
 72  
         {
 73  2
             throw new DuplicateRealmException( this, id );
 74  
         }
 75  
 
 76  
         ClassRealm realm;
 77  
 
 78  92
         realm = new ClassRealm( this, id, classLoader );
 79  
 
 80  92
         realms.put( id, realm );
 81  
 
 82  92
         for ( ClassWorldListener listener : listeners )
 83  
         {
 84  0
             listener.realmCreated( realm );
 85  0
         }
 86  
 
 87  92
         return realm;
 88  
     }
 89  
 
 90  
     public synchronized void disposeRealm( String id )
 91  
         throws NoSuchRealmException
 92  
     {
 93  0
         ClassRealm realm = (ClassRealm) realms.remove( id );
 94  
 
 95  0
         if ( realm != null )
 96  
         {
 97  0
             closeIfJava7( realm );
 98  0
             for ( ClassWorldListener listener : listeners )
 99  
             {
 100  0
                 listener.realmDisposed( realm );
 101  0
             }
 102  
         }
 103  0
     }
 104  
 
 105  
     private void closeIfJava7( ClassRealm realm )
 106  
     {
 107  
         try
 108  
         {
 109  
             //noinspection ConstantConditions
 110  0
             if ( realm instanceof Closeable )
 111  
             {
 112  
                 //noinspection RedundantCast
 113  0
                 ( (Closeable) realm ).close();
 114  
             }
 115  
         }
 116  0
         catch ( IOException ignore )
 117  
         {
 118  0
         }
 119  0
     }
 120  
 
 121  
     public synchronized ClassRealm getRealm( String id )
 122  
         throws NoSuchRealmException
 123  
     {
 124  64
         if ( realms.containsKey( id ) )
 125  
         {
 126  63
             return (ClassRealm) realms.get( id );
 127  
         }
 128  
 
 129  1
         throw new NoSuchRealmException( this, id );
 130  
     }
 131  
 
 132  
     public synchronized Collection<ClassRealm> getRealms()
 133  
     {
 134  9
         return Collections.unmodifiableList( new ArrayList<ClassRealm>( realms.values() ) );
 135  
     }
 136  
 
 137  
     // from exports branch
 138  
     public synchronized ClassRealm getClassRealm( String id )
 139  
     {
 140  0
         if ( realms.containsKey( id ) )
 141  
         {
 142  0
             return realms.get( id );
 143  
         }
 144  
 
 145  0
         return null;
 146  
     }
 147  
 
 148  
     public synchronized void addListener( ClassWorldListener listener )
 149  
     {
 150  
         // TODO ideally, use object identity, not equals
 151  0
         if ( !listeners.contains( listener ) )
 152  
         {
 153  0
             listeners.add( listener );
 154  
         }
 155  0
     }
 156  
 
 157  
     public synchronized void removeListener( ClassWorldListener listener )
 158  
     {
 159  0
         listeners.remove( listener );
 160  0
     }
 161  
 }