001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software GmbH & Co. KG, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.flex;
029
030import org.opencms.jsp.util.CmsJspDeviceSelectorDesktopMobileTablet;
031import org.opencms.jsp.util.I_CmsJspDeviceSelector;
032import org.opencms.main.CmsLog;
033
034import org.apache.commons.logging.Log;
035
036/**
037 * Flex Cache configuration class.<p>
038 *
039 * @since 6.0.0
040 */
041public class CmsFlexCacheConfiguration {
042
043    /** The log object for this class. */
044    private static final Log LOG = CmsLog.getLog(CmsFlexCacheConfiguration.class);
045
046    /** The average cache bytes. */
047    private long m_avgCacheBytes;
048
049    /** Indicates if the cache is enabled or not. */
050    private boolean m_cacheEnabled;
051
052    /** Indicates if offline resources should be cached or not. */
053    private boolean m_cacheOffline;
054
055    /** The device selector. */
056    private I_CmsJspDeviceSelector m_deviceSelector;
057
058    /** The device selector configuration. */
059    private String m_deviceSelectorConfiguration;
060
061    /**
062     * Sizing parameters for the cached "entries" (ie. pages) in the FlexCache.<p>
063     *
064     * The amount of server memory available obviously is the
065     * critical factor here. The values below are set in byte size.
066     * The default is 2mb memory for the cached pages _or_ a maximum of 4000
067     * cached page variations in total.
068     */
069    private long m_maxCacheBytes;
070
071    /** The maximum entry bytes. */
072    private int m_maxEntryBytes;
073
074    /** The maximum key. */
075    private int m_maxKeys;
076
077    /**
078     * Empty public constructor for the digester.
079     */
080    public CmsFlexCacheConfiguration() {
081
082        // empty public constructor for digester
083    }
084
085    /**
086     * Returns the average cache bytes.<p>
087     *
088     * @return the average cache bytes
089     */
090    public long getAvgCacheBytes() {
091
092        return m_avgCacheBytes;
093    }
094
095    /**
096     * Returns the deviceSelector.<p>
097     *
098     * @return the deviceSelector
099     */
100    public I_CmsJspDeviceSelector getDeviceSelector() {
101
102        if (m_deviceSelector == null) {
103            m_deviceSelector = new CmsJspDeviceSelectorDesktopMobileTablet();
104        }
105        return m_deviceSelector;
106    }
107
108    /**
109     * Returns the device selector configuration.<p>
110     *
111     * @return the device selector configuration
112     */
113    public String getDeviceSelectorConfiguration() {
114
115        return m_deviceSelectorConfiguration;
116    }
117
118    /**
119     * Returns the maxCacheBytes.<p>
120     *
121     * @return the maxCacheBytes
122     */
123    public long getMaxCacheBytes() {
124
125        return m_maxCacheBytes;
126    }
127
128    /**
129     * Returns the maxEntryBytes.<p>
130     *
131     * @return the maxEntryBytes
132     */
133    public int getMaxEntryBytes() {
134
135        return m_maxEntryBytes;
136    }
137
138    /**
139     * Returns the maxKeys.<p>
140     *
141     * @return the maxKeys
142     */
143    public int getMaxKeys() {
144
145        return m_maxKeys;
146    }
147
148    /**
149     * Initializes the flex cache configuration with required parameters.<p>
150     *
151     * @param enabled enables or disable the flexcache
152     * @param offline enable the flexcache for the offline project
153     * @param maxCacheBytes the max bytes for cache
154     * @param avgCacheBytes the average bytes for cache
155     * @param maxEntryBytes the max bytes for entry
156     * @param maxKeys the max keys
157     */
158    public void initialize(
159        String enabled,
160        String offline,
161        String maxCacheBytes,
162        String avgCacheBytes,
163        String maxEntryBytes,
164        String maxKeys) {
165
166        setCacheEnabled(Boolean.valueOf(enabled).booleanValue());
167        setCacheOffline(Boolean.valueOf(offline).booleanValue());
168        setMaxCacheBytes(Long.parseLong(maxCacheBytes));
169        setAvgCacheBytes(Long.parseLong(avgCacheBytes));
170        setMaxEntryBytes(Integer.parseInt(maxEntryBytes));
171        setMaxKeys(Integer.parseInt(maxKeys));
172    }
173
174    /**
175     * Checks if flexcache is enabled or not.<p>
176     *
177     * @return true if flexcache is enabled; otherwise false
178     */
179    public boolean isCacheEnabled() {
180
181        return m_cacheEnabled;
182    }
183
184    /**
185     * Checks the cacheOffline.<p>
186     *
187     * @return true if cacheoffline is set to true; otherwise false
188     */
189    public boolean isCacheOffline() {
190
191        return m_cacheOffline;
192    }
193
194    /**
195     * Sets the avgCacheBytes.<p>
196     *
197     * @param avgCacheBytes the avgCacheBytes to set
198     */
199    public void setAvgCacheBytes(long avgCacheBytes) {
200
201        m_avgCacheBytes = avgCacheBytes;
202    }
203
204    /**
205     * Sets the enabled.<p>
206     *
207     * @param enabled the enabled to set
208     */
209    public void setCacheEnabled(boolean enabled) {
210
211        m_cacheEnabled = enabled;
212    }
213
214    /**
215     * Sets the cacheOffline.<p>
216     *
217     * @param cacheOffline the cacheOffline to set
218     */
219    public void setCacheOffline(boolean cacheOffline) {
220
221        m_cacheOffline = cacheOffline;
222    }
223
224    /**
225     * Sets the device selector configuration.<p>
226     *
227     * @param deviceSelector the device selector to set
228     */
229    public void setDeviceSelectorConfiguration(String deviceSelector) {
230
231        m_deviceSelectorConfiguration = deviceSelector;
232
233        Object objectInstance;
234
235        try {
236            objectInstance = Class.forName(m_deviceSelectorConfiguration).newInstance();
237        } catch (Throwable t) {
238            LOG.error(
239                Messages.get().getBundle().key(Messages.LOG_CLASS_INIT_FAILURE_1, m_deviceSelectorConfiguration),
240                t);
241            return;
242        }
243        if (objectInstance instanceof I_CmsJspDeviceSelector) {
244            m_deviceSelector = (I_CmsJspDeviceSelector)objectInstance;
245            if (CmsLog.INIT.isInfoEnabled()) {
246                CmsLog.INIT.info(
247                    Messages.get().getBundle().key(
248                        Messages.INIT_FLEXCACHE_DEVICE_SELECTOR_SUCCESS_1,
249                        m_deviceSelectorConfiguration));
250            }
251        } else {
252            if (CmsLog.INIT.isFatalEnabled()) {
253                CmsLog.INIT.fatal(
254                    Messages.get().getBundle().key(
255                        Messages.INIT_FLEXCACHE_DEVICE_SELECTOR_FAILURE_1,
256                        m_deviceSelectorConfiguration));
257            }
258        }
259    }
260
261    /**
262     * Sets the maxCacheBytes.<p>
263     *
264     * @param maxCacheBytes the maxCacheBytes to set
265     */
266    public void setMaxCacheBytes(long maxCacheBytes) {
267
268        m_maxCacheBytes = maxCacheBytes;
269    }
270
271    /**
272     * Sets the maxEntryBytes.<p>
273     *
274     * @param maxEntryBytes the maxEntryBytes to set
275     */
276    public void setMaxEntryBytes(int maxEntryBytes) {
277
278        m_maxEntryBytes = maxEntryBytes;
279    }
280
281    /**
282     * Sets the maxKeys.<p>
283     *
284     * @param maxKeys the maxKeys to set
285     */
286    public void setMaxKeys(int maxKeys) {
287
288        m_maxKeys = maxKeys;
289    }
290}