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.portal.template;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.AutoResetThreadLocal;
020    import com.liferay.portal.model.PortletConstants;
021    import com.liferay.portlet.PortletPreferencesFactoryUtil;
022    import com.liferay.portlet.PortletPreferencesImpl;
023    
024    import javax.portlet.ReadOnlyException;
025    
026    /**
027     * @author Brian Wing Shun Chan
028     * @author L??szl?? Csontos
029     */
030    public class TemplatePortletPreferences {
031    
032            public void reset() {
033                    PortletPreferencesImpl portletPreferencesImpl =
034                            _portletPreferencesImplThreadLocal.get();
035    
036                    portletPreferencesImpl.reset();
037            }
038    
039            public void setValue(String key, String value) throws ReadOnlyException {
040                    PortletPreferencesImpl portletPreferencesImpl =
041                            _portletPreferencesImplThreadLocal.get();
042    
043                    portletPreferencesImpl.setValue(key, value);
044            }
045    
046            public void setValues(String key, String[] values)
047                    throws ReadOnlyException {
048    
049                    PortletPreferencesImpl portletPreferencesImpl =
050                            _portletPreferencesImplThreadLocal.get();
051    
052                    portletPreferencesImpl.setValues(key, values);
053            }
054    
055            @Override
056            public String toString() {
057                    PortletPreferencesImpl portletPreferencesImpl =
058                            _portletPreferencesImplThreadLocal.get();
059    
060                    try {
061                            return PortletPreferencesFactoryUtil.toXML(portletPreferencesImpl);
062                    }
063                    catch (Exception e) {
064                            _log.error(e, e);
065    
066                            return PortletConstants.DEFAULT_PREFERENCES;
067                    }
068            }
069    
070            private static final Log _log = LogFactoryUtil.getLog(
071                    TemplatePortletPreferences.class);
072    
073            private final ThreadLocal<PortletPreferencesImpl>
074                    _portletPreferencesImplThreadLocal =
075                            new AutoResetThreadLocal<PortletPreferencesImpl>(
076                                    TemplatePortletPreferences.class.getName()) {
077    
078                                    @Override
079                                    protected PortletPreferencesImpl initialValue() {
080                                            return new PortletPreferencesImpl();
081                                    }
082    
083                            };
084    
085    }