KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > expressions > TypeExtensionManager


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.core.internal.expressions;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.Map JavaDoc;
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 JavaDoc fExtensionPoint;
32     
33     private static final String JavaDoc TYPE= "type"; //$NON-NLS-1$
34

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 JavaDoc namespace, String JavaDoc 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 JavaDoc receiver, String JavaDoc property, Object JavaDoc[] args, Object JavaDoc expectedValue) {
51             return false;
52         }
53     };
54     
55     /*
56      * Map containing all already created type extension object.
57      */

58     private Map JavaDoc/*<Class, TypeExtension>*/ fTypeExtensionMap;
59     
60     /*
61      * Table containing mapping of class name to configuration element
62      */

63     private Map JavaDoc/*<String, List<IConfigurationElement>>*/ fConfigurationElementMap;
64     
65     /*
66      * A cache to give fast access to the last 1000 method invocations.
67      */

68     private PropertyCache fPropertyCache;
69     
70     
71     public TypeExtensionManager(String JavaDoc extensionPoint) {
72         Assert.isNotNull(extensionPoint);
73         fExtensionPoint= extensionPoint;
74         Platform.getExtensionRegistry().addRegistryChangeListener(this);
75         initializeCaches();
76     }
77
78     public Property getProperty(Object JavaDoc receiver, String JavaDoc namespace, String JavaDoc method) throws CoreException {
79         return getProperty(receiver, namespace, method, false);
80     }
81     
82     public synchronized Property getProperty(Object JavaDoc receiver, String JavaDoc namespace, String JavaDoc method, boolean forcePluginActivation) throws CoreException {
83         long start= 0;
84         if (Expressions.TRACING)
85             start= System.currentTimeMillis();
86         
87         // if we call a static method than the receiver is the class object
88
Class JavaDoc clazz= receiver instanceof Class JavaDoc ? (Class JavaDoc)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 " + //$NON-NLS-1$
95
clazz.getName() + "#" + method + //$NON-NLS-1$
96
" found in cache: " + //$NON-NLS-1$
97
(System.currentTimeMillis() - start) + " ms."); //$NON-NLS-1$
98
}
99                 return cached;
100             }
101             // The type extender isn't loaded in the cached method but can be loaded
102
// now. So remove method from cache and do the normal look up so that the
103
// implementation class gets loaded.
104
fPropertyCache.remove(cached);
105         }
106         TypeExtension extension= get(clazz);
107         IPropertyTester extender= extension.findTypeExtender(this, namespace, method, receiver instanceof Class JavaDoc, 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 JavaDoc[] {method, clazz.toString()})));
114         }
115         result.setPropertyTester(extender);
116         fPropertyCache.put(result);
117         if (Expressions.TRACING) {
118             System.out.println("[Type Extension] - method " + //$NON-NLS-1$
119
clazz.getName() + "#" + method + //$NON-NLS-1$
120
" not found in cache: " + //$NON-NLS-1$
121
(System.currentTimeMillis() - start) + " ms."); //$NON-NLS-1$
122
}
123         return result;
124     }
125     
126     /*
127      * This method doesn't need to be synchronized since it is called
128      * from withing the getProperty method which is synchronized
129      */

130     /* package */ TypeExtension get(Class JavaDoc 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     /*
140      * This method doesn't need to be synchronized since it is called
141      * from withing the getProperty method which is synchronized
142      */

143     /* package */ IPropertyTester[] loadTesters(Class JavaDoc type) {
144         if (fConfigurationElementMap == null) {
145             fConfigurationElementMap= new HashMap JavaDoc();
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 JavaDoc typeAttr= config.getAttribute(TYPE);
153                 List JavaDoc typeConfigs= (List JavaDoc)fConfigurationElementMap.get(typeAttr);
154                 if (typeConfigs == null) {
155                     typeConfigs= new ArrayList JavaDoc();
156                     fConfigurationElementMap.put(typeAttr, typeConfigs);
157                 }
158                 typeConfigs.add(config);
159             }
160         }
161         String JavaDoc typeName= type.getName();
162         List JavaDoc typeConfigs= (List JavaDoc)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 JavaDoc();
190         fConfigurationElementMap= null;
191         fPropertyCache= new PropertyCache(1000);
192     }
193 }
194
Popular Tags