KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > oscache > plugins > clustersupport > AbstractBroadcastingListener


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.oscache.plugins.clustersupport;
6
7 import com.opensymphony.oscache.base.*;
8 import com.opensymphony.oscache.base.events.*;
9
10 import org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12
13 import java.util.Date JavaDoc;
14
15 /**
16  * Implementation of a CacheEntryEventListener. It broadcasts the flush events
17  * across a cluster to other listening caches. Note that this listener cannot
18  * be used in conjection with session caches.
19  *
20  * @version $Revision: 1.1 $
21  * @author <a HREF="&#109;a&#105;&#108;&#116;&#111;:chris&#64;swebtec.&#99;&#111;&#109;">Chris Miller</a>
22  */

23 public abstract class AbstractBroadcastingListener implements CacheEntryEventListener, LifecycleAware {
24     private final static Log log = LogFactory.getLog(AbstractBroadcastingListener.class);
25
26     /**
27      * The name to use for the origin of cluster events. Using this ensures
28      * events are not fired recursively back over the cluster.
29      */

30     protected static final String JavaDoc CLUSTER_ORIGIN = "CLUSTER";
31     protected Cache cache = null;
32
33     public AbstractBroadcastingListener() {
34         if (log.isInfoEnabled()) {
35             log.info("AbstractBroadcastingListener registered");
36         }
37     }
38
39     /**
40      * Event fired when an entry is flushed from the cache. This broadcasts
41      * the flush message to any listening nodes on the network.
42      */

43     public void cacheEntryFlushed(CacheEntryEvent event) {
44         if (!Cache.NESTED_EVENT.equals(event.getOrigin()) && !CLUSTER_ORIGIN.equals(event.getOrigin())) {
45             if (log.isDebugEnabled()) {
46                 log.debug("cacheEntryFlushed called (" + event + ")");
47             }
48
49             sendNotification(new ClusterNotification(ClusterNotification.FLUSH_KEY, event.getKey()));
50         }
51     }
52
53     /**
54      * Event fired when an entry is removed from the cache. This broadcasts
55      * the remove method to any listening nodes on the network, as long as
56      * this event wasn't from a broadcast in the first place.
57      */

58     public void cacheGroupFlushed(CacheGroupEvent event) {
59         if (!Cache.NESTED_EVENT.equals(event.getOrigin()) && !CLUSTER_ORIGIN.equals(event.getOrigin())) {
60             if (log.isDebugEnabled()) {
61                 log.debug("cacheGroupFushed called (" + event + ")");
62             }
63
64             sendNotification(new ClusterNotification(ClusterNotification.FLUSH_GROUP, event.getGroup()));
65         }
66     }
67
68     public void cachePatternFlushed(CachePatternEvent event) {
69         if (!Cache.NESTED_EVENT.equals(event.getOrigin()) && !CLUSTER_ORIGIN.equals(event.getOrigin())) {
70             if (log.isDebugEnabled()) {
71                 log.debug("cachePatternFushed called (" + event + ")");
72             }
73
74             sendNotification(new ClusterNotification(ClusterNotification.FLUSH_PATTERN, event.getPattern()));
75         }
76     }
77
78     public void cacheFlushed(CachewideEvent event) {
79         if (!Cache.NESTED_EVENT.equals(event.getOrigin()) && !CLUSTER_ORIGIN.equals(event.getOrigin())) {
80             if (log.isDebugEnabled()) {
81                 log.debug("cacheFushed called (" + event + ")");
82             }
83
84             sendNotification(new ClusterNotification(ClusterNotification.FLUSH_CACHE, event.getDate()));
85         }
86     }
87
88     // --------------------------------------------------------
89
// The remaining events are of no interest to this listener
90
// --------------------------------------------------------
91
public void cacheEntryAdded(CacheEntryEvent event) {
92     }
93
94     public void cacheEntryRemoved(CacheEntryEvent event) {
95     }
96
97     public void cacheEntryUpdated(CacheEntryEvent event) {
98     }
99
100     public void cacheGroupAdded(CacheGroupEvent event) {
101     }
102
103     public void cacheGroupEntryAdded(CacheGroupEvent event) {
104     }
105
106     public void cacheGroupEntryRemoved(CacheGroupEvent event) {
107     }
108
109     public void cacheGroupRemoved(CacheGroupEvent event) {
110     }
111
112     public void cacheGroupUpdated(CacheGroupEvent event) {
113     }
114
115     /**
116      * Called by the cache administrator class when a cache is instantiated.
117      *
118      * @param cache the cache instance that this listener is attached to.
119      * @param config The cache's configuration details. This allows the event handler
120      * to initialize itself based on the cache settings, and also to receive <em>additional</em>
121      * settings that were part of the cache configuration but that the cache
122      * itself does not care about. If you are using <code>cache.properties</code>
123      * for your configuration, simply add any additional properties that your event
124      * handler requires and they will be passed through in this parameter.
125      *
126      * @throws InitializationException thrown when there was a problem initializing the
127      * listener. The cache administrator will log this error and disable the listener.
128      */

129     public void initialize(Cache cache, Config config) throws InitializationException {
130         this.cache = cache;
131     }
132
133     /**
134      * Handles incoming notification messages. This method should be called by the
135      * underlying broadcasting implementation when a message is received from another
136      * node in the cluster.
137      *
138      * @param message The incoming cluster notification message object.
139      */

140     public void handleClusterNotification(ClusterNotification message) {
141         if (cache == null) {
142             log.warn("A cluster notification (" + message + ") was received, but no cache is registered on this machine. Notification ignored.");
143
144             return;
145         }
146
147         if (log.isInfoEnabled()) {
148             log.info("Cluster notification (" + message + ") was received.");
149         }
150
151         switch (message.getType()) {
152             case ClusterNotification.FLUSH_KEY:
153                 cache.flushEntry((String JavaDoc) message.getData(), CLUSTER_ORIGIN);
154                 break;
155             case ClusterNotification.FLUSH_GROUP:
156                 cache.flushGroup((String JavaDoc) message.getData(), CLUSTER_ORIGIN);
157                 break;
158             case ClusterNotification.FLUSH_PATTERN:
159                 cache.flushPattern((String JavaDoc) message.getData(), CLUSTER_ORIGIN);
160                 break;
161             case ClusterNotification.FLUSH_CACHE:
162                 cache.flushAll((Date JavaDoc) message.getData(), CLUSTER_ORIGIN);
163                 break;
164             default:
165                 log.error("The cluster notification (" + message + ") is of an unknown type. Notification ignored.");
166         }
167     }
168
169     /**
170      * Called when a cluster notification message is to be broadcast. Implementing
171      * classes should use their underlying transport to broadcast the message across
172      * the cluster.
173      *
174      * @param message The notification message to broadcast.
175      */

176     abstract protected void sendNotification(ClusterNotification message);
177 }
178
Popular Tags