    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 */
    019package org.apache.commons.beanutils2.converters;
    020
    021import java.net.InetAddress;
    022import java.net.UnknownHostException;
    023
    024/**
    025 * {@link org.apache.commons.beanutils2.Converter} implementation that handles conversion to and from {@link InetAddress}.
    026 *
    027 * @since 2.0.0
    028 * @see <a href="https://en.wikipedia.org/wiki/Inet_address">IP Address on Wikipedia</a>
    029 */
    030public class InetAddressConverter extends AbstractConverter<InetAddress> {
    031
    032    /**
    033     * Construct a <strong>{@link InetAddress}</strong> <em>Converter</em> that throws a {@code ConversionException} if an error occurs.
    034     */
    035    public InetAddressConverter() {
    036    }
    037
    038    /**
    039     * Constructs a {@link org.apache.commons.beanutils2.Converter} that will return the specified default value if a conversion error occurs.
    040     *
    041     * @param defaultValue The default value to be returned if the value to be converted is missing or an error occurs converting the value.
    042     */
    043    public InetAddressConverter(final InetAddress defaultValue) {
    044        super(defaultValue);
    045    }
    046
    047    /**
    048     * Converts the specified input object into an output object of the specified type.
    049     *
    050     * @param type  Data type to which this value should be converted.
    051     * @param value The String property value to convert.
    052     * @return An {@link InetAddress} which represents the configuration property value.
    053     * @throws NullPointerException     If the value is null.
    054     * @throws IllegalArgumentException If a host name was specified and the IP address couldn't be obtained.
    055     */
    056    @Override
    057    protected <T> T convertToType(final Class<T> type, final Object value) throws Throwable {
    058        if (InetAddress.class.isAssignableFrom(type)) {
    059            final String stringValue = toString(value);
    060
    061            try {
    062                return type.cast(InetAddress.getByName(stringValue));
    063            } catch (final UnknownHostException ex) {
    064                throw new IllegalArgumentException("Unable to get IP address of the named host.", ex);
    065            }
    066        }
    067
    068        throw conversionException(type, value);
    069    }
    070
    071    /**
    072     * Gets the default type this {@code Converter} handles.
    073     *
    074     * @return The default type this {@code Converter} handles.
    075     * @since 2.0.0
    076     */
    077    @Override
    078    protected Class<InetAddress> getDefaultType() {
    079        return InetAddress.class;
    080    }
    081}