1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.sun.syndication.io.impl;
18
19 import java.util.*;
20
21 /***
22 * <p>
23 * @author Alejandro Abdelnur
24 *
25 */
26 public abstract class PluginManager {
27 private String[] _propertyValues;
28 private Map _pluginsMap;
29 private List _pluginsList;
30 private List _keys;
31
32 /***
33 * Creates a PluginManager
34 * <p>
35 * @param propertyKey property key defining the plugins classes
36 *
37 */
38 protected PluginManager(String propertyKey) {
39 _propertyValues = PropertiesLoader.getPropertiesLoader().getTokenizedProperty(propertyKey,", ");
40 loadPlugins();
41 _pluginsMap = Collections.unmodifiableMap(_pluginsMap);
42 _pluginsList = Collections.unmodifiableList(_pluginsList);
43 _keys = Collections.unmodifiableList(new ArrayList(_pluginsMap.keySet()));
44 }
45
46 protected abstract String getKey(Object obj);
47
48 protected List getKeys() {
49 return _keys;
50 }
51
52 protected List getPlugins() {
53 return _pluginsList;
54 }
55
56 protected Map getPluginMap() {
57 return _pluginsMap;
58 }
59
60 protected Object getPlugin(String key) {
61 return _pluginsMap.get(key);
62 }
63
64
65
66 private void loadPlugins() {
67 List finalPluginsList = new ArrayList();
68 _pluginsList = new ArrayList();
69 _pluginsMap = new HashMap();
70 try {
71 Class[] classes = getClasses();
72 for (int i=0;i<classes.length;i++) {
73 Object obj = classes[i].newInstance();
74 _pluginsMap.put(getKey(obj),obj);
75 _pluginsList.add(obj);
76 }
77 Iterator i = _pluginsMap.values().iterator();
78 while (i.hasNext()) {
79 finalPluginsList.add(i.next());
80 }
81
82 i = _pluginsList.iterator();
83 while (i.hasNext()) {
84 Object plugin = i.next();
85 if (!finalPluginsList.contains(plugin)) {
86 i.remove();
87 }
88 }
89 }
90 catch (Exception ex) {
91 throw new RuntimeException("could not instanciate plugin ",ex);
92 }
93 }
94
95 /***
96 * Loads and returns the classes defined in the properties files.
97 * <p>
98 * @return array containing the classes defined in the properties files.
99 * @throws java.lang.ClassNotFoundException thrown if one of the classes defined in the properties file cannot be loaded
100 * and hard failure is ON.
101 *
102 */
103 private Class[] getClasses() throws ClassNotFoundException {
104 ClassLoader classLoader = PluginManager.class.getClassLoader();
105 List classes = new ArrayList();
106 for (int i=0;i<_propertyValues.length;i++) {
107 Class mClass = classLoader.loadClass(_propertyValues[i]);
108 classes.add(mClass);
109 }
110 Class[] array = new Class[classes.size()];
111 classes.toArray(array);
112 return array;
113 }
114
115 }