1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.mina.common;
21
22 import java.io.InvalidObjectException;
23 import java.io.ObjectStreamException;
24 import java.io.Serializable;
25 import java.util.HashMap;
26 import java.util.Map;
27 import java.util.Set;
28 import java.util.TreeSet;
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 public final class TransportType implements Serializable {
47 private static final long serialVersionUID = 3258132470497883447L;
48
49 private static final Map<String, TransportType> name2type = new HashMap<String, TransportType>();
50
51 private static void register(String[] names, TransportType type) {
52 synchronized (name2type) {
53 for (int i = names.length - 1; i >= 0; i--) {
54 if (name2type.containsKey(names[i])) {
55 throw new IllegalArgumentException("Transport type name '"
56 + names[i] + "' is already taken.");
57 }
58 }
59
60 for (int i = names.length - 1; i >= 0; i--) {
61 name2type.put(names[i].toUpperCase(), type);
62 }
63 }
64 }
65
66
67
68
69 public static final TransportType SOCKET = new TransportType(new String[] {
70 "SOCKET", "TCP" }, false);
71
72
73
74
75 public static final TransportType DATAGRAM = new TransportType(
76 new String[] { "DATAGRAM", "UDP" }, true);
77
78
79
80
81
82
83
84 public static final TransportType VM_PIPE = new TransportType(
85 new String[] { "VM_PIPE" }, Object.class, false);
86
87
88
89
90
91
92
93
94
95 public static TransportType getInstance(String name) {
96 TransportType type = name2type.get(name.toUpperCase());
97 if (type != null) {
98 return type;
99 }
100
101 throw new IllegalArgumentException("Unknown transport type name: "
102 + name);
103 }
104
105 private final String[] names;
106
107 private final transient boolean connectionless;
108
109 private final transient Class<? extends Object> envelopeType;
110
111
112
113
114
115
116
117
118
119
120 public TransportType(String[] names, boolean connectionless) {
121 this(names, ByteBuffer.class, connectionless);
122 }
123
124
125
126
127
128
129
130
131
132
133 public TransportType(String[] names, Class<? extends Object> envelopeType,
134 boolean connectionless) {
135 if (names == null) {
136 throw new NullPointerException("names");
137 }
138 if (names.length == 0) {
139 throw new IllegalArgumentException("names is empty");
140 }
141 if (envelopeType == null) {
142 throw new NullPointerException("envelopeType");
143 }
144
145 for (int i = 0; i < names.length; i++) {
146 if (names[i] == null) {
147 throw new NullPointerException("strVals[" + i + "]");
148 }
149
150 names[i] = names[i].toUpperCase();
151 }
152
153 register(names, this);
154 this.names = names;
155 this.connectionless = connectionless;
156 this.envelopeType = envelopeType;
157 }
158
159
160
161
162
163 public boolean isConnectionless() {
164 return connectionless;
165 }
166
167 public Class<? extends Object> getEnvelopeType() {
168 return envelopeType;
169 }
170
171
172
173
174 public Set<String> getNames() {
175 Set<String> result = new TreeSet<String>();
176 for (int i = names.length - 1; i >= 0; i--) {
177 result.add(names[i]);
178 }
179
180 return result;
181 }
182
183 @Override
184 public String toString() {
185 return names[0];
186 }
187
188 private Object readResolve() throws ObjectStreamException {
189 for (int i = names.length - 1; i >= 0; i--) {
190 try {
191 return getInstance(names[i]);
192 } catch (IllegalArgumentException e) {
193
194 }
195 }
196
197 throw new InvalidObjectException("Unknown transport type.");
198 }
199 }