KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > business > PluginManagerImpl


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. The ASF licenses this file to You
4  * under the Apache License, Version 2.0 (the "License"); you may not
5  * 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. For additional information regarding
15  * copyright in this work, please see the NOTICE file in the top level
16  * directory of this distribution.
17  */

18
19 package org.apache.roller.business;
20
21 import java.util.Iterator JavaDoc;
22 import java.util.LinkedHashMap JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Map JavaDoc;
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.velocity.VelocityContext;
28 import org.apache.velocity.context.Context;
29 import org.apache.roller.config.RollerConfig;
30 import org.apache.roller.model.WeblogEntryPlugin;
31 import org.apache.roller.model.PluginManager;
32 import org.apache.roller.pojos.WeblogEntryData;
33 import org.apache.roller.pojos.WebsiteData;
34 import org.apache.commons.lang.StringUtils;
35
36
37 /**
38  * Centralized plugin management.
39  */

40 public class PluginManagerImpl implements PluginManager {
41     
42     private static Log mLogger = LogFactory.getLog(PluginManagerImpl.class);
43     
44     // Plugin classes keyed by plugin name
45
static Map JavaDoc mPagePlugins = new LinkedHashMap JavaDoc();
46     
47     
48     /**
49      * Creates a new instance of PluginManagerImpl
50      */

51     public PluginManagerImpl() {
52         loadPagePluginClasses();
53     }
54     
55     
56     public boolean hasPagePlugins() {
57         mLogger.debug("mPluginClasses.size(): " + mPagePlugins.size());
58         return (mPagePlugins != null && mPagePlugins.size() > 0);
59     }
60     
61     
62     /**
63      * Initialize PagePlugins declared in roller.properties.
64      * By using the full class name we also allow for the implementation of
65      * "external" Plugins (maybe even packaged seperately). These classes are
66      * then later instantiated by PageHelper.
67      */

68     private void loadPagePluginClasses() {
69         mLogger.debug("Initializing page plugins");
70         
71         String JavaDoc pluginStr = RollerConfig.getProperty("plugins.page");
72         if (mLogger.isDebugEnabled()) mLogger.debug(pluginStr);
73         if (pluginStr != null) {
74             String JavaDoc[] plugins = StringUtils.stripAll(
75                     StringUtils.split(pluginStr, ",") );
76             for (int i=0; i<plugins.length; i++) {
77                 if (mLogger.isDebugEnabled()) mLogger.debug("try " + plugins[i]);
78                 try {
79                     Class JavaDoc pluginClass = Class.forName(plugins[i]);
80                     if (isPagePlugin(pluginClass)) {
81                         WeblogEntryPlugin plugin = (WeblogEntryPlugin)pluginClass.newInstance();
82                         mPagePlugins.put(plugin.getName(), pluginClass);
83                     } else {
84                         mLogger.warn(pluginClass + " is not a PagePlugin");
85                     }
86                 } catch (ClassNotFoundException JavaDoc e) {
87                     mLogger.error("ClassNotFoundException for " + plugins[i]);
88                 } catch (InstantiationException JavaDoc e) {
89                     mLogger.error("InstantiationException for " + plugins[i]);
90                 } catch (IllegalAccessException JavaDoc e) {
91                     mLogger.error("IllegalAccessException for " + plugins[i]);
92                 }
93             }
94         }
95     }
96     
97     
98     /**
99      * Create and init plugins for processing entries in a specified website.
100      */

101     public Map JavaDoc getWeblogEntryPlugins(WebsiteData website) {
102         Map JavaDoc ret = new LinkedHashMap JavaDoc();
103         Iterator JavaDoc it = getPagePluginClasses().values().iterator();
104         while (it.hasNext()) {
105             try {
106                 Class JavaDoc pluginClass = (Class JavaDoc)it.next();
107                 WeblogEntryPlugin plugin = (WeblogEntryPlugin)pluginClass.newInstance();
108                 plugin.init(website);
109                 ret.put(plugin.getName(), plugin);
110             } catch (Exception JavaDoc e) {
111                 mLogger.error("Unable to init() PagePlugin: ", e);
112             }
113         }
114         return ret;
115     }
116     
117     
118     public String JavaDoc applyWeblogEntryPlugins(Map JavaDoc pagePlugins, WeblogEntryData entry, String JavaDoc str) {
119         String JavaDoc ret = str;
120         WeblogEntryData copy = new WeblogEntryData(entry);
121         List JavaDoc entryPlugins = copy.getPluginsList();
122         if (entryPlugins != null && !entryPlugins.isEmpty()) {
123             Iterator JavaDoc iter = entryPlugins.iterator();
124             while (iter.hasNext()) {
125                 String JavaDoc key = (String JavaDoc)iter.next();
126                 WeblogEntryPlugin pagePlugin = (WeblogEntryPlugin)pagePlugins.get(key);
127                 if (pagePlugin != null) {
128                     ret = pagePlugin.render(entry, ret);
129                 } else {
130                     mLogger.error("ERROR: plugin not found: " + key);
131                 }
132             }
133         }
134         return ret;
135     }
136     
137     
138     private static boolean isPagePlugin(Class JavaDoc pluginClass) {
139         Class JavaDoc[] interfaces = pluginClass.getInterfaces();
140         for (int i=0; i<interfaces.length; i++) {
141             if (interfaces[i].equals(WeblogEntryPlugin.class)) return true;
142         }
143         return false;
144     }
145     
146     
147     private Map JavaDoc getPagePluginClasses() {
148         return mPagePlugins;
149     }
150     
151     
152     public void release() {
153         // no op
154
}
155     
156 }
157
Popular Tags