KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > caching > impl > CacheImpl


1 /*
2  * Copyright 1999-2005 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.caching.impl;
17
18 import java.io.IOException JavaDoc;
19 import java.io.Serializable JavaDoc;
20
21 import org.apache.avalon.framework.activity.Disposable;
22 import org.apache.avalon.framework.logger.AbstractLogEnabled;
23 import org.apache.avalon.framework.parameters.ParameterException;
24 import org.apache.avalon.framework.parameters.Parameterizable;
25 import org.apache.avalon.framework.parameters.Parameters;
26 import org.apache.avalon.framework.service.ServiceException;
27 import org.apache.avalon.framework.service.ServiceManager;
28 import org.apache.avalon.framework.service.Serviceable;
29 import org.apache.avalon.framework.thread.ThreadSafe;
30 import org.apache.cocoon.ProcessingException;
31 import org.apache.cocoon.caching.Cache;
32 import org.apache.cocoon.caching.CachedResponse;
33 import org.apache.excalibur.store.Store;
34
35 /**
36  * This is the Cocoon cache. This component is responsible for storing
37  * and retrieving cached responses. It can be used to monitor the cache
38  * or the investigate which responses are cached etc.
39  * This component will grow!
40  *
41  * @since 2.1
42  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
43  * @version CVS $Id: CacheImpl.java 209437 2005-07-06 09:31:18Z cziegeler $
44  */

45 public class CacheImpl
46 extends AbstractLogEnabled
47 implements Cache, ThreadSafe, Serviceable, Disposable, Parameterizable {
48
49     /** The store containing the cached responses */
50     protected Store store;
51
52     /** The service manager */
53     protected ServiceManager manager;
54
55     /**
56      * Serviceable Interface
57      */

58     public void service (ServiceManager manager) throws ServiceException {
59         this.manager = manager;
60     }
61
62     /**
63      * Disposable Interface
64      */

65     public void dispose() {
66         this.manager.release(this.store);
67         this.store = null;
68         this.manager = null;
69     }
70
71     /**
72      * Store a cached response
73      * @param key the key used by the caching algorithm to identify the
74      * request
75      * @param response the cached response
76      */

77     public void store(Serializable JavaDoc key,
78                       CachedResponse response)
79     throws ProcessingException {
80         if ( this.getLogger().isInfoEnabled()) {
81             this.getLogger().info("Caching new response for " + key);
82         }
83         try {
84             this.store.store(key, response);
85         } catch (IOException JavaDoc ioe) {
86             throw new ProcessingException("Unable to cache response.", ioe);
87         }
88     }
89
90     /**
91      * Get a cached response.
92      * If it is not available <code>null</code> is returned.
93      * @param key the key used by the caching algorithm to identify the
94      * request
95      */

96     public CachedResponse get(Serializable JavaDoc key) {
97         final CachedResponse r = (CachedResponse)this.store.get(key);
98         if ( this.getLogger().isDebugEnabled()) {
99             this.getLogger().debug("Cached response for " + key + " : " +
100                                    (r == null ? "not found" : "found"));
101         }
102         return r;
103     }
104
105     /**
106      * Remove a cached response.
107      * If it is not available no operation is performed.
108      * @param key the key used by the caching algorithm to identify the
109      * request
110      */

111     public void remove(Serializable JavaDoc key) {
112         if ( this.getLogger().isInfoEnabled()) {
113             this.getLogger().info("Removing cached response for " + key);
114         }
115         this.store.remove(key);
116     }
117
118     /**
119      * clear cache of all cached responses
120      */

121     public void clear() {
122         if ( this.getLogger().isInfoEnabled()) {
123             this.getLogger().info("Clearing cache");
124         }
125         // FIXME this clears the whole store!
126
this.store.clear();
127     }
128
129     /**
130      * See if a response is cached under this key
131      */

132     public boolean containsKey(Serializable JavaDoc key) {
133         return this.store.containsKey(key);
134     }
135
136     /* (non-Javadoc)
137      * @see org.apache.avalon.framework.parameters.Parameterizable#parameterize(org.apache.avalon.framework.parameters.Parameters)
138      */

139     public void parameterize(Parameters parameters) throws ParameterException {
140         String JavaDoc storeName = parameters.getParameter("store", Store.ROLE);
141         try {
142             this.store = (Store)this.manager.lookup(storeName);
143         } catch (ServiceException e) {
144             throw new ParameterException("Unable to lookup store: " + storeName, e);
145         }
146     }
147
148 }
149
Popular Tags