001 /*
002 * Copyright 2008-2016 UnboundID Corp.
003 * All Rights Reserved.
004 */
005 /*
006 * Copyright (C) 2008-2016 UnboundID Corp.
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 */
021 package com.unboundid.ldap.sdk;
022
023
024
025 import javax.net.SocketFactory;
026
027 import com.unboundid.util.NotMutable;
028 import com.unboundid.util.ThreadSafety;
029 import com.unboundid.util.ThreadSafetyLevel;
030
031 import 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)
042 public final class SingleServerSet
043 extends ServerSet
044 {
045 // The port number of the target server.
046 private final int port;
047
048 // The set of connection options to use.
049 private final LDAPConnectionOptions connectionOptions;
050
051 // The socket factory to use to establish connections.
052 private final SocketFactory socketFactory;
053
054 // The address of the target server.
055 private final String address;
056
057
058
059 /**
060 * Creates a new single server set with the specified address and port. It
061 * will use the default socket factory provided by the JVM to create the
062 * underlying socket.
063 *
064 * @param address The address of the directory server to which the
065 * connections should be established. It must not be
066 * {@code null}.
067 * @param port The port of the directory server to which the connections
068 * should be established. It must be between 1 and 65535,
069 * inclusive.
070 */
071 public SingleServerSet(final String address, final int port)
072 {
073 this(address, port, null, null);
074 }
075
076
077
078 /**
079 * Creates a new single server set with the specified address and port. It
080 * will use the default socket factory provided by the JVM to create the
081 * underlying socket.
082 *
083 * @param address The address of the directory server to which the
084 * connections should be established. It must not
085 * be {@code null}.
086 * @param port The port of the directory server to which the
087 * connections should be established. It must be
088 * between 1 and 65535, inclusive.
089 * @param connectionOptions The set of connection options to use for the
090 * underlying connections.
091 */
092 public SingleServerSet(final String address, final int port,
093 final LDAPConnectionOptions connectionOptions)
094 {
095 this(address, port, null, connectionOptions);
096 }
097
098
099
100 /**
101 * Creates a new single server set with the specified address and port, and
102 * using the provided socket factory.
103 *
104 * @param address The address of the directory server to which the
105 * connections should be established. It must not be
106 * {@code null}.
107 * @param port The port of the directory server to which the
108 * connections should be established. It must be
109 * between 1 and 65535, inclusive.
110 * @param socketFactory The socket factory to use to create the underlying
111 * connections.
112 */
113 public SingleServerSet(final String address, final int port,
114 final SocketFactory socketFactory)
115 {
116 this(address, port, socketFactory, null);
117 }
118
119
120
121 /**
122 * Creates a new single server set with the specified address and port, and
123 * using the provided socket factory.
124 *
125 * @param address The address of the directory server to which the
126 * connections should be established. It must not
127 * be {@code null}.
128 * @param port The port of the directory server to which the
129 * connections should be established. It must be
130 * between 1 and 65535, inclusive.
131 * @param socketFactory The socket factory to use to create the
132 * underlying connections.
133 * @param connectionOptions The set of connection options to use for the
134 * underlying connections.
135 */
136 public SingleServerSet(final String address, final int port,
137 final SocketFactory socketFactory,
138 final LDAPConnectionOptions connectionOptions)
139 {
140 ensureNotNull(address);
141 ensureTrue((port > 0) && (port < 65536),
142 "SingleServerSet.port must be between 1 and 65535.");
143
144 this.address = address;
145 this.port = port;
146
147 if (socketFactory == null)
148 {
149 this.socketFactory = SocketFactory.getDefault();
150 }
151 else
152 {
153 this.socketFactory = socketFactory;
154 }
155
156 if (connectionOptions == null)
157 {
158 this.connectionOptions = new LDAPConnectionOptions();
159 }
160 else
161 {
162 this.connectionOptions = connectionOptions;
163 }
164 }
165
166
167
168 /**
169 * Retrieves the address of the directory server to which the connections
170 * should be established.
171 *
172 * @return The address of the directory server to which the connections
173 * should be established.
174 */
175 public String getAddress()
176 {
177 return address;
178 }
179
180
181
182 /**
183 * Retrieves the port of the directory server to which the connections should
184 * be established.
185 *
186 * @return The port of the directory server to which the connections should
187 * be established.
188 */
189 public int getPort()
190 {
191 return port;
192 }
193
194
195
196 /**
197 * Retrieves the socket factory that will be used to establish connections.
198 *
199 * @return The socket factory that will be used to establish connections.
200 */
201 public SocketFactory getSocketFactory()
202 {
203 return socketFactory;
204 }
205
206
207
208 /**
209 * Retrieves the set of connection options that will be used by the underlying
210 * connections.
211 *
212 * @return The set of connection options that will be used by the underlying
213 * connections.
214 */
215 public LDAPConnectionOptions getConnectionOptions()
216 {
217 return connectionOptions;
218 }
219
220
221
222 /**
223 * {@inheritDoc}
224 */
225 @Override()
226 public LDAPConnection getConnection()
227 throws LDAPException
228 {
229 return new LDAPConnection(socketFactory, connectionOptions, address, port);
230 }
231
232
233
234 /**
235 * {@inheritDoc}
236 */
237 @Override()
238 public void toString(final StringBuilder buffer)
239 {
240 buffer.append("SingleServerSet(server=");
241 buffer.append(address);
242 buffer.append(':');
243 buffer.append(port);
244 buffer.append(')');
245 }
246 }