1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet;
21  
22  import com.liferay.portal.util.PropsValues;
23  
24  import java.io.IOException;
25  import java.io.Serializable;
26  
27  import java.util.Enumeration;
28  import java.util.Map;
29  
30  import javax.portlet.PortletPreferences;
31  import javax.portlet.PortletRequest;
32  import javax.portlet.ReadOnlyException;
33  import javax.portlet.ValidatorException;
34  
35  /**
36   * <a href="PortletPreferencesWrapper.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Brian Wing Shun Chan
39   *
40   */
41  public class PortletPreferencesWrapper
42      implements PortletPreferences, Serializable {
43  
44      public PortletPreferencesWrapper(
45          PortletPreferences prefs, String lifecycle) {
46  
47          _prefs = prefs;
48          _lifecycle = lifecycle;
49      }
50  
51      public Map<String, String[]> getMap() {
52          return _prefs.getMap();
53      }
54  
55      public Enumeration<String> getNames() {
56          return _prefs.getNames();
57      }
58  
59      public String getValue(String key, String def) {
60          return _prefs.getValue(key, def);
61      }
62  
63      public void setValue(String key, String value) throws ReadOnlyException {
64          _prefs.setValue(key, value);
65      }
66  
67      public String[] getValues(String key, String[] def) {
68          return _prefs.getValues(key, def);
69      }
70  
71      public void setValues(String key, String[] values)
72          throws ReadOnlyException {
73  
74          _prefs.setValues(key, values);
75      }
76  
77      public boolean isReadOnly(String key) {
78          return _prefs.isReadOnly(key);
79      }
80  
81      public void reset(String key) throws ReadOnlyException {
82          _prefs.reset(key);
83      }
84  
85      public void store() throws IOException, ValidatorException {
86          if (PropsValues.TCK_URL) {
87  
88              // Be strict to pass the TCK
89  
90              if (_lifecycle.equals(PortletRequest.ACTION_PHASE)) {
91                  _prefs.store();
92              }
93              else {
94                  throw new IllegalStateException(
95                      "Preferences cannot be stored inside a render call");
96              }
97          }
98          else {
99  
100             // Relax so that poorly written portlets can still work
101 
102             _prefs.store();
103         }
104     }
105 
106     public PortletPreferencesImpl getPreferencesImpl() {
107         return (PortletPreferencesImpl)_prefs;
108     }
109 
110     public boolean equals(Object obj) {
111         PortletPreferencesWrapper portletPreferences =
112             (PortletPreferencesWrapper)obj;
113 
114         if (this == portletPreferences) {
115             return true;
116         }
117 
118         if (getPreferencesImpl().equals(
119                 portletPreferences.getPreferencesImpl())) {
120 
121             return true;
122         }
123         else {
124             return false;
125         }
126     }
127 
128     private PortletPreferences _prefs;
129     private String _lifecycle;
130 
131 }