001    /**
002     * Copyright (c) 2000-2013 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 {@link #getPortletPreferencesImpl}
082             */
083            public PortletPreferencesImpl getPreferencesImpl() {
084                    return getPortletPreferencesImpl();
085            }
086    
087            @Override
088            public String getValue(String key, String def) {
089                    return _portletPreferences.getValue(key, def);
090            }
091    
092            @Override
093            public String[] getValues(String key, String[] def) {
094                    return _portletPreferences.getValues(key, def);
095            }
096    
097            @Override
098            public int hashCode() {
099                    return _portletPreferences.hashCode();
100            }
101    
102            @Override
103            public boolean isReadOnly(String key) {
104                    return _portletPreferences.isReadOnly(key);
105            }
106    
107            @Override
108            public void reset(String key) throws ReadOnlyException {
109                    _portletPreferences.reset(key);
110            }
111    
112            @Override
113            public void setValue(String key, String value) throws ReadOnlyException {
114                    _portletPreferences.setValue(key, value);
115            }
116    
117            @Override
118            public void setValues(String key, String[] values)
119                    throws ReadOnlyException {
120    
121                    _portletPreferences.setValues(key, values);
122            }
123    
124            @Override
125            public void store() throws IOException, ValidatorException {
126                    if (PropsValues.TCK_URL) {
127    
128                            // Be strict to pass the TCK
129    
130                            if (_lifecycle.equals(PortletRequest.ACTION_PHASE)) {
131                                    _portletPreferences.store();
132                            }
133                            else {
134                                    throw new IllegalStateException(
135                                            "Preferences cannot be stored inside a render call");
136                            }
137                    }
138                    else {
139    
140                            // Relax so that poorly written portlets can still work
141    
142                            _portletPreferences.store();
143                    }
144            }
145    
146            private String _lifecycle;
147            private PortletPreferences _portletPreferences;
148    
149    }