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