View Javadoc

1   package net.sourceforge.blogentis.modules.screens;
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: BlogPlugins.java,v 1.3 2004/10/31 07:16:22 tassos Exp $
23  //
24  
25  import java.util.ArrayList;
26  import java.util.Iterator;
27  
28  import net.sourceforge.blogentis.plugins.BlogPluginService;
29  import net.sourceforge.blogentis.plugins.IPlugin;
30  import net.sourceforge.blogentis.turbine.BlogParameterParser;
31  import net.sourceforge.blogentis.turbine.BlogRunData;
32  import net.sourceforge.blogentis.turbine.SecureBlogScreen;
33  import net.sourceforge.blogentis.utils.AbsoluteLinkURL;
34  import net.sourceforge.blogentis.utils.BlogConstants;
35  import net.sourceforge.blogentis.utils.LinkFactoryService;
36  import net.sourceforge.blogentis.utils.MappedConfiguration;
37  
38  import org.apache.commons.collections.CollectionUtils;
39  import org.apache.commons.lang.ArrayUtils;
40  import org.apache.turbine.util.RunData;
41  import org.apache.velocity.context.Context;
42  
43  /***
44   * Screen that handles plugin management.
45   * 
46   * @author abas
47   */
48  public class BlogPlugins
49          extends SecureBlogScreen {
50      public static class PluginHelper {
51          public String getIdentifier(IPlugin plugin) {
52              return plugin.getClass().getName();
53          }
54  
55          public boolean isEnabled(BlogRunData data, IPlugin plugin) {
56              MappedConfiguration conf = data.getBlog().getConfiguration();
57              String[] stringArray = conf.getStringArray(BlogPluginService.PLUGIN_LIST);
58              if (stringArray == null || stringArray.length == 0)
59                  return true;
60              String id = getIdentifier(plugin);
61              return ArrayUtils.contains(stringArray, id);
62          }
63      }
64  
65      public static class PluginIterator
66              implements Iterator {
67          private IPlugin[] plugins;
68          private IPlugin plugin;
69          private BlogRunData data;
70          int pluginId = 0;
71          int i = -1;
72          private boolean enabled;
73  
74          private int detNext() {
75              while (++i < plugins.length) {
76                  if (pluginHelper.isEnabled(data, plugins[i]) == enabled)
77                      break;
78              }
79              return i;
80          }
81  
82          public PluginIterator(BlogRunData data, boolean state) {
83              ArrayList al = new ArrayList();
84              CollectionUtils.addAll(al, BlogPluginService.getPlugins());
85              plugins = (IPlugin[])al.toArray(new IPlugin[] {});
86              this.data = data;
87              this.enabled = state;
88              detNext();
89          }
90  
91          public boolean hasNext() {
92              return i < plugins.length;
93          }
94  
95          public Object next() {
96              pluginId = i;
97              plugin = plugins[pluginId];
98              detNext();
99              return this;
100         }
101 
102         public void remove() {}
103 
104         public String getDescription() {
105             return plugin.getDescription();
106         }
107 
108         public String getName() {
109             return plugin.getName();
110         }
111 
112         public String getIdentifier() {
113             return Integer.toString(pluginId);
114         }
115 
116         public boolean isEnabled() {
117             return pluginHelper.isEnabled(data, plugin);
118         }
119 
120         public boolean isPermanent() {
121             return pluginId == 0;
122         }
123 
124         public boolean canMoveUp() {
125             return pluginId > 0 && !isPermanent();
126         }
127 
128         public boolean canMoveDown() {
129             return !isPermanent() && pluginId < plugins.length - 1;
130         }
131 
132         public AbsoluteLinkURL getActionLink() {
133             return LinkFactoryService.getLink().thisPage(data)
134                     .addQueryData("action", "BlogPluginMod")
135                     .addQueryData("p", getIdentifier());
136         }
137     }
138 
139     protected static final PluginHelper pluginHelper = new PluginHelper();
140 
141     protected void doBuildTemplate(RunData data, Context context)
142             throws Exception {
143         super.doBuildTemplate(data, context);
144         BlogParameterParser pp = (BlogParameterParser)data.getParameters();
145         int state = pp.getInt("s", 0);
146 
147         switch (state) {
148         default:
149             context.put("plugins", new PluginIterator((BlogRunData)data, true));
150             context
151                     .put("plugouts",
152                          new PluginIterator((BlogRunData)data, false));
153             context.put("extPoints", BlogPluginService.getExtensionPoints(pp
154                     .getBlog()));
155         }
156         context.put("pluginHelper", pluginHelper);
157         context.put("state", Integer.toString(state));
158     }
159 
160     protected String[] getPermissions() {
161         return new String[] {BlogConstants.PERM_ADMIN_BLOG};
162     }
163 }