Clover coverage report -
Coverage timestamp: Thu Jul 5 2007 20:02:32 EDT
file stats: LOC: 355   Methods: 20
NCLOC: 250   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
TcpDelegatingCacheLoader.java 50% 88% 85% 80.4%
coverage coverage
 1    /*
 2    * JBoss, the OpenSource J2EE webOS
 3    *
 4    * Distributable under LGPL license.
 5    * See terms of license at gnu.org.
 6    */
 7    package org.jboss.cache.loader;
 8   
 9    import org.jboss.cache.Fqn;
 10    import org.jboss.cache.Modification;
 11    import org.jboss.cache.config.CacheLoaderConfig.IndividualCacheLoaderConfig;
 12   
 13    import java.io.BufferedInputStream;
 14    import java.io.BufferedOutputStream;
 15    import java.io.IOException;
 16    import java.io.ObjectInputStream;
 17    import java.io.ObjectOutputStream;
 18    import java.net.Socket;
 19    import java.util.List;
 20    import java.util.Map;
 21    import java.util.Set;
 22   
 23    /**
 24    * DelegatingCacheLoader implementation which delegates to a remote (not in the same VM)
 25    * CacheImpl using TCP/IP for communication. Example configuration for connecting to a TcpCacheServer
 26    * running at myHost:12345:<pre>
 27    * <attribute name="CacheLoaderClass">org.jboss.cache.loader.TcpDelegatingCacheLoader</attribute>
 28    * <attribute name="CacheLoaderConfig">
 29    * host=localhost
 30    * port=2099
 31    * </attribute>
 32    * </pre>
 33    *
 34    * @author Bela Ban
 35    * @version $Id: TcpDelegatingCacheLoader.java,v 1.7 2006/12/30 17:50:01 msurtani Exp $
 36    */
 37    public class TcpDelegatingCacheLoader extends DelegatingCacheLoader
 38    {
 39    private Socket sock;
 40    private TcpDelegatingCacheLoaderConfig config;
 41    ObjectInputStream in;
 42    ObjectOutputStream out;
 43   
 44   
 45    /**
 46    * Default constructor.
 47    */
 48  64 public TcpDelegatingCacheLoader()
 49    {
 50    // Empty.
 51    }
 52   
 53    /**
 54    * Allows programmatic configuration.
 55    *
 56    * @param host The host on which to look up the remote object.
 57    * @param port The port on which to look up the remote object.
 58    */
 59  0 public TcpDelegatingCacheLoader(String host, int port)
 60    {
 61  0 this.config = new TcpDelegatingCacheLoaderConfig(host, port);
 62    }
 63   
 64    /**
 65    * Allows configuration via XML config file.
 66    *
 67    * @see DelegatingCacheLoader#setConfig(org.jboss.cache.config.CacheLoaderConfig.IndividualCacheLoaderConfig)
 68    */
 69  64 public void setConfig(IndividualCacheLoaderConfig base)
 70    {
 71  64 if (base instanceof TcpDelegatingCacheLoaderConfig)
 72    {
 73  0 this.config = (TcpDelegatingCacheLoaderConfig) base;
 74    }
 75    else
 76    {
 77  64 config = new TcpDelegatingCacheLoaderConfig(base);
 78    }
 79    }
 80   
 81  64 public IndividualCacheLoaderConfig getConfig()
 82    {
 83  64 return config;
 84    }
 85   
 86  64 public void start() throws Exception
 87    {
 88  64 init();
 89    }
 90   
 91  64 public void stop()
 92    {
 93  64 try
 94    {
 95  64 if (in != null) in.close();
 96    }
 97    catch (IOException e)
 98    {
 99    }
 100  64 try
 101    {
 102  64 if (out != null) out.close();
 103    }
 104    catch (IOException e)
 105    {
 106    }
 107  64 try
 108    {
 109  64 if (sock != null) sock.close();
 110    }
 111    catch (IOException e)
 112    {
 113    }
 114    }
 115   
 116   
 117  64 private void init() throws IOException
 118    {
 119  64 sock = new Socket(config.getHost(), config.getPort());
 120  64 out = new ObjectOutputStream(new BufferedOutputStream(sock.getOutputStream()));
 121  64 out.flush();
 122  64 in = new ObjectInputStream(new BufferedInputStream(sock.getInputStream()));
 123    }
 124   
 125    /**
 126    * @see org.jboss.cache.loader.DelegatingCacheLoader#delegateGetChildrenNames(org.jboss.cache.Fqn)
 127    */
 128  2044 protected Set delegateGetChildrenNames(Fqn fqn) throws Exception
 129    {
 130  2044 synchronized (out)
 131    {
 132  2044 out.reset();
 133  2044 out.writeInt(DelegatingCacheLoader.delegateGetChildrenNames);
 134  2044 out.writeObject(fqn);
 135  2044 out.flush();
 136  2044 Object retval = in.readObject();
 137  2044 if (retval instanceof Exception)
 138    {
 139  0 throw (Exception) retval;
 140    }
 141  2044 return (Set) retval;
 142    }
 143    }
 144   
 145    // See http://jira.jboss.com/jira/browse/JBCACHE-118 for why this is commented out.
 146   
 147    /**
 148    * @see org.jboss.cache.loader.DelegatingCacheLoader#delegateGet(org.jboss.cache.Fqn,Object)
 149    */
 150    // protected Object delegateGet(Fqn name, Object key) throws Exception {
 151    // out.writeInt(DelegatingCacheLoader.delegateGetKey);
 152    // out.writeObject(name);
 153    // out.writeObject(key);
 154    // return in.readObject();
 155    // }
 156   
 157    /**
 158    * @see org.jboss.cache.loader.DelegatingCacheLoader#delegateGet(org.jboss.cache.Fqn)
 159    */
 160  2288 protected Map delegateGet(Fqn name) throws Exception
 161    {
 162  2288 synchronized (out)
 163    {
 164  2288 out.reset();
 165   
 166  2288 out.writeInt(DelegatingCacheLoader.delegateGet);
 167  2288 out.writeObject(name);
 168  2288 out.flush();
 169  2288 Object retval = in.readObject();
 170  2288 if (retval instanceof Exception)
 171    {
 172  0 throw (Exception) retval;
 173    }
 174  2288 return (Map) retval;
 175    }
 176    }
 177   
 178    /**
 179    * @see org.jboss.cache.loader.DelegatingCacheLoader#delegateExists(org.jboss.cache.Fqn)
 180    */
 181  71 protected boolean delegateExists(Fqn name) throws Exception
 182    {
 183  71 synchronized (out)
 184    {
 185  71 out.reset();
 186   
 187  71 out.writeInt(DelegatingCacheLoader.delegateExists);
 188  71 out.writeObject(name);
 189  71 out.flush();
 190  71 Object retval = in.readObject();
 191  71 if (retval instanceof Exception)
 192    {
 193  0 throw (Exception) retval;
 194    }
 195  71 return (Boolean) retval;
 196    }
 197    }
 198   
 199    /**
 200    * @see org.jboss.cache.loader.DelegatingCacheLoader#delegatePut(org.jboss.cache.Fqn,Object,Object)
 201    */
 202  2135 protected Object delegatePut(Fqn name, Object key, Object value) throws Exception
 203    {
 204  2135 synchronized (out)
 205    {
 206  2135 out.reset();
 207   
 208  2135 out.writeInt(DelegatingCacheLoader.delegatePutKeyVal);
 209  2135 out.writeObject(name);
 210  2135 out.writeObject(key);
 211  2135 out.writeObject(value);
 212  2135 out.flush();
 213  2135 Object retval = in.readObject();
 214  2135 if (retval instanceof Exception)
 215    {
 216  0 throw (Exception) retval;
 217    }
 218  2135 return retval;
 219    }
 220    }
 221   
 222    /**
 223    * @see org.jboss.cache.loader.DelegatingCacheLoader#delegatePut(org.jboss.cache.Fqn,java.util.Map)
 224    */
 225  2030 protected void delegatePut(Fqn name, Map attributes) throws Exception
 226    {
 227  2030 synchronized (out)
 228    {
 229  2030 out.reset();
 230   
 231  2030 out.writeInt(DelegatingCacheLoader.delegatePut);
 232  2030 out.writeObject(name);
 233  2030 out.writeObject(attributes);
 234  2030 out.flush();
 235  2030 Object retval = in.readObject();
 236  2030 if (retval instanceof Exception)
 237    {
 238  0 throw (Exception) retval;
 239    }
 240    }
 241    }
 242   
 243  61 @Override
 244    public void put(List<Modification> modifications) throws Exception
 245    {
 246  61 synchronized (out)
 247    {
 248  61 out.reset();
 249   
 250  61 out.writeInt(DelegatingCacheLoader.putList);
 251  61 int length = modifications != null ? modifications.size() : 0;
 252  61 out.writeInt(length);
 253  61 if (length > 0)
 254    {
 255  61 for (Modification m : modifications)
 256    {
 257  70 m.writeExternal(out);
 258    }
 259    }
 260  61 out.flush();
 261  61 Object retval = in.readObject();
 262  61 if (retval instanceof Exception)
 263    {
 264  0 throw (Exception) retval;
 265    }
 266    }
 267    }
 268   
 269    /**
 270    * @see org.jboss.cache.loader.DelegatingCacheLoader#delegateRemove(org.jboss.cache.Fqn,Object)
 271    */
 272  2024 protected Object delegateRemove(Fqn name, Object key) throws Exception
 273    {
 274  2024 synchronized (out)
 275    {
 276  2024 out.reset();
 277   
 278  2024 out.writeInt(DelegatingCacheLoader.delegateRemoveKey);
 279  2024 out.writeObject(name);
 280  2024 out.writeObject(key);
 281  2024 out.flush();
 282  2024 Object retval = in.readObject();
 283  2024 if (retval instanceof Exception)
 284    {
 285  0 throw (Exception) retval;
 286    }
 287  2024 return retval;
 288    }
 289    }
 290   
 291    /**
 292    * @see org.jboss.cache.loader.DelegatingCacheLoader#delegateRemove(org.jboss.cache.Fqn)
 293    */
 294  2147 protected void delegateRemove(Fqn name) throws Exception
 295    {
 296  2147 synchronized (out)
 297    {
 298  2147 out.reset();
 299   
 300  2147 out.writeInt(DelegatingCacheLoader.delegateRemove);
 301  2147 out.writeObject(name);
 302  2147 out.flush();
 303  2147 Object retval = in.readObject();
 304  2147 if (retval instanceof Exception)
 305    {
 306  0 throw (Exception) retval;
 307    }
 308    }
 309    }
 310   
 311    /**
 312    * @see org.jboss.cache.loader.DelegatingCacheLoader#delegateRemoveData(org.jboss.cache.Fqn)
 313    */
 314  3 protected void delegateRemoveData(Fqn name) throws Exception
 315    {
 316  3 synchronized (out)
 317    {
 318  3 out.reset();
 319   
 320  3 out.writeInt(DelegatingCacheLoader.delegateRemoveData);
 321  3 out.writeObject(name);
 322  3 out.flush();
 323  3 Object retval = in.readObject();
 324  3 if (retval instanceof Exception)
 325    {
 326  0 throw (Exception) retval;
 327    }
 328    }
 329    }
 330   
 331  3 @Override
 332    protected void delegateLoadEntireState(ObjectOutputStream os) throws Exception
 333    {
 334  3 throw new UnsupportedOperationException("operation is not currently supported - need to define semantics first");
 335    }
 336   
 337  0 @Override
 338    protected void delegateLoadState(Fqn subtree, ObjectOutputStream os) throws Exception
 339    {
 340  0 throw new UnsupportedOperationException("operation is not currently supported - need to define semantics first");
 341    }
 342   
 343  1 @Override
 344    protected void delegateStoreEntireState(ObjectInputStream is) throws Exception
 345    {
 346  1 throw new UnsupportedOperationException("operation is not currently supported - need to define semantics first");
 347    }
 348   
 349  0 @Override
 350    protected void delegateStoreState(Fqn subtree, ObjectInputStream is) throws Exception
 351    {
 352  0 throw new UnsupportedOperationException("operation is not currently supported - need to define semantics first");
 353    }
 354   
 355    }