001    /**
002     * Copyright (c) 2000-2012 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                    PortletPreferencesWrapper portletPreferencesWrapper =
046                            (PortletPreferencesWrapper)obj;
047    
048                    if (this == portletPreferencesWrapper) {
049                            return true;
050                    }
051    
052                    if (getPortletPreferencesImpl().equals(
053                                    portletPreferencesWrapper.getPortletPreferencesImpl())) {
054    
055                            return true;
056                    }
057                    else {
058                            return false;
059                    }
060            }
061    
062            public Map<String, String[]> getMap() {
063                    return _portletPreferences.getMap();
064            }
065    
066            public Enumeration<String> getNames() {
067                    return _portletPreferences.getNames();
068            }
069    
070            public PortletPreferencesImpl getPortletPreferencesImpl() {
071                    return (PortletPreferencesImpl)_portletPreferences;
072            }
073    
074            /**
075             * @deprecated {@link #getPortletPreferencesImpl}
076             */
077            public PortletPreferencesImpl getPreferencesImpl() {
078                    return getPortletPreferencesImpl();
079            }
080    
081            public String getValue(String key, String def) {
082                    return _portletPreferences.getValue(key, def);
083            }
084    
085            public String[] getValues(String key, String[] def) {
086                    return _portletPreferences.getValues(key, def);
087            }
088    
089            @Override
090            public int hashCode() {
091                    return _portletPreferences.hashCode();
092            }
093    
094            public boolean isReadOnly(String key) {
095                    return _portletPreferences.isReadOnly(key);
096            }
097    
098            public void reset(String key) throws ReadOnlyException {
099                    _portletPreferences.reset(key);
100            }
101    
102            public void setValue(String key, String value) throws ReadOnlyException {
103                    _portletPreferences.setValue(key, value);
104            }
105    
106            public void setValues(String key, String[] values)
107                    throws ReadOnlyException {
108    
109                    _portletPreferences.setValues(key, values);
110            }
111    
112            public void store() throws IOException, ValidatorException {
113                    if (PropsValues.TCK_URL) {
114    
115                            // Be strict to pass the TCK
116    
117                            if (_lifecycle.equals(PortletRequest.ACTION_PHASE)) {
118                                    _portletPreferences.store();
119                            }
120                            else {
121                                    throw new IllegalStateException(
122                                            "Preferences cannot be stored inside a render call");
123                            }
124                    }
125                    else {
126    
127                            // Relax so that poorly written portlets can still work
128    
129                            _portletPreferences.store();
130                    }
131            }
132    
133            private String _lifecycle;
134            private PortletPreferences _portletPreferences;
135    
136    }