KickJava   Java API By Example, From Geeks To Geeks.

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


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.caching.impl;
17
18 import java.io.Serializable JavaDoc;
19 import java.util.Iterator JavaDoc;
20
21 import org.apache.avalon.framework.activity.Initializable;
22 import org.apache.avalon.framework.service.ServiceException;
23 import org.apache.avalon.framework.service.ServiceManager;
24 import org.apache.cocoon.ProcessingException;
25 import org.apache.cocoon.caching.CachedResponse;
26 import org.apache.cocoon.caching.EventAware;
27 import org.apache.cocoon.caching.EventRegistry;
28 import org.apache.cocoon.caching.validity.Event;
29 import org.apache.cocoon.caching.validity.EventValidity;
30 import org.apache.cocoon.components.source.impl.SitemapSource;
31 import org.apache.excalibur.source.SourceValidity;
32 import org.apache.excalibur.source.impl.validity.AbstractAggregatedValidity;
33
34 /**
35  * This implementation holds all mappings between Events and PipelineCacheKeys
36  * in two MultiHashMap to facilitate efficient lookup by either as Key.
37  *
38  * @author Geoff Howard (ghoward@apache.org)
39  * @version $Id: EventAwareCacheImpl.java 56667 2004-11-05 14:25:33Z unico $
40  */

41 public class EventAwareCacheImpl extends CacheImpl implements Initializable,
42                                                               EventAware {
43     
44     private ServiceManager m_manager;
45
46     private EventRegistry m_eventRegistry;
47
48     /**
49      * Clears the entire Cache, including all registered event-pipeline key
50      * mappings..
51      */

52     public void clear() {
53         super.clear();
54         m_eventRegistry.clear();
55     }
56     
57     /**
58      * When a new Pipeline key is stored, it needs to be have its
59      * <code>SourceValidity</code> objects examined. For every
60      * <code>EventValidity</code> found, its <code>Event</code> will be
61      * registered with this key in the <code>EventRegistry</code>.
62      *
63      * <code>AggregatedValidity</code> is handled recursively.
64      */

65     public void store(Serializable JavaDoc key,
66                         CachedResponse response)
67                         throws ProcessingException {
68         SourceValidity[] validities = response.getValidityObjects();
69         for (int i=0; i< validities.length;i++) {
70             SourceValidity val = validities[i];
71             examineValidity(val, key);
72         }
73         super.store(key, response);
74     }
75
76     /* (non-Javadoc)
77      * @see org.apache.cocoon.caching.Cache#store(java.io.Serializable, org.apache.cocoon.caching.CachedResponse)
78      
79     public void store(Serializable key, CachedResponse response)
80         throws ProcessingException {
81         // TODO Auto-generated method stub
82         super.store(key, response);
83     }*/

84
85     /**
86      * Look up the EventRegistry
87      */

88     public void service(ServiceManager manager) throws ServiceException {
89         this.m_manager = manager;
90         super.service(manager);
91         this.m_eventRegistry = (EventRegistry)manager.lookup(EventRegistry.ROLE);
92     }
93
94     /**
95      * Un-register this key in the EventRegistry in addition to
96      * removing it from the Store
97      */

98     public void remove(Serializable JavaDoc key) {
99         super.remove(key);
100         m_eventRegistry.removeKey(key);
101     }
102     
103     /**
104      * Receive notification about the occurrence of an Event.
105      * If this event has registered pipeline keys, remove them
106      * from the Store and unregister them
107      * @param e The Event to be processed.
108      */

109     public void processEvent(Event e) {
110         if (e == null) return;
111         Serializable JavaDoc[] keys = m_eventRegistry.keysForEvent(e);
112         if (keys == null) return;
113         for (int i=0;i<keys.length; i++) {
114             if (keys[i] != null) {
115                 if (getLogger().isDebugEnabled()) {
116                     getLogger().debug("Processing cache event, found Pipeline key: " + keys[i].toString());
117                 }
118                 /* every pck associated with this event needs to be
119                  * removed -- regardless of event mapping. and every
120                  * event mapped to those keys needs to be removed
121                  * recursively.
122                  */

123                 remove(keys[i]);
124             }
125         }
126     }
127     
128     /**
129      * Get the EventRegistry ready, and make sure it does not contain
130      * orphaned Event/PipelineKey mappings.
131      */

132     public void initialize() throws Exception JavaDoc {
133         if (!m_eventRegistry.wasRecoverySuccessful()) {
134             super.clear();
135         } else {
136             // Not sure if we want this overhead here, but where else?
137
veryifyEventCache();
138         }
139     }
140     
141     /**
142      * Ensure that all PipelineCacheKeys registered to events still
143      * point to valid cache entries. Having an isTotallyEmpty() on
144      * Store might make this less necessary, as the most likely time
145      * to discover orphaned entries is at startup. This is because
146      * stray events could hang around indefinitely if the cache is
147      * removed abnormally or is not configured with persistence.
148      */

149     public void veryifyEventCache() {
150         Serializable JavaDoc[] keys = m_eventRegistry.allKeys();
151         if (keys == null) return;
152         for (int i=0; i<keys.length; i++) {
153             if (!this.containsKey(keys[i])) {
154                 m_eventRegistry.removeKey(keys[i]);
155                 if (getLogger().isDebugEnabled()) {
156                     getLogger().debug("Cache key no longer valid: " +
157                             keys[i]);
158                 }
159             }
160         }
161     }
162
163     /**
164      * Release resources
165      */

166     public void dispose() {
167         m_manager.release(m_eventRegistry);
168         super.dispose();
169         m_manager = null;
170         m_eventRegistry = null;
171     }
172
173     private void examineValidity(SourceValidity val, Serializable JavaDoc key) {
174         if (val instanceof AbstractAggregatedValidity) {
175             handleAggregatedValidity((AbstractAggregatedValidity)val, key);
176         } else if (val instanceof EventValidity) {
177             handleEventValidity((EventValidity)val, key);
178         } else if (val instanceof SitemapSource.SitemapSourceValidity) {
179             examineValidity(((SitemapSource.SitemapSourceValidity) val).getNestedValidity(), key);
180         }
181     }
182
183     private void handleAggregatedValidity(
184                                     AbstractAggregatedValidity val,
185                                     Serializable JavaDoc key) {
186         // AggregatedValidity must be investigated further.
187
Iterator JavaDoc it = val.getValidities().iterator();
188          while (it.hasNext()) {
189              SourceValidity thisVal = (SourceValidity)it.next();
190              // Allow recursion
191
examineValidity(thisVal, key);
192          }
193     }
194
195     private void handleEventValidity(EventValidity val, Serializable JavaDoc key) {
196         if (getLogger().isDebugEnabled()) {
197             getLogger().debug("Found EventValidity: " + val.toString());
198         }
199         m_eventRegistry.register(val.getEvent(),key);
200     }
201
202 }
203
Popular Tags