KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > frontend > components > reading > CachingReader


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.outerj.daisy.frontend.components.reading;
17
18 import org.apache.cocoon.reading.Reader;
19 import org.apache.cocoon.ProcessingException;
20 import org.apache.cocoon.caching.Cache;
21 import org.apache.cocoon.caching.CachedResponse;
22 import org.apache.cocoon.environment.SourceResolver;
23 import org.apache.cocoon.environment.Request;
24 import org.apache.cocoon.environment.ObjectModelHelper;
25 import org.apache.avalon.framework.parameters.Parameters;
26 import org.apache.avalon.framework.parameters.ParameterException;
27 import org.apache.avalon.framework.service.Serviceable;
28 import org.apache.avalon.framework.service.ServiceManager;
29 import org.apache.avalon.framework.service.ServiceException;
30 import org.apache.excalibur.source.Source;
31 import org.apache.excalibur.source.SourceValidity;
32 import org.apache.excalibur.source.SourceParameters;
33 import org.apache.excalibur.source.impl.validity.ExpiresValidity;
34 import org.xml.sax.SAXException JavaDoc;
35
36 import java.io.IOException JavaDoc;
37 import java.io.OutputStream JavaDoc;
38 import java.io.InputStream JavaDoc;
39 import java.io.ByteArrayOutputStream JavaDoc;
40 import java.util.Map JavaDoc;
41
42 /**
43  * The stupid solution to forced caching, without resolving the "cocoon:"
44  * pipeline since that costs too much energy.
45  */

46 public class CachingReader implements Reader, Serviceable {
47     private ServiceManager serviceManager;
48     private MyCachedResponse cachedResponse;
49     private OutputStream JavaDoc outputStream;
50
51     public void service(ServiceManager serviceManager) throws ServiceException {
52         this.serviceManager = serviceManager;
53     }
54
55     public void generate() throws IOException JavaDoc, SAXException JavaDoc, ProcessingException {
56         outputStream.write(cachedResponse.getResponse());
57         outputStream.flush();
58     }
59
60     public long getLastModified() {
61         return System.currentTimeMillis();
62     }
63
64     public void setup(SourceResolver sourceResolver, Map JavaDoc objectModel, String JavaDoc src, Parameters parameters) throws ProcessingException, SAXException JavaDoc, IOException JavaDoc {
65         Request request = ObjectModelHelper.getRequest(objectModel);
66         SourceParameters sourceParameters = new SourceParameters(request.getQueryString());
67         long expires;
68         try {
69             expires = parameters.getParameterAsLong("expires") * 1000;
70         } catch (ParameterException e) {
71             throw new ProcessingException(e);
72         }
73
74         // This will be good enough for our needs
75
String JavaDoc cacheKey = "----DAISY-CACHING-READER----" + request.getRequestURI() + "?" + sourceParameters.getQueryString() + "~~" + src;
76
77         Cache cache;
78         try {
79             cache = (Cache) this.serviceManager.lookup(Cache.ROLE);
80         } catch (ServiceException e) {
81             throw new ProcessingException(e);
82         }
83         try {
84             cachedResponse = (MyCachedResponse)cache.get(cacheKey);
85
86             if (cachedResponse != null) {
87                 SourceValidity[] validities = cachedResponse.getValidityObjects();
88                 ExpiresValidity validity = (ExpiresValidity)validities[0];
89                 if (validity.isValid() != SourceValidity.VALID)
90                     cachedResponse = null;
91             }
92
93             if (cachedResponse == null) {
94                 ExpiresValidity validity = new ExpiresValidity(expires);
95                 Source source = sourceResolver.resolveURI(src);
96                 byte[] data = readSource(source);
97                 cachedResponse = new MyCachedResponse(new SourceValidity[] { validity }, data, source.getMimeType());
98                 cache.store(cacheKey, cachedResponse);
99             }
100         } finally {
101             serviceManager.release(cache);
102         }
103     }
104
105     private byte[] readSource(Source source) throws IOException JavaDoc {
106         ByteArrayOutputStream JavaDoc os = new ByteArrayOutputStream JavaDoc();
107         byte[] buffer = new byte[4096];
108         InputStream JavaDoc inputStream = source.getInputStream();
109         int length;
110         while ((length = inputStream.read(buffer)) > -1) {
111             os.write(buffer, 0, length);
112         }
113         inputStream.close();
114         return os.toByteArray();
115     }
116
117
118     public void setOutputStream(OutputStream JavaDoc outputStream) throws IOException JavaDoc {
119         this.outputStream = outputStream;
120     }
121
122     public String JavaDoc getMimeType() {
123         return cachedResponse.getMimeType();
124     }
125
126     public boolean shouldSetContentLength() {
127         return false;
128     }
129
130     static class MyCachedResponse extends CachedResponse {
131         private String JavaDoc mimeType;
132         public MyCachedResponse(SourceValidity[] sourceValidities, byte[] bytes, String JavaDoc mimeType) {
133             super(sourceValidities, bytes);
134             this.mimeType = mimeType;
135         }
136
137         public String JavaDoc getMimeType() {
138             return mimeType;
139         }
140     }
141 }
142
Popular Tags