1 7 package org.jboss.cache.loader; 8 9 import org.apache.commons.logging.Log; 10 import org.apache.commons.logging.LogFactory; 11 import org.jboss.cache.Fqn; 12 import org.jboss.cache.Modification; 13 import org.jboss.cache.RegionManager; 14 import org.jboss.cache.config.CacheLoaderConfig.IndividualCacheLoaderConfig; 15 import org.jboss.cache.marshall.MethodCall; 16 import org.jboss.cache.marshall.MethodCallFactory; 17 import org.jboss.cache.marshall.MethodDeclarations; 18 import org.jgroups.Address; 19 import org.jgroups.blocks.GroupRequest; 20 21 import java.io.ObjectInputStream ; 22 import java.io.ObjectOutputStream ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 import java.util.Map ; 26 import java.util.Set ; 27 28 37 public class ClusteredCacheLoader extends AbstractCacheLoader 38 { 39 private static Log log = LogFactory.getLog(ClusteredCacheLoader.class); 40 41 private ClusteredCacheLoaderConfig config; 42 43 47 public void setConfig(IndividualCacheLoaderConfig base) 48 { 49 if (base instanceof ClusteredCacheLoaderConfig) 50 { 51 this.config = (ClusteredCacheLoaderConfig) base; 52 } 53 else 54 { 55 config = new ClusteredCacheLoaderConfig(base); 56 } 57 } 58 59 public IndividualCacheLoaderConfig getConfig() 60 { 61 return config; 62 } 63 64 public Set <String > getChildrenNames(Fqn fqn) throws Exception 65 { 66 MethodCall call = MethodCallFactory.create(MethodDeclarations.getChildrenNamesMethodLocal, fqn); 67 Object resp = callRemote(call); 68 return (Set <String >) resp; 69 } 70 71 private Object callRemote(MethodCall call) throws Exception 72 { 73 if (log.isTraceEnabled()) log.trace("cache=" + cache.getLocalAddress() + "; calling with " + call); 74 List <Address> mbrs = cache.getMembers(); 75 MethodCall clusteredGet = MethodCallFactory.create(MethodDeclarations.clusteredGetMethod, call, false); 76 List resps = cache.getRPCManager().callRemoteMethods(mbrs, clusteredGet, GroupRequest.GET_FIRST, true, config.getTimeout()); 77 if (resps == null) 78 { 79 if (log.isInfoEnabled()) log.info("No replies to call " + call + ". Perhaps we're alone in the cluster?"); 80 return null; 81 } 82 else 83 { 84 Iterator i = resps.iterator(); 86 Object result = null; 87 while (i.hasNext()) 88 { 89 Object o = i.next(); 90 if (o instanceof Exception ) 91 { 92 if (log.isDebugEnabled()) 93 log.debug("Found remote exception among responses - removing from responses list", (Exception ) o); 94 } 95 else 96 { 97 List <Boolean > clusteredGetResp = (List <Boolean >) o; 99 if (clusteredGetResp.get(0)) 101 { 102 result = clusteredGetResp.get(1); 103 break; 104 } 105 } 106 } 107 108 if (log.isTraceEnabled()) log.trace("got responses " + resps); 109 return result; 110 } 111 } 112 113 public Map get(Fqn name) throws Exception 114 { 115 return get0(name); 116 } 117 118 protected Map get0(Fqn name) throws Exception 119 { 120 MethodCall call = MethodCallFactory.create(MethodDeclarations.getDataMapMethodLocal, name); 121 Object resp = callRemote(call); 122 Map m = (Map ) resp; 123 return m; 124 } 125 126 public boolean exists(Fqn name) throws Exception 127 { 128 MethodCall call = MethodCallFactory.create(MethodDeclarations.existsMethod, name); 129 Object resp = callRemote(call); 130 131 return resp != null && (Boolean ) resp; 132 } 133 134 public Object put(Fqn name, Object key, Object value) throws Exception 135 { 136 if (cache.getInvocationContext().isOriginLocal()) 137 { 138 Object o[] = {name, key, true}; 139 MethodCall call = MethodCallFactory.create(MethodDeclarations.getKeyValueMethodLocal, o); 140 return callRemote(call); 141 } 142 else 143 { 144 log.trace("Call originated remotely. Not bothering to try and do a clustered get() for this put(). Returning null."); 145 return null; 146 } 147 } 148 149 152 public void put(Fqn name, Map attributes) throws Exception 153 { 154 } 155 156 159 public void put(List <Modification> modifications) throws Exception 160 { 161 } 162 163 167 public Object remove(Fqn name, Object key) throws Exception 168 { 169 Map map = get(name); 170 return map == null ? null : map.get(key); 171 } 172 173 176 public void remove(Fqn name) throws Exception 177 { 178 } 180 181 184 public void removeData(Fqn name) throws Exception 185 { 186 } 187 188 191 public void prepare(Object tx, List modifications, boolean one_phase) throws Exception 192 { 193 } 194 195 198 public void commit(Object tx) throws Exception 199 { 200 } 201 202 205 public void rollback(Object tx) 206 { 207 } 208 209 public void loadEntireState(ObjectOutputStream os) throws Exception 210 { 211 } 213 214 public void loadState(Fqn subtree, ObjectOutputStream os) throws Exception 215 { 216 } 218 219 public void storeEntireState(ObjectInputStream is) throws Exception 220 { 221 } 223 224 public void storeState(Fqn subtree, ObjectInputStream is) throws Exception 225 { 226 } 228 229 public void setRegionManager(RegionManager manager) 230 { 231 } 232 233 public void create() throws Exception 234 { 235 } 236 237 public void start() throws Exception 238 { 239 } 240 241 public void stop() 242 { 243 } 244 245 public void destroy() 246 { 247 } 248 249 } 250 | Popular Tags |