001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 *
019 */
020package org.apache.mina.integration.beans;
021
022import java.beans.PropertyEditor;
023import java.net.InetSocketAddress;
024
025/**
026 * A {@link PropertyEditor} which converts a {@link String} into an
027 * {@link InetSocketAddress}. Valid values include a hostname or IP
028 * address and a port number separated by a ':'. If the hostname or IP address
029 * is omitted the wildcard address will be used. E.g.:
030 * <code>google.com:80</code>, <code>:22</code>, <code>192.168.0.1:110</code>.
031 *
032 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
033 *
034 * @see java.net.InetSocketAddress
035 */
036public class InetSocketAddressEditor extends AbstractPropertyEditor {
037
038    @Override
039    protected String toText(Object value) {
040        InetSocketAddress addr = ((InetSocketAddress) value);
041        String hostname;
042        if (addr.getAddress() != null) {
043            hostname = addr.getAddress().getHostAddress();
044        } else {
045            hostname = addr.getHostName();
046        }
047
048        if (hostname.equals("0:0:0:0:0:0:0:0") || hostname.equals("0.0.0.0")
049                || hostname.equals("00:00:00:00:00:00:00:00")) {
050            hostname = "*";
051        }
052
053        return hostname + ':' + addr.getPort();
054    }
055
056    @Override
057    protected Object toValue(String text) throws IllegalArgumentException {
058        if (text.length() == 0) {
059            return defaultValue();
060        }
061
062        int colonIndex = text.lastIndexOf(':');
063        if (colonIndex > 0) {
064            String host = text.substring(0, colonIndex);
065            if (!"*".equals(host)) {
066                int port = parsePort(text.substring(colonIndex + 1));
067                return new InetSocketAddress(host, port);
068            }
069        }
070
071        int port = parsePort(text.substring(colonIndex + 1));
072        return new InetSocketAddress(port);
073    }
074
075    @Override
076    protected String defaultText() {
077        return "*:0";
078    }
079
080    @Override
081    protected Object defaultValue() {
082        return new InetSocketAddress(0);
083    }
084
085    private int parsePort(String s) {
086        try {
087            return Integer.parseInt(s);
088        } catch (NumberFormatException nfe) {
089            throw new IllegalArgumentException("Illegal port number: " + s);
090        }
091    }
092}