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     */
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.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    public final class SynchronizedSocketFactory
039           extends SocketFactory
040    {
041      // The wrapped socket factory.
042      private final SocketFactory 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 SynchronizedSocketFactory(final SocketFactory factory)
053      {
054        this.factory = factory;
055      }
056    
057    
058    
059      /**
060       * Retrieves the {@code SocketFactory} instance wrapped by this synchronized
061       * socket factory.
062       *
063       * @return  The {@code SocketFactory} instance wrapped by this synchronized
064       *          socket factory.
065       */
066      public SocketFactory getWrappedSocketFactory()
067      {
068        return factory;
069      }
070    
071    
072    
073      /**
074       * Creates a new 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 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 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 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 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 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 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 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    }