View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.mina.transport.vmpipe;
21  
22  import java.io.IOException;
23  import java.net.SocketAddress;
24  import java.util.ArrayList;
25  import java.util.HashMap;
26  import java.util.Iterator;
27  import java.util.List;
28  import java.util.Map;
29  
30  import org.apache.mina.common.IoHandler;
31  import org.apache.mina.common.IoServiceConfig;
32  import org.apache.mina.common.IoSessionConfig;
33  import org.apache.mina.common.support.BaseIoAcceptor;
34  import org.apache.mina.common.support.BaseIoAcceptorConfig;
35  import org.apache.mina.common.support.BaseIoSessionConfig;
36  import org.apache.mina.transport.vmpipe.support.VmPipe;
37  
38  /**
39   * Binds the specified {@link IoHandler} to the specified
40   * {@link VmPipeAddress}.
41   * 
42   * @author The Apache Directory Project (mina-dev@directory.apache.org)
43   * @version $Rev: 555855 $, $Date: 2007-07-13 12:19:00 +0900 (Fri, 13 Jul 2007) $
44   */
45  public class VmPipeAcceptor extends BaseIoAcceptor {
46      static final Map boundHandlers = new HashMap();
47  
48      private static final IoSessionConfig CONFIG = new BaseIoSessionConfig() {
49      };
50  
51      private final IoServiceConfig defaultConfig = new BaseIoAcceptorConfig() {
52          public IoSessionConfig getSessionConfig() {
53              return CONFIG;
54          }
55      };
56  
57      public void bind(SocketAddress address, IoHandler handler,
58              IoServiceConfig config) throws IOException {
59          if (handler == null)
60              throw new NullPointerException("handler");
61          if (address != null && !(address instanceof VmPipeAddress))
62              throw new IllegalArgumentException("address must be VmPipeAddress.");
63  
64          if (config == null) {
65              config = getDefaultConfig();
66          }
67  
68          synchronized (boundHandlers) {
69              if (address == null || ((VmPipeAddress) address).getPort() == 0) {
70                  for (int i = 1; i < Integer.MAX_VALUE; i++) {
71                      address = new VmPipeAddress(i);
72                      if (!boundHandlers.containsKey(address)) {
73                          break;
74                      }
75                  }
76              } else if (boundHandlers.containsKey(address)) {
77                  throw new IOException("Address already bound: " + address);
78              }
79  
80              boundHandlers.put(address, new VmPipe(this,
81                      (VmPipeAddress) address, handler, config, getListeners()));
82          }
83  
84          getListeners().fireServiceActivated(this, address, handler, config);
85      }
86  
87      public void unbind(SocketAddress address) {
88          if (address == null)
89              throw new NullPointerException("address");
90  
91          VmPipe pipe = null;
92          synchronized (boundHandlers) {
93              if (!boundHandlers.containsKey(address)) {
94                  throw new IllegalArgumentException("Address not bound: "
95                          + address);
96              }
97  
98              pipe = (VmPipe) boundHandlers.remove(address);
99          }
100 
101         getListeners().fireServiceDeactivated(this, pipe.getAddress(),
102                 pipe.getHandler(), pipe.getConfig());
103     }
104 
105     public void unbindAll() {
106         synchronized (boundHandlers) {
107             List addresses = new ArrayList(boundHandlers.keySet());
108             for (Iterator i = addresses.iterator(); i.hasNext();) {
109                 unbind((SocketAddress) i.next());
110             }
111         }
112     }
113 
114     public IoServiceConfig getDefaultConfig() {
115         return defaultConfig;
116     }
117 }