001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions Copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2008 Sun Microsystems, Inc.
015 */
016
017package org.forgerock.opendj.config;
018
019import org.forgerock.util.Reject;
020
021import java.net.InetAddress;
022import java.net.UnknownHostException;
023import java.util.EnumSet;
024
025/**
026 * IP address property definition.
027 */
028public final class IPAddressPropertyDefinition extends PropertyDefinition<InetAddress> {
029
030    /**
031     * An interface for incrementally constructing IP address property
032     * definitions.
033     */
034    public static final class Builder extends AbstractBuilder<InetAddress, IPAddressPropertyDefinition> {
035
036        /** Private constructor. */
037        private Builder(AbstractManagedObjectDefinition<?, ?> d, String propertyName) {
038            super(d, propertyName);
039        }
040
041        /** {@inheritDoc} */
042        @Override
043        protected IPAddressPropertyDefinition buildInstance(AbstractManagedObjectDefinition<?, ?> d,
044            String propertyName, EnumSet<PropertyOption> options, AdministratorAction adminAction,
045            DefaultBehaviorProvider<InetAddress> defaultBehavior) {
046            return new IPAddressPropertyDefinition(d, propertyName, options, adminAction, defaultBehavior);
047        }
048
049    }
050
051    /**
052     * Create a IP address property definition builder.
053     *
054     * @param d
055     *            The managed object definition associated with this property
056     *            definition.
057     * @param propertyName
058     *            The property name.
059     * @return Returns the new IP address property definition builder.
060     */
061    public static Builder createBuilder(AbstractManagedObjectDefinition<?, ?> d, String propertyName) {
062        return new Builder(d, propertyName);
063    }
064
065    /** Private constructor. */
066    private IPAddressPropertyDefinition(AbstractManagedObjectDefinition<?, ?> d, String propertyName,
067        EnumSet<PropertyOption> options, AdministratorAction adminAction,
068        DefaultBehaviorProvider<InetAddress> defaultBehavior) {
069        super(d, InetAddress.class, propertyName, options, adminAction, defaultBehavior);
070    }
071
072    /** {@inheritDoc} */
073    @Override
074    public void validateValue(InetAddress value) {
075        Reject.ifNull(value);
076
077        // No additional validation required.
078    }
079
080    /** {@inheritDoc} */
081    @Override
082    public InetAddress decodeValue(String value) {
083        Reject.ifNull(value);
084
085        try {
086            return InetAddress.getByName(value);
087        } catch (UnknownHostException e) {
088            // TODO: it would be nice to throw the cause.
089            throw PropertyException.illegalPropertyValueException(this, value);
090        }
091    }
092
093    /** {@inheritDoc} */
094    @Override
095    public String encodeValue(InetAddress value) {
096        // We should return the host name if it is available, or the IP
097        // address if not.
098
099        // Unforunately, there is no InetAddress method for doing this, so
100        // we have to resort to hacking at the toString() encoding.
101        String s = value.toString();
102        int i = s.indexOf('/');
103        if (i > 0) {
104            // Host address is before the forward slash.
105            return s.substring(0, i);
106        } else {
107            return value.getHostAddress();
108        }
109    }
110
111    /** {@inheritDoc} */
112    @Override
113    public <R, P> R accept(PropertyDefinitionVisitor<R, P> v, P p) {
114        return v.visitIPAddress(this, p);
115    }
116
117    /** {@inheritDoc} */
118    @Override
119    public <R, P> R accept(PropertyValueVisitor<R, P> v, InetAddress value, P p) {
120        return v.visitIPAddress(this, value, p);
121    }
122
123    /** {@inheritDoc} */
124    @Override
125    public int compare(InetAddress o1, InetAddress o2) {
126        return o1.getHostAddress().compareTo(o2.getHostAddress());
127    }
128}