1 19 20 package edu.umd.cs.findbugs; 21 22 import java.io.File ; 23 import java.io.UnsupportedEncodingException ; 24 import java.net.MalformedURLException ; 25 import java.net.URL ; 26 import java.net.URLDecoder ; 27 import java.security.AccessController ; 28 import java.security.PrivilegedAction ; 29 import java.security.PrivilegedActionException ; 30 import java.security.PrivilegedExceptionAction ; 31 import java.util.ArrayList ; 32 import java.util.HashMap ; 33 import java.util.Iterator ; 34 35 43 public class DetectorFactoryCollection { 44 private HashMap <String , Plugin> pluginByIdMap = new HashMap <String , Plugin>(); 45 private ArrayList <DetectorFactory> factoryList = new ArrayList <DetectorFactory>(); 46 private HashMap <String , DetectorFactory> factoriesByName = new HashMap <String , DetectorFactory>(); 47 private HashMap <String , DetectorFactory> factoriesByDetectorClassName = 48 new HashMap <String , DetectorFactory>(); 49 50 private static DetectorFactoryCollection theInstance; 51 private static final Object lock = new Object (); 52 private boolean loaded = false; 53 54 private URL [] pluginList; 55 56 61 DetectorFactoryCollection() { 62 } 63 64 71 public void setPluginList(URL [] pluginList) { 72 if (loaded) throw new IllegalStateException (); 73 this.pluginList = new URL [pluginList.length]; 74 System.arraycopy(pluginList, 0, this.pluginList, 0, pluginList.length); 75 } 76 77 82 static void setInstance(DetectorFactoryCollection instance) { 83 synchronized (lock) { 84 if (theInstance != null) { 85 throw new IllegalStateException (); 86 } 87 theInstance = instance; 88 } 89 } 90 91 94 public static DetectorFactoryCollection instance() { 95 synchronized (lock) { 96 if (theInstance == null) { 97 theInstance = new DetectorFactoryCollection(); 98 } 99 theInstance.ensureLoaded(); 100 return theInstance; 101 } 102 } 103 106 public static DetectorFactoryCollection rawInstance() { 107 synchronized (lock) { 108 if (theInstance == null) { 109 theInstance = new DetectorFactoryCollection(); 110 } 111 return theInstance; 112 } 113 } 114 117 public Iterator <Plugin> pluginIterator() { 118 ensureLoaded(); 119 return pluginByIdMap.values().iterator(); 120 } 121 122 123 129 public Plugin getPluginById(String pluginId) { 130 ensureLoaded(); 131 return pluginByIdMap.get(pluginId); 132 } 133 134 138 public Iterator <DetectorFactory> factoryIterator() { 139 ensureLoaded(); 140 return factoryList.iterator(); 141 } 142 143 149 public DetectorFactory getFactory(String name) { 150 ensureLoaded(); 151 return factoriesByName.get(name); 152 } 153 154 161 public DetectorFactory getFactoryByClassName(String className) { 162 ensureLoaded(); 163 return factoriesByDetectorClassName.get(className); 164 } 165 166 169 private void registerDetector(DetectorFactory factory) { 170 if (FindBugs.DEBUG) System.out.println("Registering detector: " + factory.getFullName()); 171 String detectorName = factory.getShortName(); 172 factoryList.add(factory); 173 factoriesByName.put(detectorName, factory); 174 factoriesByDetectorClassName.put(factory.getFullName(), factory); 175 } 176 177 private void determinePlugins() { 178 if (pluginList != null) 179 return; 180 String homeDir = FindBugs.getHome(); 181 if (homeDir == null) 182 return; 183 184 File pluginDir = new File (homeDir + File.separator + "plugin"); 185 File [] contentList = pluginDir.listFiles(); 186 if (contentList == null) { 187 System.err.println("Error: The path " + pluginDir.getPath() 188 + " does not seem to be a directory!"); 189 System.err.println("No FindBugs plugins could be loaded"); 190 pluginList = new URL [0]; 191 return; 192 } 193 194 ArrayList <URL > arr = new ArrayList <URL >(); 195 for (File aContentList : contentList) { 196 if (aContentList.getName().endsWith(".jar")) { 197 198 try { 199 arr.add(aContentList.toURL()); 200 if (FindBugs.DEBUG) 201 System.out.println("Found plugin: " + aContentList.toString()); 202 } catch (MalformedURLException e) { 203 204 } 205 206 } 207 } 208 pluginList = arr.toArray(new URL [arr.size()]); 209 210 } 211 212 public void ensureLoaded() { 213 if (loaded) return; 214 loadPlugins(); 215 } 216 222 void loadPlugins() { 223 if (loaded) throw new IllegalStateException (); 224 225 if (SystemProperties.getBoolean("findbugs.jaws")) { 227 URL u = DetectorFactoryCollection.class.getResource("/findbugs.xml"); 228 URL [] plugins = new URL [1]; 230 if (u != null) { 231 String path = u.toString(); 232 path = path.substring(0, path.length() - "findbugs.xml".length()); 233 if (FindBugs.DEBUG) System.out.println("Jaws uses plugin: " + path); 234 try { 235 plugins[0] = new URL (path); 236 237 } catch (MalformedURLException e) { 238 throw new RuntimeException (e); 239 } 240 setPluginList(plugins); 241 242 } 243 } 244 245 loaded = true; 247 determinePlugins(); 248 249 int numLoaded = 0; 250 for (final URL url : pluginList) { 251 try { 252 if (FindBugs.DEBUG) System.out.println("Loading plugin: " + url.toString()); 253 PluginLoader pluginLoader = 254 AccessController.doPrivileged(new PrivilegedExceptionAction <PluginLoader>() { 255 256 public PluginLoader run() throws PluginException { 257 return new PluginLoader(url, this.getClass().getClassLoader()); 258 } 259 260 }); 261 262 263 Plugin plugin = pluginLoader.getPlugin(); 264 pluginByIdMap.put(plugin.getPluginId(), plugin); 265 266 for (Iterator <DetectorFactory> j = plugin.detectorFactoryIterator(); 268 j.hasNext();) { 269 DetectorFactory factory = j.next(); 270 registerDetector(factory); 271 } 272 273 I18N i18n = I18N.instance(); 274 275 for (Iterator <BugPattern> j = plugin.bugPatternIterator(); j.hasNext();) { 277 BugPattern bugPattern = j.next(); 278 i18n.registerBugPattern(bugPattern); 279 } 280 281 for (Iterator <BugCode> j = plugin.bugCodeIterator(); j.hasNext();) { 283 BugCode bugCode = j.next(); 284 i18n.registerBugCode(bugCode); 285 } 286 287 ++numLoaded; 288 } catch (PluginException e) { 289 System.err.println("Warning: could not load plugin " + url + ": " + e.toString()); 290 if (FindBugs.DEBUG) 291 e.printStackTrace(); 292 } catch (PrivilegedActionException e) { 293 System.err.println("Warning: could not load plugin " + url + ": " + e.toString()); 294 if (FindBugs.DEBUG) 295 e.printStackTrace(); 296 } 297 } 298 299 300 } 302 } 303 304 | Popular Tags |