1008N/A/*
1008N/A * CDDL HEADER START
1008N/A *
1008N/A * The contents of this file are subject to the terms of the
1008N/A * Common Development and Distribution License, Version 1.0 only
1008N/A * (the "License"). You may not use this file except in compliance
1008N/A * with the License.
1008N/A *
6983N/A * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
6983N/A * or http://forgerock.org/license/CDDLv1.0.html.
1008N/A * See the License for the specific language governing permissions
1008N/A * and limitations under the License.
1008N/A *
1008N/A * When distributing Covered Code, include this CDDL HEADER in each
6983N/A * file and include the License file at legal-notices/CDDLv1_0.txt.
6983N/A * If applicable, add the following below this CDDL HEADER, with the
6983N/A * fields enclosed by brackets "[]" replaced with your own identifying
6983N/A * information:
1008N/A * Portions Copyright [yyyy] [name of copyright owner]
1008N/A *
1008N/A * CDDL HEADER END
1008N/A *
1008N/A *
3215N/A * Copyright 2008 Sun Microsystems, Inc.
1008N/A */
1008N/A
1008N/Apackage org.opends.server.admin;
1008N/A
1008N/A
1008N/A
1008N/Aimport static org.opends.server.util.Validator.ensureNotNull;
1008N/A
1008N/Aimport java.net.InetAddress;
1008N/Aimport java.net.UnknownHostException;
1008N/Aimport java.util.EnumSet;
1008N/A
1008N/A
1008N/A
1008N/A/**
1008N/A * IP address property definition.
1008N/A */
1008N/Apublic final class IPAddressPropertyDefinition extends
1470N/A PropertyDefinition<InetAddress> {
1008N/A
1008N/A /**
1008N/A * An interface for incrementally constructing IP address property
1008N/A * definitions.
1008N/A */
1008N/A public static class Builder extends
1008N/A AbstractBuilder<InetAddress, IPAddressPropertyDefinition> {
1008N/A
1008N/A // Private constructor
1140N/A private Builder(
1140N/A AbstractManagedObjectDefinition<?, ?> d, String propertyName) {
1140N/A super(d, propertyName);
1008N/A }
1008N/A
1008N/A
1008N/A
1008N/A /**
1008N/A * {@inheritDoc}
1008N/A */
1008N/A @Override
1140N/A protected IPAddressPropertyDefinition buildInstance(
1140N/A AbstractManagedObjectDefinition<?, ?> d, String propertyName,
1008N/A EnumSet<PropertyOption> options,
1565N/A AdministratorAction adminAction,
1008N/A DefaultBehaviorProvider<InetAddress> defaultBehavior) {
1140N/A return new IPAddressPropertyDefinition(d, propertyName, options,
1565N/A adminAction, defaultBehavior);
1008N/A }
1008N/A
1008N/A }
1008N/A
1008N/A
1008N/A
1008N/A /**
1008N/A * Create a IP address property definition builder.
1008N/A *
1140N/A * @param d
1140N/A * The managed object definition associated with this
1140N/A * property definition.
1008N/A * @param propertyName
1008N/A * The property name.
1008N/A * @return Returns the new IP address property definition builder.
1008N/A */
1140N/A public static Builder createBuilder(
1140N/A AbstractManagedObjectDefinition<?, ?> d, String propertyName) {
1140N/A return new Builder(d, propertyName);
1008N/A }
1008N/A
1008N/A
1008N/A
1008N/A // Private constructor.
1140N/A private IPAddressPropertyDefinition(
1140N/A AbstractManagedObjectDefinition<?, ?> d, String propertyName,
1008N/A EnumSet<PropertyOption> options,
1565N/A AdministratorAction adminAction,
1008N/A DefaultBehaviorProvider<InetAddress> defaultBehavior) {
1565N/A super(d, InetAddress.class, propertyName, options, adminAction,
1565N/A defaultBehavior);
1008N/A }
1008N/A
1008N/A
1008N/A
1008N/A /**
1008N/A * {@inheritDoc}
1008N/A */
1008N/A @Override
1008N/A public void validateValue(InetAddress value)
1008N/A throws IllegalPropertyValueException {
1008N/A ensureNotNull(value);
1008N/A
1008N/A // No additional validation required.
1008N/A }
1008N/A
1008N/A
1008N/A
1008N/A /**
1008N/A * {@inheritDoc}
1008N/A */
1008N/A @Override
1008N/A public InetAddress decodeValue(String value)
1008N/A throws IllegalPropertyValueStringException {
1008N/A ensureNotNull(value);
1008N/A
1008N/A try {
1008N/A return InetAddress.getByName(value);
1008N/A } catch (UnknownHostException e) {
1008N/A // TODO: it would be nice to throw the cause.
1008N/A throw new IllegalPropertyValueStringException(this, value);
1008N/A }
1008N/A }
1008N/A
1008N/A
1008N/A
1008N/A /**
1008N/A * {@inheritDoc}
1008N/A */
1008N/A @Override
1254N/A public String encodeValue(InetAddress value)
1254N/A throws IllegalPropertyValueException {
1566N/A // We should return the host name if it is available, or the IP
1566N/A // address if not.
1566N/A
1566N/A // Unforunately, there is no InetAddress method for doing this, so
1566N/A // we have to resort to hacking at the toString() encoding.
1566N/A String s = value.toString();
1566N/A int i = s.indexOf('/');
1566N/A if (i > 0) {
1566N/A // Host address is before the forward slash.
1566N/A return s.substring(0, i);
1566N/A } else {
1566N/A return value.getHostAddress();
1566N/A }
1254N/A }
1254N/A
1254N/A
1254N/A
1254N/A /**
1254N/A * {@inheritDoc}
1254N/A */
1254N/A @Override
1008N/A public <R, P> R accept(PropertyDefinitionVisitor<R, P> v, P p) {
1008N/A return v.visitIPAddress(this, p);
1008N/A }
1008N/A
1008N/A
1008N/A
1008N/A /**
1008N/A * {@inheritDoc}
1008N/A */
1008N/A @Override
1470N/A public <R, P> R accept(PropertyValueVisitor<R, P> v, InetAddress value, P p) {
1470N/A return v.visitIPAddress(this, value, p);
1470N/A }
1470N/A
1470N/A
1470N/A
1470N/A /**
1470N/A * {@inheritDoc}
1470N/A */
1470N/A @Override
1008N/A public int compare(InetAddress o1, InetAddress o2) {
1008N/A return o1.getHostAddress().compareTo(o2.getHostAddress());
1008N/A }
1008N/A}