1   /**
2    * Copyright (c) 2000-2007 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.kernel.util.GetterUtil;
26  import com.liferay.portal.util.PropsUtil;
27  
28  import java.io.IOException;
29  import java.io.Serializable;
30  
31  import java.util.Enumeration;
32  import java.util.Map;
33  
34  import javax.portlet.PortletPreferences;
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(PortletPreferences prefs, boolean action) {
48          _prefs = prefs;
49          _action = action;
50      }
51  
52      public Map getMap() {
53          return _prefs.getMap();
54      }
55  
56      public Enumeration getNames() {
57          return _prefs.getNames();
58      }
59  
60      public String getValue(String key, String def) {
61          return _prefs.getValue(key, def);
62      }
63  
64      public void setValue(String key, String value) throws ReadOnlyException {
65          _prefs.setValue(key, value);
66      }
67  
68      public String[] getValues(String key, String[] def) {
69          return _prefs.getValues(key, def);
70      }
71  
72      public void setValues(String key, String[] values)
73          throws ReadOnlyException {
74  
75          _prefs.setValues(key, values);
76      }
77  
78      public boolean isReadOnly(String key) {
79          return _prefs.isReadOnly(key);
80      }
81  
82      public void reset(String key) throws ReadOnlyException {
83          _prefs.reset(key);
84      }
85  
86      public void store() throws IOException, ValidatorException {
87          if (GetterUtil.getBoolean(PropsUtil.get(PropsUtil.TCK_URL))) {
88  
89              // Be strict to pass the TCK
90  
91              if (_action) {
92                  _prefs.store();
93              }
94              else {
95                  throw new IllegalStateException(
96                      "Preferences cannot be stored inside a render call");
97              }
98          }
99          else {
100 
101             // Relax so that poorly written portlets can still work
102 
103             _prefs.store();
104         }
105     }
106 
107     public PortletPreferencesImpl getPreferencesImpl() {
108         return (PortletPreferencesImpl)_prefs;
109     }
110 
111     private PortletPreferences _prefs = null;
112     private boolean _action;
113 
114 }