1 11 package org.eclipse.core.internal.expressions; 12 13 import java.util.ArrayList ; 14 import java.util.HashMap ; 15 import java.util.List ; 16 import java.util.Map ; 17 18 import org.eclipse.core.runtime.Assert; 19 import org.eclipse.core.runtime.CoreException; 20 import org.eclipse.core.runtime.IConfigurationElement; 21 import org.eclipse.core.runtime.IExtensionDelta; 22 import org.eclipse.core.runtime.IExtensionRegistry; 23 import org.eclipse.core.runtime.IRegistryChangeEvent; 24 import org.eclipse.core.runtime.IRegistryChangeListener; 25 import org.eclipse.core.runtime.Platform; 26 27 import org.eclipse.core.expressions.IPropertyTester; 28 29 public class TypeExtensionManager implements IRegistryChangeListener { 30 31 private String fExtensionPoint; 32 33 private static final String TYPE= "type"; 35 private static final IPropertyTester[] EMPTY_PROPERTY_TESTER_ARRAY= new IPropertyTester[0]; 36 37 private static final IPropertyTester NULL_PROPERTY_TESTER= new IPropertyTester() { 38 public boolean handles(String namespace, String property) { 39 return false; 40 } 41 public boolean isInstantiated() { 42 return true; 43 } 44 public boolean isDeclaringPluginActive() { 45 return true; 46 } 47 public IPropertyTester instantiate() throws CoreException { 48 return this; 49 } 50 public boolean test(Object receiver, String property, Object [] args, Object expectedValue) { 51 return false; 52 } 53 }; 54 55 58 private Map fTypeExtensionMap; 59 60 63 private Map fConfigurationElementMap; 64 65 68 private PropertyCache fPropertyCache; 69 70 71 public TypeExtensionManager(String extensionPoint) { 72 Assert.isNotNull(extensionPoint); 73 fExtensionPoint= extensionPoint; 74 Platform.getExtensionRegistry().addRegistryChangeListener(this); 75 initializeCaches(); 76 } 77 78 public Property getProperty(Object receiver, String namespace, String method) throws CoreException { 79 return getProperty(receiver, namespace, method, false); 80 } 81 82 public synchronized Property getProperty(Object receiver, String namespace, String method, boolean forcePluginActivation) throws CoreException { 83 long start= 0; 84 if (Expressions.TRACING) 85 start= System.currentTimeMillis(); 86 87 Class clazz= receiver instanceof Class ? (Class )receiver : receiver.getClass(); 89 Property result= new Property(clazz, namespace, method); 90 Property cached= fPropertyCache.get(result); 91 if (cached != null) { 92 if (cached.isValidCacheEntry(forcePluginActivation)) { 93 if (Expressions.TRACING) { 94 System.out.println("[Type Extension] - method " + clazz.getName() + "#" + method + " found in cache: " + (System.currentTimeMillis() - start) + " ms."); } 99 return cached; 100 } 101 fPropertyCache.remove(cached); 105 } 106 TypeExtension extension= get(clazz); 107 IPropertyTester extender= extension.findTypeExtender(this, namespace, method, receiver instanceof Class , forcePluginActivation); 108 if (extender == TypeExtension.CONTINUE || extender == null) { 109 throw new CoreException(new ExpressionStatus( 110 ExpressionStatus.TYPE_EXTENDER_UNKOWN_METHOD, 111 Messages.format( 112 ExpressionMessages.TypeExtender_unknownMethod, 113 new Object [] {method, clazz.toString()}))); 114 } 115 result.setPropertyTester(extender); 116 fPropertyCache.put(result); 117 if (Expressions.TRACING) { 118 System.out.println("[Type Extension] - method " + clazz.getName() + "#" + method + " not found in cache: " + (System.currentTimeMillis() - start) + " ms."); } 123 return result; 124 } 125 126 130 TypeExtension get(Class clazz) { 131 TypeExtension result= (TypeExtension)fTypeExtensionMap.get(clazz); 132 if (result == null) { 133 result= new TypeExtension(clazz); 134 fTypeExtensionMap.put(clazz, result); 135 } 136 return result; 137 } 138 139 143 IPropertyTester[] loadTesters(Class type) { 144 if (fConfigurationElementMap == null) { 145 fConfigurationElementMap= new HashMap (); 146 IExtensionRegistry registry= Platform.getExtensionRegistry(); 147 IConfigurationElement[] ces= registry.getConfigurationElementsFor( 148 ExpressionPlugin.getPluginId(), 149 fExtensionPoint); 150 for (int i= 0; i < ces.length; i++) { 151 IConfigurationElement config= ces[i]; 152 String typeAttr= config.getAttribute(TYPE); 153 List typeConfigs= (List )fConfigurationElementMap.get(typeAttr); 154 if (typeConfigs == null) { 155 typeConfigs= new ArrayList (); 156 fConfigurationElementMap.put(typeAttr, typeConfigs); 157 } 158 typeConfigs.add(config); 159 } 160 } 161 String typeName= type.getName(); 162 List typeConfigs= (List )fConfigurationElementMap.get(typeName); 163 if (typeConfigs == null) 164 return EMPTY_PROPERTY_TESTER_ARRAY; 165 else { 166 IPropertyTester[] result= new IPropertyTester[typeConfigs.size()]; 167 for (int i= 0; i < result.length; i++) { 168 IConfigurationElement config= (IConfigurationElement)typeConfigs.get(i); 169 try { 170 result[i]= new PropertyTesterDescriptor(config); 171 } catch (CoreException e) { 172 ExpressionPlugin.getDefault().getLog().log(e.getStatus()); 173 result[i]= NULL_PROPERTY_TESTER; 174 } 175 } 176 fConfigurationElementMap.remove(typeName); 177 return result; 178 } 179 } 180 181 public void registryChanged(IRegistryChangeEvent event) { 182 IExtensionDelta[] deltas= event.getExtensionDeltas(ExpressionPlugin.getPluginId(), fExtensionPoint); 183 if (deltas.length > 0) { 184 initializeCaches(); 185 } 186 } 187 188 private synchronized void initializeCaches() { 189 fTypeExtensionMap= new HashMap (); 190 fConfigurationElementMap= null; 191 fPropertyCache= new PropertyCache(1000); 192 } 193 } 194 | Popular Tags |