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 * Portions Copyright 2014-2015 ForgeRock AS. 016 */ 017 018package org.opends.server.admin; 019 020 021 022import static org.forgerock.util.Reject.ifNull; 023 024import java.net.InetAddress; 025import java.net.UnknownHostException; 026import java.util.EnumSet; 027 028 029 030/** 031 * IP address property definition. 032 */ 033public final class IPAddressPropertyDefinition extends 034 PropertyDefinition<InetAddress> { 035 036 /** 037 * An interface for incrementally constructing IP address property 038 * definitions. 039 */ 040 public static class Builder extends 041 AbstractBuilder<InetAddress, IPAddressPropertyDefinition> { 042 043 /** Private constructor. */ 044 private Builder( 045 AbstractManagedObjectDefinition<?, ?> d, String propertyName) { 046 super(d, propertyName); 047 } 048 049 050 051 /** {@inheritDoc} */ 052 @Override 053 protected IPAddressPropertyDefinition buildInstance( 054 AbstractManagedObjectDefinition<?, ?> d, String propertyName, 055 EnumSet<PropertyOption> options, 056 AdministratorAction adminAction, 057 DefaultBehaviorProvider<InetAddress> defaultBehavior) { 058 return new IPAddressPropertyDefinition(d, propertyName, options, 059 adminAction, defaultBehavior); 060 } 061 062 } 063 064 065 066 /** 067 * Create a IP address property definition builder. 068 * 069 * @param d 070 * The managed object definition associated with this 071 * property definition. 072 * @param propertyName 073 * The property name. 074 * @return Returns the new IP address property definition builder. 075 */ 076 public static Builder createBuilder( 077 AbstractManagedObjectDefinition<?, ?> d, String propertyName) { 078 return new Builder(d, propertyName); 079 } 080 081 082 083 /** Private constructor. */ 084 private IPAddressPropertyDefinition( 085 AbstractManagedObjectDefinition<?, ?> d, String propertyName, 086 EnumSet<PropertyOption> options, 087 AdministratorAction adminAction, 088 DefaultBehaviorProvider<InetAddress> defaultBehavior) { 089 super(d, InetAddress.class, propertyName, options, adminAction, 090 defaultBehavior); 091 } 092 093 094 095 /** {@inheritDoc} */ 096 @Override 097 public void validateValue(InetAddress value) 098 throws PropertyException { 099 ifNull(value); 100 101 // No additional validation required. 102 } 103 104 105 106 /** {@inheritDoc} */ 107 @Override 108 public InetAddress decodeValue(String value) 109 throws PropertyException { 110 ifNull(value); 111 112 try { 113 return InetAddress.getByName(value); 114 } catch (UnknownHostException e) { 115 // TODO: it would be nice to throw the cause. 116 throw PropertyException.illegalPropertyValueException(this, value); 117 } 118 } 119 120 121 122 /** {@inheritDoc} */ 123 @Override 124 public String encodeValue(InetAddress value) 125 throws PropertyException { 126 // We should return the host name if it is available, or the IP 127 // address if not. 128 129 // Unforunately, there is no InetAddress method for doing this, so 130 // we have to resort to hacking at the toString() encoding. 131 String s = value.toString(); 132 int i = s.indexOf('/'); 133 if (i > 0) { 134 // Host address is before the forward slash. 135 return s.substring(0, i); 136 } else { 137 return value.getHostAddress(); 138 } 139 } 140 141 142 143 /** {@inheritDoc} */ 144 @Override 145 public <R, P> R accept(PropertyDefinitionVisitor<R, P> v, P p) { 146 return v.visitIPAddress(this, p); 147 } 148 149 150 151 /** {@inheritDoc} */ 152 @Override 153 public <R, P> R accept(PropertyValueVisitor<R, P> v, InetAddress value, P p) { 154 return v.visitIPAddress(this, value, p); 155 } 156 157 158 159 /** {@inheritDoc} */ 160 @Override 161 public int compare(InetAddress o1, InetAddress o2) { 162 return o1.getHostAddress().compareTo(o2.getHostAddress()); 163 } 164}