001/*
002 * Copyright 2009-2018 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright (C) 2009-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.migrate.ldapjdk;
022
023
024
025import java.io.IOException;
026import java.net.InetAddress;
027import java.net.Socket;
028import javax.net.SocketFactory;
029
030import com.unboundid.util.Debug;
031import com.unboundid.util.NotMutable;
032import com.unboundid.util.StaticUtils;
033import com.unboundid.util.ThreadSafety;
034import com.unboundid.util.ThreadSafetyLevel;
035
036
037
038/**
039 * This class provides an {@link LDAPSocketFactory} implementation that wraps a
040 * standard Java socket factory to use when creating sockets.  It will also
041 * appear as a standard Java socket factory.
042 * <BR><BR>
043 * This class is primarily intended to be used in the process of updating
044 * applications which use the Netscape Directory SDK for Java to switch to or
045 * coexist with the UnboundID LDAP SDK for Java.  For applications not written
046 * using the Netscape Directory SDK for Java, the standard Java socket factory
047 * may be used directly without the need for the {@code LDAPSocketFactory}
048 * interface.
049 */
050@NotMutable()
051@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
052public final class JavaToLDAPSocketFactory
053       extends SocketFactory
054       implements LDAPSocketFactory
055{
056  // The socket factory that will be used.
057  private final SocketFactory f;
058
059
060
061  /**
062   * Creates a new instance of this class that will use the provided socket
063   * factory.
064   *
065   * @param  f  The socket factory to use to create the LDAP socket factory.
066   */
067  public JavaToLDAPSocketFactory(final SocketFactory f)
068  {
069    this.f = f;
070  }
071
072
073
074  /**
075   * Creates a new socket to the specified server.
076   *
077   * @param  host  The host to which the connection should be established.
078   * @param  port  The port to which the connection should be established.
079   *
080   * @return  The socket that was created.
081   *
082   * @throws  IOException  If a problem occurs while creating the socket.
083   */
084  @Override()
085  public Socket createSocket(final String host, final int port)
086         throws IOException
087  {
088    synchronized (f)
089    {
090      return f.createSocket(host, port);
091    }
092  }
093
094
095
096  /**
097   * Creates a new socket to the specified server.
098   *
099   * @param  host          The host to which the connection should be
100   *                       established.
101   * @param  port          The port to which the connection should be
102   *                       established.
103   * @param  localAddress  The local address to use for the connection.  This
104   *                       will be ignored.
105   * @param  localPort     The local port to use for the connection.  This will
106   *                       be ignored.
107   *
108   * @return  The socket that was created.
109   *
110   * @throws  IOException  If a problem occurs while creating the socket.
111   */
112  @Override()
113  public Socket createSocket(final String host, final int port,
114                             final InetAddress localAddress,
115                             final int localPort)
116         throws IOException
117  {
118    synchronized (f)
119    {
120      return f.createSocket(host, port, localAddress, localPort);
121    }
122  }
123
124
125
126  /**
127   * Creates a new socket to the specified server.
128   *
129   * @param  address  The address to which the connection should be established.
130   * @param  port     The port to which the connection should be established.
131   *
132   * @return  The socket that was created.
133   *
134   * @throws  IOException  If a problem occurs while creating the socket.
135   */
136  @Override()
137  public Socket createSocket(final InetAddress address, final int port)
138         throws IOException
139  {
140    synchronized (f)
141    {
142      return f.createSocket(address, port);
143    }
144  }
145
146
147
148  /**
149   * Creates a new socket to the specified server.
150   *
151   * @param  address       The address to which the connection should be
152   *                       established.
153   * @param  port          The port to which the connection should be
154   *                       established.
155   * @param  localAddress  The local address to use for the connection.  This
156   *                       will be ignored.
157   * @param  localPort     The local port to use for the connection.  This will
158   *                       be ignored.
159   *
160   * @return  The socket that was created.
161   *
162   * @throws  IOException  If a problem occurs while creating the socket.
163   */
164  @Override()
165  public Socket createSocket(final InetAddress address, final int port,
166                             final InetAddress localAddress,
167                             final int localPort)
168         throws IOException
169  {
170    synchronized (f)
171    {
172      return f.createSocket(address, port, localAddress, localPort);
173    }
174  }
175
176
177
178  /**
179   * {@inheritDoc}
180   */
181  @Override()
182  public Socket makeSocket(final String host, final int port)
183         throws LDAPException
184  {
185    try
186    {
187      synchronized (f)
188      {
189        return f.createSocket(host, port);
190      }
191    }
192    catch (final Exception e)
193    {
194      Debug.debugException(e);
195      throw new LDAPException(StaticUtils.getExceptionMessage(e),
196           LDAPException.CONNECT_ERROR);
197    }
198  }
199}