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 | * assumes that all modules are self-contained. In other words, when |
| 27 | * loading a class or resource for a particular module, only that |
| 28 | * particular module's resource sources are search. No classes or |
| 29 | * resources are shared among modules. |
| 30 | * </p> |
Richard S. Hall | 5a03159 | 2005-08-19 19:53:58 +0000 | [diff] [blame] | 31 | * @see org.apache.felix.moduleloader.SearchPolicy |
| 32 | * @see org.apache.felix.moduleloader.Module |
| 33 | * @see org.apache.felix.moduleloader.ModuleClassLoader |
| 34 | * @see org.apache.felix.moduleloader.ModuleManager |
Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 35 | **/ |
| 36 | public class SelfContainedSearchPolicy implements SearchPolicy |
| 37 | { |
| 38 | private ModuleManager m_mgr = null; |
| 39 | |
| 40 | /** |
| 41 | * This method is part of the <tt>SearchPolicy</tt> interface. |
| 42 | * This method is called by the <tt>ModuleManager</tt> once to |
| 43 | * give the search policy instance a reference to its associated |
| 44 | * module manager. This method should be implemented such that |
| 45 | * it cannot be called twice; calling this method a second time |
| 46 | * should produce an illegal state exception. |
| 47 | * @param mgr the module manager associated with this search policy. |
| 48 | * @throws java.lang.IllegalStateException if the method is called |
| 49 | * more than once. |
| 50 | **/ |
| 51 | public void setModuleManager(ModuleManager mgr) |
| 52 | throws IllegalStateException |
| 53 | { |
| 54 | if (m_mgr == null) |
| 55 | { |
| 56 | m_mgr = mgr; |
| 57 | } |
| 58 | else |
| 59 | { |
| 60 | throw new IllegalStateException("Module manager is already initialized"); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | public Object[] definePackage(Module module, String pkgName) |
| 65 | { |
| 66 | return null; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Simply returns <tt>null</tt> which forces the module class |
| 71 | * loader to only search the target module's resource sources |
| 72 | * for the specified class. |
| 73 | * @param parent the parent class loader of the delegating class loader. |
| 74 | * @param module the target module that is loading the class. |
| 75 | * @param name the name of the class being loaded. |
| 76 | * @return <tt>null</tt>. |
| 77 | **/ |
| 78 | public Class findClassBeforeModule(ClassLoader parent, Module module, String name) |
| 79 | { |
| 80 | // First, try to load from parent. |
| 81 | if (parent != null) |
| 82 | { |
| 83 | try |
| 84 | { |
| 85 | Class c = parent.loadClass(name); |
| 86 | if (c != null) |
| 87 | { |
| 88 | return c; |
| 89 | } |
| 90 | } |
| 91 | catch (ClassNotFoundException ex) |
| 92 | { |
| 93 | // Ignore. |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | return null; |
| 98 | } |
| 99 | |
| 100 | public Class findClassAfterModule(ClassLoader parent, Module module, String name) |
| 101 | { |
| 102 | return null; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Simply returns <tt>null</tt> which forces the module class |
| 107 | * loader to only search the target module's resource sources |
| 108 | * for the specified resource. |
| 109 | * @param parent the parent class loader of the delegating class loader. |
| 110 | * @param module the target module that is loading the class. |
| 111 | * @param name the name of the resource being loaded. |
| 112 | * @return <tt>null</tt>. |
| 113 | **/ |
| 114 | public URL findResource(ClassLoader parent, Module module, String name) |
| 115 | { |
| 116 | if (parent != null) |
| 117 | { |
| 118 | return parent.getResource(name); |
| 119 | } |
| 120 | return null; |
| 121 | } |
| 122 | } |