KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > transformation > helpers > ModifiableSourceIncludeCacheStorageProxy


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
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.apache.cocoon.transformation.helpers;
17
18 import java.io.IOException JavaDoc;
19 import java.io.InputStream JavaDoc;
20 import java.io.ObjectInputStream JavaDoc;
21 import java.io.ObjectOutputStream JavaDoc;
22 import java.io.OutputStream JavaDoc;
23 import java.io.Serializable JavaDoc;
24
25 import org.apache.avalon.framework.logger.Logger;
26 import org.apache.cocoon.CascadingIOException;
27 import org.apache.cocoon.util.HashUtil;
28 import org.apache.excalibur.source.ModifiableSource;
29 import org.apache.excalibur.source.Source;
30 import org.apache.excalibur.source.SourceResolver;
31
32 /**
33  * This is the interface between the {@link IncludeCacheManager} and a
34  * {@link Source} object that stores the cached content in a directory
35  * manner.
36  *
37  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
38  * @version CVS $Id: ModifiableSourceIncludeCacheStorageProxy.java 30932 2004-07-29 17:35:38Z vgritsenko $
39  * @since 2.1
40  */

41 public final class ModifiableSourceIncludeCacheStorageProxy
42     implements IncludeCacheStorageProxy {
43
44     private SourceResolver resolver;
45     private String JavaDoc parentURI;
46     private Logger logger;
47     
48     /**
49      * Constructor
50      * @param resolver For source resolving
51      * @param parentURI The "directory"
52      * @param logger A logger for debugging
53      */

54     public ModifiableSourceIncludeCacheStorageProxy(SourceResolver resolver,
55                                              String JavaDoc parentURI,
56                                              Logger logger) {
57         this.resolver = resolver;
58         this.parentURI= parentURI;
59         this.logger = logger;
60     }
61     
62     /**
63      * Calculate the URI for a child
64      * @param uri Child URI
65      * @return String Absolute URI
66      */

67     private String JavaDoc getURI(String JavaDoc uri) {
68         final long hash = HashUtil.hash(uri);
69         final StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(this.parentURI);
70         buffer.append('/');
71         if (hash < 0) {
72             buffer.append('M').append(hash * -1);
73         } else {
74             buffer.append(hash);
75         }
76         buffer.append(".cxml");
77         return buffer.toString();
78     }
79     
80     /**
81      * @see IncludeCacheStorageProxy#get(java.lang.String)
82      */

83     public Serializable JavaDoc get(String JavaDoc uri) {
84         if (logger.isDebugEnabled()) {
85             logger.debug("WSCProxy: Getting content for " + uri);
86         }
87
88         Source child = null;
89         Serializable JavaDoc result = null;
90         try {
91             child = this.resolver.resolveURI(this.getURI(uri));
92
93             if (logger.isDebugEnabled()) {
94                 logger.debug("WSCProxy: Resolved to " + child.getURI());
95             }
96
97             if (child.exists()) {
98                 InputStream JavaDoc is = child.getInputStream();
99                 ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(is);
100                 result = (Serializable JavaDoc)ois.readObject();
101                 ois.close();
102             }
103         } catch (Exception JavaDoc ignore) {
104         } finally {
105             this.resolver.release( child );
106         }
107
108         if (logger.isDebugEnabled()) {
109             logger.debug("WSCProxy: Result for " + uri + " : " + (result == null ? "Not in cache" : "Found"));
110         }
111         return result;
112     }
113
114     /**
115      * @see IncludeCacheStorageProxy#put(java.lang.String, java.io.Serializable)
116      */

117     public void put(String JavaDoc uri, Serializable JavaDoc object)
118     throws IOException JavaDoc {
119         if (logger.isDebugEnabled()) {
120             logger.debug("WSCProxy: Storing content for " + uri);
121         }
122         Source child = null;
123         try {
124             child = this.resolver.resolveURI(this.getURI(uri));
125
126             if (logger.isDebugEnabled()) {
127                 logger.debug("WSCProxy: Resolved to " + child.getURI());
128             }
129
130             OutputStream JavaDoc os;
131             if (child instanceof ModifiableSource) {
132                 os = ((ModifiableSource)child).getOutputStream();
133             } else {
134                 throw new IOException JavaDoc("Source " + uri + " is not writeable.");
135             }
136             ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(os);
137             oos.writeObject(object);
138             oos.flush();
139             oos.close();
140         } catch (IOException JavaDoc io) {
141             throw io;
142         } catch (Exception JavaDoc ignore) {
143             throw new CascadingIOException("Exception.", ignore);
144         } finally {
145             this.resolver.release( child );
146         }
147     }
148
149     /**
150      * @see IncludeCacheStorageProxy#remove(java.lang.String)
151      */

152     public void remove(String JavaDoc uri) {
153         if (logger.isDebugEnabled()) {
154             logger.debug("WSCProxy: Removing content for " + uri);
155         }
156         Source child = null;
157         try {
158             child = this.resolver.resolveURI(this.getURI(uri));
159
160             if (logger.isDebugEnabled()) {
161                 logger.debug("WSCProxy: Resolved to " + child.getURI());
162             }
163
164             if (child instanceof ModifiableSource) {
165                 ((ModifiableSource)child).delete();
166             } else {
167                 throw new IOException JavaDoc("Source " + uri + " is not writeable.");
168             }
169         } catch (Exception JavaDoc ignore) {
170         } finally {
171             this.resolver.release( child );
172         }
173     }
174
175     /**
176      * Compare
177      */

178     public boolean equals(Object JavaDoc object) {
179         if (object instanceof ModifiableSourceIncludeCacheStorageProxy) {
180             return this.parentURI.equals(((ModifiableSourceIncludeCacheStorageProxy)object).parentURI);
181         }
182         return false;
183     }
184
185     /**
186      * Generate a hash code
187      */

188     public int hashCode() {
189         return this.parentURI.hashCode();
190     }
191
192 }
193
Popular Tags