1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
40
41
42
43
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 }