KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > apt > core > internal > util > FactoryPath


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 BEA Systems, Inc.
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  * wharley@bea.com - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.jdt.apt.core.internal.util;
13
14 import java.io.File JavaDoc;
15 import java.util.Collections JavaDoc;
16 import java.util.LinkedHashMap JavaDoc;
17 import java.util.Map JavaDoc;
18
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IPath;
21 import org.eclipse.core.runtime.Status;
22 import org.eclipse.jdt.apt.core.internal.AptPlugin;
23 import org.eclipse.jdt.apt.core.internal.FactoryPluginManager;
24 import org.eclipse.jdt.apt.core.util.IFactoryPath;
25
26 /**
27  * Provides access to the annotation processor factory path for a Java project.
28  * This class should not be instantiated or subclassed.
29  *
30  * The factory path is an ordered Map<FactoryContainer, FactoryPath.Attributes>.
31  * Containers are things like jar files or plugins, that contain one or more
32  * annotation processor factories. In the context of a particular project,
33  * processors are given precedence according to the order of their container on
34  * the factory path; and they are executed according to the container's attributes
35  * on the factory path.
36  */

37 public class FactoryPath implements IFactoryPath {
38     
39     /**
40      * Attributes of entries on the factory path. These belong here,
41      * rather than on FactoryContainer itself, because the same container
42      * might have different attributes in different projects - e.g., it
43      * might be enabled in one project and disabled in another.
44      */

45     public static class Attributes {
46         /** Should this container's processors be executed? */
47         private boolean _enabled;
48         /** Should this container's processors execute in Sun apt compatibility mode? (Slow and limiting!) */
49         private boolean _runInBatchMode;
50         
51         // CONSTRUCTORS
52
public Attributes(boolean enabled, boolean runInBatchMode) {
53             _enabled = enabled;
54             _runInBatchMode = runInBatchMode;
55         }
56         public Attributes(Attributes attr) {
57             _enabled = attr._enabled;
58             _runInBatchMode = attr._runInBatchMode;
59         }
60         
61         // SUPPORT
62
public boolean equals(Object JavaDoc o) {
63             if (o == null || !(o instanceof Attributes))
64                 return false;
65             Attributes oA = (Attributes)o;
66             return (_enabled == oA._enabled) && (_runInBatchMode == oA._runInBatchMode );
67         }
68         public int hashCode() {
69             return (_enabled ? 1 : 0) + (_runInBatchMode ? 2 : 0);
70         }
71         
72         
73         // GETTERS
74
public boolean isEnabled() {
75             return _enabled;
76         }
77         public boolean runInBatchMode() {
78             return _runInBatchMode;
79         }
80
81         // SETTERS
82
public void setEnabled(boolean enabled) {
83             _enabled = enabled;
84         }
85         public void setRunInBatchMode(boolean runInBatchMode) {
86             _runInBatchMode = runInBatchMode;
87         }
88     }
89     
90     /**
91      * The factory path.
92      */

93     private final Map JavaDoc<FactoryContainer, Attributes> _path = Collections.synchronizedMap(
94             new LinkedHashMap JavaDoc<FactoryContainer, Attributes>());
95     
96     /* (non-Javadoc)
97      * @see org.eclipse.jdt.apt.core.util.IFactoryPath#addExternalJar(java.io.File)
98      */

99     public void addExternalJar(File JavaDoc jar) {
100         FactoryContainer fc = FactoryPathUtil.newExtJarFactoryContainer(jar);
101         Attributes a = new Attributes(true, false);
102         internalAdd(fc, a);
103     }
104
105     /* (non-Javadoc)
106      * @see org.eclipse.jdt.apt.core.util.IFactoryPath#removeExternalJar(java.io.File)
107      */

108     public void removeExternalJar(File JavaDoc jar) {
109         FactoryContainer fc = FactoryPathUtil.newExtJarFactoryContainer(jar);
110         _path.remove(fc);
111     }
112
113     /* (non-Javadoc)
114      * @see org.eclipse.jdt.apt.core.util.IFactoryPath#addVarJar(org.eclipse.core.runtime.IPath)
115      */

116     public void addVarJar(IPath jarPath) {
117         FactoryContainer fc = FactoryPathUtil.newVarJarFactoryContainer(jarPath);
118         Attributes a = new Attributes(true, false);
119         internalAdd(fc, a);
120     }
121
122     /* (non-Javadoc)
123      * @see org.eclipse.jdt.apt.core.util.IFactoryPath#removeVarJar(org.eclipse.core.runtime.IPath)
124      */

125     public void removeVarJar(IPath jarPath) {
126         FactoryContainer fc = FactoryPathUtil.newVarJarFactoryContainer(jarPath);
127         _path.remove(fc);
128     }
129
130     /* (non-Javadoc)
131      * @see org.eclipse.jdt.apt.core.util.IFactoryPath#addWkspJar(org.eclipse.core.runtime.IPath)
132      */

133     public void addWkspJar(IPath jarPath) {
134         FactoryContainer fc = FactoryPathUtil.newWkspJarFactoryContainer(jarPath);
135         Attributes a = new Attributes(true, false);
136         internalAdd(fc, a);
137     }
138
139     /* (non-Javadoc)
140      * @see org.eclipse.jdt.apt.core.util.IFactoryPath#removeWkspJar(org.eclipse.core.runtime.IPath)
141      */

142     public void removeWkspJar(IPath jarPath) {
143         FactoryContainer fc = FactoryPathUtil.newWkspJarFactoryContainer(jarPath);
144         _path.remove(fc);
145     }
146
147     /* (non-Javadoc)
148      * @see org.eclipse.jdt.apt.core.util.IFactoryPath#enablePlugin(java.lang.String)
149      */

150     public void enablePlugin(String JavaDoc pluginId) throws CoreException {
151         FactoryContainer fc = FactoryPluginManager.getPluginFactoryContainer(pluginId);
152         Attributes a = _path.get(fc);
153         if (a == null) {
154             Status status = AptPlugin.createWarningStatus(new IllegalArgumentException JavaDoc(),
155                     "Specified plugin was not found, so it could not be added to the annotation processor factory path: " + pluginId); //$NON-NLS-1$
156
throw new CoreException(status);
157         }
158         a.setEnabled(true);
159         internalAdd(fc, a);
160     }
161
162     /* (non-Javadoc)
163      * @see org.eclipse.jdt.apt.core.util.IFactoryPath#disablePlugin(java.lang.String)
164      */

165     public void disablePlugin(String JavaDoc pluginId) {
166         FactoryContainer fc = FactoryPluginManager.getPluginFactoryContainer(pluginId);
167         Attributes a = _path.get(fc);
168         if (a != null) {
169             a.setEnabled(false);
170         }
171     }
172
173     /**
174      * Add a single factory container to the head of the FactoryPath,
175      * and save the new path to the appropriate settings file.
176      * If the container specified is already in the project's list in
177      * some other FactoryPathEntry, the existing entry will be removed
178      * before the new one is added.
179      * @param fc must not be null.
180      */

181     public void addEntryToHead(FactoryContainer fc, boolean enabled, boolean runInBatchMode) {
182         Attributes a = new Attributes(enabled, runInBatchMode);
183         internalAdd(fc, a);
184     }
185     
186     /**
187      * Set the factory path based on the contents of an ordered map.
188      * @param map should be an ordered map, such as LinkedHashMap; should contain no
189      * nulls; and should contain no duplicate FactoryContainers.
190      */

191     public void setContainers(Map JavaDoc<FactoryContainer, Attributes> map) {
192         synchronized(_path) {
193             _path.clear();
194             _path.putAll(map);
195         }
196     }
197     
198     /**
199      * Add a factory container, and attributes, to the head of the list.
200      * If it already existed in the list, remove the old instance before
201      * adding the new one.
202      * <p>
203      * @param fc must not be null
204      * @param a must not be null
205      */

206     private void internalAdd(FactoryContainer fc, Attributes a) {
207         synchronized(_path) {
208             _path.remove(fc);
209             // LinkedHashMap doesn't have any way to add to the head,
210
// so we're forced to do two copies. Make the new map
211
// large enough that we don't have to rehash midway through the putAll().
212
Map JavaDoc<FactoryContainer, Attributes> newPath =
213                 new LinkedHashMap JavaDoc<FactoryContainer, Attributes>(1 + 4*(_path.size() + 1)/3);
214             newPath.put(fc, a);
215             newPath.putAll(_path);
216             _path.clear();
217             _path.putAll(newPath);
218         }
219     }
220
221     public Map JavaDoc<FactoryContainer, Attributes> getEnabledContainers() {
222         Map JavaDoc<FactoryContainer, Attributes> map = new LinkedHashMap JavaDoc<FactoryContainer, Attributes>();
223         synchronized(_path) {
224             for (Map.Entry JavaDoc<FactoryContainer, Attributes> entry : _path.entrySet()) {
225                 Attributes attr = entry.getValue();
226                 if (attr.isEnabled()) {
227                     Attributes attrClone = new Attributes(attr);
228                     map.put(entry.getKey(), attrClone);
229                 }
230             }
231         }
232         return map;
233     }
234
235     /**
236      * @return a copy of the path
237      */

238     public Map JavaDoc<FactoryContainer, Attributes> getAllContainers() {
239         Map JavaDoc<FactoryContainer, Attributes> map = new LinkedHashMap JavaDoc<FactoryContainer, Attributes>(_path.size());
240         synchronized(_path) {
241             for( Map.Entry JavaDoc<FactoryContainer, Attributes> entry : _path.entrySet() ){
242                 map.put( entry.getKey(), new Attributes(entry.getValue()) );
243             }
244         }
245         return map;
246     }
247
248 }
249
Popular Tags