001: /*
002: * The Apache Software License, Version 1.1
003: *
004: * Copyright (c) 2001-2004 Caucho Technology, Inc. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution, if
019: * any, must include the following acknowlegement:
020: * "This product includes software developed by the
021: * Caucho Technology (http://www.caucho.com/)."
022: * Alternately, this acknowlegement may appear in the software itself,
023: * if and wherever such third-party acknowlegements normally appear.
024: *
025: * 4. The names "Hessian", "Resin", and "Caucho" must not be used to
026: * endorse or promote products derived from this software without prior
027: * written permission. For written permission, please contact
028: * info@caucho.com.
029: *
030: * 5. Products derived from this software may not be called "Resin"
031: * nor may "Resin" appear in their names without prior written
032: * permission of Caucho Technology.
033: *
034: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
035: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
036: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
037: * DISCLAIMED. IN NO EVENT SHALL CAUCHO TECHNOLOGY OR ITS CONTRIBUTORS
038: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
039: * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
040: * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
041: * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
042: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
043: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
044: * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
045: *
046: * @author Sam
047: */
048:
049: package com.caucho.portal.generic;
050:
051: import javax.portlet.RenderRequest;
052: import javax.portlet.RenderResponse;
053: import java.io.IOException;
054: import java.io.OutputStream;
055: import java.io.Writer;
056: import java.util.Map;
057: import java.util.logging.Logger;
058:
059: abstract public class Cache {
060: protected static final Logger log = Logger.getLogger(Cache.class
061: .getName());
062:
063: /**
064: * Send a response from the cache, or return false if there is no
065: * response from the cache.
066: *
067: * The passed cacheKey can be manipulated by the cache without side-effects,
068: * but the cache cannot take ownership of the cacheKey because it may be
069: * pooled by the caller.
070: *
071: * Binary responses should be sent to the OutputStream obtained with
072: * response.getOutputStream().
073: *
074: * Textual response should be sent to the PrintWriter obtained with
075: * response.getWriter().
076: * response.setCharacterEncoding() should be used before getWriter()
077: * to set the character encoding to the character encoding that was in
078: * use when the response was cached.
079: *
080: * If isPrivate is true, then cacheKey.getRequestedSessionId() will have a
081: * non-null value.
082: *
083: * A cache should first perform a lookup with the CacheKey as provided.
084: * If that fails, it should call cacheKey.setLocale(null) and try again.
085: *
086: * The cache should also check the value of cacheKey.getContentType().
087: * If the contentType is null, the cache can respond if it has an entry
088: * that matches for one contentType. If a match is found, the cache
089: * sets the contentType with connection.setContentType().
090: *
091: * @return 0 if no response was written from the cache, otherwise the number
092: * of seconds that a response written from the cache is good for or -1
093: * if the cached reponse is good forever
094: */
095: abstract public int respondFromCache(CacheKey cacheKey,
096: RenderRequest request, RenderResponse response);
097:
098: /**
099: * Called to give the Cache an opportunity to cache the response.
100: *
101: * If the response can be cached, the implementation returns a Writer
102: * that receives the response.
103: *
104: * When the response is completed, finishCaching(Writer) will be called.
105: *
106: * If the response cannot be cached, the implementation returns null.
107: *
108: * At this point, the expirationCache is a best guess and the real value
109: * passed to finishCaching() may be different.
110: *
111: * @param window the portlet configuration for the portlet about to
112: * be rendered
113: *
114: * @param namespace the namespace for the portlet about to be rendered
115: *
116: * @param expirationCache a best guess at the expiration period in seconds,
117: * -1 if unlimited
118: *
119: * @return a Writer that intercepts the content and then writes to
120: * response.getWriter(), or null if the response cannot be cached.
121: */
122: abstract public Writer getCachingWriter(String namespace,
123: int expirationCache, boolean isPrivate) throws IOException;
124:
125: /**
126: * Called immediately before a Portlet is rendered to give the Cache
127: * an opportunity to cache the response.
128: *
129: * If the response can be cached, the implementation returns an OutputStream
130: * that receives the response.
131: *
132: * When the response is completed, finishCaching(OutputStream) will be called.
133: *
134: * If the response cannot be cached, the implementation returns null.
135: *
136: * At this point, the expirationCache is a best guess and the real value
137: * passed to finishCaching() may be different.
138: *
139: * @param window the portlet window for the portlet about to
140: * be rendered
141: *
142: * @param namespace the namespace for the portlet about to be rendered
143: *
144: * @param expirationCache a best guess at the expiration period in seconds,
145: * -1 if unlimited
146: *
147: * @return an OutputStream that intercepts the content and then writes to
148: * response.getOutputStream(), or null if the response cannot be cached.
149: */
150: abstract public OutputStream getCachingOutputStream(
151: String namespace, int expirationCache, boolean isPrivate)
152:
153: throws IOException;
154:
155: /**
156: * Finish with a Writer previously returned by
157: * {@link #startCachingWriter}. If the expirationCache is 0 or the cacheKey is
158: * null, the cached response must be discarded.
159: *
160: * @param writer the writer returned from {@link #startCachingWriter}
161: *
162: * @param expirationCache the updated expirationCache, this may the same
163: * value received in {@link #startWriter}, a new value set by
164: * the portlet while it rendered itself, or 0 if the cache must be
165: * discarded.
166: *
167: * @param cacheKey the {@link CacheKey} that uniquely differentiates this
168: * response from other responses
169: *
170: * @param encoding the encoding for the Writer, the cache needs to call
171: * response.setEncoding(encoding) if it later responds in respondFromCache().
172: *
173: * @param cachePropertiesMap a map of properties that begin with "Cache-",
174: * these may be directives to the cache or may be keys and values that should
175: * distinguish the uniqueness of the Cached value beyond the uniqueness
176: * established by CacheKey, depending on the portal implementation.
177: * These properties should be recreated during respondFromCache().
178: * If the value is a String, setProperty is used. If the value is
179: * an ArrayList<String>, add property is used.
180: *
181: * @param propertiesMap a map of properties that the Cache must recreate
182: * when the cached response is used in a susbsequent call to respondFromCache
183: * If the value is a String, setProperty is used. If the value is
184: * an ArrayList<String>, add property is used.
185: *
186: * @param requestAttributesMap a map of request attributes that the Cache
187: * must recreate when the cached response is used in a susbsequent call to
188: * respondFromCache
189: */
190: abstract public void finishCaching(Writer writer,
191: int expirationCache, CacheKey cacheKey, String encoding,
192: Map<String, Object> cachePropertiesMap,
193: Map<String, Object> propertiesMap,
194: Map<String, String> requestAttributeMap);
195:
196: /**
197: * Finish with an OutputStream previously returned by
198: * {@link #startCachingOutputStream}. If the cacheKey is null or
199: * the expirationCache is 0, the cached response must be discarded.
200: */
201: abstract public void finishCaching(OutputStream outputStream,
202: int expirationCache, CacheKey cacheKey,
203: Map<String, Object> cachePropertiesMap,
204: Map<String, Object> propertiesMap,
205: Map<String, String> requestAttributeMap);
206: }
|