KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > cache > JMSCacheMessage


1 /*
2  * ____.
3  * __/\ ______| |__/\. _______
4  * __ .____| | \ | +----+ \
5  * _______| /--| | | - \ _ | : - \_________
6  * \\______: :---| : : | : | \________>
7  * |__\---\_____________:______: :____|____:_____\
8  * /_____|
9  *
10  * . . . i n j a h i a w e t r u s t . . .
11  *
12  *
13  *
14  * ----- BEGIN LICENSE BLOCK -----
15  * Version: JCSL 1.0
16  *
17  * The contents of this file are subject to the Jahia Community Source License
18  * 1.0 or later (the "License"); you may not use this file except in
19  * compliance with the License. You may obtain a copy of the License at
20  * http://www.jahia.org/license
21  *
22  * Software distributed under the License is distributed on an "AS IS" basis,
23  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
24  * for the rights, obligations and limitations governing use of the contents
25  * of the file. The Original and Upgraded Code is the Jahia CMS and Portal
26  * Server. The developer of the Original and Upgraded Code is JAHIA Ltd. JAHIA
27  * Ltd. owns the copyrights in the portions it created. All Rights Reserved.
28  *
29  * The Developer of the Shared Modifications is Jahia Solution Sarl.
30  * Portions created by the Initial Developer are Copyright (C) 2002 by the
31  * Initial Developer. All Rights Reserved.
32  *
33  * Contributor(s):
34  * 05-SEP-2003, Jahia Solutions Sarl, Fulco Houkes : Initial version
35  *
36  * ----- END LICENSE BLOCK -----
37  */

38
39
40 package org.jahia.services.cache;
41
42 import javax.jms.MapMessage JavaDoc;
43 import javax.jms.JMSException JavaDoc;
44 import java.io.ByteArrayOutputStream JavaDoc;
45 import java.io.ObjectOutputStream JavaDoc;
46 import java.io.IOException JavaDoc;
47
48
49 /** This class encapsulates all the needed information that has to be sent to other
50  * clustered jahia caches using JMS.
51  *
52  * @author Fulco Houkes, Copyright (c) 2003 by Jahia Ltd.
53  * @version 1.0
54  * @since Jahia 4.0
55  */

56 class JMSCacheMessage {
57
58     /** the cache name property name. */
59     final public static String JavaDoc CACHE_NAME_KEY = "cacheKey";
60
61     /** the event type property name. */
62     final public static String JavaDoc EVENT_NAME_KEY = "eventKey";
63
64     /** the cache entry key property name. */
65     final public static String JavaDoc ENTRY_KEY = "entryKey";
66
67     /** the cache entry key property name. */
68     final public static String JavaDoc ENTRY_VALUE = "entryValue";
69
70     /** the JMS client ID. This is used to avoid receiving cyclic messages. */
71     final public static String JavaDoc CLIENT_KEY = "clientKey";
72
73
74     /** logging. */
75     final private static org.apache.log4j.Logger logger =
76             org.apache.log4j.Logger.getLogger (JMSCacheMessage.class);
77
78
79     /** this type of message will instruct the cache to flush all it's entries. */
80     final public static int FLUSH_EVENT = 1;
81
82     /** this type of message instructs the cache a new (or updated) entry has been
83      * added into the cache. */

84     final public static int PUT_EVENT = 2;
85
86     /** this type of message instructs the cache to remove the associated cache entry. */
87     final public static int REMOVE_EVENT = 3;
88
89
90     /** the cache name to which the message should be sent. */
91     private String JavaDoc cacheName;
92
93     /** the message type (flush, put, remove). */
94     private int messageType;
95
96     /** the cache entry key to which this message is associated. */
97     private Object JavaDoc entryKey;
98
99     /** the cache entry value for this message */
100     private Object JavaDoc entryValue;
101
102
103
104     /** Creates a new <code>JMSCacheMessage</code> instance.
105      *
106      * @param cacheName the associated cache name
107      * @param messageType the message type
108      * @param entryKey the cache entry key associated with the message
109      * @param entryValue the cache entry value for this message
110      */

111     protected JMSCacheMessage (final String JavaDoc cacheName, final int messageType,
112                                final Object JavaDoc entryKey, final Object JavaDoc entryValue)
113     {
114         this.cacheName = cacheName;
115         this.messageType = messageType;
116         this.entryKey = entryKey;
117         this.entryValue = entryValue;
118     }
119
120     /** Retrieve the information whether the message is of type <code>EVENT_FLUSH</code>.
121      *
122      * @return <code>true</code> if this message is of type <code>EVENT_FLUSH</code>
123      */

124     final public boolean isFlush () {
125         return messageType == FLUSH_EVENT;
126     }
127
128     /** Retrieve the information whether the message is of type <code>EVENT_REMOVE</code>.
129      *
130      * @return <code>true</code> if this message is of type <code>EVENT_REMOVE</code>
131      */

132     final public boolean isRemove () {
133         return messageType == REMOVE_EVENT;
134     }
135
136     /** Retrieve the information whether the message is of type <code>EVENT_PUT</code>.
137      *
138      * @return <code>true</code> if this message is of type <code>EVENT_PUT</code>
139      */

140     final public boolean isPut () {
141         return messageType == PUT_EVENT;
142     }
143
144     /** Retrieve the cache name to which this message should be sent.
145      *
146      * @return the related cache name
147      */

148     final public String JavaDoc getCacheName () {
149         return cacheName;
150     }
151
152     /** Retrieve the cache entry to to which this message is destinated.
153      *
154      * @return the related cache entry key
155      */

156     final public Object JavaDoc getEntryKey () {
157         return entryKey;
158     }
159
160     /** Retrieve the displayable form of this message type. Used for debugging purpose only.
161      *
162      * @return the textual message type
163      */

164     final public String JavaDoc getEventTypeStr () {
165         switch (messageType) {
166             case FLUSH_EVENT:
167                 return "FLUSH";
168
169             case REMOVE_EVENT:
170                 return "REMOVE";
171
172             case PUT_EVENT:
173                 return "PUT";
174         }
175         return "UNKNOWN"; // should never come to this point!
176
}
177
178     /** Creates a new <code>JMSCacheMessage</code> instance and associates the
179      * <code>FLUSH_EVENT</code> type to the message.
180      *
181      * @param cache the cache to which the message should be sent.
182      *
183      * @return a new message instance of type <code>FLUSH_EVENT</code>
184      */

185     public static JMSCacheMessage createFlushMessage (final Cache cache) {
186         if (cache == null)
187             return null;
188
189         return new JMSCacheMessage (cache.getName(), FLUSH_EVENT, null, null);
190     }
191
192     /** Creates a new <code>JMSCacheMessage</code> instance and associates the
193      * <code>REMOVE_EVENT</code> type to the message.
194      *
195      * @param cache the cache to which the message should be sent.
196      * @param entryKey the cache entry key to which this message is related
197      *
198      * @return a new message instance of type <code>REMOVE_EVENT</code>
199      */

200     public static JMSCacheMessage createRemoveMessage (final Cache cache, final Object JavaDoc entryKey) {
201         if ((cache == null) || (entryKey == null))
202             return null;
203
204         return new JMSCacheMessage (cache.getName(), REMOVE_EVENT, entryKey, null);
205     }
206
207     /** Creates a new <code>JMSCacheMessage</code> instance and associates the
208      * <code>PUT_EVENT</code> type to the message.
209      *
210      * @param cache the cache to which the message should be sent.
211      * @param entryKey the cache entry key to which this message is related
212      *
213      * @return a new message instance of type <code>PUT_EVENT</code>
214      */

215     public static JMSCacheMessage createPutMessage (final Cache cache, final Object JavaDoc entryKey, final Object JavaDoc entryValue) {
216         if ((cache == null) || (entryKey == null))
217             return null;
218
219         return new JMSCacheMessage (cache.getName(), PUT_EVENT, entryKey, entryValue);
220     }
221
222     /** Computes the JMS MapMessage equivalent of this message. The attributes of this message
223      * will be stored into message properties of the new created MapMessage instance.
224      *
225      * @param hub the JMS Hub holding the JMS connection and session
226      *
227      * @return the JMS MapMessage equivalent of this message
228      */

229     public MapMessage JavaDoc computeMapMessage (final JMSHub hub) {
230
231         if (hub.getTopicSession() == null) {
232             logger.debug("null session ... DO YOU REALLY THINK IT HELPS ME IF YOU PASS ME INVALID REFERENCES????");
233             return null;
234         }
235
236         try {
237             // create a new MapMessage instance
238
MapMessage JavaDoc message = hub.getTopicSession().createMapMessage();
239
240             // add the cache name
241
message.setString (CACHE_NAME_KEY, cacheName);
242
243             // add the message event type
244
message.setInt (EVENT_NAME_KEY, messageType);
245
246             // add the entry key object, this has to be streamed, otherwise we get
247
// errors when publishing the message!
248
ByteArrayOutputStream JavaDoc byteArrayOutputStream;
249             try {
250                 byteArrayOutputStream = new ByteArrayOutputStream JavaDoc();
251                 ObjectOutputStream JavaDoc objectOutputStream = new ObjectOutputStream JavaDoc(byteArrayOutputStream);
252                 objectOutputStream.writeObject (entryKey);
253
254             } catch (IOException JavaDoc ex) {
255                 logger.warn ("Could not stream the object entry key " + entryKey, ex);
256                 return null;
257             }
258             message.setBytes (ENTRY_KEY, byteArrayOutputStream.toByteArray());
259
260             if (entryValue != null) {
261                 // add the entry key object, this has to be streamed, otherwise we get
262
// errors when publishing the message!
263
ByteArrayOutputStream JavaDoc byteArrayOutputStreamValue;
264                 try {
265                     byteArrayOutputStreamValue = new ByteArrayOutputStream JavaDoc();
266                     ObjectOutputStream JavaDoc objectOutputStreamValue = new
267                         ObjectOutputStream JavaDoc(byteArrayOutputStreamValue);
268                     objectOutputStreamValue.writeObject(entryValue);
269
270                 } catch (IOException JavaDoc ex) {
271                     logger.warn("Could not stream the object entry value " + entryValue, ex);
272                     return null;
273                 }
274                 message.setBytes(ENTRY_VALUE, byteArrayOutputStreamValue.toByteArray());
275             }
276
277             // add the client connection ID
278
message.setString (CLIENT_KEY, hub.getTopicConnection().getClientID());
279
280             // finally return the initialized message
281
return message;
282
283         } catch (JMSException JavaDoc ex) {
284             logger.warn ("Could not instanciate a new MapMessage object!! TopicSession might be invalid!!", ex);
285         }
286         return null;
287     }
288
289     /** Returns a string representation of this message. Used for debugging purpose only.
290      *
291      * @return the message string representation
292      */

293     public String JavaDoc toString() {
294         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc ("JMSCacheMessage (cache:");
295         buffer.append (cacheName);
296         buffer.append (", event:");
297         buffer.append (getEventTypeStr());
298         buffer.append (", entryKey:");
299         buffer.append ((entryKey == null ? "null": entryKey.toString()));
300         buffer.append (")");
301         return buffer.toString();
302     }
303 }
304
305
Popular Tags