1 /* 2 * #%L 3 * AbstractReconnectStrategy.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.state; 21 22 import com.allanbank.mongodb.MongoClientConfiguration; 23 import com.allanbank.mongodb.client.connection.Connection; 24 import com.allanbank.mongodb.client.connection.ReconnectStrategy; 25 import com.allanbank.mongodb.client.connection.proxy.ProxiedConnectionFactory; 26 27 /** 28 * AbstractReconnectStrategy provides a base class for reconnection strategies. 29 * 30 * @api.no This class is <b>NOT</b> part of the drivers API. This class may be 31 * mutated in incompatible ways between any two releases of the driver. 32 * @copyright 2012-2013, Allanbank Consulting, Inc., All Rights Reserved 33 */ 34 public abstract class AbstractReconnectStrategy implements ReconnectStrategy { 35 36 /** The configuration for connections to the servers. */ 37 protected MongoClientConfiguration myConfig = null; 38 39 /** The connection factory for new connections. */ 40 protected ProxiedConnectionFactory myConnectionFactory = null; 41 42 /** The selector for which server to connect to. */ 43 protected ServerSelector mySelector = null; 44 45 /** The state of the cluster. */ 46 protected Cluster myState = null; 47 48 /** 49 * Creates a new AbstractReconnectStrategy. 50 */ 51 public AbstractReconnectStrategy() { 52 super(); 53 } 54 55 /** 56 * Returns the configuration for connections to the servers. 57 * 58 * @return The configuration for connections to the servers 59 */ 60 public MongoClientConfiguration getConfig() { 61 return myConfig; 62 } 63 64 /** 65 * Returns the connection factory for new connections. 66 * 67 * @return The connection factory for new connections. 68 */ 69 public ProxiedConnectionFactory getConnectionFactory() { 70 return myConnectionFactory; 71 } 72 73 /** 74 * Returns the selector for which server to connect to. 75 * 76 * @return The selector for which server to connect to. 77 */ 78 public ServerSelector getSelector() { 79 return mySelector; 80 } 81 82 /** 83 * Returns the state of the cluster. 84 * 85 * @return The state of the cluster. 86 */ 87 public Cluster getState() { 88 return myState; 89 } 90 91 /** 92 * {@inheritDoc} 93 */ 94 @Override 95 public void setConfig(final MongoClientConfiguration config) { 96 myConfig = config; 97 } 98 99 /** 100 * {@inheritDoc} 101 */ 102 @Override 103 public void setConnectionFactory( 104 final ProxiedConnectionFactory connectionFactory) { 105 myConnectionFactory = connectionFactory; 106 } 107 108 /** 109 * {@inheritDoc} 110 */ 111 @Override 112 public void setSelector(final ServerSelector selector) { 113 mySelector = selector; 114 } 115 116 /** 117 * {@inheritDoc} 118 */ 119 @Override 120 public void setState(final Cluster state) { 121 myState = state; 122 } 123 124 /** 125 * Pings the server to verify that the connection is active. 126 * 127 * @param server 128 * The server being connected to. 129 * @param connection 130 * The connection to verify. 131 * @return True if the connection is working, false otherwise. 132 */ 133 protected boolean isConnected(final Server server, 134 final Connection connection) { 135 return ClusterPinger.ping(server, connection); 136 } 137 138 }