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