File Source: PluginManagerImpl.java

     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.weblogger.business.plugins;
    20  
    21  import java.util.ArrayList;
    22  import org.apache.roller.weblogger.business.plugins.entry.WeblogEntryPlugin;
    23  import java.util.Iterator;
    24  import java.util.LinkedHashMap;
    25  import java.util.List;
    26  import java.util.Map;
    27  import org.apache.commons.logging.Log;
    28  import org.apache.commons.logging.LogFactory;
    29  import org.apache.roller.weblogger.config.WebloggerConfig;
    30  import org.apache.roller.weblogger.pojos.WeblogEntry;
    31  import org.apache.roller.weblogger.pojos.Weblog;
    32  import org.apache.commons.lang.StringUtils;
    33  import org.apache.roller.weblogger.business.plugins.comment.WeblogEntryCommentPlugin;
    34  import org.apache.roller.weblogger.pojos.WeblogEntryComment;
    35  
    36  
    37  /**
    38   * Plugin management for business layer and more generally applied plugins.
    39   */
    40  public class PluginManagerImpl implements PluginManager {
    41      
             /* 
    P/P       *  Method: org.apache.roller.weblogger.business.plugins.PluginManagerImpl__static_init
              * 
              *  Postconditions:
              *    init'ed(log)
              *    mPagePlugins == &new LinkedHashMap(PluginManagerImpl__static_init#1)
              *    new LinkedHashMap(PluginManagerImpl__static_init#1) num objects == 1
              */
    42      private static Log log = LogFactory.getLog(PluginManagerImpl.class);
    43      
    44      // Plugin classes keyed by plugin name
    45      static Map mPagePlugins = new LinkedHashMap();
    46      
    47      // Comment plugins
    48      private List<WeblogEntryCommentPlugin> commentPlugins = new ArrayList();
    49      
    50      
    51      /**
    52       * Creates a new instance of PluginManagerImpl
    53       */
             /* 
    P/P       *  Method: void org.apache.roller.weblogger.business.plugins.PluginManagerImpl()
              * 
              *  Preconditions:
              *    log != null
              *    org/apache/roller/weblogger/config/WebloggerConfig.config != null
              *    org/apache/roller/weblogger/config/WebloggerConfig.log != null
              *    (soft) mPagePlugins != null
              * 
              *  Postconditions:
              *    this.commentPlugins == &new ArrayList(PluginManagerImpl#1)
              *    new ArrayList(PluginManagerImpl#1) num objects == 1
              */
    54      public PluginManagerImpl() {
    55          // load weblog entry plugins
    56          loadPagePluginClasses();
    57          
    58          // load weblog entry comment plugins
    59          loadCommentPlugins();
    60      }
    61      
    62      
    63      public boolean hasPagePlugins() {
                 /* 
    P/P           *  Method: bool hasPagePlugins()
                  * 
                  *  Preconditions:
                  *    log != null
                  *    mPagePlugins != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
    64          log.debug("mPluginClasses.size(): " + mPagePlugins.size());
    65          return (mPagePlugins != null && mPagePlugins.size() > 0);
    66      }
    67      
    68      
    69      /**
    70       * Create and init plugins for processing entries in a specified website.
    71       */
    72      public Map getWeblogEntryPlugins(Weblog website) {
                 /* 
    P/P           *  Method: Map getWeblogEntryPlugins(Weblog)
                  * 
                  *  Preconditions:
                  *    mPagePlugins != null
                  *    (soft) log != null
                  * 
                  *  Presumptions:
                  *    java.util.Iterator:next(...)@77 != null
                  *    java.util.Map:values(...)@74 != null
                  * 
                  *  Postconditions:
                  *    return_value == &new LinkedHashMap(getWeblogEntryPlugins#1)
                  *    new LinkedHashMap(getWeblogEntryPlugins#1) num objects == 1
                  * 
                  *  Test Vectors:
                  *    java.util.Iterator:hasNext(...)@75: {0}, {1}
                  */
    73          Map ret = new LinkedHashMap();
    74          Iterator it = this.mPagePlugins.values().iterator();
    75          while (it.hasNext()) {
    76              try {
    77                  Class pluginClass = (Class)it.next();
    78                  WeblogEntryPlugin plugin = (WeblogEntryPlugin)pluginClass.newInstance();
    79                  plugin.init(website);
    80                  ret.put(plugin.getName(), plugin);
    81              } catch (Exception e) {
    82                  log.error("Unable to init() PagePlugin: ", e);
    83              }
    84          }
    85          return ret;
    86      }
    87      
    88      
    89      public String applyWeblogEntryPlugins(Map pagePlugins,WeblogEntry entry, String str) {
                 /* 
    P/P           *  Method: String applyWeblogEntryPlugins(Map, WeblogEntry, String)
                  * 
                  *  Preconditions:
                  *    (soft) log != null
                  *    (soft) pagePlugins != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  * 
                  *  Test Vectors:
                  *    java.util.Iterator:hasNext(...)@95: {0}, {1}
                  *    java.util.List:isEmpty(...)@93: {1}, {0}
                  *    java.util.Map:get(...)@97: Addr_Set{null}, Inverse{null}
                  *    org.apache.roller.weblogger.pojos.WeblogEntry:getPluginsList(...)@92: Addr_Set{null}, Inverse{null}
                  */
    90          String ret = str;
    91          WeblogEntry copy = new WeblogEntry(entry);
    92          List entryPlugins = copy.getPluginsList();
    93          if (entryPlugins != null && !entryPlugins.isEmpty()) {
    94              Iterator iter = entryPlugins.iterator();
    95              while (iter.hasNext()) {
    96                  String key = (String)iter.next();
    97                  WeblogEntryPlugin pagePlugin = (WeblogEntryPlugin)pagePlugins.get(key);
    98                  if (pagePlugin != null) {
    99                      ret = pagePlugin.render(entry, ret);
   100                  } else {
   101                      log.error("ERROR: plugin not found: " + key);
   102                  }
   103              }
   104          }
   105          return ret;
   106      }
   107      
   108      
   109      /**
   110       * @inheritDoc
   111       */
   112      public List<WeblogEntryCommentPlugin> getCommentPlugins() {
                 /* 
    P/P           *  Method: List getCommentPlugins()
                  * 
                  *  Preconditions:
                  *    init'ed(this.commentPlugins)
                  * 
                  *  Postconditions:
                  *    return_value == this.commentPlugins
                  *    init'ed(return_value)
                  */
   113          return commentPlugins;
   114      }
   115      
   116      
   117      /**
   118       * @inheritDoc
   119       */
   120      public String applyCommentPlugins(WeblogEntryComment comment, String text) {
   121          
                 /* 
    P/P           *  Method: String applyCommentPlugins(WeblogEntryComment, String)
                  * 
                  *  Preconditions:
                  *    comment != null
                  *    text != null
                  *    this.commentPlugins != null
                  *    (soft) log != null
                  * 
                  *  Presumptions:
                  *    java.util.Iterator:next(...)@129 != null
                  *    org.apache.roller.weblogger.pojos.WeblogEntryComment:getPlugins(...)@130 != null
                  * 
                  *  Postconditions:
                  *    init'ed(java.lang.StringBuffer:toString(...)._tainted)
                  *    init'ed(java.lang.StringBuilder:toString(...)._tainted)
                  *    init'ed(return_value)
                  * 
                  *  Test Vectors:
                  *    java.lang.String:indexOf(...)@130: {-1}, {-231..-2, 0..232-1}
                  *    java.util.Iterator:hasNext(...)@129: {0}, {1}
                  *    java.util.List:size(...)@128: {-231..0}, {1..232-1}
                  *    org.apache.roller.weblogger.pojos.WeblogEntryComment:getPlugins(...)@130: Addr_Set{null}, Inverse{null}
                  */
   122          if(comment == null || text == null) {
   123              throw new IllegalArgumentException("comment cannot be null");
   124          }
   125          
   126          String content = text;
   127          
   128          if (commentPlugins.size() > 0) {
   129              for( WeblogEntryCommentPlugin plugin : commentPlugins ) {
   130                  if(comment.getPlugins() != null &&
   131                          comment.getPlugins().indexOf(plugin.getId()) != -1) {
   132                      log.debug("Invoking comment plugin "+plugin.getId());
   133                      content = plugin.render(comment, content);
   134                  }
   135              }
   136          }
   137          
   138          return content;
   139      }
   140      
   141      
   142      /**
   143       * Initialize PagePlugins declared in roller.properties.
   144       * By using the full class name we also allow for the implementation of
   145       * "external" Plugins (maybe even packaged seperately). These classes are
   146       * then later instantiated by PageHelper.
   147       */
   148      private void loadPagePluginClasses() {
                 /* 
    P/P           *  Method: void loadPagePluginClasses()
                  * 
                  *  Preconditions:
                  *    log != null
                  *    org/apache/roller/weblogger/config/WebloggerConfig.config != null
                  *    org/apache/roller/weblogger/config/WebloggerConfig.log != null
                  *    (soft) mPagePlugins != null
                  * 
                  *  Presumptions:
                  *    java.lang.Class:forName(...)@159 != null
                  *    org.apache.commons.lang.StringUtils:stripAll(...)@154 != null
                  *    plugins.length@154 <= 232-1
                  * 
                  *  Test Vectors:
                  *    org.apache.commons.logging.Log:isDebugEnabled(...)@152: {0}, {1}
                  *    org.apache.commons.logging.Log:isDebugEnabled(...)@157: {0}, {1}
                  *    plugins.length@154: {1..232-1}, {0}
                  */
   149          log.debug("Initializing page plugins");
   150          
   151          String pluginStr = WebloggerConfig.getProperty("plugins.page");
   152          if (log.isDebugEnabled()) log.debug(pluginStr);
   153          if (pluginStr != null) {
   154              String[] plugins = StringUtils.stripAll(
   155                      StringUtils.split(pluginStr, ",") );
   156              for (int i=0; i<plugins.length; i++) {
   157                  if (log.isDebugEnabled()) log.debug("try " + plugins[i]);
   158                  try {
   159                      Class pluginClass = Class.forName(plugins[i]);
   160                      if (isPagePlugin(pluginClass)) {
   161                          WeblogEntryPlugin plugin = (WeblogEntryPlugin)pluginClass.newInstance();
   162                          mPagePlugins.put(plugin.getName(), pluginClass);
   163                      } else {
   164                          log.warn(pluginClass + " is not a PagePlugin");
   165                      }
   166                  } catch (ClassNotFoundException e) {
   167                      log.error("ClassNotFoundException for " + plugins[i]);
   168                  } catch (InstantiationException e) {
   169                      log.error("InstantiationException for " + plugins[i]);
   170                  } catch (IllegalAccessException e) {
   171                      log.error("IllegalAccessException for " + plugins[i]);
   172                  }
   173              }
   174          }
   175      }
   176      
   177      
   178      /**
   179       * Initialize all comment plugins defined in weblogger config.
   180       */
   181      private void loadCommentPlugins() {
   182          
                 /* 
    P/P           *  Method: void loadCommentPlugins()
                  * 
                  *  Preconditions:
                  *    log != null
                  *    org/apache/roller/weblogger/config/WebloggerConfig.config != null
                  *    org/apache/roller/weblogger/config/WebloggerConfig.log != null
                  *    (soft) this.commentPlugins != null
                  * 
                  *  Presumptions:
                  *    java.lang.Class:forName(...)@192 != null
                  *    org.apache.commons.lang.StringUtils:stripAll(...)@187 != null
                  *    plugins.length@187 <= 232-1
                  */
   183          log.debug("Initializing comment plugins");
   184          
   185          String pluginStr = WebloggerConfig.getProperty("comment.formatter.classnames");
   186          if (pluginStr != null) {
   187              String[] plugins = StringUtils.stripAll(StringUtils.split(pluginStr, ","));
   188              for (int i=0; i < plugins.length; i++) {
   189                  log.debug("trying " + plugins[i]);
   190                  
   191                  try {
   192                      Class pluginClass = Class.forName(plugins[i]);
   193                      WeblogEntryCommentPlugin plugin = 
   194                              (WeblogEntryCommentPlugin) pluginClass.newInstance();
   195                      
   196                      // make sure and maintain ordering
   197                      commentPlugins.add(i, plugin);
   198                      
   199                      log.debug("Configured comment plugin: "+plugins[i]);
   200                      
   201                  } catch (ClassCastException e) {
   202                      log.error("ClassCastException for " + plugins[i]);
   203                  } catch (ClassNotFoundException e) {
   204                      log.error("ClassNotFoundException for " + plugins[i]);
   205                  } catch (InstantiationException e) {
   206                      log.error("InstantiationException for " + plugins[i]);
   207                  } catch (IllegalAccessException e) {
   208                      log.error("IllegalAccessException for " + plugins[i]);
   209                  }
   210              }
   211          }
   212          
   213      }
   214      
   215      
   216      private static boolean isPagePlugin(Class pluginClass) {
                 /* 
    P/P           *  Method: bool isPagePlugin(Class)
                  * 
                  *  Preconditions:
                  *    pluginClass != null
                  * 
                  *  Presumptions:
                  *    interfaces.length@217 <= 232-1
                  *    java.lang.Class:getInterfaces(...)@217 != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  * 
                  *  Test Vectors:
                  *    java.lang.Object:equals(...)@219: {0}, {1}
                  */
   217          Class[] interfaces = pluginClass.getInterfaces();
   218          for (int i=0; i<interfaces.length; i++) {
+  219              if (interfaces[i].equals(WeblogEntryPlugin.class)) return true;
   220          }
   221          return false;
   222      }
   223      
   224      
   225      public void release() {
   226          // no op
             /* 
    P/P       *  Method: void release()
              */
   227      }
   228      
   229  }








SofCheck Inspector Build Version : 2.18479
PluginManagerImpl.java 2009-Jan-02 14:25:40
PluginManagerImpl.class 2009-Sep-04 03:12:31