KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > portal > generic > context > CachingResponseHandler


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.context;
51
52 import com.caucho.portal.generic.Cache;
53 import com.caucho.portal.generic.CacheKey;
54
55 import java.io.IOException JavaDoc;
56 import java.io.OutputStream JavaDoc;
57 import java.io.PrintWriter JavaDoc;
58 import java.io.Writer JavaDoc;
59 import java.util.ArrayList JavaDoc;
60 import java.util.LinkedHashMap JavaDoc;
61 import java.util.Map JavaDoc;
62 import java.util.logging.Level JavaDoc;
63
64 public class CachingResponseHandler extends AbstractResponseHandler
65 {
66   private Cache _cache;
67   private String JavaDoc _namespace;
68   private int _expirationCache ;
69   private boolean _isPrivate;
70
71   private Writer JavaDoc _cacheWriter;
72   private OutputStream JavaDoc _cacheOutputStream;
73
74   private LinkedHashMap JavaDoc<String JavaDoc, Object JavaDoc> _propertiesMap;
75   private LinkedHashMap JavaDoc<String JavaDoc, Object JavaDoc> _cachePropertiesMap;
76
77   public CachingResponseHandler( ResponseHandler responseHandler,
78                                  Cache cache, String JavaDoc namespace,
79                                  int expirationCache, boolean isPrivate )
80   {
81     super(responseHandler);
82     _cache = cache;
83     _namespace = namespace;
84     _expirationCache = expirationCache;
85     _isPrivate = isPrivate;
86   }
87
88   private LinkedHashMap JavaDoc<String JavaDoc, Object JavaDoc> getMapForProperty(String JavaDoc name)
89   {
90     if (name.startsWith("Cache-")) {
91       if (_cachePropertiesMap == null)
92         _cachePropertiesMap = new LinkedHashMap JavaDoc<String JavaDoc, Object JavaDoc>();
93
94       return _cachePropertiesMap;
95     }
96     else {
97       if (_propertiesMap == null)
98         _propertiesMap = new LinkedHashMap JavaDoc<String JavaDoc, Object JavaDoc>();
99
100       return _propertiesMap;
101     }
102   }
103
104   public void setProperty(String JavaDoc name, String JavaDoc value)
105   {
106     super.setProperty(name, value);
107
108     Map JavaDoc<String JavaDoc, Object JavaDoc> map = getMapForProperty(name);;
109
110     map.put(name, value);
111   }
112
113   public void addProperty(String JavaDoc name, String JavaDoc value)
114   {
115     super.addProperty(name, value);
116
117     Map JavaDoc<String JavaDoc, Object JavaDoc> map = getMapForProperty(name);
118
119     Object JavaDoc currentValue = map.get(name);
120
121     if (currentValue == null) {
122       ArrayList JavaDoc<String JavaDoc> values = new ArrayList JavaDoc<String JavaDoc>();
123       values.add(value);
124       map.put(name, values);
125     }
126     else if (currentValue instanceof String JavaDoc) {
127       ArrayList JavaDoc<String JavaDoc> values = new ArrayList JavaDoc<String JavaDoc>();
128       values.add( (String JavaDoc) currentValue);
129       values.add(value);
130       map.put(name, values);
131     }
132     else {
133       ArrayList JavaDoc<String JavaDoc> values = (ArrayList JavaDoc<String JavaDoc>) currentValue;
134       values.add(value);
135     }
136   }
137
138   public void finish( int expirationCache,
139                       CacheKey cacheKey,
140                       Map JavaDoc<String JavaDoc, String JavaDoc> requestAttributesMap )
141     throws IOException JavaDoc
142   {
143     Cache cache = _cache;
144     OutputStream JavaDoc cacheOutputStream = _cacheOutputStream;
145     Writer JavaDoc cacheWriter = _cacheWriter;
146
147     _cacheOutputStream = null;
148     _cacheWriter = null;
149     _cache = null;
150     _namespace = null;
151     _isPrivate = false;
152     _expirationCache = 0;
153
154     String JavaDoc enc = null;
155
156     boolean fail = true;
157
158     try {
159       if (cacheWriter != null)
160         enc = getCharacterEncoding();
161
162       boolean isError = isError();
163
164       super.finish();
165
166       if (!isError) {
167
168         Map JavaDoc<String JavaDoc, Object JavaDoc> cachePropertiesMap = null;
169         Map JavaDoc<String JavaDoc, Object JavaDoc> propertiesMap = null;
170
171         if ( _cachePropertiesMap != null && !_cachePropertiesMap.isEmpty())
172           cachePropertiesMap = _cachePropertiesMap;
173
174         if ( _propertiesMap != null && !_propertiesMap.isEmpty())
175           propertiesMap = _propertiesMap;
176
177         if (cacheWriter != null) {
178           cache.finishCaching(
179             cacheWriter,
180             expirationCache,
181             cacheKey,
182             enc,
183             cachePropertiesMap,
184             propertiesMap,
185             requestAttributesMap );
186
187           fail = false;
188         }
189
190         if (cacheOutputStream != null) {
191           cache.finishCaching(
192             cacheOutputStream,
193             expirationCache,
194             cacheKey,
195             cachePropertiesMap,
196             propertiesMap,
197             requestAttributesMap );
198
199           fail = false;
200         }
201       }
202     }
203     finally {
204
205       if (fail) {
206         if (cacheWriter != null) {
207           cache.finishCaching(cacheWriter, 0, null, null, null, null, null);
208         }
209
210         if (cacheOutputStream != null) {
211           cache.finishCaching(cacheOutputStream, 0, null, null, null, null);
212         }
213       }
214     }
215   }
216
217   public void finish()
218     throws IOException JavaDoc
219   {
220     throw new UnsupportedOperationException JavaDoc();
221   }
222
223   public PrintWriter JavaDoc getWriter()
224     throws IOException JavaDoc
225   {
226     PrintWriter JavaDoc writer = super.getWriter();
227
228     try {
229       _cacheWriter = _cache.getCachingWriter(_namespace, _expirationCache, _isPrivate);
230     }
231     catch (Exception JavaDoc ex) {
232       log.log(Level.WARNING, ex.toString(), ex);
233     }
234
235     return writer;
236   }
237
238   public OutputStream JavaDoc getOutputStream()
239     throws IOException JavaDoc
240   {
241     OutputStream JavaDoc outputStream = super.getOutputStream();
242
243     try {
244       _cacheOutputStream
245         = _cache.getCachingOutputStream(_namespace, _expirationCache, _isPrivate);
246     }
247     catch (Exception JavaDoc ex) {
248       log.log(Level.WARNING, ex.toString(), ex);
249     }
250
251     return outputStream;
252   }
253
254   protected void print(char buf[], int off, int len)
255     throws IOException JavaDoc
256   {
257     super.print(buf, off, len);
258
259     if (_cacheWriter != null)
260       _cacheWriter.write(buf, off, len);
261   }
262
263   protected void print(String JavaDoc str, int off, int len)
264     throws IOException JavaDoc
265   {
266     super.print(str, off, len);
267
268     if (_cacheWriter != null)
269       _cacheWriter.write(str, off, len);
270   }
271
272   protected void print(char c)
273     throws IOException JavaDoc
274   {
275     super.print(c);
276
277     if (_cacheWriter != null)
278       _cacheWriter.write((int)c);
279   }
280
281   protected void write(byte[] buf, int off, int len)
282     throws IOException JavaDoc
283   {
284     super.write(buf, off, len);
285
286     if (_cacheOutputStream != null)
287       _cacheOutputStream.write(buf, off, len);
288   }
289
290   protected void write(byte b)
291     throws IOException JavaDoc
292   {
293     super.write(b);
294
295     if (_cacheOutputStream != null)
296       _cacheOutputStream.write((int)b);
297   }
298 }
299
Popular Tags