View Javadoc

1   /*
2    * @(#) $Id: VmPipeConnector.java 210062 2005-07-11 03:52:38Z trustin $
3    */
4   package org.apache.mina.protocol.vmpipe;
5   
6   import java.io.IOException;
7   import java.net.SocketAddress;
8   
9   import org.apache.mina.common.BaseSessionManager;
10  import org.apache.mina.protocol.ProtocolConnector;
11  import org.apache.mina.protocol.ProtocolFilterChain;
12  import org.apache.mina.protocol.ProtocolProvider;
13  import org.apache.mina.protocol.ProtocolSession;
14  import org.apache.mina.protocol.vmpipe.VmPipeAcceptor.Entry;
15  
16  /***
17   * Connects to {@link ProtocolProvider}s which is bound on the specified
18   * {@link VmPipeAddress}.
19   * 
20   * @author Trustin Lee (trustin@apache.org)
21   * @version $Rev: 210062 $, $Date: 2005-07-11 12:52:38 +0900 $
22   */
23  public class VmPipeConnector extends BaseSessionManager implements ProtocolConnector
24  {
25      private final VmPipeSessionManagerFilterChain filterChain =
26          new VmPipeSessionManagerFilterChain( this );
27  
28      /***
29       * Creates a new instance.
30       */
31      public VmPipeConnector()
32      {
33          filterChain.addFirst( "VMPipe", new VmPipeFilter() );
34      }
35      
36      public ProtocolFilterChain getFilterChain()
37      {
38          return filterChain;
39      }
40  
41      public ProtocolSession connect( SocketAddress address, ProtocolProvider protocolProvider ) throws IOException 
42      {
43          return connect( address, null, Integer.MAX_VALUE, protocolProvider );
44      }
45  
46      public ProtocolSession connect( SocketAddress address, SocketAddress localAddress, ProtocolProvider protocolProvider ) throws IOException
47      {
48          return connect( address, localAddress, Integer.MAX_VALUE, protocolProvider );
49      }
50  
51      public ProtocolSession connect( SocketAddress address, int timeout, ProtocolProvider protocolProvider ) throws IOException
52      {
53          return connect( address, null, timeout, protocolProvider );
54      }
55  
56      public ProtocolSession connect( SocketAddress address, SocketAddress localAddress, int timeout, ProtocolProvider protocolProvider ) throws IOException
57      {
58          if( address == null )
59              throw new NullPointerException( "address" );
60          if( protocolProvider == null )
61              throw new NullPointerException( "protocolProvider" );
62          if( ! ( address instanceof VmPipeAddress ) )
63              throw new IllegalArgumentException(
64                                                  "address must be VmPipeAddress." );
65  
66          Entry entry = ( Entry ) VmPipeAcceptor.boundHandlers.get( address );
67          if( entry == null )
68              throw new IOException( "Endpoint unavailable: " + address );
69  
70          VmPipeSession session = new VmPipeSession( new Object(), // lock
71                                                     AnonymousVmPipeAddress.INSTANCE,
72                                                     filterChain,
73                                                     protocolProvider.getHandler(),
74                                                     entry );
75  
76          VmPipeIdleStatusChecker.INSTANCE.addSession( session );
77          return session;
78      }
79  }