1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet;
16  
17  import com.liferay.portal.util.PropsValues;
18  
19  import java.io.IOException;
20  import java.io.Serializable;
21  
22  import java.util.Enumeration;
23  import java.util.Map;
24  
25  import javax.portlet.PortletPreferences;
26  import javax.portlet.PortletRequest;
27  import javax.portlet.ReadOnlyException;
28  import javax.portlet.ValidatorException;
29  
30  /**
31   * <a href="PortletPreferencesWrapper.java.html"><b><i>View Source</i></b></a>
32   *
33   * @author Brian Wing Shun Chan
34   */
35  public class PortletPreferencesWrapper
36      implements PortletPreferences, Serializable {
37  
38      public PortletPreferencesWrapper(
39          PortletPreferences preferences, String lifecycle) {
40  
41          _preferences = preferences;
42          _lifecycle = lifecycle;
43      }
44  
45      public Map<String, String[]> getMap() {
46          return _preferences.getMap();
47      }
48  
49      public Enumeration<String> getNames() {
50          return _preferences.getNames();
51      }
52  
53      public String getValue(String key, String def) {
54          return _preferences.getValue(key, def);
55      }
56  
57      public void setValue(String key, String value) throws ReadOnlyException {
58          _preferences.setValue(key, value);
59      }
60  
61      public String[] getValues(String key, String[] def) {
62          return _preferences.getValues(key, def);
63      }
64  
65      public void setValues(String key, String[] values)
66          throws ReadOnlyException {
67  
68          _preferences.setValues(key, values);
69      }
70  
71      public boolean isReadOnly(String key) {
72          return _preferences.isReadOnly(key);
73      }
74  
75      public void reset(String key) throws ReadOnlyException {
76          _preferences.reset(key);
77      }
78  
79      public void store() throws IOException, ValidatorException {
80          if (PropsValues.TCK_URL) {
81  
82              // Be strict to pass the TCK
83  
84              if (_lifecycle.equals(PortletRequest.ACTION_PHASE)) {
85                  _preferences.store();
86              }
87              else {
88                  throw new IllegalStateException(
89                      "Preferences cannot be stored inside a render call");
90              }
91          }
92          else {
93  
94              // Relax so that poorly written portlets can still work
95  
96              _preferences.store();
97          }
98      }
99  
100     public PortletPreferencesImpl getPreferencesImpl() {
101         return (PortletPreferencesImpl)_preferences;
102     }
103 
104     public boolean equals(Object obj) {
105         PortletPreferencesWrapper portletPreferences =
106             (PortletPreferencesWrapper)obj;
107 
108         if (this == portletPreferences) {
109             return true;
110         }
111 
112         if (getPreferencesImpl().equals(
113                 portletPreferences.getPreferencesImpl())) {
114 
115             return true;
116         }
117         else {
118             return false;
119         }
120     }
121 
122     private PortletPreferences _preferences;
123     private String _lifecycle;
124 
125 }