001 /*
002 * Copyright 2012-2016 UnboundID Corp.
003 * All Rights Reserved.
004 */
005 /*
006 * Copyright (C) 2012-2016 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.util;
022
023
024
025 import java.io.IOException;
026 import java.net.InetAddress;
027 import java.net.Socket;
028 import javax.net.ssl.SSLSocketFactory;
029
030
031
032 /**
033 * This class provides an implementation of a Java socket factory that will
034 * wrap a provided socket factory but will synchronize on each use of that
035 * factory to ensure that only a single thread may use that factory to create
036 * a socket at any given time.
037 */
038 public final class SynchronizedSSLSocketFactory
039 extends SSLSocketFactory
040 {
041 // The wrapped SSL socket factory.
042 private final SSLSocketFactory factory;
043
044
045
046 /**
047 * Creates a new synchronous socket factory instance that will wrap the
048 * provided socket factory.
049 *
050 * @param factory The socket factory to be wrapped.
051 */
052 public SynchronizedSSLSocketFactory(final SSLSocketFactory factory)
053 {
054 this.factory = factory;
055 }
056
057
058
059 /**
060 * Retrieves the {@code SSLSocketFactory} instance wrapped by this
061 * synchronized SSL socket factory.
062 *
063 * @return The {@code SSLSocketFactory} instance wrapped by this synchronized
064 * SSL socket factory.
065 */
066 public SSLSocketFactory getWrappedSocketFactory()
067 {
068 return factory;
069 }
070
071
072
073 /**
074 * Creates a new SSL socket to the specified server.
075 *
076 * @param host The host to which the connection should be established.
077 * @param port The port to which the connection should be established.
078 *
079 * @return The SSL socket that was created.
080 *
081 * @throws IOException If a problem occurs while creating the socket.
082 */
083 @Override()
084 public Socket createSocket(final String host, final int port)
085 throws IOException
086 {
087 synchronized (factory)
088 {
089 return factory.createSocket(host, port);
090 }
091 }
092
093
094
095 /**
096 * Creates a new SSL socket to the specified server.
097 *
098 * @param host The host to which the connection should be
099 * established.
100 * @param port The port to which the connection should be
101 * established.
102 * @param localAddress The local address to use for the connection. This
103 * will be ignored.
104 * @param localPort The local port to use for the connection. This will
105 * be ignored.
106 *
107 * @return The SSL socket that was created.
108 *
109 * @throws IOException If a problem occurs while creating the socket.
110 */
111 @Override()
112 public Socket createSocket(final String host, final int port,
113 final InetAddress localAddress,
114 final int localPort)
115 throws IOException
116 {
117 synchronized (factory)
118 {
119 return factory.createSocket(host, port, localAddress, localPort);
120 }
121 }
122
123
124
125 /**
126 * Creates a new SSL socket to the specified server.
127 *
128 * @param address The address to which the connection should be established.
129 * @param port The port to which the connection should be established.
130 *
131 * @return The SSL socket that was created.
132 *
133 * @throws IOException If a problem occurs while creating the socket.
134 */
135 @Override()
136 public Socket createSocket(final InetAddress address, final int port)
137 throws IOException
138 {
139 synchronized (factory)
140 {
141 return factory.createSocket(address, port);
142 }
143 }
144
145
146
147 /**
148 * Creates a new SSL socket to the specified server.
149 *
150 * @param address The address to which the connection should be
151 * established.
152 * @param port The port to which the connection should be
153 * established.
154 * @param localAddress The local address to use for the connection. This
155 * will be ignored.
156 * @param localPort The local port to use for the connection. This will
157 * be ignored.
158 *
159 * @return The SSL socket that was created.
160 *
161 * @throws IOException If a problem occurs while creating the socket.
162 */
163 @Override()
164 public Socket createSocket(final InetAddress address, final int port,
165 final InetAddress localAddress,
166 final int localPort)
167 throws IOException
168 {
169 synchronized (factory)
170 {
171 return factory.createSocket(address, port, localAddress, localPort);
172 }
173 }
174
175
176
177 /**
178 * Creates a new SSL socket that wraps the provided socket.
179 *
180 * @param s The existing socket to be wrapped to create an SSL
181 * socket.
182 * @param host The host to which the connection is established.
183 * @param port The port to which the connection is established.
184 * @param autoClose Indicates whether the provided socket should be closed
185 * when the created SSL socket is closed.
186 *
187 * @return The SSL socket that was created.
188 *
189 * @throws IOException If a problem occurs while creating the socket.
190 */
191 @Override()
192 public Socket createSocket(final Socket s, final String host, final int port,
193 final boolean autoClose)
194 throws IOException
195 {
196 synchronized (factory)
197 {
198 return factory.createSocket(s, host, port, autoClose);
199 }
200 }
201
202
203
204 /**
205 * Retrieves the set of cipher suites which are enabled by default.
206 *
207 * @return The set of cipher suites which are enabled by default.
208 */
209 @Override()
210 public String[] getDefaultCipherSuites()
211 {
212 synchronized (factory)
213 {
214 return factory.getDefaultCipherSuites();
215 }
216 }
217
218
219
220 /**
221 * Retrieves the entire set of cipher suites that could be used.
222 *
223 * @return The entire set of cipher suites that could be used.
224 */
225 @Override()
226 public String[] getSupportedCipherSuites()
227 {
228 synchronized (factory)
229 {
230 return factory.getSupportedCipherSuites();
231 }
232 }
233 }