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