KickJava   Java API By Example, From Geeks To Geeks.

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


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  */
package org.apache.cocoon.caching.impl;
16
17 import javax.jms.Message JavaDoc;
18
19 import org.apache.avalon.framework.parameters.ParameterException;
20 import org.apache.avalon.framework.parameters.Parameters;
21 import org.apache.avalon.framework.thread.ThreadSafe;
22 import org.apache.cocoon.caching.Cache;
23 import org.apache.cocoon.caching.EventAware;
24 import org.apache.cocoon.caching.validity.Event;
25 import org.apache.cocoon.caching.validity.NamedEvent;
26 import org.apache.cocoon.components.jms.AbstractMessageListener;
27
28 /**
29  * JMS listener will notify an {@link org.apache.cocoon.caching.EventAware} component
30  * of external events. This could be used for example to do external cache invalidation.
31  *
32  * <p>
33  * Besides those inherited from
34  * {@link org.apache.cocoon.components.jms.AbstractMessageListener}
35  * parameters are:
36  * </p>
37  * <table border="1">
38  * <tbody>
39  * <tr>
40  * <th align="left">parameter</th>
41  * <th align="left">required</th>
42  * <th align="left">default</th>
43  * <th align="left">description</th>
44  * </tr>
45  * <tr>
46  * <td valign="top">eventcache-role</td>
47  * <td valign="top">no</td>
48  * <td valign="top">org.apache.cocoon.caching.Cache/EventAware</td>
49  * <td valign="top">The role name to lookup the event cache from the service manager.</td>
50  * </tr>
51  * </tbody>
52  * </table>
53  */

54 public class JMSEventMessageListener extends AbstractMessageListener implements ThreadSafe {
55
56     // ---------------------------------------------------- Constants
57

58     private static final String JavaDoc DEFAULT_EVENTCACHE_ROLE = Cache.ROLE + "/EventAware";
59     private static final String JavaDoc EVENTCACHE_ROLE_PARAM = "eventcache-role";
60
61     // ---------------------------------------------------- Instance variables
62

63     private String JavaDoc m_eventAwareRole;
64     private EventAware m_eventCache;
65
66     // ---------------------------------------------------- Lifecycle
67

68     public JMSEventMessageListener() {
69     }
70
71     public void parameterize(Parameters parameters) throws ParameterException {
72         super.parameterize(parameters);
73         m_eventAwareRole = parameters.getParameter(EVENTCACHE_ROLE_PARAM, DEFAULT_EVENTCACHE_ROLE);
74     }
75
76     public void initialize() throws Exception JavaDoc {
77         super.initialize();
78         m_eventCache = (EventAware) m_manager.lookup(m_eventAwareRole);
79     }
80
81     public void dispose() {
82         super.dispose();
83         this.m_manager.release(m_eventCache);
84     }
85
86     /**
87      * Notifies the event cache of events occurred.
88      */

89     public synchronized void onMessage(Message JavaDoc message) {
90         if (getLogger().isDebugEnabled()) {
91             getLogger().debug("Receiving message: " + message);
92         }
93         final Event[] events = eventsFromMessage(message);
94         for (int i = 0; i < events.length; i++) {
95             if (getLogger().isDebugEnabled()) {
96                 getLogger().debug("Notifying " + m_eventAwareRole + " of " + events[i]);
97             }
98             m_eventCache.processEvent(events[i]);
99         }
100     }
101
102     /**
103      * Convert the message contents to (a series of) cache event. The default implementation
104      * assumes that the message contains the trigger name, a '|', and a table name.
105      * It extracts the tablename and creates a NamedEvent with it.
106      * Override this method to provide a custom message to event mapping.
107      *
108      * @param message the JMS message.
109      * @return the cache event.
110      */

111     protected Event[] eventsFromMessage(Message JavaDoc message) {
112         String JavaDoc name = message.toString();
113         int pos = name.indexOf('|');
114         return new Event[] { new NamedEvent(name.substring(pos + 1)) };
115     }
116     
117 }
118
Popular Tags