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 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      // PRIVATE - LOADER PART
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); // to preserve the order of definition in the rome.properties files
100             }
101             Iterator i = _pluginsMap.values().iterator();
102             while (i.hasNext()) {
103                 finalPluginsList.add(i.next()); // to remove overriden plugin impls
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 }