01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
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.integration.beans;
21:
22: import java.beans.PropertyEditor;
23: import java.net.InetSocketAddress;
24:
25: /**
26: * A {@link PropertyEditor} which converts a {@link String} into an
27: * {@link InetSocketAddress}. Valid values include a hostname or IP
28: * address and a port number separated by a ':'. If the hostname or IP address
29: * is omitted the wildcard address will be used. E.g.:
30: * <code>google.com:80</code>, <code>:22</code>, <code>192.168.0.1:110</code>.
31: *
32: * @author The Apache MINA Project (dev@mina.apache.org)
33: * @version $Revision: 601972 $, $Date: 2007-12-06 20:34:55 -0700 (Thu, 06 Dec 2007) $
34: *
35: * @see java.net.InetSocketAddress
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")
50: || hostname.equals("0.0.0.0")
51: || hostname.equals("00:00:00:00:00:00:00:00")) {
52: hostname = "*";
53: }
54:
55: return hostname + ':' + addr.getPort();
56: }
57:
58: @Override
59: protected Object toValue(String text)
60: throws IllegalArgumentException {
61: if (text.length() == 0) {
62: return defaultValue();
63: }
64:
65: int colonIndex = text.lastIndexOf(":");
66: if (colonIndex > 0) {
67: String host = text.substring(0, colonIndex);
68: if (!"*".equals(host)) {
69: int port = parsePort(text.substring(colonIndex + 1));
70: return new InetSocketAddress(host, port);
71: }
72: }
73:
74: int port = parsePort(text.substring(colonIndex + 1));
75: return new InetSocketAddress(port);
76: }
77:
78: @Override
79: protected String defaultText() {
80: return "*:0";
81: }
82:
83: @Override
84: protected Object defaultValue() {
85: return new InetSocketAddress(0);
86: }
87:
88: private int parsePort(String s) {
89: try {
90: return Integer.parseInt(s);
91: } catch (NumberFormatException nfe) {
92: throw new IllegalArgumentException("Illegal port number: "
93: + s);
94: }
95: }
96: }
|