1 11 package org.eclipse.ui.internal.components.registry; 12 13 import java.util.ArrayList ; 14 import java.util.Collection ; 15 import java.util.HashSet ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 19 import org.eclipse.ui.internal.WorkbenchPlugin; 20 import org.eclipse.ui.internal.components.ComponentUtil; 21 import org.eclipse.ui.internal.components.framework.ClassIdentifier; 22 import org.eclipse.ui.internal.components.framework.ComponentException; 23 import org.eclipse.ui.internal.components.framework.ComponentFactory; 24 import org.eclipse.ui.internal.components.framework.ComponentHandle; 25 import org.eclipse.ui.internal.components.framework.IServiceProvider; 26 import org.eclipse.ui.internal.components.framework.NonDisposingHandle; 27 import org.eclipse.ui.internal.components.framework.ServiceFactory; 28 29 32 public class ComponentScope extends ServiceFactory implements IComponentScope { 33 34 private String scopeId; 35 36 private ComponentTypeMap types = new ComponentTypeMap(); 37 private ComponentTypeMap modifiers = new ComponentTypeMap(); 38 private IScopeReference[] parentScopes = new IScopeReference[0]; 39 private ClassIdentifier[] dependencies = new ClassIdentifier[0]; 40 private static final String impossibleToSatisfyDependency = "Dependency that is impossible to satisfy"; 42 private boolean loaded = false; 43 44 private ArrayList children = new ArrayList (); 45 46 private ServiceFactory[] parentContexts = new ServiceFactory[0]; 47 48 private ArrayList scopeDependencies = new ArrayList (); 49 50 public ComponentScope(String scopeId) { 51 this.scopeId = scopeId; 52 } 53 54 public IServiceProvider getContainer(ServiceFactory context) { 55 return null; 56 } 57 58 61 public ComponentHandle createHandle(Object key, IServiceProvider container) throws ComponentException { 62 63 if (key == this) { 64 return new NonDisposingHandle(this); 65 } 66 67 if (key instanceof Class ) { 68 ComponentFactory factory = (ComponentFactory)types.get((Class )key); 69 70 if (factory != null) { 71 return factory.createHandle(container); 72 } 73 } 74 75 for (int i = 0; i < parentContexts.length; i++) { 76 ServiceFactory context = parentContexts[i]; 77 78 ComponentHandle handle = context.createHandle(key, container); 79 if (handle != null) { 80 return handle; 81 } 82 } 83 84 return null; 85 } 86 87 90 public boolean hasService(Object componentKey) { 91 return hasKey(componentKey, null); 92 } 93 94 97 private boolean hasKey(Object key, ServiceFactory parentScopeToSkip) { 98 if (key == this) { 99 return true; 100 } 101 102 if (key instanceof Class ) { 103 if (types.containsKey((Class )key)) { 104 return true; 105 } 106 } 107 108 for (int i = 0; i < parentContexts.length; i++) { 109 ServiceFactory context = parentContexts[i]; 110 111 if (context == parentScopeToSkip) { 112 continue; 113 } 114 115 if (context.hasService(key)) { 116 return true; 117 } 118 } 119 120 return false; 121 } 122 123 149 public ComponentFactory lookup(ClassIdentifier type) { 150 return (ComponentFactory)types.get(type); 151 } 152 153 public void put(ClassIdentifier type, ComponentFactory factory) { 154 types.put(type, factory); 155 } 156 157 public void remove(ClassIdentifier type) { 158 types.remove(type); 159 } 160 161 164 public ClassIdentifier[] getTypes() { 165 return types.getTypes(); 166 } 167 168 171 public String getScopeId() { 172 return scopeId; 173 } 174 175 176 179 public IScopeReference[] getParentScopes() { 180 return parentScopes; 181 } 182 183 186 public ClassIdentifier[] getDependencies() { 187 return dependencies; 188 } 189 190 193 public ServiceFactory getContext() { 194 return this; 195 } 196 197 200 public Collection getMissingDependencies() { 201 if (!loaded) { 202 List result = new ArrayList (); 203 result.add (impossibleToSatisfyDependency); 204 return result; 205 } 206 207 HashSet result = new HashSet (); 208 for (int i = 0; i < dependencies.length; i++) { 209 ClassIdentifier type = dependencies[i]; 210 211 try { 212 Class dep = ComponentUtil.loadClass(type); 213 if (types.get(dep) == null) { 215 result.add(dep); 216 } 217 } catch (ComponentException e) { 218 WorkbenchPlugin.log(e); 219 result.add(impossibleToSatisfyDependency); 220 } 221 } 222 223 for (int i = 0; i < parentScopes.length; i++) { 226 IScopeReference ref = parentScopes[i]; 227 228 if (ref.getRelationship() == IScopeReference.REL_REQUIRES) { 229 ServiceFactory context = ((ComponentScope)ref.getTarget()).getContext(); 230 231 if (!hasService(context)) { 232 result.add(context); 233 } 234 } 235 } 236 237 239 for (int i = 0; i < parentContexts.length; i++) { 240 ServiceFactory next = parentContexts[i]; 241 242 Collection parentMissing = next.getMissingDependencies(); 244 245 for (Iterator iter = parentMissing.iterator(); iter.hasNext();) { 246 Object object = (Object ) iter.next(); 247 248 if (!hasKey(object, next)) { 250 result.add(object); 251 } 252 } 253 } 254 255 return result; 256 } 257 258 public void load(ScopeDefinition def, ComponentRegistry reg) { 259 if (loaded) { 260 unload(reg); 261 } 262 263 dependencies = def.getDependencies(); 264 266 SymbolicScopeReference[] refs = def.getExtends(); 267 IScopeReference[] parents = new IScopeReference[refs.length]; 268 List parentContextList = new ArrayList (); 269 270 for (int i = 0; i < refs.length; i++) { 271 SymbolicScopeReference reference = refs[i]; 272 273 IScopeReference ref = new ScopeReference(reference.relationship, 274 reg.linkSubScope(reference.scopeId, this, reference.relationship)); 275 parents[i] = ref; 276 277 if (reference.relationship == IScopeReference.REL_REQUIRES) { 278 scopeDependencies.add(((ComponentScope) ref.getTarget()).getContext()); 279 } else { 280 parentContextList.add(((ComponentScope) ref.getTarget()).getContext()); 281 } 282 } 283 284 parentContexts = (ServiceFactory[]) parentContextList.toArray( 285 new ServiceFactory[parentContextList.size()]); 286 parentScopes = parents; 287 loaded = true; 288 } 289 290 public void unload(ComponentRegistry reg) { 291 for (int i = 0; i < parentScopes.length; i++) { 292 IScopeReference ref = parentScopes[i]; 293 294 reg.unlinkSubScope(ref.getTarget().getScopeId(), this); 295 } 296 297 scopeDependencies = new ArrayList (); 298 parentScopes = new IScopeReference[0]; 299 dependencies = new ClassIdentifier[0]; 300 parentContexts = new ServiceFactory[0]; 301 loaded = false; 303 } 304 305 public boolean isRedundant() { 306 return (!loaded) && getChildScopes().length == 0 && types.isEmpty() && modifiers.isEmpty(); 307 } 308 309 314 public void addChild(ComponentScope child, int relationship) { 315 children.add(new ScopeReference(relationship, child)); 316 } 317 318 public void removeChild(ComponentScope child) { 319 IScopeReference[] refs = (IScopeReference[]) children.toArray(new IScopeReference[children.size()]); 320 for (int i = 0; i < refs.length; i++) { 321 IScopeReference reference = refs[i]; 322 if (reference.getTarget() == child) { 323 children.remove(reference); 324 } 325 } 326 } 327 328 331 public String toString() { 332 return "scope " + scopeId; } 334 335 338 public IScopeReference[] getChildScopes() { 339 return (IScopeReference[]) children.toArray(new IScopeReference[children.size()]); 340 } 341 342 343 344 } 345 | Popular Tags |