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