Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2005 The Apache Software Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | * |
| 16 | */ |
Richard S. Hall | 5a03159 | 2005-08-19 19:53:58 +0000 | [diff] [blame^] | 17 | package org.apache.felix.moduleloader.search; |
Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 18 | |
| 19 | import java.net.URL; |
| 20 | |
Richard S. Hall | 5a03159 | 2005-08-19 19:53:58 +0000 | [diff] [blame^] | 21 | import org.apache.felix.moduleloader.*; |
Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 22 | |
| 23 | /** |
| 24 | * <p> |
| 25 | * This class implements a <tt>ModuleLoader</tt> search policy that |
| 26 | * exhaustively and linearly searches all modules when trying to load |
| 27 | * a particular class or resource. As a result of this algorithm, every class loader |
| 28 | * for every module is essentially identical, meaning that each will |
| 29 | * load a given class or resource from the same class loader. This search policy |
| 30 | * provides behavior similar to the standard <tt>CLASSPATH</tt> environment |
| 31 | * variable approach. The main difference is that modules can be added |
| 32 | * to the module manager at run time; thus, the class path is dynamically |
| 33 | * extended. This search policy is not fully dynamic, since it does not |
| 34 | * support the removal of modules at run time; if a module is removed from |
| 35 | * the module manager at run time, there is no attempt to clean up its |
| 36 | * loaded classes. |
| 37 | * </p> |
Richard S. Hall | 5a03159 | 2005-08-19 19:53:58 +0000 | [diff] [blame^] | 38 | * @see org.apache.felix.moduleloader.SearchPolicy |
| 39 | * @see org.apache.felix.moduleloader.Module |
| 40 | * @see org.apache.felix.moduleloader.ModuleClassLoader |
| 41 | * @see org.apache.felix.moduleloader.ModuleManager |
Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 42 | **/ |
| 43 | public class ExhaustiveSearchPolicy implements SearchPolicy |
| 44 | { |
| 45 | private ModuleManager m_mgr = null; |
| 46 | |
| 47 | /** |
| 48 | * This method is part of the <tt>SearchPolicy</tt> interface. |
| 49 | * This method is called by the <tt>ModuleManager</tt> once to |
| 50 | * give the search policy instance a reference to its associated |
| 51 | * module manager. This method should be implemented such that |
| 52 | * it cannot be called twice; calling this method a second time |
| 53 | * should produce an illegal state exception. |
| 54 | * @param mgr the module manager associated with this search policy. |
| 55 | * @throws java.lang.IllegalStateException if the method is called |
| 56 | * more than once. |
| 57 | **/ |
| 58 | public void setModuleManager(ModuleManager mgr) |
| 59 | throws IllegalStateException |
| 60 | { |
| 61 | if (m_mgr == null) |
| 62 | { |
| 63 | m_mgr = mgr; |
| 64 | } |
| 65 | else |
| 66 | { |
| 67 | throw new IllegalStateException("Module manager is already initialized"); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | public Object[] definePackage(Module module, String pkgName) |
| 72 | { |
| 73 | return null; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * This method finds the specified class for the specified module. It |
| 78 | * finds the class by linearly asking each module in the module manager |
| 79 | * for the specific class. As soon as the class is found, it is returned. |
| 80 | * @param parent the parent class loader of the delegating class loader. |
| 81 | * @param module the target module that is loading the class. |
| 82 | * @param name the name of the class being loaded. |
| 83 | * @return the class if found, <tt>null</tt> otherwise. |
| 84 | **/ |
| 85 | public Class findClassBeforeModule(ClassLoader parent, Module module, String name) |
| 86 | { |
| 87 | // First, try to load from parent. |
| 88 | if (parent != null) |
| 89 | { |
| 90 | try |
| 91 | { |
| 92 | Class c = parent.loadClass(name); |
| 93 | if (c != null) |
| 94 | { |
| 95 | return c; |
| 96 | } |
| 97 | } |
| 98 | catch (ClassNotFoundException ex) |
| 99 | { |
| 100 | // Ignore and search modules. |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | Module[] modules = m_mgr.getModules(); |
| 105 | for (int i = 0; i < modules.length; i++) |
| 106 | { |
| 107 | try { |
| 108 | Class clazz = modules[i].getClassLoader().loadClassFromModule(name); |
| 109 | if (clazz != null) |
| 110 | { |
| 111 | return clazz; |
| 112 | } |
| 113 | } catch (Throwable th) { |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | return null; |
| 118 | } |
| 119 | |
| 120 | public Class findClassAfterModule(ClassLoader parent, Module module, String name) |
| 121 | { |
| 122 | return null; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * This method finds the specified resource for the specified module. It |
| 127 | * finds the resource by linearly asking each module in the module manager |
| 128 | * for specific resource. As soon as the resource is found, a <tt>URL</tt> |
| 129 | * to it is returned. |
| 130 | * @param parent the parent class loader of the delegating class loader. |
| 131 | * @param module the target module that is loading the resource. |
| 132 | * @param name the name of the resource being loaded. |
| 133 | * @return a <tt>URL</tt> to the resource if found, <tt>null</tt> otherwise. |
| 134 | **/ |
| 135 | public URL findResource(ClassLoader parent, Module module, String name) |
| 136 | { |
| 137 | // First, try to load from parent. |
| 138 | if (parent != null) |
| 139 | { |
| 140 | URL url = parent.getResource(name); |
| 141 | if (url != null) |
| 142 | { |
| 143 | return url; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | Module[] modules = m_mgr.getModules(); |
| 148 | for (int i = 0; i < modules.length; i++) |
| 149 | { |
| 150 | try { |
| 151 | URL url = modules[i].getClassLoader().getResourceFromModule(name); |
| 152 | if (url != null) |
| 153 | { |
| 154 | return url; |
| 155 | } |
| 156 | } catch (Throwable th) { |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | return null; |
| 161 | } |
| 162 | } |