KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opencms > template > A_CmsCacheDirectives


1 /*
2 * File : $Source: /usr/local/cvs/opencms/src-modules/com/opencms/template/A_CmsCacheDirectives.java,v $
3 * Date : $Date: 2005/06/21 15:49:58 $
4 * Version: $Revision: 1.2 $
5 *
6 * This library is part of OpenCms -
7 * the Open Source Content Mananagement System
8 *
9 * Copyright (C) 2001 The OpenCms Group
10 *
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 *
21 * For further information about OpenCms, please see the
22 * OpenCms Website: http://www.opencms.org
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 */

28
29 package com.opencms.template;
30
31 import org.opencms.file.CmsObject;
32 import org.opencms.file.CmsRequestContext;
33
34 import com.opencms.legacy.CmsXmlTemplateLoader;
35 import com.opencms.template.cache.CmsTimeout;
36
37 import java.util.Enumeration JavaDoc;
38 import java.util.Hashtable JavaDoc;
39 import java.util.Vector JavaDoc;
40
41 /**
42  * Abstact class for all CacheDirectives classes.
43  *
44  * @author Hanjo Riege
45  * @version 1.0
46  *
47  * @deprecated Will not be supported past the OpenCms 6 release.
48  */

49
50 public abstract class A_CmsCacheDirectives {
51
52     /** Bitfield for storing external cache properties. */
53     protected int m_cd;
54
55     // everthing to get the cache key
56

57     // indicates if the username is part of the cache key
58
private boolean m_user;
59     // indicates if the groupname is part of the cache key
60
private boolean m_group;
61     //indicates if the uri is part of the cache key
62
private boolean m_uri;
63     //the parameters for which the element is cacheable
64
private Vector JavaDoc m_cacheParameter;
65
66     // if one of these parameters occures the element is dynamic
67
private Vector JavaDoc m_dynamicParameter;
68
69     // indicates if this element should removed from cache after publish even if the template is unchanged.
70
private boolean m_renewAfterEveryPublish;
71
72     /** The timeout object. */
73     protected CmsTimeout m_timeout;
74     /** Flag to check the time. */
75     protected boolean m_timecheck;
76
77     /** Indicates if the user has set the value: proxyprivate flag. */
78     protected boolean m_userSetProxyPrivate;
79     /** Indicates if the user has set the value: proxypublic flag. */
80     protected boolean m_userSetProxyPublic;
81     /** Indicates if the user has set the value: export flag. */
82     protected boolean m_userSetExport;
83
84
85     /** Flag for internal cacheable. */
86     public static final int C_CACHE_INTERNAL = 1;
87     /** Flag for cacheable in private proxies. */
88     public static final int C_CACHE_PROXY_PRIVATE = 2;
89     /** Flag for cacheable in public proxies. */
90     public static final int C_CACHE_PROXY_PUBLIC = 4;
91     /** Flag for exportable. */
92     public static final int C_CACHE_EXPORT = 8;
93     /** Flag for streamable. */
94     public static final int C_CACHE_STREAM = 16;
95
96     /**
97      * Get the state of the "internal cacheable" property.<p>
98      *
99      * @return <code>true</code> if internal caching is possible, <code>false</code> otherwise.
100      */

101     public boolean isInternalCacheable() {
102         return (m_cd & C_CACHE_INTERNAL) == C_CACHE_INTERNAL;
103     }
104
105     /**
106      * Get information about the cacheKey.<p>
107      *
108      * @return <code>true</code> if user or group is in the key, <code>false</code> otherwise.
109      */

110     public boolean isUserPartOfKey() {
111         return m_group || m_user;
112     }
113     /**
114      * Get information about the cacheKey.<p>
115      *
116      * @return <code>true</code> if parameters are in the key, <code>false</code> otherwise.
117      */

118     public boolean isParameterPartOfKey() {
119         return (m_cacheParameter != null) && (!m_cacheParameter.isEmpty());
120     }
121
122     /**
123      * Get the timeout object(used if the element should be reloaded every x minutes.<p>
124      *
125      * @return timeout object.
126      */

127     public CmsTimeout getTimeout() {
128         return m_timeout;
129     }
130
131     /**
132      * Get information if the element is time critical.<p>
133      *
134      * @return <code>true</code> if a timeout object was set, <code>false</code> otherwise.
135      */

136     public boolean isTimeCritical() {
137         return m_timecheck;
138     }
139
140     /**
141      * set the timeout object(used if the element should be reloaded every x minutes.
142      * @param timeout a CmsTimeout object.
143      */

144     public abstract void setTimeout(CmsTimeout timeout);
145
146     /**
147      * Sets the renewAfterEveryPublish value to true. This means that this element
148      * is removed from cache every time a project is published even if its template or class
149      * was not changed in the project.<p>
150      */

151     public void renewAfterEveryPublish() {
152         m_renewAfterEveryPublish = true;
153     }
154
155     /**
156      * Switches off the renewAfterPublish function. If someone needs the uri in the key
157      * but don't need the renew function he has to use this to get rid of it. To stay
158      * compatible to old versions we can't remove it from the setUri method.<p>
159      */

160     public void noAutoRenewAfterPublish() {
161         m_renewAfterEveryPublish = false;
162     }
163
164     /**
165      * Returns true if this element has to be deleted from cache every time a project
166      * is published. This is when the uri is part of the cacheKey or if the user says so.<p>
167      *
168      * @return true if this element has to be deleted from cache every time a project is published
169      */

170     public boolean shouldRenew() {
171         return m_renewAfterEveryPublish;
172     }
173
174     /**
175      * Calculates the cacheKey for the element.<p>
176      *
177      * @param cms the initialized cms request object
178      * @param parameters the parameters
179      * @return the cache key or null if it is not cacheable
180      */

181     public String JavaDoc getCacheKey(CmsObject cms, Hashtable JavaDoc parameters) {
182
183         if (! this.isInternalCacheable()) {
184             return null;
185         }
186         if (parameters == null) {
187             parameters = new Hashtable JavaDoc();
188         }
189         // first test the parameters which say it is dynamic
190
if ((m_dynamicParameter != null) && (!m_dynamicParameter.isEmpty())) {
191             for (int i=0; i < m_dynamicParameter.size(); i++) {
192                 String JavaDoc testparameter = (String JavaDoc)m_dynamicParameter.elementAt(i);
193                 if (parameters.containsKey(testparameter)) {
194                     return null;
195                 }
196             }
197         }
198         CmsRequestContext reqContext = cms.getRequestContext();
199         String JavaDoc groupKey = "";
200 // if(m_group){
201
// groupKey = reqContext.currentGroup().getName();
202
// if((m_cacheGroups != null) && (!m_cacheGroups.isEmpty())){
203
// if(!m_cacheGroups.contains(groupKey)){
204
// return null;
205
// }
206
// }
207
// }
208

209         // ok, a cachekey exists. lets put it together
210
// first we need the scheme of the request
211
String JavaDoc scheme = "http";
212         try {
213             scheme = (CmsXmlTemplateLoader.getRequest(cms.getRequestContext()).getOriginalRequest()).getScheme();
214         } catch (Exception JavaDoc e) {
215             // noop
216
}
217         String JavaDoc key = "key_"+scheme+"_";
218         if (m_uri) {
219             key += reqContext.getUri();
220         }
221         if (m_user) {
222             key += reqContext.currentUser().getName();
223         }
224         key += groupKey;
225         if ((m_cacheParameter != null) && (!m_cacheParameter.isEmpty())) {
226             for (int i=0; i < m_cacheParameter.size(); i++) {
227                 String JavaDoc para = (String JavaDoc)m_cacheParameter.elementAt(i);
228                 if (parameters.containsKey(para)) {
229                     key += (String JavaDoc)parameters.get(para);
230                 }
231             }
232         }
233         if (key.equals("")) {
234             return null;
235         }
236         // now add the elementdefinition-parameters
237
String JavaDoc elementName = (String JavaDoc)parameters.get("_ELEMENT_");
238         if (elementName == null) {
239             // this is the root-template
240
elementName = com.opencms.core.I_CmsConstants.C_ROOT_TEMPLATE_NAME;
241         }
242         Enumeration JavaDoc paramKeys = parameters.keys();
243         while (paramKeys.hasMoreElements()) {
244             String JavaDoc paramKey = (String JavaDoc) paramKeys.nextElement();
245             if (paramKey.startsWith(elementName)) {
246                 key += paramKey + "=" + parameters.get(paramKey) + ";";
247             }
248         }
249         return key;
250     }
251
252     /**
253      * Defines if the group is part of the cacheKey.<p>
254      *
255      * @param groupCache if true the group will be part of the cache key.
256      */

257     public void setCacheGroups(boolean groupCache) {
258         m_group = groupCache;
259     }
260
261     /**
262      * If this Vector is set the groups are part of the cache key and the element
263      * will be cacheable only if the current group is in the Vector. If not it is dynamic.<p>
264      *
265      * @param groupNames A Vector with the names of the groups for which the element is cacheable.
266      */

267     public void setCacheGroups(Vector JavaDoc groupNames) {
268 // m_group = true;
269
// m_cacheGroups = groupNames;
270
// this is for style reasons...
271
groupNames.hashCode();
272     }
273
274     /**
275      * Includes the user in the cache key.<p>
276      *
277      * @param userCache if true the user will be part of the cachekey.
278      */

279     public void setCacheUser(boolean userCache) {
280         m_user = userCache;
281         //autoSetExternalCache();
282
}
283
284     /**
285      * Includes the uri in the cache key.<p>
286      *
287      * @param uriCache if true the uri will be part of the cachekey.
288      */

289     public void setCacheUri(boolean uriCache) {
290         m_uri = uriCache;
291         m_renewAfterEveryPublish = true;
292     }
293
294     /**
295      * If this Vector is set the values of each of the parameters in the Vector
296      * will appear in the cachekey.<p>
297      *
298      * @param parameterNames the parameter that should take part in the cachkey generation.
299      */

300     public void setCacheParameters(Vector JavaDoc parameterNames) {
301         m_cacheParameter = parameterNames;
302     }
303
304     /**
305      * If one of this parameters apear in the request the element is dynamic! (no cachekey).<p>
306      *
307      * @param parameterNames the names of the parameter that make a element dynamic
308      */

309     public void setNoCacheParameters(Vector JavaDoc parameterNames) {
310         m_dynamicParameter = parameterNames;
311     }
312
313     /**
314      * Method for setting all caching properties given boolean
315      * values.
316      * @param internal Initial value for "internal cacheable" property.
317      * @param proxyPriv Initial value for "proxy private cacheable" property.
318      * @param proxyPub Initial value for "internal cacheable" property.
319      * @param export Initial value for "exportable" property.
320      * @param stream Initial value for "streamable" property.
321      */

322     protected void setExternalCaching(boolean internal, boolean proxyPriv, boolean proxyPub, boolean export, boolean stream) {
323         m_cd = 0;
324         m_cd |= internal?C_CACHE_INTERNAL:0;
325         m_cd |= proxyPriv?C_CACHE_PROXY_PRIVATE:0;
326         m_cd |= proxyPub?C_CACHE_PROXY_PUBLIC:0;
327         m_cd |= export?C_CACHE_EXPORT:0;
328         m_cd |= stream?C_CACHE_STREAM:0;
329     }
330
331     /**
332      * Merge the current CmsCacheDirective object with another cache directive.
333      * Resulting properties will be build by a conjunction (logical AND) of
334      * the two source properties.<p>
335      *
336      * @param cd CmsCacheDirectives to be merged.
337      */

338     public void merge(A_CmsCacheDirectives cd) {
339         m_cd &= cd.m_cd;
340     }
341
342     /**
343      * Get the state of the "proxy private cacheable" property.<p>
344      *
345      * @return <code>true</code> if proxy private caching is possible, <code>false</code> otherwise.
346      */

347     public boolean isProxyPrivateCacheable() {
348         return (m_cd & C_CACHE_PROXY_PRIVATE) == C_CACHE_PROXY_PRIVATE;
349     }
350
351     /**
352      * Get the state of the "proxy public cacheable" property.<p>
353      *
354      * @return <code>true</code> if proxy public caching is possible, <code>false</code> otherwise.
355      */

356     public boolean isProxyPublicCacheable() {
357         return (m_cd & C_CACHE_PROXY_PUBLIC) == C_CACHE_PROXY_PUBLIC;
358     }
359
360     /**
361      * Get the state of the "exporting ability" property.<p>
362      *
363      * @return <code>true</code> if exporting is possible, <code>false</code> otherwise.
364      */

365     public boolean isExportable() {
366         return (m_cd & C_CACHE_EXPORT) == C_CACHE_EXPORT;
367     }
368
369     /**
370      * Get the state of the "streaming ability" property.<p>
371      *
372      * @return <code>true</code> if streaming is possible, <code>false</code> otherwise.
373      */

374     public boolean isStreamable() {
375         return (m_cd & C_CACHE_STREAM) == C_CACHE_STREAM;
376     }
377
378     /**
379      * Get the userSet information.<p>
380      *
381      * @return <code>true</code> if the proxyPrivate flag was set, <code>false</code> otherwise.
382      */

383     public boolean userSetProxyPrivate() {
384         return m_userSetProxyPrivate;
385     }
386     /**
387      * Get the userSet information.<p>
388      *
389      * @return <code>true</code> if the proxyPublic flag was set, <code>false</code> otherwise.
390      */

391     public boolean userSetProxyPublic() {
392         return m_userSetProxyPublic;
393     }
394     /**
395      * Get the userSet information.<p>
396      * @return <code>true</code> if the export flag was set, <code>false</code> otherwise.
397      */

398     public boolean userSetExport() {
399         return m_userSetExport;
400     }
401
402
403 }
Popular Tags