Coverage Report - com.allanbank.mongodb.client.SerialClientImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
SerialClientImpl
94%
17/18
100%
4/4
1.2
 
 1  
 /*
 2  
  * #%L
 3  
  * SerialClientImpl.java - mongodb-async-driver - Allanbank Consulting, Inc.
 4  
  * %%
 5  
  * Copyright (C) 2011 - 2014 Allanbank Consulting, Inc.
 6  
  * %%
 7  
  * Licensed under the Apache License, Version 2.0 (the "License");
 8  
  * you may not use this file except in compliance with the License.
 9  
  * You may obtain a copy of the License at
 10  
  * 
 11  
  *      http://www.apache.org/licenses/LICENSE-2.0
 12  
  * 
 13  
  * Unless required by applicable law or agreed to in writing, software
 14  
  * distributed under the License is distributed on an "AS IS" BASIS,
 15  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 16  
  * See the License for the specific language governing permissions and
 17  
  * limitations under the License.
 18  
  * #L%
 19  
  */
 20  
 package com.allanbank.mongodb.client;
 21  
 
 22  
 import java.io.Closeable;
 23  
 
 24  
 import com.allanbank.mongodb.Durability;
 25  
 import com.allanbank.mongodb.MongoClientConfiguration;
 26  
 import com.allanbank.mongodb.MongoCursorControl;
 27  
 import com.allanbank.mongodb.MongoDbException;
 28  
 import com.allanbank.mongodb.MongoIterator;
 29  
 import com.allanbank.mongodb.ReadPreference;
 30  
 import com.allanbank.mongodb.StreamCallback;
 31  
 import com.allanbank.mongodb.bson.Document;
 32  
 import com.allanbank.mongodb.bson.DocumentAssignable;
 33  
 import com.allanbank.mongodb.client.connection.Connection;
 34  
 
 35  
 /**
 36  
  * A specialization of the {@link ClientImpl} to always try to use the same
 37  
  * connection.
 38  
  * 
 39  
  * @api.no This class is <b>NOT</b> part of the drivers API. This class may be
 40  
  *         mutated in incompatible ways between any two releases of the driver.
 41  
  * @copyright 2012-2013, Allanbank Consulting, Inc., All Rights Reserved
 42  
  */
 43  
 public class SerialClientImpl extends AbstractClient {
 44  
 
 45  
     /** If true then assertions have been enabled for the class. */
 46  
     protected static final boolean ASSERTIONS_ENABLED;
 47  
     static {
 48  1
         ASSERTIONS_ENABLED = SerialClientImpl.class.desiredAssertionStatus();
 49  1
     }
 50  
 
 51  
     /** The current active Connection to the MongoDB Servers. */
 52  
     private Connection myConnection;
 53  
 
 54  
     /** The delegate client for accessing connections. */
 55  
     private final ClientImpl myDelegate;
 56  
 
 57  
     /**
 58  
      * Create a new SerialClientImpl.
 59  
      * 
 60  
      * @param client
 61  
      *            The delegate client for accessing connections.
 62  
      */
 63  21
     public SerialClientImpl(final ClientImpl client) {
 64  21
         myDelegate = client;
 65  21
     }
 66  
 
 67  
     /**
 68  
      * {@inheritDoc}
 69  
      * <p>
 70  
      * Overridden to close all of the open connections.
 71  
      * </p>
 72  
      * 
 73  
      * @see Closeable#close()
 74  
      */
 75  
     @Override
 76  
     public void close() {
 77  1
         super.close();
 78  
 
 79  
         // Don't close the delegate.
 80  1
         myConnection = null;
 81  1
     }
 82  
 
 83  
     /**
 84  
      * {@inheritDoc}
 85  
      * <p>
 86  
      * Overridden to return the cluster stats from the delegate client.
 87  
      * </p>
 88  
      */
 89  
     @Override
 90  
     public ClusterStats getClusterStats() {
 91  0
         return myDelegate.getClusterStats();
 92  
     }
 93  
 
 94  
     /**
 95  
      * {@inheritDoc}
 96  
      * <p>
 97  
      * Overridden to return the {@link ClusterType} of delegate
 98  
      * {@link ClientImpl}.
 99  
      * </p>
 100  
      */
 101  
     @Override
 102  
     public ClusterType getClusterType() {
 103  1
         return myDelegate.getClusterType();
 104  
     }
 105  
 
 106  
     /**
 107  
      * {@inheritDoc}
 108  
      * <p>
 109  
      * Overridden to return the configuration used when the client was
 110  
      * constructed.
 111  
      * </p>
 112  
      */
 113  
     @Override
 114  
     public MongoClientConfiguration getConfig() {
 115  1
         return myDelegate.getConfig();
 116  
     }
 117  
 
 118  
     /**
 119  
      * {@inheritDoc}
 120  
      * <p>
 121  
      * Overridden to return the configurations default durability.
 122  
      * </p>
 123  
      * 
 124  
      * @see Client#getDefaultDurability()
 125  
      */
 126  
     @Override
 127  
     public Durability getDefaultDurability() {
 128  2
         return myDelegate.getDefaultDurability();
 129  
     }
 130  
 
 131  
     /**
 132  
      * {@inheritDoc}
 133  
      * <p>
 134  
      * Overridden to return the configurations default read preference.
 135  
      * </p>
 136  
      * 
 137  
      * @see Client#getDefaultReadPreference()
 138  
      */
 139  
     @Override
 140  
     public ReadPreference getDefaultReadPreference() {
 141  1
         return myDelegate.getDefaultReadPreference();
 142  
     }
 143  
 
 144  
     /**
 145  
      * {@inheritDoc}
 146  
      * <p>
 147  
      * Overridden to forward to the delegate client.
 148  
      * </p>
 149  
      * 
 150  
      * @see Client#restart(DocumentAssignable)
 151  
      */
 152  
     @Override
 153  
     public MongoIterator<Document> restart(
 154  
             final DocumentAssignable cursorDocument)
 155  
             throws IllegalArgumentException {
 156  12
         return myDelegate.restart(cursorDocument);
 157  
     }
 158  
 
 159  
     /**
 160  
      * {@inheritDoc}
 161  
      * <p>
 162  
      * Overridden to forward to the delegate client.
 163  
      * </p>
 164  
      * 
 165  
      * @see Client#restart(StreamCallback, DocumentAssignable)
 166  
      */
 167  
     @Override
 168  
     public MongoCursorControl restart(final StreamCallback<Document> results,
 169  
             final DocumentAssignable cursorDocument)
 170  
             throws IllegalArgumentException {
 171  12
         return myDelegate.restart(results, cursorDocument);
 172  
     }
 173  
 
 174  
     /**
 175  
      * Tries to reuse the last connection used. If the connection it closed or
 176  
      * does not exist then the request is delegated to the {@link ClientImpl}
 177  
      * and the result cached for future requests.
 178  
      * 
 179  
      * @return The found connection.
 180  
      * @throws MongoDbException
 181  
      *             On a failure to talk to the MongoDB servers.
 182  
      */
 183  
     @Override
 184  
     protected Connection findConnection(final Message message1,
 185  
             final Message message2) throws MongoDbException {
 186  15
         if ((myConnection == null) || !myConnection.isAvailable()) {
 187  12
             myConnection = myDelegate.findConnection(message1, message2);
 188  
         }
 189  
 
 190  14
         return myConnection;
 191  
     }
 192  
 }