KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > rcp > extensionpoint > AbstractEPProcessor


1 /*
2  * Created on Oct 28, 2004
3  * by Alexander Bieber
4  *
5  */

6 package com.nightlabs.rcp.extensionpoint;
7
8 import org.eclipse.core.runtime.IConfigurationElement;
9 import org.eclipse.core.runtime.IExtension;
10 import org.eclipse.core.runtime.IExtensionPoint;
11 import org.eclipse.core.runtime.IExtensionRegistry;
12 import org.eclipse.core.runtime.Platform;
13
14 /**
15  * Used for parsing extensions to a certian point.
16  * Calling {@link #process()} will cause {@link #processElement(IConfigurationElement)}
17  * to be called for every extension defined to the point returned by {@link #getExtensionPointID()}.
18  * The IConfigurationElement passed to processElement is the root element of the extension.
19  * Usually one will use element.createExecutableExtension() to get a instance of
20  * the extensions object.<br/>
21  * A common usage is subclassing this to a registry which registeres its
22  * entries in processElement lazily by doing
23  * <pre>
24  * if (!isProcessed())
25  * process();
26  * </pre>
27  * everytime when asked for one element fist.
28  *
29  * @author Alexander Bieber
30  */

31 public abstract class AbstractEPProcessor
32 implements IEPProcessor
33 {
34
35     public abstract String JavaDoc getExtensionPointID();
36     
37     public abstract void processElement(IConfigurationElement element) throws EPProcessorException;
38   
39     private boolean processed = false;
40     public boolean isProcessed() {
41         return processed;
42     }
43     
44     private boolean processing = false;
45     
46     protected boolean isProcessing() {
47         return processing;
48     }
49     
50   public synchronized void process() throws EPProcessorException{
51     processing = true;
52     try {
53         try{
54             IExtensionRegistry registry = Platform.getExtensionRegistry();
55             IExtensionPoint extensionPoint = registry.getExtensionPoint(getExtensionPointID());
56             if (extensionPoint == null) {
57                 throw new EPProcessorException(
58                         "Unable to resolve extension-point: " + getExtensionPointID());
59             }
60     
61           IExtension[] extensions = extensionPoint.getExtensions();
62           // For each extension ...
63
for (int i = 0; i < extensions.length; i++) {
64               IExtension extension = extensions[i];
65               IConfigurationElement[] elements =
66                 extension.getConfigurationElements();
67               // For each member of the extension ...
68
for (int j = 0; j < elements.length; j++) {
69                 IConfigurationElement element = elements[j];
70                 processElement(element);
71               }
72           }
73           processed = true;
74         }catch(Throwable JavaDoc e){
75             if (e instanceof EPProcessorException)
76                 throw (EPProcessorException)e;
77             else
78                 throw new EPProcessorException(e);
79         }
80     } finally {
81         processing = false;
82     }
83   }
84
85     /**
86      * Assures that this processor
87      * has processed its extensions
88      */

89   public void checkProcessing()
90   {
91     if (!isProcessed()) {
92         try {
93             process();
94         } catch (EPProcessorException e) {
95             throw new RuntimeException JavaDoc(e);
96         }
97     }
98   }
99   
100 }
101
Popular Tags