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.io.IOException;
20  import java.io.InputStream;
21  import java.net.URL;
22  import java.util.Enumeration;
23  
24  /**
25   * Adapter that wraps a modern {@link org.codehaus.plexus.classworlds.realm.ClassRealm} and exposes
26   * it as the legacy {@link ClassRealm} interface.
27   *
28   * <p>This class is referenced directly by the compiled bytecode of
29   * {@code org.eclipse.sisu:org.eclipse.sisu.plexus} and must remain on the classpath for
30   * Maven 3+ to function. Do not remove or rename it. New code should not use this adapter;
31   * use {@link org.codehaus.plexus.classworlds.realm.ClassRealm} directly.</p>
32   *
33   * @author Andrew Williams
34   * @deprecated Legacy adapter retained for Sisu binary compatibility.
35   */
36  @SuppressWarnings({"UnnecessaryLocalVariable", "DeprecatedIsStillUsed", "rawtypes"})
37  @Deprecated
38  public class ClassRealmAdapter implements ClassRealm {
39  
40      public static ClassRealmAdapter getInstance(org.codehaus.plexus.classworlds.realm.ClassRealm newRealm) {
41          ClassRealmAdapter adapter = new ClassRealmAdapter(newRealm);
42  
43          return adapter;
44      }
45  
46      private final org.codehaus.plexus.classworlds.realm.ClassRealm realm;
47  
48      private ClassRealmAdapter(org.codehaus.plexus.classworlds.realm.ClassRealm newRealm) {
49          this.realm = newRealm;
50      }
51  
52      public String getId() {
53          return realm.getId();
54      }
55  
56      public ClassWorld getWorld() {
57          return ClassWorldAdapter.getInstance(realm.getWorld());
58      }
59  
60      public void importFrom(String realmId, String pkgName) throws NoSuchRealmException {
61          try {
62              realm.importFrom(realmId, pkgName);
63          } catch (org.codehaus.plexus.classworlds.realm.NoSuchRealmException e) {
64              throw new NoSuchRealmException(getWorld(), e.getId());
65          }
66      }
67  
68      public void addConstituent(URL constituent) {
69          realm.addURL(constituent);
70      }
71  
72      public ClassRealm locateSourceRealm(String className) {
73          ClassLoader importLoader = realm.getImportClassLoader(className);
74  
75          if (importLoader instanceof org.codehaus.plexus.classworlds.realm.ClassRealm) {
76              return ClassRealmAdapter.getInstance((org.codehaus.plexus.classworlds.realm.ClassRealm) importLoader);
77          } else {
78              return null;
79          }
80      }
81  
82      public void setParent(ClassRealm classRealm) {
83          if (classRealm != null) {
84              realm.setParentRealm(ClassRealmReverseAdapter.getInstance(classRealm));
85          }
86      }
87  
88      public ClassRealm createChildRealm(String id) throws DuplicateRealmException {
89          try {
90              return ClassRealmAdapter.getInstance(realm.createChildRealm(id));
91          } catch (org.codehaus.plexus.classworlds.realm.DuplicateRealmException e) {
92              throw new DuplicateRealmException(getWorld(), e.getId());
93          }
94      }
95  
96      public ClassLoader getClassLoader() {
97          return realm;
98      }
99  
100     public ClassRealm getParent() {
101         return ClassRealmAdapter.getInstance(realm.getParentRealm());
102     }
103 
104     public ClassRealm getParentRealm() {
105         return ClassRealmAdapter.getInstance(realm.getParentRealm());
106     }
107 
108     public URL[] getConstituents() {
109         return realm.getURLs();
110     }
111 
112     public Class loadClass(String name) throws ClassNotFoundException {
113         return realm.loadClass(name);
114     }
115 
116     public URL getResource(String name) {
117         return realm.getResource(trimLeadingSlash(name));
118     }
119 
120     public Enumeration findResources(String name) throws IOException {
121         return realm.findResources(trimLeadingSlash(name));
122     }
123 
124     public InputStream getResourceAsStream(String name) {
125         return realm.getResourceAsStream(trimLeadingSlash(name));
126     }
127 
128     public void display() {
129         realm.display();
130     }
131 
132     public boolean equals(Object o) {
133         if (!(o instanceof ClassRealm)) return false;
134 
135         return getId().equals(((ClassRealm) o).getId());
136     }
137 
138     /**
139      * Provides backward-compat with the old classworlds which accepted resource names with leading slashes.
140      */
141     private String trimLeadingSlash(String resource) {
142         if (resource != null && resource.startsWith("/")) {
143             return resource.substring(1);
144         } else {
145             return resource;
146         }
147     }
148 }