1 12 13 package org.eclipse.jdt.apt.core.internal; 14 15 import java.util.HashMap ; 16 import java.util.LinkedHashMap ; 17 import java.util.Map ; 18 import java.util.TreeMap ; 19 20 import org.eclipse.core.runtime.CoreException; 21 import org.eclipse.core.runtime.IConfigurationElement; 22 import org.eclipse.core.runtime.IExtension; 23 import org.eclipse.core.runtime.IExtensionPoint; 24 import org.eclipse.core.runtime.IStatus; 25 import org.eclipse.core.runtime.Platform; 26 import org.eclipse.core.runtime.Status; 27 import org.eclipse.jdt.apt.core.internal.util.FactoryContainer; 28 import org.eclipse.jdt.apt.core.internal.util.FactoryPath; 29 30 import com.sun.mirror.apt.AnnotationProcessorFactory; 31 32 37 public class FactoryPluginManager { 38 44 private static final HashMap <String , AnnotationProcessorFactory> PLUGIN_JAVA5_FACTORY_MAP = new HashMap <String , AnnotationProcessorFactory>(); 45 46 52 private static final HashMap <String , IServiceFactory> PLUGIN_JAVA6_FACTORY_MAP = new HashMap <String , IServiceFactory>(); 53 54 61 private static final TreeMap <String , PluginFactoryContainer> PLUGIN_CONTAINER_MAP = new TreeMap <String , PluginFactoryContainer>(); 62 63 67 private static boolean mapsInitialized = false; 68 69 77 public static synchronized Map <FactoryContainer, FactoryPath.Attributes> getAllPluginFactoryContainers() 78 { 79 Map <FactoryContainer, FactoryPath.Attributes> map = 80 new LinkedHashMap <FactoryContainer, FactoryPath.Attributes>(getPluginContainerMap().size()); 81 for (PluginFactoryContainer pfc : getPluginContainerMap().values()) { 82 FactoryPath.Attributes a = new FactoryPath.Attributes(pfc.getEnableDefault(), false); 83 map.put(pfc, a); 84 } 85 return map; 86 } 87 88 public static synchronized AnnotationProcessorFactory getJava5FactoryFromPlugin( String factoryName ) 89 { 90 AnnotationProcessorFactory apf = getJava5PluginFactoryMap().get( factoryName ); 91 if ( apf == null ) 92 { 93 String s = "could not find AnnotationProcessorFactory " + factoryName + " from available factories defined by plugins"; AptPlugin.log(new Status(IStatus.WARNING, AptPlugin.PLUGIN_ID, AptPlugin.STATUS_NOTOOLSJAR, s, null)); 96 } 97 return apf; 98 } 99 100 public static synchronized IServiceFactory getJava6FactoryFromPlugin( String factoryName ) 101 { 102 IServiceFactory isf = getJava6PluginFactoryMap().get( factoryName ); 103 if ( isf == null ) 104 { 105 String s = "could not find annotation processor " + factoryName + " from available factories defined by plugins"; AptPlugin.log(new Status(IStatus.WARNING, AptPlugin.PLUGIN_ID, AptPlugin.STATUS_NOTOOLSJAR, s, null)); 108 } 109 return isf; 110 } 111 112 119 public static synchronized FactoryContainer getPluginFactoryContainer(String pluginId) { 120 return getPluginContainerMap().get(pluginId); 121 } 122 123 127 private static TreeMap <String , PluginFactoryContainer> getPluginContainerMap() { 128 loadFactoryPlugins(); 129 return PLUGIN_CONTAINER_MAP; 130 } 131 132 136 private static HashMap <String , AnnotationProcessorFactory> getJava5PluginFactoryMap() { 137 loadFactoryPlugins(); 138 return PLUGIN_JAVA5_FACTORY_MAP; 139 } 140 141 145 private static HashMap <String , IServiceFactory> getJava6PluginFactoryMap() { 146 loadFactoryPlugins(); 147 return PLUGIN_JAVA6_FACTORY_MAP; 148 } 149 150 156 private static synchronized void loadFactoryPlugins() { 157 if (mapsInitialized) { 158 return; 159 } 160 IExtensionPoint extensionPoint = Platform.getExtensionRegistry().getExtensionPoint( 161 AptPlugin.PLUGIN_ID, "annotationProcessorFactory"); 164 for (IExtension extension : extensionPoint.getExtensions()) 167 { 168 for(IConfigurationElement factories : extension.getConfigurationElements()) 170 { 171 if ("factories".equals(factories.getName())) { loadJava5Factories(extension, factories); 173 } 174 else if ("java6processors".equals(factories.getName())) { loadJava6Factories(extension, factories); 176 } 177 178 } 179 } 180 mapsInitialized = true; 181 } 182 183 private static void loadJava6Factories(IExtension extension, IConfigurationElement factories) { 184 if (!AptPlugin.canRunJava6Processors()) { 185 return; 186 } 187 188 String enableDefaultStr = factories.getAttribute("enableDefault"); boolean enableDefault = true; 191 if ("false".equals(enableDefaultStr)) { enableDefault = false; 193 } 194 195 String pluginId = extension.getNamespaceIdentifier(); 197 PluginFactoryContainer pfc = new PluginFactoryContainer(pluginId, enableDefault); 199 PLUGIN_CONTAINER_MAP.put(pluginId, pfc); 200 201 for (IConfigurationElement factory : factories.getChildren()) { 203 if (!"java6processor".equals(factory.getName())) { continue; 205 } 206 String factoryName = null; 207 try { 208 factoryName = factory.getAttribute("class"); Object execExt = factory.createExecutableExtension("class"); Class <?> clazz = execExt.getClass(); 211 if (AptPlugin.getJava6ProcessorClass().isInstance(execExt)){ 212 assert(clazz.getName().equals(factoryName)); 213 IServiceFactory isf = new ClassServiceFactory(clazz); 214 PLUGIN_JAVA6_FACTORY_MAP.put( factoryName, isf ); 215 pfc.addFactoryName(factoryName, AptPlugin.JAVA6_FACTORY_NAME); 216 } 217 else { 218 reportFailureToLoadProcessor(null, factoryName, extension.getNamespaceIdentifier()); 219 } 220 } catch(CoreException e) { 221 reportFailureToLoadProcessor(e, factoryName, extension.getNamespaceIdentifier()); 222 } 223 } 224 } 225 226 private static void loadJava5Factories(IExtension extension, IConfigurationElement factories) { 227 String enableDefaultStr = factories.getAttribute("enableDefault"); boolean enableDefault = true; 230 if ("false".equals(enableDefaultStr)) { enableDefault = false; 232 } 233 234 String pluginId = extension.getNamespaceIdentifier(); 236 PluginFactoryContainer pfc = new PluginFactoryContainer(pluginId, enableDefault); 237 PLUGIN_CONTAINER_MAP.put(pluginId, pfc); 238 239 for (IConfigurationElement factory : factories.getChildren()) { 241 if (!"factory".equals(factory.getName())) { continue; 243 } 244 String factoryName = null; 245 try { 246 factoryName = factory.getAttribute("class"); Object execExt = factory.createExecutableExtension("class"); if (execExt instanceof AnnotationProcessorFactory){ 249 assert(execExt.getClass().getName().equals(factoryName)); 250 PLUGIN_JAVA5_FACTORY_MAP.put( factoryName, (AnnotationProcessorFactory)execExt ); 251 pfc.addFactoryName(factoryName, AptPlugin.JAVA5_FACTORY_NAME); 252 } 253 else { 254 reportFailureToLoadProcessor(null, factory.getName(), extension.getNamespaceIdentifier()); 255 } 256 } catch(CoreException e) { 257 reportFailureToLoadProcessor(e, factory.getName(), extension.getNamespaceIdentifier()); 258 } 259 } 260 } 261 262 private static void reportFailureToLoadProcessor(Exception e, String factoryName, String pluginId) { 263 AptPlugin.log(e, "Unable to load annotation processor "+ factoryName + " from plug-in " + pluginId); } 266 267 268 } 269 | Popular Tags |