View Javadoc

1   package net.sourceforge.blogentis.plugins;
2   
3   //-----------------------------------------------------------------------
4   //Blogentis - a blog publishing platform.
5   //Copyright (C) 2004 Tassos Bassoukos <abassouk@gmail.com>
6   //
7   //This library is free software; you can redistribute it and/or
8   //modify it under the terms of the GNU Lesser General Public
9   //License as published by the Free Software Foundation; either
10  //version 2.1 of the License, or (at your option) any later version.
11  //
12  //This library is distributed in the hope that it will be useful,
13  //but WITHOUT ANY WARRANTY; without even the implied warranty of
14  //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  //Lesser General Public License for more details.
16  //
17  //You should have received a copy of the GNU Lesser General Public
18  //License along with this library; if not, write to the Free Software
19  //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  //-----------------------------------------------------------------------
21  //
22  //$Id: AbstractExtensionPoint.java,v 1.1 2004/10/22 17:34:14 tassos Exp $
23  //
24  
25  import java.util.Iterator;
26  import java.util.LinkedList;
27  
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  
31  /***
32   * A simple IExtensionPoint base class implementation. It uses a LinkedList as
33   * an underlying extension store. Only the extension modification methods are
34   * synchronized.
35   * 
36   * @author abas
37   */
38  public abstract class AbstractExtensionPoint
39          implements IExtensionPoint {
40      private final Log log = LogFactory.getLog(this.getClass());
41      protected LinkedList extensions = new LinkedList();
42      protected IPlugin plugin;
43  
44      public AbstractExtensionPoint(IPlugin plugin) {
45          this.plugin = plugin;
46      }
47  
48      public synchronized void addExtension(IExtension ext) {
49          if (!getExtensionClass().isInstance(ext)) {
50              throw new ClassCastException(ext.getClass() + " is not a "
51                                           + getClass());
52          }
53          if (log.isDebugEnabled())
54              log.debug("Adding extension '" + ext.getName() + "'");
55          extensions.addLast(ext);
56      }
57  
58      public synchronized void removeExtension(IExtension extension) {
59          extensions.remove(extension);
60      }
61  
62      public synchronized void removeExtensions(IPlugin fromPlugin) {
63          for(Iterator i = getExtensions(); i.hasNext();) {
64              IExtension ext = (IExtension)i.next();
65              if (ext.getPlugin().equals(fromPlugin))
66                  i.remove();
67          }
68      }
69  
70      public Iterator getExtensions() {
71          return extensions.iterator();
72      }
73  
74      public IPlugin getPlugin() {
75          return plugin;
76      }
77  }