| 1 package org.shiftone.cache.decorator.cluster; 2 3 4 5 import org.shiftone.cache.Cache; 6 import org.shiftone.cache.CacheException; 7 import org.shiftone.cache.CacheFactory; 8 import org.shiftone.cache.util.Log; 9 import org.shiftone.cache.util.WeakMap; 10 11 import java.util.Random ; 12 13 14 18 public class ClusterCacheFactory implements CacheFactory 19 { 20 21 private static final Log LOG = new Log(ClusterCacheFactory.class); 22 private static final Random random = new Random (); 23 public static final String DEFAULT_BUS_NAME = "shiftone-cache"; 24 private WeakMap registeredCacheMap = new WeakMap(); 25 private final long instanceId = random.nextLong(); 26 private ClusterBus bus; 27 private CacheFactory delegate = null; private String channelProperties = ""; private String busName = DEFAULT_BUS_NAME; 31 private void init() throws CacheException 32 { 33 34 if (bus == null) 36 { 37 try 38 { 39 bus = new ClusterBus(this); 40 } 41 catch (Exception e) 42 { 43 throw new CacheException("error initializing JGroups NotificationBus", e); 44 } 45 } 46 } 47 48 49 public Cache newInstance(String cacheName, long timeoutMilliSeconds, int maxSize) 50 { 51 52 Cache delegateCache; 53 ClusterCache clusterCache; 54 55 try 56 { 57 init(); 58 59 clusterCache = getRegisteredCache(cacheName); 60 61 if (clusterCache == null) 69 { 70 delegateCache = delegate.newInstance(cacheName, timeoutMilliSeconds, maxSize); 71 clusterCache = new ClusterCache(cacheName, delegateCache, this); 72 73 registerCache(cacheName, clusterCache); 74 } 75 else 76 { 77 LOG.info("returning existing cache : " + cacheName); 78 } 79 } 80 catch (Exception e) 81 { 82 throw new RuntimeException ("unable to create cluster decorator"); 83 } 84 85 return clusterCache; 86 } 87 88 89 private synchronized void registerCache(String cacheName, ClusterCache clusterCache) 90 { 91 registeredCacheMap.put(cacheName, clusterCache); 92 } 93 94 95 private synchronized ClusterCache getRegisteredCache(String cacheName) 96 { 97 return (ClusterCache) registeredCacheMap.get(cacheName); 98 } 99 100 101 public String getBusName() 102 { 103 return busName; 104 } 105 106 107 public void setBusName(String busName) 108 { 109 this.busName = busName; 110 } 111 112 113 public String getChannelProperties() 114 { 115 return channelProperties; 116 } 117 118 119 public void setChannelProperties(String channelProperties) 120 { 121 this.channelProperties = channelProperties; 122 } 123 124 125 public void setDelegate(CacheFactory delegate) 126 { 127 this.delegate = delegate; 128 } 129 130 131 public String toString() 132 { 133 return "ClusterCacheFactory[" + busName + "]->" + delegate; 134 } 135 136 137 public void sendRemoveNotification(String cacheName, Object key) 139 { 140 bus.sendNotification(new RemoveNotification(instanceId, cacheName, key)); 141 } 142 143 144 public void sendClearNotification(String cacheName) 145 { 146 bus.sendNotification(new ClearNotification(instanceId, cacheName)); 147 } 148 149 150 public void handleNotification(Notification notification) 151 { 152 153 ClusterCache clusterCache; 154 155 if (notification.getSenderInstanceId() != instanceId) 157 { 158 clusterCache = getRegisteredCache(notification.getCacheName()); 159 160 if (clusterCache != null) 163 { 164 LOG.debug("execute : " + notification); 165 notification.execute(clusterCache.getCache()); 166 } 167 } 168 } 169 } 170 | Popular Tags |