1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.mina.integration.beans;
21
22 import java.beans.PropertyEditor;
23 import java.net.InetSocketAddress;
24
25
26
27
28
29
30
31
32
33
34
35
36 public class InetSocketAddressEditor extends AbstractPropertyEditor {
37
38 @Override
39 protected String toText(Object value) {
40 InetSocketAddress addr = ((InetSocketAddress) value);
41 String hostname;
42 if (addr.getAddress() != null) {
43 hostname = addr.getAddress().getHostAddress();
44 } else {
45 hostname = addr.getHostName();
46 }
47
48 if (hostname.equals("0:0:0:0:0:0:0:0") || hostname.equals("0.0.0.0") ||
49 hostname.equals("00:00:00:00:00:00:00:00")) {
50 hostname = "*";
51 }
52
53 return hostname + ':' + addr.getPort();
54 }
55
56 @Override
57 protected Object toValue(String text) throws IllegalArgumentException {
58 if (text.length() == 0) {
59 return defaultValue();
60 }
61
62 int colonIndex = text.lastIndexOf(":");
63 if (colonIndex > 0) {
64 String host = text.substring(0, colonIndex);
65 if (!"*".equals(host)) {
66 int port = parsePort(text.substring(colonIndex + 1));
67 return new InetSocketAddress(host, port);
68 }
69 }
70
71 int port = parsePort(text.substring(colonIndex + 1));
72 return new InetSocketAddress(port);
73 }
74
75 @Override
76 protected String defaultText() {
77 return "*:0";
78 }
79
80 @Override
81 protected Object defaultValue() {
82 return new InetSocketAddress(0);
83 }
84
85 private int parsePort(String s) {
86 try {
87 return Integer.parseInt(s);
88 } catch (NumberFormatException nfe) {
89 throw new IllegalArgumentException("Illegal port number: " + s);
90 }
91 }
92 }