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