Coverage report

  %line %branch
org.apache.jcs.auxiliary.lateral.socket.tcp.LateralTCPService
43% 
87% 

 1  
 package org.apache.jcs.auxiliary.lateral.socket.tcp;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *   http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import java.io.BufferedReader;
 23  
 import java.io.IOException;
 24  
 import java.io.InputStreamReader;
 25  
 import java.io.Serializable;
 26  
 import java.util.Set;
 27  
 
 28  
 import org.apache.commons.logging.Log;
 29  
 import org.apache.commons.logging.LogFactory;
 30  
 import org.apache.jcs.auxiliary.lateral.LateralCacheInfo;
 31  
 import org.apache.jcs.auxiliary.lateral.LateralElementDescriptor;
 32  
 import org.apache.jcs.auxiliary.lateral.behavior.ILateralCacheObserver;
 33  
 import org.apache.jcs.auxiliary.lateral.behavior.ILateralCacheService;
 34  
 import org.apache.jcs.auxiliary.lateral.socket.tcp.behavior.ITCPLateralCacheAttributes;
 35  
 import org.apache.jcs.engine.CacheElement;
 36  
 import org.apache.jcs.engine.behavior.ICacheElement;
 37  
 import org.apache.jcs.engine.behavior.ICacheListener;
 38  
 
 39  
 /**
 40  
  * A lateral cache service implementation. Does not implement getGroupKey
 41  
  * <p>
 42  
  * @version $Id: LateralTCPService.java 536904 2007-05-10 16:03:42Z tv $
 43  
  */
 44  
 public class LateralTCPService
 45  
     implements ILateralCacheService, ILateralCacheObserver
 46  
 {
 47  28
     private final static Log log = LogFactory.getLog( LateralTCPService.class );
 48  
 
 49  
     private ITCPLateralCacheAttributes tcpLateralCacheAttributes;
 50  
 
 51  
     private LateralTCPSender sender;
 52  
 
 53  
     /**
 54  
      * use the vmid by default
 55  
      */
 56  34
     private long listenerId = LateralCacheInfo.listenerId;
 57  
 
 58  
     /**
 59  
      * Constructor for the LateralTCPService object
 60  
      * <p>
 61  
      * @param lca ITCPLateralCacheAttributes
 62  
      * @exception IOException
 63  
      */
 64  
     public LateralTCPService( ITCPLateralCacheAttributes lca )
 65  
         throws IOException
 66  34
     {
 67  34
         this.setTcpLateralCacheAttributes( lca );
 68  
         try
 69  
         {
 70  34
             log.debug( "creating sender, attributes = " + getTcpLateralCacheAttributes() );
 71  
 
 72  34
             sender = new LateralTCPSender( lca );
 73  
 
 74  20
             if ( log.isInfoEnabled() )
 75  
             {
 76  20
                 log.debug( "Created sender to [" + lca.getTcpServer() + "]" );
 77  
             }
 78  
         }
 79  14
         catch ( IOException e )
 80  
         {
 81  
             // log.error( "Could not create sender", e );
 82  
             // This gets thrown over and over in recovery mode.
 83  
             // The stack trace isn't useful here.
 84  14
             log.error( "Could not create sender to [" + lca.getTcpServer() + "] -- " + e.getMessage() );
 85  
 
 86  14
             throw e;
 87  20
         }
 88  20
     }
 89  
 
 90  
     /**
 91  
      * @param item
 92  
      * @throws IOException
 93  
      */
 94  
     public void update( ICacheElement item )
 95  
         throws IOException
 96  
     {
 97  1419
         update( item, getListenerId() );
 98  1419
     }
 99  
 
 100  
     /**
 101  
      * If put is allowed, we will issue a put. If issue put on remove is configured, we will issue a
 102  
      * remove. Either way, we create a lateral element descriptor, which is essentially a JCS TCP
 103  
      * packet. It describes what operation the receiver should take when it gets the packet.
 104  
      * <p>
 105  
      * @see org.apache.jcs.auxiliary.lateral.behavior.ILateralCacheService#update(org.apache.jcs.engine.behavior.ICacheElement,
 106  
      *      long)
 107  
      */
 108  
     public void update( ICacheElement item, long requesterId )
 109  
         throws IOException
 110  
     {
 111  
         // if we don't allow put, see if we should remove on put
 112  1419
         if ( !this.getTcpLateralCacheAttributes().isAllowPut() )
 113  
         {
 114  
             // if we can't remove on put, and we can't put then return
 115  1412
             if ( !this.getTcpLateralCacheAttributes().isIssueRemoveOnPut() )
 116  
             {
 117  0
                 return;
 118  
             }
 119  
         }
 120  
 
 121  
         // if we shouldn't remove on put, then put
 122  1419
         if ( !this.getTcpLateralCacheAttributes().isIssueRemoveOnPut() )
 123  
         {
 124  7
             LateralElementDescriptor led = new LateralElementDescriptor( item );
 125  7
             led.requesterId = requesterId;
 126  7
             led.command = LateralElementDescriptor.UPDATE;
 127  7
             sender.send( led );
 128  7
         }
 129  
         // else issue a remove with the hashcode for remove check on
 130  
         // on the other end, this will be a server config option
 131  
         else
 132  
         {
 133  1412
             if ( log.isDebugEnabled() )
 134  
             {
 135  0
                 log.debug( "Issuing a remove for a put" );
 136  
             }
 137  
             // set the value to null so we don't send the item
 138  1412
             CacheElement ce = new CacheElement( item.getCacheName(), item.getKey(), null );
 139  1412
             LateralElementDescriptor led = new LateralElementDescriptor( ce );
 140  1412
             led.requesterId = requesterId;
 141  1412
             led.command = LateralElementDescriptor.REMOVE;
 142  1412
             led.valHashCode = item.getVal().hashCode();
 143  1412
             sender.send( led );
 144  
         }
 145  1419
     }
 146  
 
 147  
     /**
 148  
      * Uses the default listener id and calls the next remove method.
 149  
      * <p>
 150  
      * @see org.apache.jcs.engine.behavior.ICacheService#remove(java.lang.String,
 151  
      *      java.io.Serializable)
 152  
      */
 153  
     public void remove( String cacheName, Serializable key )
 154  
         throws IOException
 155  
     {
 156  0
         remove( cacheName, key, getListenerId() );
 157  0
     }
 158  
 
 159  
     /**
 160  
      * Wraps the key in a LateralElementDescriptor.
 161  
      * <p>
 162  
      * @see org.apache.jcs.auxiliary.lateral.behavior.ILateralCacheService#remove(java.lang.String,
 163  
      *      java.io.Serializable, long)
 164  
      */
 165  
     public void remove( String cacheName, Serializable key, long requesterId )
 166  
         throws IOException
 167  
     {
 168  0
         CacheElement ce = new CacheElement( cacheName, key, null );
 169  0
         LateralElementDescriptor led = new LateralElementDescriptor( ce );
 170  0
         led.requesterId = requesterId;
 171  0
         led.command = LateralElementDescriptor.REMOVE;
 172  0
         sender.send( led );
 173  0
     }
 174  
 
 175  
     /*
 176  
      * (non-Javadoc)
 177  
      * @see org.apache.jcs.engine.behavior.ICacheService#release()
 178  
      */
 179  
     public void release()
 180  
         throws IOException
 181  
     {
 182  
         // nothing needs to be done
 183  0
     }
 184  
 
 185  
     /**
 186  
      * Will close the connection.
 187  
      * <p>
 188  
      * @param cacheName
 189  
      * @throws IOException
 190  
      */
 191  
     public void dispose( String cacheName )
 192  
         throws IOException
 193  
     {
 194  0
         sender.dispose( cacheName );
 195  0
     }
 196  
 
 197  
     /**
 198  
      * The service does not get via this method, so this return null.
 199  
      * <p>
 200  
      * @param key
 201  
      * @return always null.
 202  
      * @throws IOException
 203  
      */
 204  
     public Serializable get( String key )
 205  
         throws IOException
 206  
     {
 207  0
         if ( log.isDebugEnabled() )
 208  
         {
 209  0
             log.debug( "balking at get for key [" + key + "]" );
 210  
         }
 211  
         // p( "junk get" );
 212  
         // return get( cattr.cacheName, key, true );
 213  0
         return null;
 214  
         // nothing needs to be done
 215  
     }
 216  
 
 217  
     /*
 218  
      * (non-Javadoc)
 219  
      * @see org.apache.jcs.engine.behavior.ICacheService#get(java.lang.String, java.io.Serializable)
 220  
      */
 221  
     public ICacheElement get( String cacheName, Serializable key )
 222  
         throws IOException
 223  
     {
 224  
         // if get is not allowed return
 225  0
         if ( this.getTcpLateralCacheAttributes().isAllowGet() )
 226  
         {
 227  0
             CacheElement ce = new CacheElement( cacheName, key, null );
 228  0
             LateralElementDescriptor led = new LateralElementDescriptor( ce );
 229  
             // led.requesterId = requesterId; // later
 230  0
             led.command = LateralElementDescriptor.GET;
 231  0
             return sender.sendAndReceive( led );
 232  
         }
 233  
         else
 234  
         {
 235  
             // nothing needs to be done
 236  0
             return null;
 237  
         }
 238  
     }
 239  
 
 240  
     /**
 241  
      * Gets the set of keys of objects currently in the group throws UnsupportedOperationException
 242  
      * <p>
 243  
      * @param cacheName
 244  
      * @param group
 245  
      * @return Set
 246  
      */
 247  
     public Set getGroupKeys( String cacheName, String group )
 248  
     {
 249  
         if ( true )
 250  
         {
 251  0
             throw new UnsupportedOperationException( "Groups not implemented." );
 252  
         }
 253  
         return null;
 254  
     }
 255  
 
 256  
     /*
 257  
      * (non-Javadoc)
 258  
      * @see org.apache.jcs.engine.behavior.ICacheService#removeAll(java.lang.String)
 259  
      */
 260  
     public void removeAll( String cacheName )
 261  
         throws IOException
 262  
     {
 263  0
         removeAll( cacheName, getListenerId() );
 264  0
     }
 265  
 
 266  
     /*
 267  
      * (non-Javadoc)
 268  
      * @see org.apache.jcs.auxiliary.lateral.behavior.ILateralCacheService#removeAll(java.lang.String,
 269  
      *      long)
 270  
      */
 271  
     public void removeAll( String cacheName, long requesterId )
 272  
         throws IOException
 273  
     {
 274  0
         CacheElement ce = new CacheElement( cacheName, "ALL", null );
 275  0
         LateralElementDescriptor led = new LateralElementDescriptor( ce );
 276  0
         led.requesterId = requesterId;
 277  0
         led.command = LateralElementDescriptor.REMOVEALL;
 278  0
         sender.send( led );
 279  0
     }
 280  
 
 281  
     /**
 282  
      * @param args
 283  
      */
 284  
     public static void main( String args[] )
 285  
     {
 286  
         try
 287  
         {
 288  0
             LateralTCPSender sender = new LateralTCPSender( class="keyword">new TCPLateralCacheAttributes() );
 289  
 
 290  
             // process user input till done
 291  0
             boolean notDone = true;
 292  0
             String message = null;
 293  
             // wait to dispose
 294  0
             BufferedReader br = new BufferedReader( class="keyword">new InputStreamReader( System.in ) );
 295  
 
 296  0
             while ( notDone )
 297  
             {
 298  0
                 System.out.println( "enter mesage:" );
 299  0
                 message = br.readLine();
 300  0
                 CacheElement ce = new CacheElement( "test", "test", message );
 301  0
                 LateralElementDescriptor led = new LateralElementDescriptor( ce );
 302  0
                 sender.send( led );
 303  0
             }
 304  
         }
 305  0
         catch ( Exception e )
 306  
         {
 307  0
             System.out.println( e.toString() );
 308  0
         }
 309  0
     }
 310  
 
 311  
     // ILateralCacheObserver methods, do nothing here since
 312  
     // the connection is not registered, the udp service is
 313  
     // is not registered.
 314  
 
 315  
     /*
 316  
      * (non-Javadoc)
 317  
      * @see org.apache.jcs.engine.behavior.ICacheObserver#addCacheListener(java.lang.String,
 318  
      *      org.apache.jcs.engine.behavior.ICacheListener)
 319  
      */
 320  
     public void addCacheListener( String cacheName, ICacheListener obj )
 321  
         throws IOException
 322  
     {
 323  
         // Empty
 324  0
     }
 325  
 
 326  
     /*
 327  
      * (non-Javadoc)
 328  
      * @see org.apache.jcs.engine.behavior.ICacheObserver#addCacheListener(org.apache.jcs.engine.behavior.ICacheListener)
 329  
      */
 330  
     public void addCacheListener( ICacheListener obj )
 331  
         throws IOException
 332  
     {
 333  
         // Empty
 334  0
     }
 335  
 
 336  
     /*
 337  
      * (non-Javadoc)
 338  
      * @see org.apache.jcs.engine.behavior.ICacheObserver#removeCacheListener(java.lang.String,
 339  
      *      org.apache.jcs.engine.behavior.ICacheListener)
 340  
      */
 341  
     public void removeCacheListener( String cacheName, ICacheListener obj )
 342  
         throws IOException
 343  
     {
 344  
         // Empty
 345  0
     }
 346  
 
 347  
     /*
 348  
      * (non-Javadoc)
 349  
      * @see org.apache.jcs.engine.behavior.ICacheObserver#removeCacheListener(org.apache.jcs.engine.behavior.ICacheListener)
 350  
      */
 351  
     public void removeCacheListener( ICacheListener obj )
 352  
         throws IOException
 353  
     {
 354  
         // Empty
 355  0
     }
 356  
 
 357  
     /**
 358  
      * @param listernId The listernId to set.
 359  
      */
 360  
     protected void setListenerId( long listernId )
 361  
     {
 362  20
         this.listenerId = listernId;
 363  20
     }
 364  
 
 365  
     /**
 366  
      * @return Returns the listernId.
 367  
      */
 368  
     protected long getListenerId()
 369  
     {
 370  1419
         return listenerId;
 371  
     }
 372  
 
 373  
     /**
 374  
      * @param tcpLateralCacheAttributes The tcpLateralCacheAttributes to set.
 375  
      */
 376  
     public void setTcpLateralCacheAttributes( ITCPLateralCacheAttributes tcpLateralCacheAttributes )
 377  
     {
 378  34
         this.tcpLateralCacheAttributes = tcpLateralCacheAttributes;
 379  34
     }
 380  
 
 381  
     /**
 382  
      * @return Returns the tcpLateralCacheAttributes.
 383  
      */
 384  
     public ITCPLateralCacheAttributes getTcpLateralCacheAttributes()
 385  
     {
 386  4284
         return tcpLateralCacheAttributes;
 387  
     }
 388  
 
 389  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.