View Javadoc
1   package org.codehaus.plexus.classworlds.strategy;
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.net.URL;
21  import java.util.Enumeration;
22  
23  import org.codehaus.plexus.classworlds.realm.ClassRealm;
24  
25  public class OsgiBundleStrategy extends AbstractStrategy {
26  
27      // java.* from parent
28      // imported packages [Import-Package header with explicit constraints on the exporter]
29      // requires bundle [Required-Bundle]
30      // self [Bundle-Classpath header]
31      // attached fragments
32      //
33      // We need to trya and be OSGi r4 compliant in the loading of all the bundles so that we can try to
34      // load eclipse without requiring equinox. Or any other OSGi container for that matter.
35      public OsgiBundleStrategy(ClassRealm realm) {
36          super(realm);
37      }
38  
39      public Class<?> loadClass(String name) throws ClassNotFoundException {
40          Class<?> clazz = realm.loadClassFromImport(name);
41  
42          if (clazz == null) {
43              clazz = realm.loadClassFromSelf(name);
44  
45              if (clazz == null) {
46                  clazz = realm.loadClassFromParent(name);
47  
48                  if (clazz == null) {
49                      throw new ClassNotFoundException(name);
50                  }
51              }
52          }
53  
54          return clazz;
55      }
56  
57      public URL getResource(String name) {
58          URL resource = realm.loadResourceFromImport(name);
59  
60          if (resource == null) {
61              resource = realm.loadResourceFromSelf(name);
62  
63              if (resource == null) {
64                  resource = realm.loadResourceFromParent(name);
65              }
66          }
67  
68          return resource;
69      }
70  
71      public Enumeration<URL> getResources(String name) throws IOException {
72          Enumeration<URL> imports = realm.loadResourcesFromImport(name);
73          Enumeration<URL> self = realm.loadResourcesFromSelf(name);
74          Enumeration<URL> parent = realm.loadResourcesFromParent(name);
75  
76          return combineResources(imports, self, parent);
77      }
78  }