KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > portal > generic > CacheKey


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2001-2004 Caucho Technology, Inc. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution, if
19  * any, must include the following acknowlegement:
20  * "This product includes software developed by the
21  * Caucho Technology (http://www.caucho.com/)."
22  * Alternately, this acknowlegement may appear in the software itself,
23  * if and wherever such third-party acknowlegements normally appear.
24  *
25  * 4. The names "Hessian", "Resin", and "Caucho" must not be used to
26  * endorse or promote products derived from this software without prior
27  * written permission. For written permission, please contact
28  * info@caucho.com.
29  *
30  * 5. Products derived from this software may not be called "Resin"
31  * nor may "Resin" appear in their names without prior written
32  * permission of Caucho Technology.
33  *
34  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
35  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
36  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
37  * DISCLAIMED. IN NO EVENT SHALL CAUCHO TECHNOLOGY OR ITS CONTRIBUTORS
38  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
39  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
40  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
41  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
42  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
43  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
44  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45  *
46  * @author Sam
47  */

48
49
50 package com.caucho.portal.generic;
51
52 import javax.portlet.PortletMode;
53 import javax.portlet.WindowState;
54 import java.util.Locale JavaDoc;
55
56
57 /**
58  * XXX: Cache implementation currently not complete.
59  *
60  * The ConnectionContext should start building a CacheKey
61  * as soon as it sees a window with expiration-cache != 0.
62  * It should do this in the action stage, because each window is
63  * seen in the action stage.
64  * It should add parameters for every child namespace encountered, to whatever
65  * depth.
66  *
67  * Then when it gets to the render stage, it can use the CacheKey
68  * to see if a cached value is available.
69  *
70  * If not, it inserts a CachingPortletWriteStream, and fills the cache.
71  */

72 public class CacheKey {
73   private String JavaDoc _namespace;
74   private int _namespaceValue;
75
76   private PortletMode _portletMode;
77   private int _portletModeValue;
78
79   private WindowState _windowState;
80   private int _windowStateValue;
81
82   private long _value1, _value2; // parameters
83

84   private String JavaDoc _contentType;
85   private int _contentTypeValue;
86   private Locale JavaDoc _locale;
87   private int _localeValue;
88   private boolean _isPrivate = true;
89   private String JavaDoc _sessionId;
90
91   private String JavaDoc _stringValue;
92
93   public void setNamespace(String JavaDoc namespace)
94   {
95     _stringValue = null;
96     _namespace = namespace;
97     _namespaceValue = namespace.hashCode();
98   }
99
100   public void setPortletMode(PortletMode portletMode)
101   {
102     _stringValue = null;
103     _portletMode = portletMode;
104     _portletModeValue = portletMode.hashCode();
105   }
106
107   public void setWindowState(WindowState windowState)
108   {
109     _stringValue = null;
110     _windowState = windowState;
111     _windowStateValue = windowState.hashCode();
112   }
113
114   private void add(String JavaDoc name, String JavaDoc value)
115   {
116     _stringValue = null;
117     int v = (name == null ? 65353 : name.hashCode());
118
119     _value1 += v;
120     _value2 += v + (value == null ? 65353 : value.hashCode());
121   }
122
123   public void addParameter(String JavaDoc name, String JavaDoc[] values)
124   {
125     _stringValue = null;
126     if (values == null || values.length == 0)
127       add(name, null);
128     else {
129       int i = values.length;
130
131       while (i-- > 0)
132         add(name, values[i]);
133     }
134   }
135
136   public void setContentType(String JavaDoc contentType)
137   {
138     _stringValue = null;
139     _contentType = contentType;
140     _contentTypeValue = contentType.hashCode();
141   }
142
143   /**
144    * When a cache key is created to check for a cached response the locale
145    * is set to the value of request.getLocale().
146    * A cache should first check the cache for an entry with the cacheKey
147    * as it is received, and then call cacheKey.setLocale(null) and try again.
148    *
149    * When a cache key is created to cache a response the locale is only set in
150    * the cache key if a portlet has called request.getLocale() or
151    * request.getLocales() or request.setLocale() method. It is set to the locale
152    * set by the portlet with response.setLocale(), or if the portlet does not
153    * call response.setLocale(), the value of request.getLocale().
154    */

155   public void setLocale(Locale JavaDoc locale)
156   {
157     _stringValue = null;
158     _locale = locale;
159     _localeValue = locale.hashCode();
160   }
161
162   public void setPrivate(boolean isPrivate)
163   {
164     _isPrivate = isPrivate;
165   }
166
167   public void setRequestedSessionId(String JavaDoc sessionId)
168   {
169     _stringValue = null;
170     _sessionId = sessionId;
171   }
172
173   public boolean isPrivate()
174   {
175     return _isPrivate;
176   }
177
178   public String JavaDoc getNamespace()
179   {
180     return _namespace;
181   }
182
183   public PortletMode getPortletMode()
184   {
185     return _portletMode;
186   }
187
188   public WindowState getWindowState()
189   {
190     return _windowState;
191   }
192
193   public String JavaDoc getContentType()
194   {
195     return _contentType;
196   }
197
198   public Locale JavaDoc getLocale()
199   {
200     return _locale;
201   }
202
203   /**
204    * Return a session id if isPrivate() is true, null if isPrivate() is false.
205    */

206   public String JavaDoc getRequestedSessionId()
207   {
208     return _isPrivate ? _sessionId : null;
209   }
210
211   /**
212    * Reset to a state similar to that following construction.
213    */

214   public void reset()
215   {
216     _stringValue = null;
217
218     _namespace = null;
219     _namespaceValue = 0;
220     _portletMode = null;
221     _portletModeValue = 0;
222     _windowState = null;
223     _windowStateValue = 0;
224     _value1 = 0;
225     _value2 = 0;
226
227     _localeValue = 0;
228     _locale = null;
229     _contentTypeValue = 0;
230     _contentType = null;
231
232     _isPrivate = true;
233     _sessionId = null;
234   }
235
236   public int hashCode()
237   {
238     int isPrivateValue = _isPrivate ? 31 : 33331;
239
240     int sessionIdValue = _isPrivate && _sessionId != null
241                          ? _sessionId.hashCode()
242                          : 7331;
243     return (int)
244       ((long) _namespaceValue
245               * _portletModeValue
246               * _windowStateValue
247               * _localeValue
248               * _contentTypeValue
249               * isPrivateValue
250               * sessionIdValue
251               * _value1
252               * _value2);
253   }
254
255   public boolean equals(Object JavaDoc o)
256   {
257     if (o == null || !(o instanceof CacheKey))
258       return false;
259
260     CacheKey other = (CacheKey) o;
261
262     return
263       (_namespaceValue == other._namespaceValue)
264       && (_portletModeValue == other._portletModeValue)
265       && (_windowStateValue == other._windowStateValue)
266       && (_value1 == other._value1)
267       && (_value2 == other._value2)
268       && (_localeValue == other._localeValue)
269       && (_contentTypeValue == other._contentTypeValue)
270       && (_isPrivate == other._isPrivate)
271       && (_isPrivate ? _sessionId != null : true)
272       && (_isPrivate ? _sessionId.equals(other._sessionId) : true);
273   }
274
275   static private void encode(long number, StringBuffer JavaDoc buf)
276   {
277     int code;
278     char ch = '-';
279
280     do {
281       code = ((int) number) & 0x3f; // 6 bits
282

283       if (code < 26)
284         ch = (char) ('a' + code);
285       else if (code < 52)
286         ch = (char) ('A' + code - 26);
287       else if (code < 62)
288         ch = (char) ('0' + code - 52);
289       else if (code == 62 || code == 63)
290         ch = '_';
291       else
292         ch = 'z';
293
294       number = number >> 6;
295
296     } while (number != 0);
297   }
298
299   /**
300    * Return a string that uniquely identifies this CacheKey.
301    * The set of characters that form the string is "A-Z a-z 0-9 _".
302    * That means the returned string is suitable as a file name, for example.
303    */

304   public String JavaDoc toString()
305   {
306     if (_stringValue != null)
307       return _stringValue;
308
309     StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
310
311     encode(_value1, buf);
312     encode(_value2, buf);
313
314     int valuesIndex = buf.length();
315
316     encode(_namespaceValue, buf);
317     encode(_portletModeValue, buf);
318     encode(_windowStateValue, buf);
319     encode(_localeValue, buf);
320     encode(_contentTypeValue, buf);
321
322     if (_isPrivate)
323       buf.append(getRequestedSessionId());
324      
325     // mix up the string so that if it is truncated by the cache
326
// there is less likelyhood of significant loss
327

328     int len = buf.length();
329     int half = len / 2;
330
331     int i = valuesIndex;
332     int j = len - 1;
333
334     while (i < half) {
335       char ch = buf.charAt(i);
336       buf.setCharAt(i, buf.charAt(j));
337       buf.setCharAt(j, ch);
338       i += 3;
339       j -= 3;
340     }
341
342     i = valuesIndex;
343     j = half > i ? half : len;
344
345     while (j < len) {
346       char ch = buf.charAt(i);
347       buf.setCharAt(i, buf.charAt(j));
348       buf.setCharAt(j, ch);
349       i += 5;
350       j += 5;
351     }
352
353     // return the string
354

355     _stringValue = buf.toString();
356
357     return _stringValue;
358   }
359 } // CacheKey
360

361
Popular Tags