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