View Javadoc

1   /*
2    * @(#) $Id: VmPipeAcceptor.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   import java.util.HashMap;
9   import java.util.Map;
10  
11  import org.apache.mina.common.BaseSessionManager;
12  import org.apache.mina.protocol.ProtocolAcceptor;
13  import org.apache.mina.protocol.ProtocolFilterChain;
14  import org.apache.mina.protocol.ProtocolHandler;
15  import org.apache.mina.protocol.ProtocolProvider;
16  
17  /***
18   * Binds the specified {@link ProtocolProvider} to the specified
19   * {@link VmPipeAddress}.
20   * 
21   * @author Trustin Lee (trustin@apache.org)
22   * @version $Rev: 210062 $, $Date: 2005-07-11 12:52:38 +0900 $
23   */
24  public class VmPipeAcceptor extends BaseSessionManager implements ProtocolAcceptor
25  {
26      static final Map boundHandlers = new HashMap();
27  
28      private final VmPipeSessionManagerFilterChain filterChain =
29          new VmPipeSessionManagerFilterChain( this );
30  
31      /***
32       * Creates a new instance.
33       */
34      public VmPipeAcceptor()
35      {
36          filterChain.addFirst( "VMPipe", new VmPipeFilter() );
37      }
38      
39      public void bind( SocketAddress address, ProtocolProvider protocolProvider ) throws IOException
40      {
41          if( address == null )
42              throw new NullPointerException( "address" );
43          if( protocolProvider == null )
44              throw new NullPointerException( "protocolProvider" );
45          if( !( address instanceof VmPipeAddress ) )
46              throw new IllegalArgumentException(
47                      "address must be VmPipeAddress." );
48  
49          synchronized( boundHandlers )
50          {
51              if( boundHandlers.containsKey( address ) )
52              {
53                  throw new IOException( "Address already bound: " + address );
54              }
55  
56              boundHandlers.put( address, 
57                                 new Entry( this,
58                                            ( VmPipeAddress ) address,
59                                            filterChain,
60                                            protocolProvider.getHandler() ) );
61          }
62      }
63  
64      public void unbind( SocketAddress address )
65      {
66          if( address == null )
67              throw new NullPointerException( "address" );
68  
69          synchronized( boundHandlers )
70          {
71              boundHandlers.remove( address );
72          }
73      }
74      
75      public ProtocolFilterChain getFilterChain()
76      {
77          return filterChain;
78      }
79  
80      static class Entry
81      {
82          final VmPipeAcceptor acceptor;
83          
84          final VmPipeAddress address;
85  
86          final VmPipeSessionManagerFilterChain managerFilterChain;
87  
88          final ProtocolHandler handler;
89          
90          private Entry( VmPipeAcceptor acceptor,
91                         VmPipeAddress address,
92                         VmPipeSessionManagerFilterChain managerFilterChain,
93                         ProtocolHandler handler )
94          {
95              this.acceptor = acceptor;
96              this.address = address;
97              this.managerFilterChain = managerFilterChain;
98              this.handler = handler;
99          }
100     }
101 }