1 11 package org.eclipse.core.internal.runtime; 12 13 import java.util.*; 14 import org.eclipse.core.runtime.IAdapterFactory; 15 import org.eclipse.core.runtime.IAdapterManager; 16 17 36 public final class AdapterManager implements IAdapterManager { 37 44 private Map adapterLookup; 45 46 54 private Map classLookup; 55 56 59 private final Object classLookupLock = new Object (); 60 61 67 private Map classSearchOrderLookup; 68 69 74 private final HashMap factories; 75 76 private final ArrayList lazyFactoryProviders; 77 78 private static final AdapterManager singleton = new AdapterManager(); 79 80 public static AdapterManager getDefault() { 81 return singleton; 82 } 83 84 87 private AdapterManager() { 88 factories = new HashMap(5); 89 lazyFactoryProviders = new ArrayList(1); 90 } 91 92 97 private void addFactoriesFor(String typeName, Map table) { 98 List factoryList = (List) getFactories().get(typeName); 99 if (factoryList == null) 100 return; 101 for (int i = 0, imax = factoryList.size(); i < imax; i++) { 102 IAdapterFactory factory = (IAdapterFactory) factoryList.get(i); 103 if (factory instanceof IAdapterFactoryExt) { 104 String [] adapters = ((IAdapterFactoryExt) factory).getAdapterNames(); 105 for (int j = 0; j < adapters.length; j++) { 106 if (table.get(adapters[j]) == null) 107 table.put(adapters[j], factory); 108 } 109 } else { 110 Class [] adapters = factory.getAdapterList(); 111 for (int j = 0; j < adapters.length; j++) { 112 String adapterName = adapters[j].getName(); 113 if (table.get(adapterName) == null) 114 table.put(adapterName, factory); 115 } 116 } 117 } 118 } 119 120 private void cacheClassLookup(IAdapterFactory factory, Class clazz) { 121 synchronized (classLookupLock) { 122 Map lookup = classLookup; 124 if (lookup == null) 125 classLookup = lookup = new HashMap(4); 126 HashMap classes = (HashMap) lookup.get(factory); 127 if (classes == null) { 128 classes = new HashMap(4); 129 lookup.put(factory, classes); 130 } 131 classes.put(clazz.getName(), clazz); 132 } 133 } 134 135 private Class cachedClassForName(IAdapterFactory factory, String typeName) { 136 synchronized (classLookupLock) { 137 Class clazz = null; 138 Map lookup = classLookup; 140 if (lookup != null) { 141 HashMap classes = (HashMap) lookup.get(factory); 142 if (classes != null) { 143 clazz = (Class ) classes.get(typeName); 144 } 145 } 146 return clazz; 147 } 148 } 149 150 155 private Class classForName(IAdapterFactory factory, String typeName) { 156 Class clazz = cachedClassForName(factory, typeName); 157 if (clazz == null) { 158 try { 159 if (factory instanceof IAdapterFactoryExt) 160 factory = ((IAdapterFactoryExt) factory).loadFactory(false); 161 if (factory != null) { 162 clazz = factory.getClass().getClassLoader().loadClass(typeName); 163 cacheClassLookup(factory, clazz); 164 } 165 } catch (ClassNotFoundException e) { 166 } 168 } 169 return clazz; 170 } 171 172 175 public String [] computeAdapterTypes(Class adaptable) { 176 Set types = getFactories(adaptable).keySet(); 177 return (String []) types.toArray(new String [types.size()]); 178 } 179 180 186 private Map getFactories(Class adaptable) { 187 Map lookup = adapterLookup; 189 if (lookup == null) 190 adapterLookup = lookup = Collections.synchronizedMap(new HashMap(30)); 191 Map table = (Map) lookup.get(adaptable.getName()); 192 if (table == null) { 193 table = new HashMap(4); 195 Class [] classes = computeClassOrder(adaptable); 196 for (int i = 0; i < classes.length; i++) 197 addFactoriesFor(classes[i].getName(), table); 198 lookup.put(adaptable.getName(), table); 200 } 201 return table; 202 } 203 204 public Class [] computeClassOrder(Class adaptable) { 205 Class [] classes = null; 206 Map lookup = classSearchOrderLookup; 208 if (lookup == null) 209 classSearchOrderLookup = lookup = Collections.synchronizedMap(new HashMap()); 210 else 211 classes = (Class []) lookup.get(adaptable); 212 if (classes == null) { 214 ArrayList classList = new ArrayList(); 215 computeClassOrder(adaptable, classList); 216 classes = (Class []) classList.toArray(new Class [classList.size()]); 217 lookup.put(adaptable, classes); 218 } 219 return classes; 220 } 221 222 232 private void computeClassOrder(Class adaptable, Collection classes) { 233 Class clazz = adaptable; 234 Set seen = new HashSet(4); 235 while (clazz != null) { 236 classes.add(clazz); 237 computeInterfaceOrder(clazz.getInterfaces(), classes, seen); 238 clazz = clazz.getSuperclass(); 239 } 240 } 241 242 private void computeInterfaceOrder(Class [] interfaces, Collection classes, Set seen) { 243 List newInterfaces = new ArrayList(interfaces.length); 244 for (int i = 0; i < interfaces.length; i++) { 245 Class interfac = interfaces[i]; 246 if (seen.add(interfac)) { 247 classes.add(interfac); 249 newInterfaces.add(interfac); 250 } 251 } 252 for (Iterator it = newInterfaces.iterator(); it.hasNext();) 253 computeInterfaceOrder(((Class ) it.next()).getInterfaces(), classes, seen); 254 } 255 256 264 public synchronized void flushLookup() { 265 adapterLookup = null; 266 classLookup = null; 267 classSearchOrderLookup = null; 268 } 269 270 273 public Object getAdapter(Object adaptable, Class adapterType) { 274 IAdapterFactory factory = (IAdapterFactory) getFactories(adaptable.getClass()).get(adapterType.getName()); 275 Object result = null; 276 if (factory != null) 277 result = factory.getAdapter(adaptable, adapterType); 278 if (result == null && adapterType.isInstance(adaptable)) 279 return adaptable; 280 return result; 281 } 282 283 286 public Object getAdapter(Object adaptable, String adapterType) { 287 return getAdapter(adaptable, adapterType, false); 288 } 289 290 298 private Object getAdapter(Object adaptable, String adapterType, boolean force) { 299 IAdapterFactory factory = (IAdapterFactory) getFactories(adaptable.getClass()).get(adapterType); 300 if (force && factory instanceof IAdapterFactoryExt) 301 factory = ((IAdapterFactoryExt) factory).loadFactory(true); 302 Object result = null; 303 if (factory != null) { 304 Class clazz = classForName(factory, adapterType); 305 if (clazz != null) 306 result = factory.getAdapter(adaptable, clazz); 307 } 308 if (result == null && adaptable.getClass().getName().equals(adapterType)) 309 return adaptable; 310 return result; 311 } 312 313 public boolean hasAdapter(Object adaptable, String adapterTypeName) { 314 return getFactories(adaptable.getClass()).get(adapterTypeName) != null; 315 } 316 317 320 public int queryAdapter(Object adaptable, String adapterTypeName) { 321 IAdapterFactory factory = (IAdapterFactory) getFactories(adaptable.getClass()).get(adapterTypeName); 322 if (factory == null) 323 return NONE; 324 if (factory instanceof IAdapterFactoryExt) { 325 factory = ((IAdapterFactoryExt) factory).loadFactory(false); if (factory == null) 327 return NOT_LOADED; 328 } 329 return LOADED; 330 } 331 332 335 public Object loadAdapter(Object adaptable, String adapterTypeName) { 336 return getAdapter(adaptable, adapterTypeName, true); 337 } 338 339 342 public synchronized void registerAdapters(IAdapterFactory factory, Class adaptable) { 343 registerFactory(factory, adaptable.getName()); 344 flushLookup(); 345 } 346 347 350 public void registerFactory(IAdapterFactory factory, String adaptableType) { 351 List list = (List) factories.get(adaptableType); 352 if (list == null) { 353 list = new ArrayList(5); 354 factories.put(adaptableType, list); 355 } 356 list.add(factory); 357 } 358 359 362 public synchronized void unregisterAdapters(IAdapterFactory factory) { 363 for (Iterator it = factories.values().iterator(); it.hasNext();) 364 ((List) it.next()).remove(factory); 365 flushLookup(); 366 } 367 368 371 public synchronized void unregisterAdapters(IAdapterFactory factory, Class adaptable) { 372 List factoryList = (List) factories.get(adaptable.getName()); 373 if (factoryList == null) 374 return; 375 factoryList.remove(factory); 376 flushLookup(); 377 } 378 379 384 public synchronized void unregisterAllAdapters() { 385 factories.clear(); 386 flushLookup(); 387 } 388 389 public void registerLazyFactoryProvider(IAdapterManagerProvider factoryProvider) { 390 synchronized (lazyFactoryProviders) { 391 lazyFactoryProviders.add(factoryProvider); 392 } 393 } 394 395 public boolean unregisterLazyFactoryProvider(IAdapterManagerProvider factoryProvider) { 396 synchronized (lazyFactoryProviders) { 397 return lazyFactoryProviders.remove(factoryProvider); 398 } 399 } 400 401 public HashMap getFactories() { 402 if (lazyFactoryProviders.size() == 0) 404 return factories; 405 synchronized (lazyFactoryProviders) { 406 while (lazyFactoryProviders.size() > 0) { 407 IAdapterManagerProvider provider = (IAdapterManagerProvider) lazyFactoryProviders.remove(0); 408 if (provider.addFactories(this)) 409 flushLookup(); 410 } 411 } 412 return factories; 413 } 414 } 415 | Popular Tags |