1 7 package org.jboss.cache.aop; 8 9 import org.jboss.cache.TreeCache; 10 import org.jboss.cache.Fqn; 11 import org.jboss.cache.CacheException; 12 import org.jboss.cache.TreeCacheListener; 13 import org.jboss.cache.lock.IsolationLevel; 14 import org.jboss.invocation.MarshalledValue; 15 import org.jgroups.JChannel; 16 import org.jgroups.View; 17 import org.jgroups.stack.IpAddress; 18 19 import java.util.HashMap ; 20 import java.util.Map ; 21 import java.io.IOException ; 22 23 35 public class MarshalledTreeCache extends TreeCache implements TreeCacheListener { 36 protected TreeCache localCopy_; 40 protected String nodeId_; 41 protected static final String NODEID_KEY = "__NODEID_KEY__"; 44 protected ClassLoader tcl_ = null; 46 protected boolean useLocalOptimization_ = true; 48 protected boolean marshalling_ = true; 50 51 public MarshalledTreeCache(String cluster_name, 52 String props, 53 long state_fetch_timeout) 54 throws Exception 55 { 56 super(cluster_name, props, state_fetch_timeout); 57 this._init(); 58 } 59 60 public MarshalledTreeCache() throws Exception 61 { 62 this._init(); 63 } 64 65 public MarshalledTreeCache(JChannel channel) throws Exception 66 { 67 super(channel); 68 this._init(); 69 } 70 71 private void _init() throws Exception { 72 localCopy_ = new TreeCache(); 73 localCopy_.setCacheMode(TreeCache.LOCAL); 74 localCopy_.setIsolationLevel(IsolationLevel.REPEATABLE_READ); 75 marshalling_ = true; 76 useLocalOptimization_ = true; 77 tcl_ = null; 78 } 79 80 public void startService() throws Exception 81 { 82 super.addTreeCacheListener(this); 83 super.startService(); 84 if(localCopy_ == null) 85 throw new RuntimeException ("startService(): null localCopy_"); 86 localCopy_.startService(); 87 obtainNodeId(); 88 } 89 90 public void stopService() 91 { 92 nodeId_ = null; 93 localCopy_.stopService(); 94 super.stopService(); 95 } 96 97 100 protected void obtainNodeId() 101 { 102 IpAddress address = (IpAddress)getLocalAddress(); 103 if(address == null) 104 { 105 log.info("obtainNodeId(): has null IpAddress. Assume it is running in local mode."); 106 nodeId_ = "local"; 107 return; 108 } 109 110 if (address.getAdditionalData() == null) 111 { 112 nodeId_ = address.getIpAddress().getHostAddress() + ":" + address.getPort(); 113 } 114 else 115 { 116 nodeId_ = new String (address.getAdditionalData()); 117 } 118 } 119 120 123 public String getNodeId() { 124 return nodeId_; 125 } 126 127 128 132 public void setMarshalling(boolean marshalling) 133 { 134 marshalling_ = marshalling; 135 } 136 137 142 public void setLocalOptimization(boolean optimization) 143 { 144 useLocalOptimization_ = optimization; 145 throw new RuntimeException ("MarshalledTreeCache.setLocalOptimization(): operation not supported yet."); 146 } 147 148 151 public void setClassLoader(ClassLoader tcl) 152 { 153 tcl_ = tcl; 154 } 155 156 public void marshalledPut(String fqn, Object key, Object value) throws CacheException { 157 marshalledPut(Fqn.fromString(fqn), key, value); 158 } 159 160 164 public void marshalledPut(Fqn fqn, Object key, Object value) throws CacheException 165 { 166 if(marshalling_) 167 { 168 marshalledPut_(fqn, key, value); 169 } else { 170 put(fqn, key, value); 171 } 172 } 173 174 public void marshalledPut_(Fqn fqn, Object key, Object value) throws CacheException 175 { 176 MarshalledValue mv = null; 177 try { 178 mv = new MarshalledValue(value); 179 } catch (IOException e) { 180 e.printStackTrace(); 181 throw new CacheException("marshalledPut() exception: " +e); 182 } 183 184 localCopy_.put(fqn, key, value); 186 Map map = new HashMap (); 188 map.put(key, mv); 189 map.put(NODEID_KEY, nodeId_); 190 this.put(fqn, map); 191 } 192 193 public Object marshalledGet(String fqn, Object key) throws CacheException { 194 return marshalledGet(Fqn.fromString(fqn), key); 195 } 196 197 201 public Object marshalledGet(Fqn fqn, Object key) throws CacheException { 202 if(marshalling_) 203 { 204 ClassLoader prevTCL = null; 205 if(tcl_ != null) 206 { 207 prevTCL = Thread.currentThread().getContextClassLoader(); 208 Thread.currentThread().setContextClassLoader(tcl_); 209 } 210 try { 211 return marshalledGet_(fqn, key); 212 } finally 213 { 214 if(tcl_ != null && prevTCL != null) 215 { 216 Thread.currentThread().setContextClassLoader(prevTCL); 217 } 218 } 219 } else 220 { 221 return get(fqn, key); 222 } 223 } 224 225 public Object marshalledGet_(Fqn fqn, Object key) throws CacheException { 226 Object value; 228 try { 229 if( (value = localCopy_.get(fqn, key)) != null) 230 return value; 231 else 232 { value = get(fqn, key); 234 if(value == null) return null; 235 checkValue(value); 236 MarshalledValue mv = (MarshalledValue)value; 237 value = mv.get(); 238 localCopy_.put(fqn, key, value); 240 return value; 241 } 242 } catch (IOException e) { 243 e.printStackTrace(); 244 throw new CacheException("marshalledGet(): exception encountered: ", e); 245 } catch (ClassNotFoundException e) { 246 e.printStackTrace(); 247 throw new CacheException("marshalledGet(): exception encountered: ", e); 248 } 249 } 250 251 public Object marshalledRemove(String fqn, Object key) throws CacheException 252 { 253 return marshalledRemove(Fqn.fromString(fqn), key); 254 } 255 256 260 public Object marshalledRemove(Fqn fqn, Object key) throws CacheException 261 { 262 if(marshalling_) 263 { 264 return marshalledRemove_(fqn, key); 265 } else 266 { 267 return remove(fqn, key); 268 } 269 } 270 271 public Object marshalledRemove_(Fqn fqn, Object key) throws CacheException 272 { 273 if( !exists(fqn, key) ) 274 log.warn("marshalledRemove(): fqn: " +fqn + " key: " +key + " not found."); 275 276 Object value = localCopy_.get(fqn, key); 277 localCopy_.remove(fqn); 278 remove(fqn, NODEID_KEY); 279 Object obj = remove(fqn, key); 280 if(value != null) return value; 281 checkValue(obj); 282 try { 283 return ((MarshalledValue)obj).get(); 284 } catch (IOException e) { 285 e.printStackTrace(); 286 throw new CacheException("marshalledRemove(): exception encountered: ", e); 287 } catch (ClassNotFoundException e) { 288 e.printStackTrace(); 289 throw new CacheException("marshalledRemove(): exception encountered: ", e); 290 } 291 } 292 293 public void nodeCreated(Fqn fqn) 294 { 295 } 297 298 public void nodeRemoved(Fqn fqn) 299 { 300 invalidate(fqn); 301 } 302 303 public void nodeLoaded(Fqn fqn) 304 { 305 } 307 308 public void nodeEvicted(Fqn fqn) 309 { 310 invalidate(fqn); 311 } 312 313 public void nodeModified(Fqn fqn) 314 { 315 invalidate(fqn); 316 } 317 318 public void nodeVisited(Fqn fqn) 319 { 320 } 322 323 public void cacheStarted(TreeCache cache) 324 { 325 } 327 328 public void cacheStopped(TreeCache cache) 329 { 330 } 332 333 public void viewChange(View new_view) 334 { 335 } 337 338 protected void checkValue(Object value) 339 { 340 if( value != null && !(value instanceof MarshalledValue)) 341 throw new RuntimeException ("checkValue: return object is not instance of MarshalledValue. object: "+value); 342 } 343 344 349 protected void invalidate(Fqn fqn) 350 { 351 if(!marshalling_) return; if(fqn.toString().equals("/")) return; if( !localCopy_.exists(fqn)) return; 355 try { 356 String eventId = (String )get(fqn, NODEID_KEY); 357 if(eventId == null) 358 throw new RuntimeException ("invlidate(): fqn to invlidate has null node id. fqn: " +fqn); 359 360 if( nodeId_.equals(eventId) ) return; localCopy_.remove(fqn); 362 } catch (CacheException e) { 363 e.printStackTrace(); 364 } 365 } 366 } 367 | Popular Tags |