View Javadoc

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