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.ArrayList;
20 import java.util.Collections;
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.StringTokenizer;
25 import java.util.Properties;
26 import java.util.Enumeration;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.net.URL;
30
31 /***
32 * <p>
33 * @author Alejandro Abdelnur
34 *
35 */
36 public abstract class PluginManager {
37 private Map _pluginsMap;
38 private List _pluginsList;
39 private List _keys;
40
41 /***
42 * Creates a PluginManager
43 * <p>
44 * @param propertyKey property key defining the plugins classes
45 *
46 */
47 protected PluginManager(String propertyKey) {
48 loadPlugins(propertyKey);
49 _pluginsMap = Collections.unmodifiableMap(_pluginsMap);
50 _pluginsList = Collections.unmodifiableList(_pluginsList);
51 _keys = Collections.unmodifiableList(new ArrayList(_pluginsMap.keySet()));
52 }
53
54 protected abstract String getKey(Object obj);
55
56
57 protected List getKeys() {
58 return _keys;
59 }
60
61 protected List getPlugins() {
62 return _pluginsList;
63 }
64
65 protected Map getPluginMap() {
66 return _pluginsMap;
67 }
68
69 protected Object getPlugin(String key) {
70 return _pluginsMap.get(key);
71 }
72
73
74
75 private void loadPlugins(String key) {
76 _pluginsList = new ArrayList();
77 _pluginsMap = new HashMap();
78 try {
79 Class[] classes = getClasses(key);
80 for (int i=0;i<classes.length;i++) {
81 Object obj = classes[i].newInstance();
82 _pluginsList.add(obj);
83 _pluginsMap.put(getKey(obj),obj);
84 }
85 }
86 catch (Exception ex) {
87 throw new RuntimeException("could not instanciate plugin ",ex);
88 }
89 }
90
91 private static final String MASTER_PLUGIN_FILE = "com/sun/syndication/rome.properties";
92 private static final String EXTRA_PLUGIN_FILE = "rome.properties";
93 private static final Properties[] PLUGINS_DEFS;
94
95 static {
96 PLUGINS_DEFS = loadAllProperties();
97 }
98
99 private static Properties[] loadAllProperties() {
100 ClassLoader classLoader = PluginManager.class.getClassLoader();
101 List allProps = new ArrayList();
102
103 try {
104
105 InputStream is = classLoader.getResourceAsStream(MASTER_PLUGIN_FILE);
106 allProps.add(loadProperties(is));
107 }
108 catch (IOException ex) {
109 throw new RuntimeException("could not load Rome plugins file",ex);
110 }
111
112
113 try {
114 Enumeration urls = classLoader.getResources(EXTRA_PLUGIN_FILE);
115 while (urls.hasMoreElements()) {
116 URL url = (URL) urls.nextElement();
117 try {
118 InputStream is = url.openStream();
119 allProps.add(loadProperties(is));
120 }
121 catch (IOException ex) {
122 throw new RuntimeException("could not load Rome extensions plugins file ["+url.toString()+"] ",ex);
123 }
124 }
125 }
126 catch (IOException ex) {
127 throw new RuntimeException("could not load fetch resources for Rome extensions plugins");
128 }
129
130 Properties[] array = new Properties[allProps.size()];
131 allProps.toArray(array);
132 return array;
133 }
134
135 private static Properties loadProperties(InputStream is) throws IOException {
136 Properties props = new Properties();
137 props.load(is);
138 return props;
139 }
140
141 private static List parseAndLoadClasses(String classNames) throws ClassNotFoundException {
142 ClassLoader classLoader = PluginManager.class.getClassLoader();
143 List classes = new ArrayList();
144 StringTokenizer st = new StringTokenizer(classNames,", ");
145 while (st.hasMoreTokens()) {
146 String className = st.nextToken();
147 Class mClass = classLoader.loadClass(className);
148 classes.add(mClass);
149 }
150 return classes;
151 }
152
153 /***
154 * Loads and returns the classes defined in the properties files.
155 * <p>
156 * @param pluginKey property key defining the plugin classes.
157 * @return array containing the classes defined in the properties files.
158 * @throws java.lang.ClassNotFoundException thrown if one of the classes defined in the properties file cannot be loaded
159 * and hard failure is ON.
160 *
161 */
162 private static Class[] getClasses(String pluginKey) throws ClassNotFoundException {
163 List classes = new ArrayList();
164 for (int i=0;i<PLUGINS_DEFS.length;i++) {
165 String classNames = PLUGINS_DEFS[i].getProperty(pluginKey);
166 if (classNames!=null) {
167 classes.addAll(parseAndLoadClasses(classNames));
168 }
169 }
170 Class[] array = new Class[classes.size()];
171 classes.toArray(array);
172 return array;
173 }
174
175
176
177 }