1 36 package org.ungoverned.moduleloader.search.selection; 37 38 import java.util.Iterator ; 39 import java.util.Map ; 40 import java.util.HashMap ; 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 60 public class SimpleSelectionPolicy implements SelectionPolicy, ModuleListener 61 { 62 private Map m_resolvedPackageMap = new HashMap (); 63 private Map m_resolvedModuleMap = new HashMap (); 64 65 81 public synchronized Module select(Module module, Object identifier, 82 Object version, Module[] candidates, CompatibilityPolicy compatPolicy) 83 { 84 Module selModule = (Module) m_resolvedPackageMap.get(identifier); 86 87 if (selModule == null) 90 { 91 Object selVersion = null; 92 93 for (int i = 0; i < candidates.length; i++) 98 { 99 Object tmpVersion = 100 ImportSearchPolicy.getExportVersion(candidates[i], identifier); 101 102 if (selVersion == null) 105 { 106 selModule = candidates[i]; 107 selVersion = tmpVersion; 108 } 109 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 else 125 { 126 Object 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 (m_resolvedModuleMap.get(event.getModule()) != null) 155 { 156 m_resolvedModuleMap.remove(event.getModule()); 158 Iterator iter = m_resolvedPackageMap.entrySet().iterator(); 160 while (iter.hasNext()) 161 { 162 Map.Entry entry = (Map.Entry ) iter.next(); 163 if (entry.getValue() == event.getModule()) 164 { 165 iter.remove(); 166 } 167 } 168 } 169 } 170 } | Popular Tags |