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