1 15 package org.apache.hivemind.lib.util; 16 17 import java.util.HashMap ; 18 import java.util.Iterator ; 19 import java.util.LinkedList ; 20 import java.util.Map ; 21 import java.util.WeakHashMap ; 22 23 import org.apache.hivemind.service.ClassFabUtils; 24 import org.apache.hivemind.util.Defense; 25 26 32 33 public class StrategyRegistryImpl implements StrategyRegistry 34 { 35 38 39 private Map _registrations = new HashMap (); 40 41 44 45 private Map _cache = new WeakHashMap (); 46 47 public synchronized void register(Class registrationClass, Object adaptor) 48 { 49 Defense.notNull(registrationClass, "registrationClass"); 50 Defense.notNull(adaptor, "adaptor"); 51 52 if (_registrations.containsKey(registrationClass)) 53 throw new IllegalArgumentException (UtilMessages 54 .duplicateRegistration(registrationClass)); 55 56 _registrations.put(registrationClass, adaptor); 57 58 62 _cache.clear(); 63 } 64 65 public synchronized Object getStrategy(Class subjectClass) 66 { 67 Defense.notNull(subjectClass, "subjectClass"); 68 69 Object result = _cache.get(subjectClass); 70 71 if (result != null) 72 return result; 73 74 result = searchForAdaptor(subjectClass); 75 76 78 _cache.put(subjectClass, result); 79 80 return result; 81 } 82 83 106 107 private Object searchForAdaptor(Class subjectClass) 108 { 109 LinkedList queue = null; 110 Object result = null; 111 112 114 Class searchClass = subjectClass; 115 116 119 while (searchClass != Object .class && searchClass != null) 120 { 121 result = _registrations.get(searchClass); 122 if (result != null) 123 return result; 124 125 128 Class [] interfaces = searchClass.getInterfaces(); 129 int length = interfaces.length; 130 131 if (queue == null && length > 0) 132 queue = new LinkedList (); 133 134 for (int i = 0; i < length; i++) 135 queue.addLast(interfaces[i]); 136 137 139 searchClass = getSuperclass(searchClass); 140 141 } 142 143 146 if (queue != null) 147 { 148 while (!queue.isEmpty()) 149 { 150 searchClass = (Class ) queue.removeFirst(); 151 152 result = _registrations.get(searchClass); 153 if (result != null) 154 return result; 155 156 159 Class [] interfaces = searchClass.getInterfaces(); 160 int length = interfaces.length; 161 162 for (int i = 0; i < length; i++) 163 queue.addLast(interfaces[i]); 164 } 165 } 166 167 170 result = _registrations.get(Object .class); 171 if (result != null) 172 return result; 173 174 176 throw new IllegalArgumentException (UtilMessages.strategyNotFound(subjectClass)); 177 } 178 179 186 187 private Class getSuperclass(Class searchClass) 188 { 189 if (searchClass.isArray()) 190 { 191 Class componentType = searchClass.getComponentType(); 192 193 if (!componentType.isPrimitive() && componentType != Object .class) 194 return Object [].class; 195 } 196 197 return searchClass.getSuperclass(); 198 } 199 200 public synchronized String toString() 201 { 202 StringBuffer buffer = new StringBuffer (); 203 buffer.append("AdaptorRegistry["); 204 205 Iterator i = _registrations.entrySet().iterator(); 206 boolean showSep = false; 207 208 while (i.hasNext()) 209 { 210 if (showSep) 211 buffer.append(' '); 212 213 Map.Entry entry = (Map.Entry ) i.next(); 214 215 Class registeredClass = (Class ) entry.getKey(); 216 217 buffer.append(ClassFabUtils.getJavaClassName(registeredClass)); 218 buffer.append("="); 219 buffer.append(entry.getValue()); 220 221 showSep = true; 222 } 223 224 buffer.append("]"); 225 226 return buffer.toString(); 227 } 228 } | Popular Tags |