I am using SocketConnector to establish a connection behind a
firewall. I have amended the SocketConnector class as follows:

-- added proxy field
   private final Proxy socketProxy;

-- added new method to create SocketConnector with proxy
   /**
    * Create a connector with a single processing thread
    */
   public SocketConnector(Proxy proxy)
   {
       this( 1, proxy );
   }

   /**
    * Create a connector with a single processing thread
    */
   public SocketConnector()
   {
       this( 1, Proxy.NO_PROXY );
   }

   /**
    * Create a connector with the desired number of processing threads
    *
    * @param processorCount Number of processing threads
    */
   public SocketConnector( int processorCount, Proxy proxy )
   {
       if( processorCount < 1 )
       {
           throw new IllegalArgumentException( "Must have at least
one processor" );
       }

       this.processorCount = processorCount;
       this.socketProxy = proxy == null ? Proxy.NO_PROXY : proxy;
       ioProcessors = new SocketIoProcessor[processorCount];

       for( int i = 0; i < processorCount; i++ )
       {
           ioProcessors[i] = new SocketIoProcessor(
"SocketConnectorIoProcessor-" + id + "." + i );
       }
   }

-- modified connection method to create socket with proxy.
   public ConnectFuture connect( SocketAddress address, SocketAddress
localAddress,
                                 IoHandler handler, IoServiceConfig config )
   {
       if( address == null )
           throw new NullPointerException( "address" );
       if( handler == null )
           throw new NullPointerException( "handler" );

       if( ! ( address instanceof InetSocketAddress ) )
           throw new IllegalArgumentException( "Unexpected address type: "
                                               + address.getClass() );

       if( localAddress != null && !( localAddress instanceof
InetSocketAddress ) )
           throw new IllegalArgumentException( "Unexpected local
address type: "
                                               + localAddress.getClass() );

       if( config == null )
       {
           config = getDefaultConfig();
       }

       SocketChannel ch = null;
       boolean success = false;
       try
       {
//            ch = SocketChannel.open();
//            ch.socket().setReuseAddress( true );
           Socket s = new Socket(socketProxy);
           s.setReuseAddress( true );
           ch = s.getChannel().open();
           if( localAddress != null )
           {
               s.bind( localAddress );
           }

           ch.configureBlocking( false );

           if( ch.connect( address ) )
           {
               SocketSessionImpl session = newSession( ch, handler, config );
               success = true;
               DefaultConnectFuture future = new DefaultConnectFuture();
               future.setSession( session );
               return future;
           }

           success = true;
       }
       catch( IOException e )
       {
           return DefaultConnectFuture.newFailedFuture( e );
       }
       finally
       {
           if( !success && ch != null )
           {
               try
               {
                   ch.close();
               }
               catch( IOException e )
               {
                   ExceptionMonitor.getInstance().exceptionCaught( e );
               }
           }
       }

       ConnectionRequest request = new ConnectionRequest( ch,
handler, config );
       synchronized( lock )
       {
           try
           {
               startupWorker();
           }
           catch( IOException e )
           {
               try
               {
                   ch.close();
               }
               catch( IOException e2 )
               {
                   ExceptionMonitor.getInstance().exceptionCaught( e2 );
               }

               return DefaultConnectFuture.newFailedFuture( e );
           }
       }

       synchronized( connectQueue )
       {
           connectQueue.push( request );
       }
       selector.wakeup();

       return request;
   }

It seems to work. Would it be possible to get the additons commited
after whatever regression testing is carried out, so that I don't have
to include the mina core code in my application.

Reply via email to