View Javadoc
1   package org.codehaus.plexus.classworlds.strategy;
2   
3   /*
4    * Copyright 2001-2006 Codehaus Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
7    * in compliance with the License. You may obtain a copy of the License at
8    *
9    * http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software distributed under the License
12   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13   * or implied. See the License for the specific language governing permissions and limitations under
14   * the License.
15   */
16  
17  import java.io.IOException;
18  import java.net.URL;
19  import java.util.Enumeration;
20  
21  import org.codehaus.plexus.classworlds.realm.ClassRealm;
22  
23  /**
24   * @author Jason van Zyl
25   */
26  public class SelfFirstStrategy extends AbstractStrategy {
27  
28      public SelfFirstStrategy(ClassRealm realm) {
29          super(realm);
30      }
31  
32      public Class<?> loadClass(String name) throws ClassNotFoundException {
33          Class<?> clazz = realm.loadClassFromImport(name);
34  
35          if (clazz == null) {
36              clazz = realm.loadClassFromSelf(name);
37  
38              if (clazz == null) {
39                  clazz = realm.loadClassFromParent(name);
40  
41                  if (clazz == null) {
42                      throw new ClassNotFoundException(name);
43                  }
44              }
45          }
46  
47          return clazz;
48      }
49  
50      public URL getResource(String name) {
51          URL resource = realm.loadResourceFromImport(name);
52  
53          if (resource == null) {
54              resource = realm.loadResourceFromSelf(name);
55  
56              if (resource == null) {
57                  resource = realm.loadResourceFromParent(name);
58              }
59          }
60  
61          return resource;
62      }
63  
64      public Enumeration<URL> getResources(String name) throws IOException {
65          Enumeration<URL> imports = realm.loadResourcesFromImport(name);
66          Enumeration<URL> self = realm.loadResourcesFromSelf(name);
67          Enumeration<URL> parent = realm.loadResourcesFromParent(name);
68  
69          return combineResources(imports, self, parent);
70      }
71  }