Coverage Report - com.sun.grizzly.connectioncache.impl.transport.ConnectionCacheBase
 
Classes in this File Line Coverage Branch Coverage Complexity
ConnectionCacheBase
50 %
22/44
41 %
9/22
0
 
 1  
 /*
 2  
  * 
 3  
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 4  
  * 
 5  
  * Copyright 2007-2008 Sun Microsystems, Inc. All rights reserved.
 6  
  * 
 7  
  * The contents of this file are subject to the terms of either the GNU
 8  
  * General Public License Version 2 only ("GPL") or the Common Development
 9  
  * and Distribution License("CDDL") (collectively, the "License").  You
 10  
  * may not use this file except in compliance with the License. You can obtain
 11  
  * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
 12  
  * or glassfish/bootstrap/legal/LICENSE.txt.  See the License for the specific
 13  
  * language governing permissions and limitations under the License.
 14  
  * 
 15  
  * When distributing the software, include this License Header Notice in each
 16  
  * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
 17  
  * Sun designates this particular file as subject to the "Classpath" exception
 18  
  * as provided by Sun in the GPL Version 2 section of the License file that
 19  
  * accompanied this code.  If applicable, add the following below the License
 20  
  * Header, with the fields enclosed by brackets [] replaced by your own
 21  
  * identifying information: "Portions Copyrighted [year]
 22  
  * [name of copyright owner]"
 23  
  * 
 24  
  * Contributor(s):
 25  
  * 
 26  
  * If you wish your version of this file to be governed by only the CDDL or
 27  
  * only the GPL Version 2, indicate your decision by adding "[Contributor]
 28  
  * elects to include this software in this distribution under the [CDDL or GPL
 29  
  * Version 2] license."  If you don't indicate a single choice of license, a
 30  
  * recipient has the option to distribute your version of this file under
 31  
  * either the CDDL, the GPL Version 2 or to extend the choice of license to
 32  
  * its licensees as provided above.  However, if you add GPL Version 2 code
 33  
  * and therefore, elected the GPL Version 2 license, then the option applies
 34  
  * only if the new code is made subject to such option by the copyright
 35  
  * holder.
 36  
  *
 37  
  */
 38  
 
 39  
 package com.sun.grizzly.connectioncache.impl.transport;
 40  
 
 41  
 import com.sun.grizzly.connectioncache.spi.concurrent.ConcurrentQueue;
 42  
 import com.sun.grizzly.connectioncache.spi.transport.ConnectionCache;
 43  
 import java.io.Closeable;
 44  
 import java.util.logging.Logger ;
 45  
 import java.util.logging.Level ;
 46  
 
 47  
 
 48  
 public abstract class ConnectionCacheBase<C extends Closeable> 
 49  
     implements ConnectionCache<C> {
 50  
 
 51  
     // A name for this instance, provided for convenience.
 52  
     private final String cacheType ;
 53  
 
 54  
     // Log to this logger if FINER is enabled.
 55  
     protected final Logger logger ;
 56  
 
 57  
     // Configuration data
 58  
     // XXX we may want this data to be dynamically re-configurable
 59  
     private final int highWaterMark ;                // Maximum number of 
 60  
                                                 // connections before we start 
 61  
                                                 // closing idle connections
 62  
     private final int numberToReclaim ;                // How many connections to 
 63  
                                                 // reclaim at once
 64  
 
 65  
     // MUST be initialized in a subclass
 66  5
     protected ConcurrentQueue<C> reclaimableConnections = null ;
 67  
 
 68  
     protected boolean debug() {
 69  14607
         return logger.isLoggable( Level.FINER ) ;
 70  
     }
 71  
 
 72  
     public final String getCacheType() {
 73  0
         return cacheType ;
 74  
     }
 75  
 
 76  
     public final int numberToReclaim() {
 77  8
         return numberToReclaim ;
 78  
     }
 79  
 
 80  
     public final int highWaterMark() {
 81  2139
         return highWaterMark ;
 82  
     }
 83  
 
 84  
     // The name of this class, which is implemented in the subclass.
 85  
     // I could derive this from this.getClass().getClassName(), but
 86  
     // this is easier.
 87  
     protected abstract String thisClassName() ;
 88  
 
 89  
     ConnectionCacheBase( final String cacheType, 
 90  
         final int highWaterMark, final int numberToReclaim, 
 91  5
         final Logger logger ) {
 92  
 
 93  5
         if (cacheType == null)
 94  0
             throw new IllegalArgumentException( "cacheType must not be null" ) ;
 95  
 
 96  5
         if (highWaterMark < 0)
 97  0
             throw new IllegalArgumentException( "highWaterMark must be non-negative" ) ;
 98  
 
 99  5
         if (numberToReclaim < 1)
 100  0
             throw new IllegalArgumentException( "numberToReclaim must be at least 1" ) ;
 101  
 
 102  5
         if (logger == null)
 103  0
             throw new IllegalArgumentException( "logger must not be null" ) ;
 104  
 
 105  5
         this.cacheType = cacheType ;
 106  5
         this.logger = logger ;
 107  5
         this.highWaterMark = highWaterMark ;
 108  5
         this.numberToReclaim = numberToReclaim ;
 109  5
     }
 110  
     
 111  
     protected final void dprint(final String msg) {
 112  0
         logger.finer(thisClassName() + msg);
 113  0
     }
 114  
 
 115  
     public String toString() {
 116  0
         return thisClassName() + "[" 
 117  
             + getCacheType() + "]";
 118  
     }
 119  
      
 120  
     public void dprintStatistics() {
 121  0
         dprint( ".stats:"
 122  
                + " idle=" + numberOfIdleConnections() 
 123  
                + " reclaimable=" + numberOfReclaimableConnections() 
 124  
                + " busy=" + numberOfBusyConnections() 
 125  
                + " total=" + numberOfConnections() 
 126  
                + " (" 
 127  
                + highWaterMark() + "/"
 128  
                + numberToReclaim() 
 129  
                + ")");
 130  0
     }
 131  
 
 132  
     /** Reclaim some idle cached connections.  Will never 
 133  
      * close a connection that is busy.
 134  
      * @return  any connections reclaimed, (yes or no)
 135  
      */
 136  
     protected boolean reclaim() {
 137  8
         if (debug())
 138  0
             dprint( ".reclaim: starting" ) ;
 139  
 
 140  8
         int ctr = 0 ;
 141  8
         while (ctr < numberToReclaim()) {
 142  8
             C candidate = reclaimableConnections.poll() ;
 143  8
             if (candidate == null)
 144  
                 // If we have closed all idle connections, we must stop 
 145  
                 // reclaiming.
 146  8
                 break ;
 147  
 
 148  0
             if (debug())
 149  0
                 dprint( ".reclaim: closing connection " + candidate ) ;
 150  
 
 151  
             try {
 152  0
                 close( candidate ) ;            
 153  0
             } catch (RuntimeException exc) {
 154  0
                 if (debug())
 155  0
                     dprint( ".reclaim: caught exception on close: " + exc ) ;
 156  0
                 throw exc ;
 157  0
             }
 158  
 
 159  0
             ctr++ ;
 160  0
         }
 161  
 
 162  8
         if (debug())
 163  0
             dprint( ".reclaim: reclaimed " + ctr + " connection(s)" ) ;
 164  
 
 165  8
         return ctr > 0 ;
 166  
     }
 167  
 }