Coverage Report - org.codehaus.classworlds.ClassRealmAdapter
 
Classes in this File Line Coverage Branch Coverage Complexity
ClassRealmAdapter
25 %
10/40
20 %
2/10
1,65
 
 1  
 package org.codehaus.classworlds;
 2  
 
 3  
 /*
 4  
  * Copyright 2001-2010 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.net.URL;
 20  
 import java.util.Enumeration;
 21  
 import java.io.IOException;
 22  
 import java.io.InputStream;
 23  
 
 24  
 /**
 25  
  * An adapter for ClassRealms
 26  
  *
 27  
  * @author Andrew Williams
 28  
  */
 29  
 @Deprecated
 30  
 public class ClassRealmAdapter
 31  
     implements ClassRealm
 32  
 {
 33  
 
 34  
     public static ClassRealmAdapter getInstance( org.codehaus.plexus.classworlds.realm.ClassRealm newRealm )
 35  
     {
 36  1
         ClassRealmAdapter adapter = new ClassRealmAdapter( newRealm );
 37  
 
 38  1
         return adapter;
 39  
     }
 40  
 
 41  
     private org.codehaus.plexus.classworlds.realm.ClassRealm realm;
 42  
 
 43  
     private ClassRealmAdapter( org.codehaus.plexus.classworlds.realm.ClassRealm newRealm )
 44  1
     {
 45  1
         this.realm = newRealm;
 46  1
     }
 47  
 
 48  
     public String getId()
 49  
     {
 50  0
         return realm.getId();
 51  
     }
 52  
 
 53  
     public ClassWorld getWorld()
 54  
     {
 55  0
         return ClassWorldAdapter.getInstance( realm.getWorld() );
 56  
     }
 57  
 
 58  
     public void importFrom( String realmId,
 59  
                             String pkgName )
 60  
         throws NoSuchRealmException
 61  
     {
 62  
         try
 63  
         {
 64  0
             realm.importFrom( realmId, pkgName );
 65  
         }
 66  0
         catch ( org.codehaus.plexus.classworlds.realm.NoSuchRealmException e )
 67  
         {
 68  0
             throw new NoSuchRealmException( getWorld(), e.getId() );
 69  0
         }
 70  0
     }
 71  
 
 72  
     public void addConstituent( URL constituent )
 73  
     {
 74  0
         realm.addURL( constituent );
 75  0
     }
 76  
 
 77  
     public ClassRealm locateSourceRealm( String className )
 78  
     {
 79  0
         ClassLoader importLoader = realm.getImportClassLoader( className );
 80  
 
 81  0
         if ( importLoader instanceof org.codehaus.plexus.classworlds.realm.ClassRealm )
 82  
         {
 83  0
             return ClassRealmAdapter.getInstance( (org.codehaus.plexus.classworlds.realm.ClassRealm) importLoader );
 84  
         }
 85  
         else
 86  
         {
 87  0
             return null;
 88  
         }
 89  
     }
 90  
 
 91  
     public void setParent( ClassRealm classRealm )
 92  
     {
 93  0
         if ( classRealm != null )
 94  
         {
 95  0
             realm.setParentRealm( ClassRealmReverseAdapter.getInstance( classRealm ) );
 96  
         }
 97  0
     }
 98  
 
 99  
     public ClassRealm createChildRealm( String id )
 100  
         throws DuplicateRealmException
 101  
     {
 102  
         try
 103  
         {
 104  0
             return ClassRealmAdapter.getInstance( realm.createChildRealm( id ) );
 105  
         }
 106  0
         catch ( org.codehaus.plexus.classworlds.realm.DuplicateRealmException e )
 107  
         {
 108  0
             throw new DuplicateRealmException( getWorld(), e.getId() );
 109  
         }
 110  
     }
 111  
 
 112  
     public ClassLoader getClassLoader()
 113  
     {
 114  0
         return realm;
 115  
     }
 116  
 
 117  
     public ClassRealm getParent()
 118  
     {
 119  0
         return ClassRealmAdapter.getInstance( realm.getParentRealm() );
 120  
     }
 121  
 
 122  
     public ClassRealm getParentRealm()
 123  
     {
 124  0
         return ClassRealmAdapter.getInstance( realm.getParentRealm() );
 125  
     }
 126  
 
 127  
     public URL[] getConstituents()
 128  
     {
 129  0
         return realm.getURLs();
 130  
     }
 131  
 
 132  
     public Class loadClass( String name )
 133  
         throws ClassNotFoundException
 134  
     {
 135  0
         return realm.loadClass( name );
 136  
     }
 137  
 
 138  
     public URL getResource( String name )
 139  
     {
 140  1
         return realm.getResource( trimLeadingSlash( name ) );
 141  
     }
 142  
 
 143  
     public Enumeration findResources( String name )
 144  
         throws IOException
 145  
     {
 146  1
         return realm.findResources( trimLeadingSlash( name ) );
 147  
     }
 148  
 
 149  
     public InputStream getResourceAsStream( String name )
 150  
     {
 151  1
         return realm.getResourceAsStream( trimLeadingSlash( name ) );
 152  
     }
 153  
 
 154  
     public void display()
 155  
     {
 156  0
         realm.display();
 157  0
     }
 158  
 
 159  
     public boolean equals(Object o)
 160  
     {
 161  0
         if ( !( o instanceof ClassRealm ) )
 162  0
             return false;
 163  
 
 164  0
         return getId().equals( ( (ClassRealm) o ).getId() );
 165  
     }
 166  
 
 167  
     /**
 168  
      * Provides backward-compat with the old classworlds which accepted resource names with leading slashes.
 169  
      */
 170  
     private String trimLeadingSlash( String resource )
 171  
     {
 172  3
         if ( resource != null && resource.startsWith( "/" ) )
 173  
         {
 174  3
             return resource.substring( 1 );
 175  
         }
 176  
         else
 177  
         {
 178  0
             return resource;
 179  
         }
 180  
     }
 181  
 
 182  
 }