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 <a href="http://mina.apache.org">Apache MINA Project</a>
    33   *
    34   * @see java.net.InetSocketAddress
    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  }