KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > DetectorFactoryCollection


1 /*
2  * FindBugs - Find bugs in Java programs
3  * Copyright (C) 2003-2005 University of Maryland
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package edu.umd.cs.findbugs;
21
22 import java.io.File JavaDoc;
23 import java.io.UnsupportedEncodingException JavaDoc;
24 import java.net.MalformedURLException JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.net.URLDecoder JavaDoc;
27 import java.security.AccessController JavaDoc;
28 import java.security.PrivilegedAction JavaDoc;
29 import java.security.PrivilegedActionException JavaDoc;
30 import java.security.PrivilegedExceptionAction JavaDoc;
31 import java.util.ArrayList JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.Iterator JavaDoc;
34
35 /**
36  * The DetectorFactoryCollection stores all of the DetectorFactory objects
37  * used to create the Detectors which implement the various analyses.
38  * It is a singleton class.
39  *
40  * @author David Hovemeyer
41  * @see DetectorFactory
42  */

43 public class DetectorFactoryCollection {
44     private HashMap JavaDoc<String JavaDoc, Plugin> pluginByIdMap = new HashMap JavaDoc<String JavaDoc, Plugin>();
45     private ArrayList JavaDoc<DetectorFactory> factoryList = new ArrayList JavaDoc<DetectorFactory>();
46     private HashMap JavaDoc<String JavaDoc, DetectorFactory> factoriesByName = new HashMap JavaDoc<String JavaDoc, DetectorFactory>();
47     private HashMap JavaDoc<String JavaDoc, DetectorFactory> factoriesByDetectorClassName =
48         new HashMap JavaDoc<String JavaDoc, DetectorFactory>();
49
50     private static DetectorFactoryCollection theInstance;
51     private static final Object JavaDoc lock = new Object JavaDoc();
52     private boolean loaded = false;
53
54     private URL JavaDoc[] pluginList;
55
56     /**
57      * Constructor.
58      * loadPlugins() method must be called before
59      * any detector factories can be accessed.
60      */

61     DetectorFactoryCollection() {
62     }
63
64     /**
65      * Set the list of plugins to load explicitly.
66      * This must be done before the instance of DetectorFactoryCollection
67      * is created.
68      *
69      * @param pluginList list of plugin Jar files to load
70      */

71     public void setPluginList(URL JavaDoc[] pluginList) {
72         if (loaded) throw new IllegalStateException JavaDoc();
73         this.pluginList = new URL JavaDoc[pluginList.length];
74         System.arraycopy(pluginList, 0, this.pluginList, 0, pluginList.length);
75     }
76
77     /**
78      * Set the instance that should be retured as the singleton instance.
79      *
80      * @param instance the singleton instance to be set
81      */

82     static void setInstance(DetectorFactoryCollection instance) {
83         synchronized (lock) {
84             if (theInstance != null) {
85                 throw new IllegalStateException JavaDoc();
86             }
87             theInstance = instance;
88         }
89     }
90
91     /**
92      * Get the single instance of DetectorFactoryCollection.
93      */

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     /**
104      * Get the single instance of DetectorFactoryCollection.
105      */

106     public static DetectorFactoryCollection rawInstance() {
107         synchronized (lock) {
108             if (theInstance == null) {
109                 theInstance = new DetectorFactoryCollection();
110             }
111             return theInstance;
112         }
113     }
114     /**
115      * Return an Iterator over all available Plugin objects.
116      */

117     public Iterator JavaDoc<Plugin> pluginIterator() {
118         ensureLoaded();
119         return pluginByIdMap.values().iterator();
120     }
121
122     
123     /**
124      * Get a Plugin by its unique id.
125      *
126      * @param pluginId the unique id
127      * @return the Plugin with that id, or null if no such Plugin is found
128      */

129     public Plugin getPluginById(String JavaDoc pluginId) {
130         ensureLoaded();
131         return pluginByIdMap.get(pluginId);
132     }
133
134     /**
135      * Return an Iterator over the DetectorFactory objects for all
136      * registered Detectors.
137      */

138     public Iterator JavaDoc<DetectorFactory> factoryIterator() {
139         ensureLoaded();
140         return factoryList.iterator();
141     }
142
143     /**
144      * Look up a DetectorFactory by its short name.
145      *
146      * @param name the short name
147      * @return the DetectorFactory, or null if there is no factory with that short name
148      */

149     public DetectorFactory getFactory(String JavaDoc name) {
150         ensureLoaded();
151         return factoriesByName.get(name);
152     }
153     
154     /**
155      * Look up a DetectorFactory by its class name.
156      *
157      * @param className the class name
158      * @return the DetectoryFactory, or null if there is no factory with
159      * that class name
160      */

161     public DetectorFactory getFactoryByClassName(String JavaDoc className) {
162         ensureLoaded();
163         return factoriesByDetectorClassName.get(className);
164     }
165
166     /**
167      * Register a DetectorFactory.
168      */

169     private void registerDetector(DetectorFactory factory) {
170         if (FindBugs.DEBUG) System.out.println("Registering detector: " + factory.getFullName());
171         String JavaDoc 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 JavaDoc homeDir = FindBugs.getHome();
181         if (homeDir == null)
182             return;
183
184         File JavaDoc pluginDir = new File JavaDoc(homeDir + File.separator + "plugin");
185         File JavaDoc[] 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 JavaDoc[0];
191             return;
192         }
193
194         ArrayList JavaDoc<URL JavaDoc> arr = new ArrayList JavaDoc<URL JavaDoc>();
195         for (File JavaDoc 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 JavaDoc e) {
203
204                 }
205                 
206             }
207         }
208         pluginList = arr.toArray(new URL JavaDoc[arr.size()]);
209
210     }
211     
212     public void ensureLoaded() {
213         if (loaded) return;
214         loadPlugins();
215     }
216     /**
217      * Load all plugins. If a setPluginList() has been called, then those
218      * plugins are loaded. Otherwise, the "findbugs.home" property is checked to
219      * determine where FindBugs is installed, and the plugin files are
220      * dynamically loaded from the plugin directory.
221      */

222     void loadPlugins() {
223         if (loaded) throw new IllegalStateException JavaDoc();
224     
225         //If we are running under jaws, just use the loaded plugin
226
if (SystemProperties.getBoolean("findbugs.jaws")) {
227             URL JavaDoc u = DetectorFactoryCollection.class.getResource("/findbugs.xml");
228             // JOptionPane.showMessageDialog(null, "Loading plugin from " + u);
229
URL JavaDoc[] plugins = new URL JavaDoc[1];
230             if (u != null) {
231                 String JavaDoc 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 JavaDoc(path);
236                     
237                 } catch (MalformedURLException JavaDoc e) {
238                     throw new RuntimeException JavaDoc(e);
239                 }
240                 setPluginList(plugins);
241             
242             }
243         }
244         
245         // Load all detector plugins.
246
loaded = true;
247         determinePlugins();
248
249         int numLoaded = 0;
250         for (final URL JavaDoc url : pluginList) {
251             try {
252                 if (FindBugs.DEBUG) System.out.println("Loading plugin: " + url.toString());
253                 PluginLoader pluginLoader =
254                     AccessController.doPrivileged(new PrivilegedExceptionAction JavaDoc<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                 // Register all of the detectors that this plugin contains
267
for (Iterator JavaDoc<DetectorFactory> j = plugin.detectorFactoryIterator();
268                      j.hasNext();) {
269                     DetectorFactory factory = j.next();
270                     registerDetector(factory);
271                 }
272
273                 I18N i18n = I18N.instance();
274
275                 // Register the BugPatterns
276
for (Iterator JavaDoc<BugPattern> j = plugin.bugPatternIterator(); j.hasNext();) {
277                     BugPattern bugPattern = j.next();
278                     i18n.registerBugPattern(bugPattern);
279                 }
280
281                 // Register the BugCodes
282
for (Iterator JavaDoc<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 JavaDoc 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         //System.out.println("Loaded " + numLoaded + " plugins");
301
}
302 }
303
304 // vim:ts=4
305
Popular Tags