KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ungoverned > moduleloader > search > ExhaustiveSearchPolicy


1 /*
2  * ModuleLoader - A generic, policy-driven class loader.
3  * Copyright (c) 2004, Richard S. Hall
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  * * Neither the name of the ungoverned.org nor the names of its
17  * contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * Contact: Richard S. Hall (heavy@ungoverned.org)
33  * Contributor(s):
34  *
35 **/

36 package org.ungoverned.moduleloader.search;
37
38 import java.net.URL JavaDoc;
39
40 import org.ungoverned.moduleloader.Module;
41 import org.ungoverned.moduleloader.ModuleManager;
42 import org.ungoverned.moduleloader.SearchPolicy;
43
44 /**
45  * <p>
46  * This class implements a <tt>ModuleLoader</tt> search policy that
47  * exhaustively and linearly searches all modules when trying to load
48  * a particular class or resource. As a result of this algorithm, every class loader
49  * for every module is essentially identical, meaning that each will
50  * load a given class or resource from the same class loader. This search policy
51  * provides behavior similar to the standard <tt>CLASSPATH</tt> environment
52  * variable approach. The main difference is that modules can be added
53  * to the module manager at run time; thus, the class path is dynamically
54  * extended. This search policy is not fully dynamic, since it does not
55  * support the removal of modules at run time; if a module is removed from
56  * the module manager at run time, there is no attempt to clean up its
57  * loaded classes.
58  * </p>
59  * @see org.ungoverned.moduleloader.SearchPolicy
60  * @see org.ungoverned.moduleloader.Module
61  * @see org.ungoverned.moduleloader.ModuleClassLoader
62  * @see org.ungoverned.moduleloader.ModuleManager
63 **/

64 public class ExhaustiveSearchPolicy implements SearchPolicy
65 {
66     private ModuleManager m_mgr = null;
67
68     /**
69      * This method is part of the <tt>SearchPolicy</tt> interface.
70      * This method is called by the <tt>ModuleManager</tt> once to
71      * give the search policy instance a reference to its associated
72      * module manager. This method should be implemented such that
73      * it cannot be called twice; calling this method a second time
74      * should produce an illegal state exception.
75      * @param mgr the module manager associated with this search policy.
76      * @throws java.lang.IllegalStateException if the method is called
77      * more than once.
78     **/

79     public void setModuleManager(ModuleManager mgr)
80         throws IllegalStateException JavaDoc
81     {
82         if (m_mgr == null)
83         {
84             m_mgr = mgr;
85         }
86         else
87         {
88             throw new IllegalStateException JavaDoc("Module manager is already initialized");
89         }
90     }
91
92     /**
93      * This method finds the specified class for the specified module. It
94      * finds the class by linearly asking each module in the module manager
95      * for the specific class. As soon as the class is found, it is returned.
96      * @param module the target module that is loading the class.
97      * @param name the name of the class being loaded.
98      * @return the class if found, <tt>null</tt> otherwise.
99     **/

100     public Class JavaDoc findClass(Module module, String JavaDoc name)
101     {
102         Module[] modules = m_mgr.getModules();
103         for (int i = 0; i < modules.length; i++)
104         {
105             try {
106                 Class JavaDoc clazz = modules[i].getClassLoader().searchForClass(name);
107                 if (clazz != null)
108                 {
109                     return clazz;
110                 }
111             } catch (Throwable JavaDoc th) {
112             }
113         }
114
115         return null;
116     }
117
118     /**
119      * This method finds the specified resource for the specified module. It
120      * finds the resource by linearly asking each module in the module manager
121      * for specific resource. As soon as the resource is found, a <tt>URL</tt>
122      * to it is returned.
123      * @param module the target module that is loading the resource.
124      * @param name the name of the resource being loaded.
125      * @return a <tt>URL</tt> to the resource if found, <tt>null</tt> otherwise.
126     **/

127     public URL JavaDoc findResource(Module module, String JavaDoc name)
128     {
129         Module[] modules = m_mgr.getModules();
130         for (int i = 0; i < modules.length; i++)
131         {
132             try {
133                 URL JavaDoc url = modules[i].getClassLoader().searchForResource(name);
134                 if (url != null)
135                 {
136                     return url;
137                 }
138             } catch (Throwable JavaDoc th) {
139             }
140         }
141
142         return null;
143     }
144 }
Popular Tags