| 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 |
|
|
| 17 |
|
|
| 18 |
|
@author |
| 19 |
|
|
|
|
|
| 55,9% |
Uncovered Elements: 45 (102) |
Complexity: 24 |
Complexity Density: 0,32 |
|
| 20 |
|
public class PluginLocator { |
| 21 |
|
private static final Log log = LogFactory.getLog(PluginLocator.class); |
| 22 |
|
private static final Map<String, PluginConfigType> PLUGIN_CONFIG_TYPES; |
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0,2 |
|
| 23 |
2
|
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 |
|
|
| 35 |
|
|
| 36 |
|
@return |
| 37 |
|
|
|
|
|
| 0% |
Uncovered Elements: 15 (15) |
Complexity: 4 |
Complexity Density: 0,36 |
|
| 38 |
0
|
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 |
|
|
| 51 |
0
|
for (List<Plugin> pluginsList : plugins.values()) |
| 52 |
|
{ |
| 53 |
0
|
Collections.sort(pluginsList, new Comparator<Plugin>() { |
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 54 |
0
|
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 |
|
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0,33 |
|
| 64 |
0
|
public static AvailablePlugins getAvailablePluginsSortedForBlog(Blog blog) {... |
| 65 |
0
|
Map<String, List<Plugin>> plugins = locateAvailablePlugins().copyMap(); |
| 66 |
|
|
| 67 |
0
|
plugins.put(AvailablePlugins.CONTENT_DECORATOR, sortPlugins(plugins.get(AvailablePlugins.CONTENT_DECORATOR), |
| 68 |
|
blog.getContentDecorators())); |
| 69 |
0
|
return new AvailablePlugins(plugins); |
| 70 |
|
} |
| 71 |
|
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0,5 |
|
| 72 |
0
|
private static List<Plugin> sortPlugins(List<Plugin> plugins, final List<String> installedPlugins) {... |
| 73 |
0
|
Collections.sort(plugins, new Comparator<Plugin>() { |
|
|
|
| 0% |
Uncovered Elements: 15 (15) |
Complexity: 6 |
Complexity Density: 0,67 |
|
| 74 |
0
|
public int compare(Plugin plugin1, Plugin plugin2) {... |
| 75 |
|
|
| 76 |
|
|
| 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 |
|
|
| 99 |
|
@param |
| 100 |
|
@param |
| 101 |
|
|
|
|
|
| 88,9% |
Uncovered Elements: 4 (36) |
Complexity: 8 |
Complexity Density: 0,27 |
|
| 102 |
18
|
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 |
|
|
| 129 |
72
|
try { |
| 130 |
72
|
Thread.currentThread().getContextClassLoader().loadClass(className); |
| 131 |
|
|
| 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 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (17) |
Complexity: 2 |
Complexity Density: 0,13 |
|
| 153 |
72
|
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 |
|
|
| 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 |
|
} |