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
37 public class InetSocketAddressEditor extends AbstractPropertyEditor {
38
39 @Override
40 protected String toText(Object value) {
41 InetSocketAddress addr = ((InetSocketAddress) value);
42 String hostname;
43 if (addr.getAddress() != null) {
44 hostname = addr.getAddress().getHostAddress();
45 } else {
46 hostname = addr.getHostName();
47 }
48
49 if (hostname.equals("0:0:0:0:0:0:0:0") || hostname.equals("0.0.0.0") ||
50 hostname.equals("00:00:00:00:00:00:00:00")) {
51 hostname = "*";
52 }
53
54 return hostname + ':' + addr.getPort();
55 }
56
57 @Override
58 protected Object toValue(String text) throws IllegalArgumentException {
59 if (text.length() == 0) {
60 return defaultValue();
61 }
62
63 int colonIndex = text.lastIndexOf(":");
64 if (colonIndex > 0) {
65 String host = text.substring(0, colonIndex);
66 if (!"*".equals(host)) {
67 int port = parsePort(text.substring(colonIndex + 1));
68 return new InetSocketAddress(host, port);
69 }
70 }
71
72 int port = parsePort(text.substring(colonIndex + 1));
73 return new InetSocketAddress(port);
74 }
75
76 @Override
77 protected String defaultText() {
78 return "*:0";
79 }
80
81 @Override
82 protected Object defaultValue() {
83 return new InetSocketAddress(0);
84 }
85
86 private int parsePort(String s) {
87 try {
88 return Integer.parseInt(s);
89 } catch (NumberFormatException nfe) {
90 throw new IllegalArgumentException("Illegal port number: " + s);
91 }
92 }
93 }