View Javadoc

1   /*
2    * Copyright 2004 Sun Microsystems, Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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      // PRIVATE - LOADER PART
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); // to preserve the order of definition in the rome.properties files
76              }
77              Iterator i = _pluginsMap.values().iterator();
78              while (i.hasNext()) {
79                  finalPluginsList.add(i.next()); // to remove overriden plugin impls
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 }