1 11 12 package org.eclipse.jdt.apt.core.internal.util; 13 14 import java.io.File ; 15 import java.util.Collections ; 16 import java.util.LinkedHashMap ; 17 import java.util.Map ; 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 37 public class FactoryPath implements IFactoryPath { 38 39 45 public static class Attributes { 46 47 private boolean _enabled; 48 49 private boolean _runInBatchMode; 50 51 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 public boolean equals(Object 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 public boolean isEnabled() { 75 return _enabled; 76 } 77 public boolean runInBatchMode() { 78 return _runInBatchMode; 79 } 80 81 public void setEnabled(boolean enabled) { 83 _enabled = enabled; 84 } 85 public void setRunInBatchMode(boolean runInBatchMode) { 86 _runInBatchMode = runInBatchMode; 87 } 88 } 89 90 93 private final Map <FactoryContainer, Attributes> _path = Collections.synchronizedMap( 94 new LinkedHashMap <FactoryContainer, Attributes>()); 95 96 99 public void addExternalJar(File jar) { 100 FactoryContainer fc = FactoryPathUtil.newExtJarFactoryContainer(jar); 101 Attributes a = new Attributes(true, false); 102 internalAdd(fc, a); 103 } 104 105 108 public void removeExternalJar(File jar) { 109 FactoryContainer fc = FactoryPathUtil.newExtJarFactoryContainer(jar); 110 _path.remove(fc); 111 } 112 113 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 125 public void removeVarJar(IPath jarPath) { 126 FactoryContainer fc = FactoryPathUtil.newVarJarFactoryContainer(jarPath); 127 _path.remove(fc); 128 } 129 130 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 142 public void removeWkspJar(IPath jarPath) { 143 FactoryContainer fc = FactoryPathUtil.newWkspJarFactoryContainer(jarPath); 144 _path.remove(fc); 145 } 146 147 150 public void enablePlugin(String 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 (), 155 "Specified plugin was not found, so it could not be added to the annotation processor factory path: " + pluginId); throw new CoreException(status); 157 } 158 a.setEnabled(true); 159 internalAdd(fc, a); 160 } 161 162 165 public void disablePlugin(String 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 181 public void addEntryToHead(FactoryContainer fc, boolean enabled, boolean runInBatchMode) { 182 Attributes a = new Attributes(enabled, runInBatchMode); 183 internalAdd(fc, a); 184 } 185 186 191 public void setContainers(Map <FactoryContainer, Attributes> map) { 192 synchronized(_path) { 193 _path.clear(); 194 _path.putAll(map); 195 } 196 } 197 198 206 private void internalAdd(FactoryContainer fc, Attributes a) { 207 synchronized(_path) { 208 _path.remove(fc); 209 Map <FactoryContainer, Attributes> newPath = 213 new LinkedHashMap <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 <FactoryContainer, Attributes> getEnabledContainers() { 222 Map <FactoryContainer, Attributes> map = new LinkedHashMap <FactoryContainer, Attributes>(); 223 synchronized(_path) { 224 for (Map.Entry <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 238 public Map <FactoryContainer, Attributes> getAllContainers() { 239 Map <FactoryContainer, Attributes> map = new LinkedHashMap <FactoryContainer, Attributes>(_path.size()); 240 synchronized(_path) { 241 for( Map.Entry <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 |