View Javadoc

1   /*
2    * Copyright 2000-2004 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not 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.
15   */
16  package org.apache.portals.bridges.portletfilter;
17  
18  import org.apache.commons.logging.Log;
19  import org.apache.commons.logging.LogFactory;
20  
21  import java.util.ArrayList;
22  import java.util.Enumeration;
23  import java.util.HashMap;
24  import java.util.Iterator;
25  import java.util.Map;
26  import java.util.NoSuchElementException;
27  
28  import javax.portlet.PortletConfig;
29  import javax.portlet.PortletException;
30  
31  /***
32   * A filter configuration object used by FilterPortlet to pass 
33   * information to a filter during initialization.
34   *
35   * The initialization parameter provided by getInitParameter(String) is 
36   * specified in the portlet descriptor(portlet.xml) with the target 
37   * PortletFilter name and a separator(:). 
38   * 
39   * Example:
40   * <pre>
41   * &lt;portlet-app id="example-portlets" version="1.0"&gt;
42   *    &lt;portlet id="ExamplePortlet"&gt;
43   * ...
44   *        &lt;init-param&gt;
45   *            &lt;name&gt;portlet-class&lt;/name&gt;
46   *            &lt;value&gt;org.apache.myfaces.portlet.MyFacesGenericPortlet&lt;/value&gt;
47   *        &lt;/init-param&gt;
48   *        &lt;init-param&gt;
49   *            &lt;name&gt;portlet-filters&lt;/name&gt;
50   *            &lt;value&gt;org.apache.myfaces.portlet.TomahawkPortletFilter&lt;/value&gt;
51   *        &lt;/init-param&gt;
52   *        &lt;init-param&gt;
53   *            &lt;name&gt;org.apache.myfaces.portlet.TomahawkPortletFilter:upload-threshold-size&lt;/name&gt;
54   *            &lt;value&gt;1m&lt;/value&gt;
55   *        &lt;/init-param&gt;
56   *        &lt;init-param&gt;
57   *            &lt;name&gt;org.apache.myfaces.portlet.TomahawkPortletFilter:upload-max-file-size&lt;/name&gt;
58   *            &lt;value&gt;10m&lt;/value&gt;
59   *        &lt;/init-param&gt;
60   * ...
61   * </pre>
62   * 
63   * @author <a href="mailto:shinsuke@yahoo.co.jp">Shinsuke Sugaya</a>
64   *
65   */
66  public class PortletFilterConfig
67  {
68      /***
69       * Logger for this class
70       */
71      private static final Log log = LogFactory.getLog(PortletFilterConfig.class);
72  
73      private String PARAMETER_SEPRATOR = ":";
74  
75      private PortletFilter portletFilter = null;
76  
77      private PortletConfig portletConfig;
78  
79      private String filterName;
80  
81      private Map parameters;
82  
83      public PortletFilterConfig(String filterName, PortletConfig config) throws PortletException
84      {
85          setPortletConfig(config);
86          setFilterName(filterName);
87          parseParameters();
88  
89          try
90          {
91              Class portletFilterClass = Class.forName(filterName);
92              if (portletFilterClass != null)
93              {
94                  Object portletFilter = portletFilterClass.newInstance();
95                  if (portletFilter instanceof PortletFilter)
96                  {
97                      this.portletFilter = (PortletFilter) portletFilter;
98                  }
99                  else
100                 {
101                     throw new PortletException(filterName + " is not PortletFilter class.");
102                 }
103             }
104             else
105             {
106                 throw new PortletException(filterName + " is not found.");
107             }
108         }
109         catch (ClassNotFoundException e)
110         {
111             throw new PortletException("ClassNotFoundException occurred.", e);
112         }
113         catch (InstantiationException e)
114         {
115             throw new PortletException("InstantiationException occurred.", e);
116         }
117         catch (IllegalAccessException e)
118         {
119             throw new PortletException("IllegalAccessException occurred.", e);
120         }
121 
122         // init
123         this.portletFilter.init(this);
124     }
125 
126     /***
127      * Parses initialization parameters in a portlet descriptor(portlet.xml).
128      */
129     private void parseParameters()
130     {
131         parameters = new HashMap();
132         for (Enumeration e = getPortletConfig().getInitParameterNames(); e.hasMoreElements();)
133         {
134             String key = (String) e.nextElement();
135             if (key.startsWith(getFilterName() + PARAMETER_SEPRATOR))
136             {
137                 String newKey = key.substring(getFilterName().length() + PARAMETER_SEPRATOR.length());
138                 parameters.put(newKey, getPortletConfig().getInitParameter(key));
139             }
140         }
141     }
142 
143     /***
144      * Return a <code>String</code> containing the value of the named initialization parameter, or <code>null</code>
145      * if the parameter does not exist.
146      * 
147      * @param name Name of the requested initialization parameter
148      */
149     public String getInitParameter(String name)
150     {
151         if (parameters == null)
152         {
153             return (null);
154         }
155         else
156         {
157             return ((String) parameters.get(name));
158         }
159     }
160 
161     /***
162      * Return an <code>Enumeration</code> of the names of the initialization parameters for this Filter.
163      */
164     public Enumeration getInitParameterNames()
165     {
166         if (parameters == null)
167             return (new Enumerator(new ArrayList().iterator()));
168         else
169             return (new Enumerator(parameters.keySet().iterator()));
170 
171     }
172 
173     /***
174      * Returns the PortletFilter instance.
175      * 
176      * @return
177      */
178     public PortletFilter getPortletFilter()
179     {
180         return portletFilter;
181     }
182 
183     /***
184      * @return Returns the portletConfig.
185      */
186     public PortletConfig getPortletConfig()
187     {
188         return portletConfig;
189     }
190 
191     /***
192      * @param portletConfig The portletConfig to set.
193      */
194     public void setPortletConfig(PortletConfig portletConfig)
195     {
196         this.portletConfig = portletConfig;
197     }
198 
199     public void release()
200     {
201         portletFilter.destroy();
202         portletConfig = null;
203     }
204 
205     /***
206      * @return Returns the filterName.
207      */
208     public String getFilterName()
209     {
210         return filterName;
211     }
212 
213     /***
214      * @param filterName The filterName to set.
215      */
216     public void setFilterName(String filterName)
217     {
218         this.filterName = filterName;
219     }
220 
221     /***
222      * Uitlity class to wraps an <code>Iterator</code>
223      * 
224      * @author <a href="mailto:shinsuke@yahoo.co.jp">Shinsuke Sugaya</a>
225      *
226      */
227     public final class Enumerator implements Enumeration
228     {
229         /***
230          * Return an Enumeration over the values returned by the specified 
231          * Iterator.
232          * 
233          * @param iterator Iterator to be wrapped
234          */
235         public Enumerator(Iterator iterator)
236         {
237 
238             super();
239             this.iterator = iterator;
240 
241         }
242 
243         /***
244          * The <code>Iterator</code> over which the <code>Enumeration</code> 
245          * represented by this class actually operates.
246          */
247         private Iterator iterator = null;
248 
249         /***
250          * Tests if this enumeration contains more elements.
251          * 
252          * @return <code>true</code> if and only if this enumeration object 
253          *                           contains at least one more element to
254          *                           provide, <code>false</code> otherwise
255          */
256         public boolean hasMoreElements()
257         {
258 
259             return (iterator.hasNext());
260 
261         }
262 
263         /***
264          * Returns the next element of this enumeration if this enumeration 
265          * has at least one more element to provide.
266          * 
267          * @return the next element of this enumeration
268          * @exception NoSuchElementException if no more elements exist
269          */
270         public Object nextElement() throws NoSuchElementException
271         {
272 
273             return (iterator.next());
274 
275         }
276 
277     }
278 }