1 18 19 package org.apache.roller.business; 20 21 import java.util.Iterator ; 22 import java.util.LinkedHashMap ; 23 import java.util.List ; 24 import java.util.Map ; 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 40 public class PluginManagerImpl implements PluginManager { 41 42 private static Log mLogger = LogFactory.getLog(PluginManagerImpl.class); 43 44 static Map mPagePlugins = new LinkedHashMap (); 46 47 48 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 68 private void loadPagePluginClasses() { 69 mLogger.debug("Initializing page plugins"); 70 71 String pluginStr = RollerConfig.getProperty("plugins.page"); 72 if (mLogger.isDebugEnabled()) mLogger.debug(pluginStr); 73 if (pluginStr != null) { 74 String [] 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 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 e) { 87 mLogger.error("ClassNotFoundException for " + plugins[i]); 88 } catch (InstantiationException e) { 89 mLogger.error("InstantiationException for " + plugins[i]); 90 } catch (IllegalAccessException e) { 91 mLogger.error("IllegalAccessException for " + plugins[i]); 92 } 93 } 94 } 95 } 96 97 98 101 public Map getWeblogEntryPlugins(WebsiteData website) { 102 Map ret = new LinkedHashMap (); 103 Iterator it = getPagePluginClasses().values().iterator(); 104 while (it.hasNext()) { 105 try { 106 Class pluginClass = (Class )it.next(); 107 WeblogEntryPlugin plugin = (WeblogEntryPlugin)pluginClass.newInstance(); 108 plugin.init(website); 109 ret.put(plugin.getName(), plugin); 110 } catch (Exception e) { 111 mLogger.error("Unable to init() PagePlugin: ", e); 112 } 113 } 114 return ret; 115 } 116 117 118 public String applyWeblogEntryPlugins(Map pagePlugins, WeblogEntryData entry, String str) { 119 String ret = str; 120 WeblogEntryData copy = new WeblogEntryData(entry); 121 List entryPlugins = copy.getPluginsList(); 122 if (entryPlugins != null && !entryPlugins.isEmpty()) { 123 Iterator iter = entryPlugins.iterator(); 124 while (iter.hasNext()) { 125 String key = (String )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 pluginClass) { 139 Class [] 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 getPagePluginClasses() { 148 return mPagePlugins; 149 } 150 151 152 public void release() { 153 } 155 156 } 157 | Popular Tags |