001/* 002 * Copyright 2012-2014 UnboundID Corp. 003 * All Rights Reserved. 004 */ 005/* 006 * Copyright (C) 2012-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.util; 022 023 024 025import java.io.IOException; 026import java.net.InetAddress; 027import java.net.Socket; 028import javax.net.SocketFactory; 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@NotMutable() 039@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE) 040public final class SynchronizedSocketFactory 041 extends SocketFactory 042{ 043 // The wrapped socket factory. 044 private final SocketFactory factory; 045 046 047 048 /** 049 * Creates a new synchronous socket factory instance that will wrap the 050 * provided socket factory. 051 * 052 * @param factory The socket factory to be wrapped. 053 */ 054 public SynchronizedSocketFactory(final SocketFactory factory) 055 { 056 this.factory = factory; 057 } 058 059 060 061 /** 062 * Retrieves the {@code SocketFactory} instance wrapped by this synchronized 063 * socket factory. 064 * 065 * @return The {@code SocketFactory} instance wrapped by this synchronized 066 * socket factory. 067 */ 068 public SocketFactory getWrappedSocketFactory() 069 { 070 return factory; 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 (factory) 090 { 091 return factory.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 (factory) 120 { 121 return factory.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 (factory) 142 { 143 return factory.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 (factory) 172 { 173 return factory.createSocket(address, port, localAddress, localPort); 174 } 175 } 176}