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 com.sun.syndication.io.DelegatingModuleGenerator;
20 import com.sun.syndication.io.DelegatingModuleParser;
21 import com.sun.syndication.io.WireFeedGenerator;
22 import com.sun.syndication.io.WireFeedParser;
23
24 import java.util.*;
25
26 /***
27 * <p>
28 * @author Alejandro Abdelnur
29 *
30 */
31 public abstract class PluginManager {
32 private String[] _propertyValues;
33 private Map _pluginsMap;
34 private List _pluginsList;
35 private List _keys;
36 private WireFeedParser _parentParser;
37 private WireFeedGenerator _parentGenerator;
38
39 /***
40 * Creates a PluginManager
41 * <p>
42 * @param propertyKey property key defining the plugins classes
43 *
44 */
45 protected PluginManager(String propertyKey) {
46 this(propertyKey, null, null);
47 }
48
49 protected PluginManager(String propertyKey, WireFeedParser parentParser,
50 WireFeedGenerator parentGenerator)
51 {
52 _parentParser = parentParser;
53 _parentGenerator = parentGenerator;
54 _propertyValues = PropertiesLoader.getPropertiesLoader().getTokenizedProperty(propertyKey,", ");
55 loadPlugins();
56 _pluginsMap = Collections.unmodifiableMap(_pluginsMap);
57 _pluginsList = Collections.unmodifiableList(_pluginsList);
58 _keys = Collections.unmodifiableList(new ArrayList(_pluginsMap.keySet()));
59 }
60
61 protected abstract String getKey(Object obj);
62
63 protected List getKeys() {
64 return _keys;
65 }
66
67 protected List getPlugins() {
68 return _pluginsList;
69 }
70
71 protected Map getPluginMap() {
72 return _pluginsMap;
73 }
74
75 protected Object getPlugin(String key) {
76 return _pluginsMap.get(key);
77 }
78
79
80
81 private void loadPlugins() {
82 List finalPluginsList = new ArrayList();
83 _pluginsList = new ArrayList();
84 _pluginsMap = new HashMap();
85 String className = null;
86 try {
87 Class[] classes = getClasses();
88 for (int i=0;i<classes.length;i++) {
89 className = classes[i].getName();
90 Object plugin = classes[i].newInstance();
91 if (plugin instanceof DelegatingModuleParser) {
92 ((DelegatingModuleParser) plugin).setFeedParser(_parentParser);
93 }
94 if (plugin instanceof DelegatingModuleGenerator) {
95 ((DelegatingModuleGenerator) plugin).setFeedGenerator(_parentGenerator);
96 }
97
98 _pluginsMap.put(getKey(plugin), plugin);
99 _pluginsList.add(plugin);
100 }
101 Iterator i = _pluginsMap.values().iterator();
102 while (i.hasNext()) {
103 finalPluginsList.add(i.next());
104 }
105
106 i = _pluginsList.iterator();
107 while (i.hasNext()) {
108 Object plugin = i.next();
109 if (!finalPluginsList.contains(plugin)) {
110 i.remove();
111 }
112 }
113 }
114 catch (Exception ex) {
115 throw new RuntimeException("could not instantiate plugin "+className,ex);
116 }catch (ExceptionInInitializerError er) {
117 throw new RuntimeException("could not instantiate plugin "+className,er);
118 }
119 }
120
121 /***
122 * Loads and returns the classes defined in the properties files.
123 * <p>
124 * @return array containing the classes defined in the properties files.
125 * @throws java.lang.ClassNotFoundException thrown if one of the classes defined in the properties file cannot be loaded
126 * and hard failure is ON.
127 *
128 */
129 private Class[] getClasses() throws ClassNotFoundException {
130 ClassLoader classLoader = PluginManager.class.getClassLoader();
131 List classes = new ArrayList();
132 for (int i = 0; i <_propertyValues.length; i++) {
133 Class mClass = Class.forName(_propertyValues[i], true, classLoader);
134 classes.add(mClass);
135 }
136 Class[] array = new Class[classes.size()];
137 classes.toArray(array);
138 return array;
139 }
140
141 }