1 /* 2 * #%L 3 * AbstractClient.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 import java.util.concurrent.atomic.AtomicBoolean; 24 25 import com.allanbank.mongodb.MongoDbException; 26 import com.allanbank.mongodb.ReadPreference; 27 import com.allanbank.mongodb.client.callback.ReplyCallback; 28 import com.allanbank.mongodb.client.connection.Connection; 29 import com.allanbank.mongodb.error.MongoClientClosedException; 30 31 /** 32 * AbstractClient provides a base class for {@link Client} implementations. 33 * 34 * @api.no This class is <b>NOT</b> part of the drivers API. This class may be 35 * mutated in incompatible ways between any two releases of the driver. 36 * @copyright 2012-2014, Allanbank Consulting, Inc., All Rights Reserved 37 */ 38 public abstract class AbstractClient implements Client { 39 40 /** Tracks if the client is closed. */ 41 private final AtomicBoolean myClosed = new AtomicBoolean(false); 42 43 /** 44 * Creates a new AbstractClient. 45 */ 46 public AbstractClient() { 47 super(); 48 } 49 50 /** 51 * {@inheritDoc} 52 * <p> 53 * Overridden to close all of the open connections. 54 * </p> 55 * 56 * @see Closeable#close() 57 */ 58 @Override 59 public void close() { 60 myClosed.set(true); 61 } 62 63 /** 64 * {@inheritDoc} 65 * <p> 66 * Overridden to locate the a connection to send the messages and then 67 * forward the messages to that connection. 68 * </p> 69 */ 70 @Override 71 public void send(final Message message1, final Message message2, 72 final ReplyCallback replyCallback) throws MongoDbException { 73 74 assertOpen(message1); 75 76 findConnection(message1, message2).send(message1, message2, 77 replyCallback); 78 } 79 80 /** 81 * {@inheritDoc} 82 * <p> 83 * Overridden to locate the a connection to send the message and then 84 * forward the message to that connection. 85 * </p> 86 */ 87 @Override 88 public void send(final Message message, final ReplyCallback replyCallback) 89 throws MongoDbException { 90 91 assertOpen(message); 92 93 findConnection(message, null).send(message, replyCallback); 94 } 95 96 /** 97 * Locates a {@link Connection} to send a message on. 98 * 99 * @param message1 100 * The first message that will be sent. The connection return 101 * should be compatible with all of the messages 102 * {@link ReadPreference}. 103 * @param message2 104 * The second message that will be sent. The connection return 105 * should be compatible with all of the messages 106 * {@link ReadPreference}. May be <code>null</code>. 107 * 108 * @return The {@link Connection} to send a message on. 109 * @throws MongoDbException 110 * In the case of an error finding a {@link Connection}. 111 */ 112 protected abstract Connection findConnection(Message message1, 113 Message message2) throws MongoDbException; 114 115 /** 116 * Asserts that the command is open. 117 * 118 * @param message 119 * The message being sent. 120 * @throws MongoClientClosedException 121 * If the client has been closed. 122 */ 123 private void assertOpen(final Message message) 124 throws MongoClientClosedException { 125 if (myClosed.get()) { 126 throw new MongoClientClosedException(message); 127 } 128 } 129 130 }