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