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