KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > acting > CacheEventAction


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.acting;
17
18 import java.util.Map JavaDoc;
19
20 import org.apache.avalon.framework.parameters.Parameters;
21 import org.apache.avalon.framework.thread.ThreadSafe;
22
23 import org.apache.cocoon.caching.Cache;
24 import org.apache.cocoon.caching.impl.EventAwareCacheImpl;
25 import org.apache.cocoon.caching.validity.NamedEvent;
26 import org.apache.cocoon.environment.Redirector;
27 import org.apache.cocoon.environment.SourceResolver;
28
29 /**
30  * Simple action to cause notification of a NamedEvent to an EventAwareCacheImpl.
31  * The event name is taken from a sitemap parameter named "event".
32  *
33  * This action returns null (fails) if the configured event is null or the
34  * empty string. Otherwise, it succeeds and returns an empty Map.
35  *
36  * This is used in the Event based cache example.
37  *
38  * @author Geoff Howard (ghoward@apache.org)
39  * @version CVS $Id: CacheEventAction.java 30932 2004-07-29 17:35:38Z vgritsenko $
40  */

41 public class CacheEventAction extends ServiceableAction implements ThreadSafe {
42
43     /**
44      * Lookup the cache and call its processEvent method. Returns an
45      * empty map to signal success.
46      */

47     public Map JavaDoc act(Redirector redirector,
48                     SourceResolver resolver,
49                     Map JavaDoc objectModel,
50                     String JavaDoc src,
51                     Parameters par
52     ) throws Exception JavaDoc {
53         final String JavaDoc cacheRole = par.getParameter("cache-role", Cache.ROLE + "/EventAware");
54         Cache cache = (Cache)this.manager.lookup(cacheRole);
55         try {
56             // FIXME - This cast might not work with every container!
57
if (cache instanceof EventAwareCacheImpl) {
58                 String JavaDoc eventName = par.getParameter("event");
59                 if (getLogger().isDebugEnabled()) {
60                     getLogger().debug("Configured for cache event named: " + eventName);
61                 }
62                 if (eventName == null || "".equals(eventName)) {
63                     return null;
64                 }
65                 ((EventAwareCacheImpl)cache).processEvent(
66                                                     new NamedEvent(eventName));
67             }
68         } finally {
69             this.manager.release(cache);
70         }
71         return EMPTY_MAP;
72     }
73 }
Popular Tags