1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class PortletPreferencesWrapper
44      implements PortletPreferences, Serializable {
45  
46      public PortletPreferencesWrapper(
47          PortletPreferences prefs, String lifecycle) {
48  
49          _prefs = prefs;
50          _lifecycle = lifecycle;
51      }
52  
53      public Map<String, String[]> getMap() {
54          return _prefs.getMap();
55      }
56  
57      public Enumeration<String> getNames() {
58          return _prefs.getNames();
59      }
60  
61      public String getValue(String key, String def) {
62          return _prefs.getValue(key, def);
63      }
64  
65      public void setValue(String key, String value) throws ReadOnlyException {
66          _prefs.setValue(key, value);
67      }
68  
69      public String[] getValues(String key, String[] def) {
70          return _prefs.getValues(key, def);
71      }
72  
73      public void setValues(String key, String[] values)
74          throws ReadOnlyException {
75  
76          _prefs.setValues(key, values);
77      }
78  
79      public boolean isReadOnly(String key) {
80          return _prefs.isReadOnly(key);
81      }
82  
83      public void reset(String key) throws ReadOnlyException {
84          _prefs.reset(key);
85      }
86  
87      public void store() throws IOException, ValidatorException {
88          if (PropsValues.TCK_URL) {
89  
90              // Be strict to pass the TCK
91  
92              if (_lifecycle.equals(PortletRequest.ACTION_PHASE)) {
93                  _prefs.store();
94              }
95              else {
96                  throw new IllegalStateException(
97                      "Preferences cannot be stored inside a render call");
98              }
99          }
100         else {
101 
102             // Relax so that poorly written portlets can still work
103 
104             _prefs.store();
105         }
106     }
107 
108     public PortletPreferencesImpl getPreferencesImpl() {
109         return (PortletPreferencesImpl)_prefs;
110     }
111 
112     public boolean equals(Object obj) {
113         PortletPreferencesWrapper portletPreferences =
114             (PortletPreferencesWrapper)obj;
115 
116         if (this == portletPreferences) {
117             return true;
118         }
119 
120         if (getPreferencesImpl().equals(
121                 portletPreferences.getPreferencesImpl())) {
122 
123             return true;
124         }
125         else {
126             return false;
127         }
128     }
129 
130     private PortletPreferences _prefs;
131     private String _lifecycle;
132 
133 }