001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. The ASF licenses this file to You
004: * under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License. For additional information regarding
015: * copyright in this work, please see the NOTICE file in the top level
016: * directory of this distribution.
017: */
018:
019: package org.apache.roller.business;
020:
021: import java.util.Iterator;
022: import java.util.LinkedHashMap;
023: import java.util.List;
024: import java.util.Map;
025: import org.apache.commons.logging.Log;
026: import org.apache.commons.logging.LogFactory;
027: import org.apache.roller.config.RollerConfig;
028: import org.apache.roller.pojos.WeblogEntryData;
029: import org.apache.roller.pojos.WebsiteData;
030: import org.apache.commons.lang.StringUtils;
031:
032: /**
033: * Plugin management for business layer and more generally applied plugins.
034: */
035: public class PluginManagerImpl implements PluginManager {
036:
037: private static Log log = LogFactory.getLog(PluginManagerImpl.class);
038:
039: // Plugin classes keyed by plugin name
040: static Map mPagePlugins = new LinkedHashMap();
041:
042: /**
043: * Creates a new instance of PluginManagerImpl
044: */
045: public PluginManagerImpl() {
046: loadPagePluginClasses();
047: }
048:
049: public boolean hasPagePlugins() {
050: log.debug("mPluginClasses.size(): " + mPagePlugins.size());
051: return (mPagePlugins != null && mPagePlugins.size() > 0);
052: }
053:
054: /**
055: * Create and init plugins for processing entries in a specified website.
056: */
057: public Map getWeblogEntryPlugins(WebsiteData website) {
058: Map ret = new LinkedHashMap();
059: Iterator it = this .mPagePlugins.values().iterator();
060: while (it.hasNext()) {
061: try {
062: Class pluginClass = (Class) it.next();
063: WeblogEntryPlugin plugin = (WeblogEntryPlugin) pluginClass
064: .newInstance();
065: plugin.init(website);
066: ret.put(plugin.getName(), plugin);
067: } catch (Exception e) {
068: log.error("Unable to init() PagePlugin: ", e);
069: }
070: }
071: return ret;
072: }
073:
074: public String applyWeblogEntryPlugins(Map pagePlugins,
075: WeblogEntryData entry, String str) {
076: String ret = str;
077: WeblogEntryData copy = new WeblogEntryData(entry);
078: List entryPlugins = copy.getPluginsList();
079: if (entryPlugins != null && !entryPlugins.isEmpty()) {
080: Iterator iter = entryPlugins.iterator();
081: while (iter.hasNext()) {
082: String key = (String) iter.next();
083: WeblogEntryPlugin pagePlugin = (WeblogEntryPlugin) pagePlugins
084: .get(key);
085: if (pagePlugin != null) {
086: ret = pagePlugin.render(entry, ret);
087: } else {
088: log.error("ERROR: plugin not found: " + key);
089: }
090: }
091: }
092: return ret;
093: }
094:
095: /**
096: * Initialize PagePlugins declared in roller.properties.
097: * By using the full class name we also allow for the implementation of
098: * "external" Plugins (maybe even packaged seperately). These classes are
099: * then later instantiated by PageHelper.
100: */
101: private void loadPagePluginClasses() {
102: log.debug("Initializing page plugins");
103:
104: String pluginStr = RollerConfig.getProperty("plugins.page");
105: if (log.isDebugEnabled())
106: log.debug(pluginStr);
107: if (pluginStr != null) {
108: String[] plugins = StringUtils.stripAll(StringUtils.split(
109: pluginStr, ","));
110: for (int i = 0; i < plugins.length; i++) {
111: if (log.isDebugEnabled())
112: log.debug("try " + plugins[i]);
113: try {
114: Class pluginClass = Class.forName(plugins[i]);
115: if (isPagePlugin(pluginClass)) {
116: WeblogEntryPlugin plugin = (WeblogEntryPlugin) pluginClass
117: .newInstance();
118: mPagePlugins.put(plugin.getName(), pluginClass);
119: } else {
120: log.warn(pluginClass + " is not a PagePlugin");
121: }
122: } catch (ClassNotFoundException e) {
123: log.error("ClassNotFoundException for "
124: + plugins[i]);
125: } catch (InstantiationException e) {
126: log.error("InstantiationException for "
127: + plugins[i]);
128: } catch (IllegalAccessException e) {
129: log.error("IllegalAccessException for "
130: + plugins[i]);
131: }
132: }
133: }
134: }
135:
136: private static boolean isPagePlugin(Class pluginClass) {
137: Class[] interfaces = pluginClass.getInterfaces();
138: for (int i = 0; i < interfaces.length; i++) {
139: if (interfaces[i].equals(WeblogEntryPlugin.class))
140: return true;
141: }
142: return false;
143: }
144:
145: public void release() {
146: // no op
147: }
148:
149: }
|