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
27      extends AbstractStrategy
28  {
29  
30      public SelfFirstStrategy( ClassRealm realm )
31      {
32          super( realm );
33      }
34  
35      public Class<?> loadClass( String name )
36          throws ClassNotFoundException
37      {
38          Class<?> clazz = realm.loadClassFromImport( name );
39  
40          if ( clazz == null )
41          {
42              clazz = realm.loadClassFromSelf( name );
43  
44              if ( clazz == null )
45              {
46                  clazz = realm.loadClassFromParent( name );
47  
48                  if ( clazz == null )
49                  {
50                      throw new ClassNotFoundException( name );
51                  }
52              }
53          }
54  
55          return clazz;
56      }
57  
58      public URL getResource( String name )
59      {
60          URL resource = realm.loadResourceFromImport( name );
61  
62          if ( resource == null )
63          {
64              resource = realm.loadResourceFromSelf( name );
65  
66              if ( resource == null )
67              {
68                  resource = realm.loadResourceFromParent( name );
69              }
70          }
71  
72          return resource;
73      }
74  
75      public Enumeration<URL> getResources( String name )
76          throws IOException
77      {
78          Enumeration<URL> imports = realm.loadResourcesFromImport( name );
79          Enumeration<URL> self = realm.loadResourcesFromSelf( name );
80          Enumeration<URL> parent = realm.loadResourcesFromParent( name );
81  
82          return combineResources( imports, self, parent );
83      }
84  
85  }