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