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