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  /**
20   * A compatibility wrapper for org.codehaus.plexus.classworlds.realm.ClassRealm
21   * provided for legacy code
22   *
23   * @author Andrew Williams
24   * @version $Id$
25   */
26  
27  import java.io.File;
28  import java.io.IOException;
29  import java.io.InputStream;
30  import java.net.URL;
31  import java.util.Enumeration;
32  
33  @Deprecated
34  public class DefaultClassRealm
35      implements ClassRealm
36  {
37      private ClassRealmAdapter adapter;
38  
39      public DefaultClassRealm( ClassWorld world, String id )
40      {
41          this( world, id, null );
42      }
43  
44      public DefaultClassRealm( ClassWorld world, String id, ClassLoader foreignClassLoader )
45      {
46          this.adapter = ClassRealmAdapter.getInstance(
47              new org.codehaus.plexus.classworlds.realm.ClassRealm(
48                  ClassWorldReverseAdapter.getInstance( world ), id, foreignClassLoader ) );
49      }
50  
51      public URL[] getConstituents()
52      {
53          return adapter.getConstituents();
54      }
55  
56      public ClassRealm getParent()
57      {
58          return adapter.getParentRealm();
59      }
60  
61      public void setParent( ClassRealm parent )
62      {
63          adapter.setParent( parent );
64      }
65  
66      public String getId()
67      {
68          return adapter.getId();
69      }
70  
71      public ClassWorld getWorld()
72      {
73          return adapter.getWorld();
74      }
75  
76      public void importFrom( String realmId, String packageName )
77          throws NoSuchRealmException
78      {
79          adapter.importFrom( realmId, packageName );
80      }
81  
82      public void addConstituent( URL constituent )
83      {
84          adapter.addConstituent( constituent );
85      }
86  
87      /**
88       *  Adds a byte[] class definition as a constituent for locating classes.
89       *  Currently uses BytesURLStreamHandler to hold a reference of the byte[] in memory.
90       *  This ensures we have a unifed URL resource model for all constituents.
91       *  The code to cache to disk is commented out - maybe a property to choose which method?
92       *
93       *  @param constituent class name
94       *  @param b the class definition as a byte[]
95       */
96      public void addConstituent(String constituent,
97                                 byte[] b) throws ClassNotFoundException
98      {
99          try
100         {
101             File path, file;
102             if (constituent.lastIndexOf('.') != -1)
103             {
104                 path = new File("byteclass/" + constituent.substring(0, constituent.lastIndexOf('.') + 1).replace('.', File.separatorChar));
105 
106                 file = new File(path, constituent.substring(constituent.lastIndexOf('.') + 1) + ".class");
107             }
108             else
109             {
110                 path = new File("byteclass/");
111 
112                 file = new File(path, constituent + ".class");
113             }
114 
115             addConstituent( new URL( null,
116                                      file.toURI().toURL().toExternalForm(),
117                                      new BytesURLStreamHandler(b) ) );
118         }
119         catch (java.io.IOException e)
120         {
121             throw new ClassNotFoundException( "Couldn't load byte stream.", e );
122         }
123     }
124 
125     public ClassRealm locateSourceRealm( String classname )
126     {
127         return adapter.locateSourceRealm( classname );
128     }
129 
130     public ClassLoader getClassLoader()
131     {
132         return adapter.getClassLoader();
133     }
134 
135     public ClassRealm createChildRealm( String id )
136         throws DuplicateRealmException
137     {
138         return adapter.createChildRealm( id );
139     }
140 
141     // ----------------------------------------------------------------------
142     // ClassLoader API
143     // ----------------------------------------------------------------------
144 
145     public Class loadClass( String name )
146         throws ClassNotFoundException
147     {
148         return adapter.loadClass( name );
149     }
150 
151     public URL getResource( String name )
152     {
153         return adapter.getResource( name );
154     }
155 
156     public InputStream getResourceAsStream( String name )
157     {
158         return adapter.getResourceAsStream( name );
159     }
160 
161     public Enumeration findResources(String name)
162         throws IOException
163 	{
164 		return adapter.findResources( name );
165 	}
166 
167     public void display()
168     {
169         adapter.display();
170     }
171 }