View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
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-07 04:34:55 +0100 (ven, 07 déc 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") || 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  }