01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.cocoon.acting;
18:
19: import java.util.Map;
20:
21: import org.apache.avalon.framework.parameters.Parameters;
22: import org.apache.avalon.framework.thread.ThreadSafe;
23:
24: import org.apache.cocoon.caching.Cache;
25: import org.apache.cocoon.caching.impl.EventAwareCacheImpl;
26: import org.apache.cocoon.caching.validity.NamedEvent;
27: import org.apache.cocoon.environment.Redirector;
28: import org.apache.cocoon.environment.SourceResolver;
29:
30: /**
31: * Simple action to cause notification of a NamedEvent to an EventAwareCacheImpl.
32: * The event name is taken from a sitemap parameter named "event".
33: *
34: * This action returns null (fails) if the configured event is null or the
35: * empty string. Otherwise, it succeeds and returns an empty Map.
36: *
37: * This is used in the Event based cache example.
38: *
39: * @author Geoff Howard (ghoward@apache.org)
40: * @version CVS $Id: CacheEventAction.java 433543 2006-08-22 06:22:54Z crossley $
41: */
42: public class CacheEventAction extends ServiceableAction implements
43: ThreadSafe {
44:
45: /**
46: * Lookup the cache and call its processEvent method. Returns an
47: * empty map to signal success.
48: */
49: public Map act(Redirector redirector, SourceResolver resolver,
50: Map objectModel, String src, Parameters par)
51: throws Exception {
52: final String cacheRole = par.getParameter("cache-role",
53: 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 eventName = par.getParameter("event");
59: if (getLogger().isDebugEnabled()) {
60: getLogger().debug(
61: "Configured for cache event named: "
62: + eventName);
63: }
64: if (eventName == null || "".equals(eventName)) {
65: return null;
66: }
67: ((EventAwareCacheImpl) cache)
68: .processEvent(new NamedEvent(eventName));
69: }
70: } finally {
71: this.manager.release(cache);
72: }
73: return EMPTY_MAP;
74: }
75: }
|