Clover Coverage Report - Pebble 2.5-SNAPSHOT
Coverage timestamp: Sat Jun 12 2010 09:39:29 EST
../../../../img/srcFileCovDistChart6.png 36% of files have more coverage
76   181   24   9,5
18   150   0,32   8
8     3  
1    
This report was generated with an evaluation server license. Purchase Clover or configure your license.
 
  PluginLocator       Line # 20 76 0% 24 45 55,9% 0.5588235
 
  (18)
 
1    package net.sourceforge.pebble.plugins;
2   
3    import net.sourceforge.pebble.domain.Blog;
4    import org.apache.commons.logging.Log;
5    import org.apache.commons.logging.LogFactory;
6    import org.jdom.Document;
7    import org.jdom.Element;
8    import org.jdom.JDOMException;
9    import org.jdom.input.SAXBuilder;
10   
11    import java.io.IOException;
12    import java.net.URL;
13    import java.util.*;
14   
15    /**
16    * Locates plugins
17    *
18    * @author James Roper
19    */
 
20    public class PluginLocator {
21    private static final Log log = LogFactory.getLog(PluginLocator.class);
22    private static final Map<String, PluginConfigType> PLUGIN_CONFIG_TYPES;
 
23  2 toggle static {
24  2 PLUGIN_CONFIG_TYPES = new HashMap<String, PluginConfigType>();
25  2 PLUGIN_CONFIG_TYPES.put("string", PlainTextPluginConfigType.INSTANCE);
26  2 PLUGIN_CONFIG_TYPES.put("textarea", TextAreaPluginConfigType.INSTANCE);
27  2 PLUGIN_CONFIG_TYPES.put("checkbox", CheckboxPluginConfigType.INSTANCE);
28  2 PLUGIN_CONFIG_TYPES.put("password", PasswordPluginConfigType.INSTANCE);
29    }
30   
31    private static AvailablePlugins availablePlugins;
32   
33    /**
34    * Locates all the plugins installed in Pebble
35    *
36    * @return A map of plugin types to lists of plugin class names
37    */
 
38  0 toggle public static synchronized AvailablePlugins locateAvailablePlugins() {
39  0 if (availablePlugins == null) {
40  0 Map<String, List<Plugin>> plugins = new HashMap<String, List<Plugin>>();
41  0 try {
42  0 Enumeration<URL> resources = Thread.currentThread().getContextClassLoader().getResources("/pebble-plugins.xml");
43  0 while (resources.hasMoreElements()) {
44  0 installPlugins(plugins, resources.nextElement());
45    }
46    }
47    catch (IOException ioe) {
48  0 log.error("Error occured while scanning context classloader for plugins", ioe);
49    }
50    // Sort the plugins
51  0 for (List<Plugin> pluginsList : plugins.values())
52    {
53  0 Collections.sort(pluginsList, new Comparator<Plugin>() {
 
54  0 toggle public int compare(Plugin o1, Plugin o2) {
55  0 return o1.getWeight() - o2.getWeight();
56    }
57    });
58    }
59  0 availablePlugins = new AvailablePlugins(plugins);
60    }
61  0 return availablePlugins;
62    }
63   
 
64  0 toggle public static AvailablePlugins getAvailablePluginsSortedForBlog(Blog blog) {
65  0 Map<String, List<Plugin>> plugins = locateAvailablePlugins().copyMap();
66    // Sort the decorators
67  0 plugins.put(AvailablePlugins.CONTENT_DECORATOR, sortPlugins(plugins.get(AvailablePlugins.CONTENT_DECORATOR),
68    blog.getContentDecorators()));
69  0 return new AvailablePlugins(plugins);
70    }
71   
 
72  0 toggle private static List<Plugin> sortPlugins(List<Plugin> plugins, final List<String> installedPlugins) {
73  0 Collections.sort(plugins, new Comparator<Plugin>() {
 
74  0 toggle public int compare(Plugin plugin1, Plugin plugin2) {
75    // This comparator ensures installed plugins are at the top, in the order specified by the user,
76    // and that not installed plugins are sorted by their weight.
77  0 int index1 = installedPlugins.indexOf(plugin1.getPluginClass());
78  0 int index2 = installedPlugins.indexOf(plugin2.getPluginClass());
79  0 if (index1 >= 0 && index2 >= 0)
80    {
81  0 return index1 - index2;
82    }
83  0 if (index1 < 0 && index2 < 0)
84    {
85  0 return plugin1.getWeight() - plugin2.getWeight();
86    }
87  0 if (index1 >= 0)
88    {
89  0 return -1;
90    }
91  0 return 1;
92    }
93    });
94  0 return plugins;
95    }
96   
97    /**
98    * Install the plugins from the given resource
99    * @param plugins The plugins to install
100    * @param resource The resource to install them from
101    */
 
102  18 toggle static void installPlugins(Map<String, List<Plugin>> plugins, URL resource) {
103  18 SAXBuilder saxBuilder = new SAXBuilder();
104  18 try {
105  18 Document document = saxBuilder.build(resource);
106  18 Element root = document.getRootElement();
107  18 for (Element element : (Iterable<Element>) root.getChildren())
108    {
109  72 String type = element.getName();
110  72 String name = element.getAttributeValue("name");
111  72 Element descriptionElement = element.getChild("description");
112  72 String description = null;
113  72 if (descriptionElement != null)
114    {
115  18 description = descriptionElement.getText();
116    }
117  72 String className = element.getAttributeValue("class");
118  72 String weightStr = element.getAttributeValue("weight");
119  72 int weight = 100;
120  72 if (weightStr != null)
121    {
122  18 try {
123  18 weight = Integer.parseInt(weightStr);
124    } catch (NumberFormatException e) {
125  0 log.error("Invalid weight for plugin " + name + ": " + weightStr);
126    }
127    }
128    // Try and load the class - for validation
129  72 try {
130  72 Thread.currentThread().getContextClassLoader().loadClass(className);
131    // Successful, add it
132  72 log.debug("Installing plugin '" + name + "' of type " + type + " with class " + className);
133  72 List<Plugin> list = plugins.get(type);
134  72 if (list == null)
135    {
136  54 list = new ArrayList<Plugin>();
137  54 plugins.put(type, list);
138    }
139  72 Collection<PluginConfig> pluginConfig = parsePluginConfig(element);
140  72 list.add(new Plugin(name, description, className, weight, pluginConfig));
141    } catch (ClassNotFoundException e) {
142  0 log.error("Plugin class " + className + " of type " + type + " from descriptor " + resource +
143    " could not be found.");
144    }
145    }
146    } catch (JDOMException e) {
147  0 log.error("Error parsing plugin descriptor at " + resource, e);
148    } catch (IOException e) {
149  0 log.error("Error reading plugin descriptor at " + resource, e);
150    }
151    }
152   
 
153  72 toggle private static Collection<PluginConfig> parsePluginConfig(Element element)
154    {
155  72 Collection<PluginConfig> results = new ArrayList<PluginConfig>();
156  72 for (Element configElement : (Iterable<Element>) element.getChildren("config"))
157    {
158  108 String key = configElement.getAttributeValue("key");
159  108 String name = configElement.getAttributeValue("name");
160  108 boolean required = Boolean.parseBoolean(configElement.getAttributeValue("required"));
161  108 String type = configElement.getAttributeValue("type");
162  108 PluginConfigType pluginConfigType;
163  108 if (type == null)
164    {
165  54 pluginConfigType = PlainTextPluginConfigType.INSTANCE;
166    }
167    else
168    {
169  54 pluginConfigType = PLUGIN_CONFIG_TYPES.get(type);
170    }
171    // todo custom class instantiation
172  108 Properties properties = new Properties();
173  108 for (Element property : (Iterable<Element>) configElement.getChildren())
174    {
175  18 properties.setProperty(property.getName(), property.getText());
176    }
177  108 results.add(new PluginConfig(key, name, pluginConfigType, required, properties));
178    }
179  72 return results;
180    }
181    }