View Javadoc
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          ClassRealmAdapter adapter = new ClassRealmAdapter( newRealm );
37  
38          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      {
45          this.realm = newRealm;
46      }
47  
48      public String getId()
49      {
50          return realm.getId();
51      }
52  
53      public ClassWorld getWorld()
54      {
55          return ClassWorldAdapter.getInstance( realm.getWorld() );
56      }
57  
58      public void importFrom( String realmId,
59                              String pkgName )
60          throws NoSuchRealmException
61      {
62          try
63          {
64              realm.importFrom( realmId, pkgName );
65          }
66          catch ( org.codehaus.plexus.classworlds.realm.NoSuchRealmException e )
67          {
68              throw new NoSuchRealmException( getWorld(), e.getId() );
69          }
70      }
71  
72      public void addConstituent( URL constituent )
73      {
74          realm.addURL( constituent );
75      }
76  
77      public ClassRealm locateSourceRealm( String className )
78      {
79          ClassLoader importLoader = realm.getImportClassLoader( className );
80  
81          if ( importLoader instanceof org.codehaus.plexus.classworlds.realm.ClassRealm )
82          {
83              return ClassRealmAdapter.getInstance( (org.codehaus.plexus.classworlds.realm.ClassRealm) importLoader );
84          }
85          else
86          {
87              return null;
88          }
89      }
90  
91      public void setParent( ClassRealm classRealm )
92      {
93          if ( classRealm != null )
94          {
95              realm.setParentRealm( ClassRealmReverseAdapter.getInstance( classRealm ) );
96          }
97      }
98  
99      public ClassRealm createChildRealm( String id )
100         throws DuplicateRealmException
101     {
102         try
103         {
104             return ClassRealmAdapter.getInstance( realm.createChildRealm( id ) );
105         }
106         catch ( org.codehaus.plexus.classworlds.realm.DuplicateRealmException e )
107         {
108             throw new DuplicateRealmException( getWorld(), e.getId() );
109         }
110     }
111 
112     public ClassLoader getClassLoader()
113     {
114         return realm;
115     }
116 
117     public ClassRealm getParent()
118     {
119         return ClassRealmAdapter.getInstance( realm.getParentRealm() );
120     }
121 
122     public ClassRealm getParentRealm()
123     {
124         return ClassRealmAdapter.getInstance( realm.getParentRealm() );
125     }
126 
127     public URL[] getConstituents()
128     {
129         return realm.getURLs();
130     }
131 
132     public Class loadClass( String name )
133         throws ClassNotFoundException
134     {
135         return realm.loadClass( name );
136     }
137 
138     public URL getResource( String name )
139     {
140         return realm.getResource( trimLeadingSlash( name ) );
141     }
142 
143     public Enumeration findResources( String name )
144         throws IOException
145     {
146         return realm.findResources( trimLeadingSlash( name ) );
147     }
148 
149     public InputStream getResourceAsStream( String name )
150     {
151         return realm.getResourceAsStream( trimLeadingSlash( name ) );
152     }
153 
154     public void display()
155     {
156         realm.display();
157     }
158 
159     public boolean equals(Object o)
160     {
161         if ( !( o instanceof ClassRealm ) )
162             return false;
163 
164         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         if ( resource != null && resource.startsWith( "/" ) )
173         {
174             return resource.substring( 1 );
175         }
176         else
177         {
178             return resource;
179         }
180     }
181 
182 }