001/*
002 * Copyright 2008-2018 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright (C) 2008-2018 Ping Identity Corporation
007 *
008 * This program is free software; you can redistribute it and/or modify
009 * it under the terms of the GNU General Public License (GPLv2 only)
010 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
011 * as published by the Free Software Foundation.
012 *
013 * This program is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016 * GNU General Public License for more details.
017 *
018 * You should have received a copy of the GNU General Public License
019 * along with this program; if not, see <http://www.gnu.org/licenses>.
020 */
021package com.unboundid.ldap.sdk;
022
023
024
025import javax.net.SocketFactory;
026
027import com.unboundid.util.NotMutable;
028import com.unboundid.util.ThreadSafety;
029import com.unboundid.util.ThreadSafetyLevel;
030
031import static com.unboundid.util.Validator.*;
032
033
034
035/**
036 * This class provides a server set implementation that only provides the
037 * ability to connect to a single server.  It may be used in cases where a
038 * {@link ServerSet} is required but only a single server is needed.
039 */
040@NotMutable()
041@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
042public final class SingleServerSet
043       extends ServerSet
044{
045  // The bind request to use to authenticate connections created by this
046  // server set.
047  private final BindRequest bindRequest;
048
049  // The port number of the target server.
050  private final int port;
051
052  // The set of connection options to use.
053  private final LDAPConnectionOptions connectionOptions;
054
055  // The post-connect processor to invoke against connections created by this
056  // server set.
057  private final PostConnectProcessor postConnectProcessor;
058
059  // The socket factory to use to establish connections.
060  private final SocketFactory socketFactory;
061
062  // The address of the target server.
063  private final String address;
064
065
066
067  /**
068   * Creates a new single server set with the specified address and port.  It
069   * will use the default socket factory provided by the JVM to create the
070   * underlying socket.
071   *
072   * @param  address  The address of the directory server to which the
073   *                  connections should be established.  It must not be
074   *                  {@code null}.
075   * @param  port     The port of the directory server to which the connections
076   *                  should be established.  It must be between 1 and 65535,
077   *                  inclusive.
078   */
079  public SingleServerSet(final String address, final int port)
080  {
081    this(address, port, null, null);
082  }
083
084
085
086  /**
087   * Creates a new single server set with the specified address and port.  It
088   * will use the default socket factory provided by the JVM to create the
089   * underlying socket.
090   *
091   * @param  address            The address of the directory server to which the
092   *                            connections should be established.  It must not
093   *                            be {@code null}.
094   * @param  port               The port of the directory server to which the
095   *                            connections should be established.  It must be
096   *                            between 1 and 65535, inclusive.
097   * @param  connectionOptions  The set of connection options to use for the
098   *                            underlying connections.
099   */
100  public SingleServerSet(final String address, final int port,
101                         final LDAPConnectionOptions connectionOptions)
102  {
103    this(address, port, null, connectionOptions);
104  }
105
106
107
108  /**
109   * Creates a new single server set with the specified address and port, and
110   * using the provided socket factory.
111   *
112   * @param  address        The address of the directory server to which the
113   *                        connections should be established.  It must not be
114   *                        {@code null}.
115   * @param  port           The port of the directory server to which the
116   *                        connections should be established.  It must be
117   *                        between 1 and 65535, inclusive.
118   * @param  socketFactory  The socket factory to use to create the underlying
119   *                        connections.
120   */
121  public SingleServerSet(final String address, final int port,
122                         final SocketFactory socketFactory)
123  {
124    this(address, port, socketFactory, null);
125  }
126
127
128
129  /**
130   * Creates a new single server set with the specified address and port, and
131   * using the provided socket factory.
132   *
133   * @param  address            The address of the directory server to which the
134   *                            connections should be established.  It must not
135   *                            be {@code null}.
136   * @param  port               The port of the directory server to which the
137   *                            connections should be established.  It must be
138   *                            between 1 and 65535, inclusive.
139   * @param  socketFactory      The socket factory to use to create the
140   *                            underlying connections.
141   * @param  connectionOptions  The set of connection options to use for the
142   *                            underlying connections.
143   */
144  public SingleServerSet(final String address, final int port,
145                         final SocketFactory socketFactory,
146                         final LDAPConnectionOptions connectionOptions)
147  {
148    this(address, port, socketFactory, connectionOptions, null, null);
149  }
150
151
152
153  /**
154   * Creates a new single server set with the specified address and port, and
155   * using the provided socket factory.
156   *
157   * @param  address               The address of the directory server to which
158   *                               the connections should be established.  It
159   *                               must not be {@code null}.
160   * @param  port                  The port of the directory server to which the
161   *                               connections should be established.  It must
162   *                               be between 1 and 65535, inclusive.
163   * @param  socketFactory         The socket factory to use to create the
164   *                               underlying connections.
165   * @param  connectionOptions     The set of connection options to use for the
166   *                               underlying connections.
167   * @param  bindRequest           The bind request that should be used to
168   *                               authenticate newly-established connections.
169   *                               It may be {@code null} if this server set
170   *                               should not perform any authentication.
171   * @param  postConnectProcessor  The post-connect processor that should be
172   *                               invoked on newly-established connections.  It
173   *                               may be {@code null} if this server set should
174   *                               not perform any post-connect processing.
175   */
176  public SingleServerSet(final String address, final int port,
177                         final SocketFactory socketFactory,
178                         final LDAPConnectionOptions connectionOptions,
179                         final BindRequest bindRequest,
180                         final PostConnectProcessor postConnectProcessor)
181  {
182    ensureNotNull(address);
183    ensureTrue((port > 0) && (port < 65536),
184               "SingleServerSet.port must be between 1 and 65535.");
185
186    this.address = address;
187    this.port = port;
188    this.bindRequest = bindRequest;
189    this.postConnectProcessor = postConnectProcessor;
190
191    if (socketFactory == null)
192    {
193      this.socketFactory = SocketFactory.getDefault();
194    }
195    else
196    {
197      this.socketFactory = socketFactory;
198    }
199
200    if (connectionOptions == null)
201    {
202      this.connectionOptions = new LDAPConnectionOptions();
203    }
204    else
205    {
206      this.connectionOptions = connectionOptions;
207    }
208  }
209
210
211
212  /**
213   * Retrieves the address of the directory server to which the connections
214   * should be established.
215   *
216   * @return  The address of the directory server to which the connections
217   *          should be established.
218   */
219  public String getAddress()
220  {
221    return address;
222  }
223
224
225
226  /**
227   * Retrieves the port of the directory server to which the connections should
228   * be established.
229   *
230   * @return  The port of the directory server to which the connections should
231   *          be established.
232   */
233  public int getPort()
234  {
235    return port;
236  }
237
238
239
240  /**
241   * Retrieves the socket factory that will be used to establish connections.
242   *
243   * @return  The socket factory that will be used to establish connections.
244   */
245  public SocketFactory getSocketFactory()
246  {
247    return socketFactory;
248  }
249
250
251
252  /**
253   * Retrieves the set of connection options that will be used by the underlying
254   * connections.
255   *
256   * @return  The set of connection options that will be used by the underlying
257   *          connections.
258   */
259  public LDAPConnectionOptions getConnectionOptions()
260  {
261    return connectionOptions;
262  }
263
264
265
266  /**
267   * {@inheritDoc}
268   */
269  @Override()
270  public boolean includesAuthentication()
271  {
272    return (bindRequest != null);
273  }
274
275
276
277  /**
278   * {@inheritDoc}
279   */
280  @Override()
281  public boolean includesPostConnectProcessing()
282  {
283    return (postConnectProcessor != null);
284  }
285
286
287
288  /**
289   * {@inheritDoc}
290   */
291  @Override()
292  public LDAPConnection getConnection()
293         throws LDAPException
294  {
295    return getConnection(null);
296  }
297
298
299
300  /**
301   * {@inheritDoc}
302   */
303  @Override()
304  public LDAPConnection getConnection(
305                             final LDAPConnectionPoolHealthCheck healthCheck)
306         throws LDAPException
307  {
308    final LDAPConnection connection =
309         new LDAPConnection(socketFactory, connectionOptions, address, port);
310    doBindPostConnectAndHealthCheckProcessing(connection, bindRequest,
311         postConnectProcessor, healthCheck);
312    return connection;
313  }
314
315
316
317  /**
318   * {@inheritDoc}
319   */
320  @Override()
321  public void toString(final StringBuilder buffer)
322  {
323    buffer.append("SingleServerSet(server=");
324    buffer.append(address);
325    buffer.append(':');
326    buffer.append(port);
327    buffer.append(", includesAuthentication=");
328    buffer.append(bindRequest != null);
329    buffer.append(", includesPostConnectProcessing=");
330    buffer.append(postConnectProcessor != null);
331    buffer.append(')');
332  }
333}