KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ungoverned > moduleloader > search > selection > SimpleSelectionPolicy


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.selection;
37
38 import java.util.Iterator JavaDoc;
39 import java.util.Map JavaDoc;
40 import java.util.HashMap JavaDoc;
41
42 import org.ungoverned.moduleloader.Module;
43 import org.ungoverned.moduleloader.ModuleEvent;
44 import org.ungoverned.moduleloader.ModuleListener;
45 import org.ungoverned.moduleloader.search.ImportSearchPolicy;
46 import org.ungoverned.moduleloader.search.SelectionPolicy;
47 import org.ungoverned.moduleloader.search.CompatibilityPolicy;
48
49 /**
50  * This class implements a reasonably simple selection policy for the
51  * <tt>ImportSearchPolicy</tt>. When given a choice, this selection
52  * policy will always select the newest version of the available
53  * candidates to satisfy the import identifier. In the case where
54  * a candidate has already been selected for a given import identifier,
55  * then the previously selected module will be returned, if possible.
56  * If it is not possible to return the previously selected module, then
57  * a <tt>null</tt> is returned. This policy assumes that classes are
58  * shared globally.
59 **/

60 public class SimpleSelectionPolicy implements SelectionPolicy, ModuleListener
61 {
62     private Map JavaDoc m_resolvedPackageMap = new HashMap JavaDoc();
63     private Map JavaDoc m_resolvedModuleMap = new HashMap JavaDoc();
64
65     /**
66      * Selects a single module to resolve the specified import identifier
67      * from the array of compatible candidate modules. If the import
68      * identifier has not been resolved before, then this selection policy
69      * chooses the module that exports the newest version of the
70      * import identifer. If the import identifier has been resolved already,
71      * then the same module that was chosen before is chosen again.
72      * This ensures that all modules use the same version of all
73      * exported classes.
74      * @param module the module that is importing the target.
75      * @param identifier the identifier of the import target.
76      * @param version the version number of the import target.
77      * @param candidates array of compatible candidate modules from which to choose.
78      * @return the selected module or <tt>null</tt> if no module
79      * can be selected.
80     **/

81     public synchronized Module select(Module module, Object JavaDoc identifier,
82         Object JavaDoc version, Module[] candidates, CompatibilityPolicy compatPolicy)
83     {
84         // See if package is already resolved.
85
Module selModule = (Module) m_resolvedPackageMap.get(identifier);
86
87         // If no module was previously selected to export the package,
88
// then try to choose one now.
89
if (selModule == null)
90         {
91             Object JavaDoc selVersion = null;
92
93             // Examine all exported instances of the identifier and
94
// choose the one with the newest version number. If
95
// there is more than one source for the newest version,
96
// then just select the first one found.
97
for (int i = 0; i < candidates.length; i++)
98             {
99                 Object JavaDoc tmpVersion =
100                     ImportSearchPolicy.getExportVersion(candidates[i], identifier);
101
102                 // If this is the first comparison, then
103
// just record it.
104
if (selVersion == null)
105                 {
106                     selModule = candidates[i];
107                     selVersion = tmpVersion;
108                 }
109                 // If the current export package version is greater
110
// than the selected export package version, then
111
// record it instead.
112
else if (compatPolicy.compare(identifier, tmpVersion, identifier, selVersion) >= 0)
113                 {
114                     selModule = candidates[i];
115                     selVersion = tmpVersion;
116                 }
117             }
118
119             m_resolvedPackageMap.put(identifier, selModule);
120             m_resolvedModuleMap.put(selModule, selModule);
121         }
122         // See if the previously selected export module satisfies
123
// the current request, otherwise return null.
124
else
125         {
126             Object JavaDoc selVersion =
127                 ImportSearchPolicy.getExportVersion(selModule, identifier);
128             Module tmpModule = selModule;
129             selModule = null;
130             if (compatPolicy.isCompatible(identifier, selVersion, identifier, version))
131             {
132                 selModule = tmpModule;
133             }
134         }
135
136         return selModule;
137     }
138
139     public void moduleAdded(ModuleEvent event)
140     {
141     }
142
143     public void moduleReset(ModuleEvent event)
144     {
145         moduleRemoved(event);
146     }
147
148     public synchronized void moduleRemoved(ModuleEvent event)
149     {
150         // If the module that was removed was chosen for
151
// exporting identifier, then flush it from our
152
// data structures; we assume here that the application
153
// will flush references to the removed module's classes.
154
if (m_resolvedModuleMap.get(event.getModule()) != null)
155         {
156             // Remove from module map.
157
m_resolvedModuleMap.remove(event.getModule());
158             // Remove each exported package from package map.
159
Iterator JavaDoc iter = m_resolvedPackageMap.entrySet().iterator();
160             while (iter.hasNext())
161             {
162                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iter.next();
163                 if (entry.getValue() == event.getModule())
164                 {
165                     iter.remove();
166                 }
167             }
168         }
169     }
170 }
Popular Tags