1 38 39 40 package org.jahia.services.cache; 41 42 import javax.jms.MapMessage ; 43 import javax.jms.JMSException ; 44 import java.io.ByteArrayOutputStream ; 45 import java.io.ObjectOutputStream ; 46 import java.io.IOException ; 47 48 49 56 class JMSCacheMessage { 57 58 59 final public static String CACHE_NAME_KEY = "cacheKey"; 60 61 62 final public static String EVENT_NAME_KEY = "eventKey"; 63 64 65 final public static String ENTRY_KEY = "entryKey"; 66 67 68 final public static String ENTRY_VALUE = "entryValue"; 69 70 71 final public static String CLIENT_KEY = "clientKey"; 72 73 74 75 final private static org.apache.log4j.Logger logger = 76 org.apache.log4j.Logger.getLogger (JMSCacheMessage.class); 77 78 79 80 final public static int FLUSH_EVENT = 1; 81 82 84 final public static int PUT_EVENT = 2; 85 86 87 final public static int REMOVE_EVENT = 3; 88 89 90 91 private String cacheName; 92 93 94 private int messageType; 95 96 97 private Object entryKey; 98 99 100 private Object entryValue; 101 102 103 104 111 protected JMSCacheMessage (final String cacheName, final int messageType, 112 final Object entryKey, final Object entryValue) 113 { 114 this.cacheName = cacheName; 115 this.messageType = messageType; 116 this.entryKey = entryKey; 117 this.entryValue = entryValue; 118 } 119 120 124 final public boolean isFlush () { 125 return messageType == FLUSH_EVENT; 126 } 127 128 132 final public boolean isRemove () { 133 return messageType == REMOVE_EVENT; 134 } 135 136 140 final public boolean isPut () { 141 return messageType == PUT_EVENT; 142 } 143 144 148 final public String getCacheName () { 149 return cacheName; 150 } 151 152 156 final public Object getEntryKey () { 157 return entryKey; 158 } 159 160 164 final public String 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"; } 177 178 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 200 public static JMSCacheMessage createRemoveMessage (final Cache cache, final Object entryKey) { 201 if ((cache == null) || (entryKey == null)) 202 return null; 203 204 return new JMSCacheMessage (cache.getName(), REMOVE_EVENT, entryKey, null); 205 } 206 207 215 public static JMSCacheMessage createPutMessage (final Cache cache, final Object entryKey, final Object entryValue) { 216 if ((cache == null) || (entryKey == null)) 217 return null; 218 219 return new JMSCacheMessage (cache.getName(), PUT_EVENT, entryKey, entryValue); 220 } 221 222 229 public MapMessage 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 MapMessage message = hub.getTopicSession().createMapMessage(); 239 240 message.setString (CACHE_NAME_KEY, cacheName); 242 243 message.setInt (EVENT_NAME_KEY, messageType); 245 246 ByteArrayOutputStream byteArrayOutputStream; 249 try { 250 byteArrayOutputStream = new ByteArrayOutputStream (); 251 ObjectOutputStream objectOutputStream = new ObjectOutputStream (byteArrayOutputStream); 252 objectOutputStream.writeObject (entryKey); 253 254 } catch (IOException 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 ByteArrayOutputStream byteArrayOutputStreamValue; 264 try { 265 byteArrayOutputStreamValue = new ByteArrayOutputStream (); 266 ObjectOutputStream objectOutputStreamValue = new 267 ObjectOutputStream (byteArrayOutputStreamValue); 268 objectOutputStreamValue.writeObject(entryValue); 269 270 } catch (IOException 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 message.setString (CLIENT_KEY, hub.getTopicConnection().getClientID()); 279 280 return message; 282 283 } catch (JMSException ex) { 284 logger.warn ("Could not instanciate a new MapMessage object!! TopicSession might be invalid!!", ex); 285 } 286 return null; 287 } 288 289 293 public String toString() { 294 StringBuffer buffer = new StringBuffer ("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 |