View Javadoc

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