001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portlet;
016    
017    import com.liferay.portal.util.PropsValues;
018    
019    import java.io.IOException;
020    import java.io.Serializable;
021    
022    import java.util.Enumeration;
023    import java.util.Map;
024    
025    import javax.portlet.PortletPreferences;
026    import javax.portlet.PortletRequest;
027    import javax.portlet.ReadOnlyException;
028    import javax.portlet.ValidatorException;
029    
030    /**
031     * @author Brian Wing Shun Chan
032     */
033    public class PortletPreferencesWrapper
034            implements PortletPreferences, Serializable {
035    
036            public PortletPreferencesWrapper(
037                    PortletPreferences preferences, String lifecycle) {
038    
039                    _preferences = preferences;
040                    _lifecycle = lifecycle;
041            }
042    
043            public Map<String, String[]> getMap() {
044                    return _preferences.getMap();
045            }
046    
047            public Enumeration<String> getNames() {
048                    return _preferences.getNames();
049            }
050    
051            public String getValue(String key, String def) {
052                    return _preferences.getValue(key, def);
053            }
054    
055            public void setValue(String key, String value) throws ReadOnlyException {
056                    _preferences.setValue(key, value);
057            }
058    
059            public String[] getValues(String key, String[] def) {
060                    return _preferences.getValues(key, def);
061            }
062    
063            public void setValues(String key, String[] values)
064                    throws ReadOnlyException {
065    
066                    _preferences.setValues(key, values);
067            }
068    
069            public boolean isReadOnly(String key) {
070                    return _preferences.isReadOnly(key);
071            }
072    
073            public void reset(String key) throws ReadOnlyException {
074                    _preferences.reset(key);
075            }
076    
077            public void store() throws IOException, ValidatorException {
078                    if (PropsValues.TCK_URL) {
079    
080                            // Be strict to pass the TCK
081    
082                            if (_lifecycle.equals(PortletRequest.ACTION_PHASE)) {
083                                    _preferences.store();
084                            }
085                            else {
086                                    throw new IllegalStateException(
087                                            "Preferences cannot be stored inside a render call");
088                            }
089                    }
090                    else {
091    
092                            // Relax so that poorly written portlets can still work
093    
094                            _preferences.store();
095                    }
096            }
097    
098            public PortletPreferencesImpl getPreferencesImpl() {
099                    return (PortletPreferencesImpl)_preferences;
100            }
101    
102            public boolean equals(Object obj) {
103                    PortletPreferencesWrapper portletPreferences =
104                            (PortletPreferencesWrapper)obj;
105    
106                    if (this == portletPreferences) {
107                            return true;
108                    }
109    
110                    if (getPreferencesImpl().equals(
111                                    portletPreferences.getPreferencesImpl())) {
112    
113                            return true;
114                    }
115                    else {
116                            return false;
117                    }
118            }
119    
120            public int hashCode() {
121                    return _preferences.hashCode();
122            }
123    
124            private PortletPreferences _preferences;
125            private String _lifecycle;
126    
127    }