View Javadoc

    1   /*
    2    * $Id: ServletPortletPreferences.java 502294 2007-02-01 17:28:00Z niallp $
    3    *
    4    * Licensed to the Apache Software Foundation (ASF) under one
    5    * or more contributor license agreements.  See the NOTICE file
    6    * distributed with this work for additional information
    7    * regarding copyright ownership.  The ASF licenses this file
    8    * to you under the Apache License, Version 2.0 (the
    9    * "License"); you may not use this file except in compliance
    10   * with the License.  You may obtain a copy of the License at
    11   *
    12   *  http://www.apache.org/licenses/LICENSE-2.0
    13   *
    14   * Unless required by applicable law or agreed to in writing,
    15   * software distributed under the License is distributed on an
    16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    17   * KIND, either express or implied.  See the License for the
    18   * specific language governing permissions and limitations
    19   * under the License.
    20   */
    21  package org.apache.struts2.portlet.interceptor;
    22
    23  import java.io.IOException;
    24  import java.util.Enumeration;
    25  import java.util.HashMap;
    26  import java.util.Map;
    27  import java.util.Vector;
    28
    29  import javax.portlet.PortletPreferences;
    30  import javax.portlet.ReadOnlyException;
    31  import javax.portlet.ValidatorException;
    32
    33  /***
    34   * Simple portlet preferences implementation that uses a map in the Session
    35   * as storage.
    36   */
    37  public class ServletPortletPreferences implements PortletPreferences {
    38
    39      private Map session;
    40      private String PREFERENCES_KEY = "_portlet-preferences";
    41
    42      public ServletPortletPreferences(Map session) {
    43          this.session = session;
    44      }
    45
    46      public Map getMap() {
    47          Map map = (Map) session.get(PREFERENCES_KEY);
    48          if (map == null) {
    49              map = new HashMap();
    50              session.put(PREFERENCES_KEY, map);
    51          }
    52          return map;
    53      }
    54
    55      public Enumeration getNames() {
    56          return new Vector(getMap().keySet()).elements();
    57      }
    58
    59      public String getValue(String key, String def) {
    60          String val = (String) getMap().get(key);
    61          if (val == null) {
    62              val = def;
    63          }
    64          return val;
    65      }
    66
    67      public String[] getValues(String key, String[] def) {
    68          String[] val = (String[]) getMap().get(key);
    69          if (val == null) {
    70              val = def;
    71          }
    72          return val;
    73      }
    74
    75      public boolean isReadOnly(String arg0) {
    76          return false;
    77      }
    78
    79      public void reset(String arg0) throws ReadOnlyException {
    80          session.put(PREFERENCES_KEY, new HashMap());
    81      }
    82
    83      public void setValue(String key, String value) throws ReadOnlyException {
    84          getMap().put(key, value);
    85      }
    86
    87      public void setValues(String key, String[] value) throws ReadOnlyException {
    88          getMap().put(key, value);
    89      }
    90
    91      public void store() throws IOException, ValidatorException {
    92
    93      }
    94
    95  }