1 7 package org.jboss.cache.loader.tcp; 8 9 import org.jboss.cache.Fqn; 10 import org.jboss.cache.TreeCache; 11 import org.jboss.cache.loader.DelegatingCacheLoader; 12 13 import java.io.IOException ; 14 import java.io.ObjectInputStream ; 15 import java.io.ObjectOutputStream ; 16 import java.net.Socket ; 17 import java.util.Map ; 18 import java.util.Properties ; 19 import java.util.Set ; 20 21 35 public class TcpDelegatingCacheLoader extends DelegatingCacheLoader { 36 private Socket sock; 37 private String host; 38 private int port; 39 ObjectInputStream in; 40 ObjectOutputStream out; 41 42 43 46 public TcpDelegatingCacheLoader() { 47 } 49 50 57 public TcpDelegatingCacheLoader(String host, int port) { 58 this.host = host; 59 this.port = port; 60 } 61 62 67 public void setConfig(Properties props) { 68 this.host = props.getProperty("host"); 69 if(this.host == null || this.host.length() == 0) { 70 this.host = "localhost"; 71 } 72 this.port = Integer.parseInt(props.getProperty("port")); 73 } 74 75 public void start() throws Exception { 76 init(); 77 } 78 79 public void stop() { 80 try {if(in != null) in.close();} catch(IOException e) {} 81 try {if(out != null) out.close();} catch(IOException e) {} 82 try {if(sock != null) sock.close();} catch(IOException e) {} 83 } 84 85 86 private void init() throws IOException { 87 if(host == null) 88 host="localhost"; 89 sock=new Socket (host, port); 90 out=new ObjectOutputStream (sock.getOutputStream()); 91 in=new ObjectInputStream (sock.getInputStream()); 92 } 93 94 99 public void setCache(TreeCache cache) { 100 } 101 102 105 protected Set delegateGetChildrenNames(Fqn fqn) throws Exception { 106 out.writeInt(DelegatingCacheLoader.delegateGetChildrenNames); 107 out.writeObject(fqn); 108 return (Set )in.readObject(); 109 } 110 111 114 protected Object delegateGet(Fqn name, Object key) throws Exception { 115 out.writeInt(DelegatingCacheLoader.delegateGetKey); 116 out.writeObject(name); 117 out.writeObject(key); 118 return in.readObject(); 119 } 120 121 124 protected Map delegateGet(Fqn name) throws Exception { 125 out.writeInt(DelegatingCacheLoader.delegateGet); 126 out.writeObject(name); 127 return (Map )in.readObject(); 128 } 129 130 133 protected boolean delegateExists(Fqn name) throws Exception { 134 out.writeInt(DelegatingCacheLoader.delegateExists); 135 out.writeObject(name); 136 return in.readBoolean(); 137 } 138 139 142 protected Object delegatePut(Fqn name, Object key, Object value) throws Exception { 143 out.writeInt(DelegatingCacheLoader.delegatePutKeyVal); 144 out.writeObject(name); 145 out.writeObject(key); 146 out.writeObject(value); 147 return in.readObject(); 148 } 149 150 153 protected void delegatePut(Fqn name, Map attributes) throws Exception { 154 out.writeInt(DelegatingCacheLoader.delegatePut); 155 out.writeObject(name); 156 out.writeObject(attributes); 157 out.flush(); 158 } 159 160 163 protected Object delegateRemove(Fqn name, Object key) throws Exception { 164 out.writeInt(DelegatingCacheLoader.delegateRemoveKey); 165 out.writeObject(name); 166 out.writeObject(key); 167 return in.readObject(); 168 } 169 170 173 protected void delegateRemove(Fqn name) throws Exception { 174 out.writeInt(DelegatingCacheLoader.delegateRemove); 175 out.writeObject(name); 176 out.flush(); 177 } 178 179 182 protected void delegateRemoveData(Fqn name) throws Exception { 183 out.writeInt(DelegatingCacheLoader.delegateRemoveData); 184 out.writeObject(name); 185 out.flush(); 186 } 187 188 191 public byte[] delegateLoadEntireState() throws Exception { 192 out.writeInt(DelegatingCacheLoader.delegateLoadEntireState); 193 return (byte[])in.readObject(); 194 } 195 196 199 public void delegateStoreEntireState(byte[] state) throws Exception { 200 out.writeInt(DelegatingCacheLoader.delegateStoreEntireState); 201 out.writeObject(state); 202 out.flush(); 203 } 204 205 206 207 } 208 | Popular Tags |