Coverage Report - org.codehaus.plexus.ClassRealmUtil
 
Classes in this File Line Coverage Branch Coverage Complexity
ClassRealmUtil
92 %
23/25
90 %
18/20
11
 
 1  
 package org.codehaus.plexus;
 2  
 
 3  
 /*
 4  
  * Copyright 2001-2009 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.util.Collection;
 20  
 import java.util.Iterator;
 21  
 import java.util.LinkedHashSet;
 22  
 import java.util.LinkedList;
 23  
 import java.util.Queue;
 24  
 import java.util.Set;
 25  
 
 26  
 import org.codehaus.plexus.classworlds.ClassWorld;
 27  
 import org.codehaus.plexus.classworlds.realm.ClassRealm;
 28  
 
 29  0
 public class ClassRealmUtil
 30  
 {
 31  
 
 32  
     public static Set<ClassRealm> getContextRealms( ClassWorld world )
 33  
     {
 34  383057
         Set<ClassRealm> realms = new LinkedHashSet<ClassRealm>();
 35  
 
 36  769686
         for ( ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); classLoader != null; classLoader =
 37  
             classLoader.getParent() )
 38  
         {
 39  386069
             if ( classLoader instanceof ClassRealm )
 40  
             {
 41  380951
                 realms.add( (ClassRealm) classLoader );
 42  
 
 43  380634
                 Queue<ClassRealm> queue = new LinkedList<ClassRealm>();
 44  380665
                 queue.add( (ClassRealm) classLoader );
 45  
 
 46  1139045
                 while ( !queue.isEmpty() )
 47  
                 {
 48  760582
                     ClassRealm realm = queue.remove();
 49  
 
 50  760126
                     Collection<ClassRealm> importRealms = realm.getImportRealms();
 51  760249
                     for ( ClassRealm importRealm : importRealms )
 52  
                     {
 53  14
                         if ( realms.add( importRealm ) )
 54  
                         {
 55  14
                             queue.add( importRealm );
 56  
                         }
 57  14
                     }
 58  
 
 59  760600
                     ClassRealm parentRealm = realm.getParentRealm();
 60  760590
                     if ( parentRealm != null && realms.add( parentRealm ) )
 61  
                     {
 62  381075
                         queue.add( parentRealm );
 63  
                     }
 64  760561
                 }
 65  
             }
 66  
         }
 67  
 
 68  383638
         if ( world != null )
 69  
         {
 70  127853
             for ( Iterator<ClassRealm> it = realms.iterator(); it.hasNext(); )
 71  
             {
 72  253329
                 ClassRealm realm = it.next();
 73  253329
                 if ( realm.getWorld() != world )
 74  
                 {
 75  0
                     it.remove();
 76  
                 }
 77  253329
             }
 78  
         }
 79  
 
 80  383354
         return realms;
 81  
     }
 82  
 
 83  
 }