View Javadoc
1   /*
2    * #%L
3    * AuthenticationConnectionFactory.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  
21  package com.allanbank.mongodb.client.connection.auth;
22  
23  import java.io.IOException;
24  
25  import com.allanbank.mongodb.MongoClientConfiguration;
26  import com.allanbank.mongodb.client.ClusterStats;
27  import com.allanbank.mongodb.client.ClusterType;
28  import com.allanbank.mongodb.client.connection.ConnectionFactory;
29  import com.allanbank.mongodb.client.connection.ReconnectStrategy;
30  import com.allanbank.mongodb.client.connection.proxy.ProxiedConnectionFactory;
31  import com.allanbank.mongodb.client.state.Server;
32  import com.allanbank.mongodb.util.IOUtils;
33  
34  /**
35   * AuthenticationConnectionFactory wraps all of the connections with
36   * {@link AuthenticatingConnection}s.
37   * 
38   * @api.no This class is <b>NOT</b> part of the drivers API. This class may be
39   *         mutated in incompatible ways between any two releases of the driver.
40   * @copyright 2012-2013, Allanbank Consulting, Inc., All Rights Reserved
41   */
42  public class AuthenticationConnectionFactory implements
43          ProxiedConnectionFactory {
44  
45      /** The default config. */
46      private final MongoClientConfiguration myConfig;
47  
48      /** The connection factory to proxy connections to. */
49      private final ProxiedConnectionFactory myProxiedConnectionFactory;
50  
51      /**
52       * Creates a new AuthenticationConnectionFactory.
53       * 
54       * @param factory
55       *            The factory to wrap connections wit
56       *            {@link AuthenticatingConnection}.
57       * @param config
58       *            The default config.
59       */
60      public AuthenticationConnectionFactory(
61              final ProxiedConnectionFactory factory,
62              final MongoClientConfiguration config) {
63          myProxiedConnectionFactory = factory;
64          myConfig = config;
65      }
66  
67      /**
68       * {@inheritDoc}
69       * <p>
70       * Overridden to close the proxied {@link ConnectionFactory}.
71       * </p>
72       */
73      @Override
74      public void close() {
75          IOUtils.close(myProxiedConnectionFactory);
76      }
77  
78      /**
79       * {@inheritDoc}
80       * <p>
81       * Overridden to return a connection then ensures the connection is properly
82       * authenticated before reconnecting.
83       * </p>
84       */
85      @Override
86      public AuthenticatingConnection connect() throws IOException {
87          return new AuthenticatingConnection(
88                  myProxiedConnectionFactory.connect(), myConfig);
89      }
90  
91      /**
92       * {@inheritDoc}
93       * <p>
94       * Overridden to wrap the returned connection with an
95       * {@link AuthenticatingConnection}.
96       * </p>
97       */
98      @Override
99      public AuthenticatingConnection connect(final Server server,
100             final MongoClientConfiguration config) throws IOException {
101         return new AuthenticatingConnection(myProxiedConnectionFactory.connect(
102                 server, config), config);
103     }
104 
105     /**
106      * {@inheritDoc}
107      * <p>
108      * Overridden to return the cluster stats of the proxied
109      * {@link ConnectionFactory}.
110      * </p>
111      */
112     @Override
113     public ClusterStats getClusterStats() {
114         return myProxiedConnectionFactory.getClusterStats();
115     }
116 
117     /**
118      * {@inheritDoc}
119      * <p>
120      * Overridden to return the cluster type of the proxied
121      * {@link ConnectionFactory}.
122      * </p>
123      */
124     @Override
125     public ClusterType getClusterType() {
126         return myProxiedConnectionFactory.getClusterType();
127     }
128 
129     /**
130      * {@inheritDoc}
131      * <p>
132      * Overridden to return the delegates strategy but replace his connection
133      * factory with our own.
134      * </p>
135      */
136     @Override
137     public ReconnectStrategy getReconnectStrategy() {
138         final ReconnectStrategy delegates = myProxiedConnectionFactory
139                 .getReconnectStrategy();
140 
141         delegates.setConnectionFactory(this);
142 
143         return delegates;
144     }
145 }