1 7 package org.jboss.cache.loader; 8 9 import org.jboss.cache.CacheImpl; 10 import org.jboss.cache.CacheSPI; 11 import org.jboss.cache.Fqn; 12 import org.jboss.cache.NodeSPI; 13 import org.jboss.cache.config.CacheLoaderConfig.IndividualCacheLoaderConfig; 14 import org.jboss.cache.loader.rmi.RemoteTreeCache; 15 16 import java.io.ObjectInputStream ; 17 import java.io.ObjectOutputStream ; 18 import java.rmi.Naming ; 19 import java.util.Map ; 20 import java.util.Set ; 21 22 38 public class RmiDelegatingCacheLoader extends DelegatingCacheLoader 39 { 40 private RmiDelegatingCacheLoaderConfig config = new RmiDelegatingCacheLoaderConfig(); 41 private CacheImpl cache; 42 private RemoteTreeCache remoteCache; 43 private boolean programmaticInit; 44 45 48 public RmiDelegatingCacheLoader() 49 { 50 } 52 53 61 public RmiDelegatingCacheLoader(CacheImpl cache, String host, int port, String bindName) 62 { 63 this.cache = cache; 64 config.setHost(host); 65 config.setPort(String.valueOf(port)); 66 config.setBindName(bindName); 67 this.programmaticInit = true; 68 this.tryToInitRemoteCache(); 69 } 70 71 74 public void setConfig(IndividualCacheLoaderConfig base) 75 { 76 if (base instanceof RmiDelegatingCacheLoaderConfig) 77 { 78 this.config = (RmiDelegatingCacheLoaderConfig) base; 79 } 80 else 81 { 82 config = new RmiDelegatingCacheLoaderConfig(base); 83 } 84 this.tryToInitRemoteCache(); 85 } 86 87 public IndividualCacheLoaderConfig getConfig() 88 { 89 return config; 90 } 91 92 97 public void setCache(CacheSPI cache) 98 { 99 super.setCache(cache); 100 this.tryToInitRemoteCache(); 101 } 102 103 106 protected Set delegateGetChildrenNames(Fqn fqn) throws Exception 107 { 108 return (this.remoteCache != null ? this.remoteCache.getChildrenNames(fqn) : null); 109 } 110 111 115 119 122 protected Map delegateGet(Fqn name) throws Exception 123 { 124 NodeSPI n = (NodeSPI) (this.remoteCache != null ? this.remoteCache.get(name) : null); 125 if (n == null) 126 { 127 return null; 128 } 129 return n.getDataDirect(); 130 } 131 132 135 protected boolean delegateExists(Fqn name) throws Exception 136 { 137 return (this.remoteCache != null && this.remoteCache.exists(name)); 138 } 139 140 143 protected Object delegatePut(Fqn name, Object key, Object value) throws Exception 144 { 145 return (this.remoteCache != null ? this.remoteCache.put(name, key, value) : null); 146 } 147 148 151 protected void delegatePut(Fqn name, Map attributes) throws Exception 152 { 153 if (this.remoteCache != null) this.remoteCache.put(name, attributes); 154 } 155 156 159 protected Object delegateRemove(Fqn name, Object key) throws Exception 160 { 161 return (this.remoteCache != null ? this.remoteCache.remove(name, key) : null); 162 } 163 164 167 protected void delegateRemove(Fqn name) throws Exception 168 { 169 if (this.remoteCache != null) this.remoteCache.remove(name); 170 } 171 172 175 protected void delegateRemoveData(Fqn name) throws Exception 176 { 177 if (this.remoteCache != null) this.remoteCache.removeData(name); 178 } 179 180 @Override 181 protected void delegateLoadEntireState(ObjectOutputStream os) throws Exception 182 { 183 throw new UnsupportedOperationException ("setting and loading state for specific Fqns not supported"); 184 } 185 186 @Override 187 protected void delegateLoadState(Fqn subtree, ObjectOutputStream os) throws Exception 188 { 189 throw new UnsupportedOperationException ("setting and loading state for specific Fqns not supported"); 190 } 191 192 @Override 193 protected void delegateStoreEntireState(ObjectInputStream is) throws Exception 194 { 195 throw new UnsupportedOperationException ("setting and loading state for specific Fqns not supported"); 196 } 197 198 @Override 199 protected void delegateStoreState(Fqn subtree, ObjectInputStream is) throws Exception 200 { 201 throw new UnsupportedOperationException ("setting and loading state for specific Fqns not supported"); 202 } 203 204 208 private void tryToInitRemoteCache() 209 { 210 if (config.getHost() == null || config.getPort() == null || this.cache == null) 211 { 212 return; 213 } 214 if (config.getBindName() == null) 215 { 216 config.setBindName(this.cache.getConfiguration().getClusterName()); 217 } 218 if (!this.programmaticInit && this.cache.isCoordinator()) 219 { 220 this.remoteCache = null; 222 return; 223 } 224 String name = "//" + config.getHost() + ":" + config.getPort() + "/" + config.getBindName(); 225 try 226 { 227 this.remoteCache = (RemoteTreeCache) Naming.lookup(name); 228 } 229 catch (Throwable t) 230 { 231 log.error("Unable to lookup remote cache at '" + name + "'.", t); 232 } 233 } 234 } 235 | Popular Tags |