001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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 portletPreferences, String lifecycle) {
038    
039                    _portletPreferences = portletPreferences;
040                    _lifecycle = lifecycle;
041            }
042    
043            @Override
044            public boolean equals(Object obj) {
045                    if (this == obj) {
046                            return true;
047                    }
048    
049                    if (!(obj instanceof PortletPreferencesWrapper)) {
050                            return false;
051                    }
052    
053                    PortletPreferencesWrapper portletPreferencesWrapper =
054                            (PortletPreferencesWrapper)obj;
055    
056                    if (getPortletPreferencesImpl().equals(
057                                    portletPreferencesWrapper.getPortletPreferencesImpl())) {
058    
059                            return true;
060                    }
061                    else {
062                            return false;
063                    }
064            }
065    
066            @Override
067            public Map<String, String[]> getMap() {
068                    return _portletPreferences.getMap();
069            }
070    
071            @Override
072            public Enumeration<String> getNames() {
073                    return _portletPreferences.getNames();
074            }
075    
076            public PortletPreferencesImpl getPortletPreferencesImpl() {
077                    return (PortletPreferencesImpl)_portletPreferences;
078            }
079    
080            /**
081             * @deprecated As of 6.1.0, replaced by {@link #getPortletPreferencesImpl}
082             */
083            @Deprecated
084            public PortletPreferencesImpl getPreferencesImpl() {
085                    return getPortletPreferencesImpl();
086            }
087    
088            @Override
089            public String getValue(String key, String def) {
090                    return _portletPreferences.getValue(key, def);
091            }
092    
093            @Override
094            public String[] getValues(String key, String[] def) {
095                    return _portletPreferences.getValues(key, def);
096            }
097    
098            @Override
099            public int hashCode() {
100                    return _portletPreferences.hashCode();
101            }
102    
103            @Override
104            public boolean isReadOnly(String key) {
105                    return _portletPreferences.isReadOnly(key);
106            }
107    
108            @Override
109            public void reset(String key) throws ReadOnlyException {
110                    _portletPreferences.reset(key);
111            }
112    
113            @Override
114            public void setValue(String key, String value) throws ReadOnlyException {
115                    _portletPreferences.setValue(key, value);
116            }
117    
118            @Override
119            public void setValues(String key, String[] values)
120                    throws ReadOnlyException {
121    
122                    _portletPreferences.setValues(key, values);
123            }
124    
125            @Override
126            public void store() throws IOException, ValidatorException {
127                    if (PropsValues.TCK_URL) {
128    
129                            // Be strict to pass the TCK
130    
131                            if (_lifecycle.equals(PortletRequest.ACTION_PHASE)) {
132                                    _portletPreferences.store();
133                            }
134                            else {
135                                    throw new IllegalStateException(
136                                            "Preferences cannot be stored inside a render call");
137                            }
138                    }
139                    else {
140    
141                            // Relax so that poorly written portlets can still work
142    
143                            _portletPreferences.store();
144                    }
145            }
146    
147            private final String _lifecycle;
148            private final PortletPreferences _portletPreferences;
149    
150    }