1 11 package org.eclipse.ui.internal.components.framework; 12 13 import java.util.ArrayList ; 14 import java.util.Collection ; 15 import java.util.HashMap ; 16 import java.util.HashSet ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 import java.util.Map ; 20 21 import org.eclipse.ui.internal.components.Assert; 22 import org.eclipse.ui.internal.components.DependencyOnlyFactory; 23 import org.eclipse.ui.internal.components.util.InstanceToComponentFactoryAdapter; 24 import org.eclipse.ui.internal.components.util.InstanceToServiceFactoryAdapter; 25 import org.eclipse.ui.internal.components.util.ServiceProviderToServiceFactoryAdapter; 26 27 73 public final class FactoryMap extends ServiceFactory { 74 75 78 private Map adapters = null; 79 80 private List exclusions = null; 81 82 85 private List parentContexts = null; 86 87 90 private List registeredContainers = null; 91 92 95 public FactoryMap() { 96 } 97 98 109 public FactoryMap mapExclusion(Object interface_) { 110 Assert.isNotNull(interface_); 111 if (exclusions == null) { 112 exclusions = new ArrayList (); 113 } 114 exclusions.add(interface_); 115 internalAddMapping(interface_, null); 116 return this; 117 } 118 119 129 public FactoryMap map(Object interface_, ComponentFactory adapter) { 130 Assert.isNotNull(adapter); 131 Assert.isNotNull(interface_); 132 133 internalAddMapping(interface_, adapter); 134 return this; 135 } 136 137 147 public FactoryMap map(Object interfaceType, ServiceFactory context) { 148 Assert.isNotNull(interfaceType); 149 Assert.isNotNull(context); 150 151 internalAddMapping(interfaceType, context); 152 153 add(new DependencyOnlyFactory(context)); 155 return this; 156 } 157 158 169 public FactoryMap mapInstance(Object interfaceType, Object component) { 170 Assert.isNotNull(interfaceType); 171 Assert.isNotNull(component); 172 173 if (interfaceType instanceof Class ) { 174 Class c = (Class )interfaceType; 175 176 Assert.isTrue(c.isInstance(component)); 177 } 178 179 return map(interfaceType, new InstanceToComponentFactoryAdapter(component)); 180 } 181 182 public FactoryMap addInstance(Object instance) { 183 return add(new InstanceToServiceFactoryAdapter(instance)); 184 } 185 186 196 public FactoryMap add(ServiceFactory context) { 197 Assert.isNotNull(context); 198 internalAddInstance(context); 199 200 return this; 201 } 202 203 public FactoryMap add(IServiceProvider toAdd) { 204 Assert.isNotNull(toAdd); 205 internalAddInstance(new ServiceProviderToServiceFactoryAdapter(toAdd)); 206 207 return this; 208 } 209 210 213 ComponentHandle getInstance(Object type, IServiceProvider availableDependencies) throws ComponentException { 214 if (parentContexts == null) { 215 return null; 216 } 217 218 for (Iterator iter = parentContexts.iterator(); iter.hasNext();) { 219 ServiceFactory context = (ServiceFactory) iter.next(); 220 221 ComponentHandle result = context.createHandle(type, availableDependencies); 222 if (result != null) { 223 return result; 224 } 225 } 226 return null; 227 } 228 229 232 public ComponentHandle createHandle(Object key, IServiceProvider availableDependencies) throws ComponentException { 233 if (adapters != null) { 234 if (adapters.containsKey(key)) { 235 Object target = adapters.get(key); 236 if (target == null) { 237 return null; 238 } 239 240 if (target instanceof ComponentFactory) { 241 ComponentFactory factory = (ComponentFactory)target; 242 return factory.createHandle(availableDependencies); 243 } else if (target instanceof ServiceFactory) { 244 ServiceFactory targetContext = (ServiceFactory)target; 245 246 ComponentHandle handle = targetContext.createHandle(key, availableDependencies); 247 if (handle != null) { 248 return handle; 249 } 250 } 251 } 252 } 253 254 return getInstance(key, availableDependencies); 256 } 257 258 private void internalAddInstance(ServiceFactory component) { 259 if (parentContexts == null) { 260 parentContexts = new ArrayList (); 261 } 262 263 if (!parentContexts.contains(component)) { 264 parentContexts.add(component); 265 } 266 } 267 268 private void internalRemoveInstance(ServiceFactory component) { 269 if (parentContexts == null) { 270 return; 271 } 272 273 parentContexts.remove(component); 274 if (parentContexts.isEmpty()) { 275 parentContexts = null; 276 } 277 } 278 279 private void internalAddMapping(Object interface_, Object toMap) { 280 if (adapters == null) { 281 adapters = new HashMap (); 282 } 283 284 adapters.put(interface_, toMap); 285 } 286 287 290 private boolean hasKey(Object componentKey, ServiceFactory toSkip) { 291 if (adapters != null) { 292 if (adapters.containsKey(componentKey)) { 293 return (adapters.get(componentKey) != null); 294 } 295 } 296 297 if (parentContexts != null) { 298 for (Iterator iter = parentContexts.iterator(); iter.hasNext();) { 299 ServiceFactory context = (ServiceFactory) iter.next(); 300 301 if (context == toSkip) { 302 continue; 303 } 304 305 if (context.hasService(componentKey)) { 306 return true; 307 } 308 } 309 } 310 311 return false; 312 } 313 314 317 public Collection getMissingDependencies() { 318 319 HashSet result = new HashSet (); 320 321 if (parentContexts != null) { 322 for (Iterator iter = parentContexts.iterator(); iter.hasNext();) { 323 ServiceFactory next = (ServiceFactory) iter.next(); 324 325 Collection inheritedDeps = next.getMissingDependencies(); 326 327 for (Iterator iterator = inheritedDeps.iterator(); iterator 328 .hasNext();) { 329 Object dep = (Object ) iterator.next(); 330 331 if (!hasKey(dep, next)) { 332 result.add(dep); 333 } 334 } 335 } 336 } 337 338 if (exclusions != null) { 339 result.addAll(exclusions); 340 } 341 342 return result; 343 } 344 345 348 public boolean hasService(Object componentKey) { 349 return hasKey(componentKey, null); 350 } 351 } 352 | Popular Tags |