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 010 * trunk/opends/resource/legal-notices/OpenDS.LICENSE 011 * or https://OpenDS.dev.java.net/OpenDS.LICENSE. 012 * See the License for the specific language governing permissions 013 * and limitations under the License. 014 * 015 * When distributing Covered Code, include this CDDL HEADER in each 016 * file and include the License file at 017 * trunk/opends/resource/legal-notices/OpenDS.LICENSE. If applicable, 018 * add the following below this CDDL HEADER, with the fields enclosed 019 * by brackets "[]" replaced with your own identifying information: 020 * Portions Copyright [yyyy] [name of copyright owner] 021 * 022 * CDDL HEADER END 023 * 024 * 025 * Copyright 2008 Sun Microsystems, Inc. 026 */ 027 028 package org.opends.server.admin; 029 030 031 032 import static org.opends.server.util.Validator.ensureNotNull; 033 034 import java.net.InetAddress; 035 import java.net.UnknownHostException; 036 import java.util.EnumSet; 037 038 039 040 /** 041 * IP address property definition. 042 */ 043 public 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 /** 062 * {@inheritDoc} 063 */ 064 @Override 065 protected IPAddressPropertyDefinition buildInstance( 066 AbstractManagedObjectDefinition<?, ?> d, String propertyName, 067 EnumSet<PropertyOption> options, 068 AdministratorAction adminAction, 069 DefaultBehaviorProvider<InetAddress> defaultBehavior) { 070 return new IPAddressPropertyDefinition(d, propertyName, options, 071 adminAction, defaultBehavior); 072 } 073 074 } 075 076 077 078 /** 079 * Create a IP address property definition builder. 080 * 081 * @param d 082 * The managed object definition associated with this 083 * property definition. 084 * @param propertyName 085 * The property name. 086 * @return Returns the new IP address property definition builder. 087 */ 088 public static Builder createBuilder( 089 AbstractManagedObjectDefinition<?, ?> d, String propertyName) { 090 return new Builder(d, propertyName); 091 } 092 093 094 095 // Private constructor. 096 private IPAddressPropertyDefinition( 097 AbstractManagedObjectDefinition<?, ?> d, String propertyName, 098 EnumSet<PropertyOption> options, 099 AdministratorAction adminAction, 100 DefaultBehaviorProvider<InetAddress> defaultBehavior) { 101 super(d, InetAddress.class, propertyName, options, adminAction, 102 defaultBehavior); 103 } 104 105 106 107 /** 108 * {@inheritDoc} 109 */ 110 @Override 111 public void validateValue(InetAddress value) 112 throws IllegalPropertyValueException { 113 ensureNotNull(value); 114 115 // No additional validation required. 116 } 117 118 119 120 /** 121 * {@inheritDoc} 122 */ 123 @Override 124 public InetAddress decodeValue(String value) 125 throws IllegalPropertyValueStringException { 126 ensureNotNull(value); 127 128 try { 129 return InetAddress.getByName(value); 130 } catch (UnknownHostException e) { 131 // TODO: it would be nice to throw the cause. 132 throw new IllegalPropertyValueStringException(this, value); 133 } 134 } 135 136 137 138 /** 139 * {@inheritDoc} 140 */ 141 @Override 142 public String encodeValue(InetAddress value) 143 throws IllegalPropertyValueException { 144 // We should return the host name if it is available, or the IP 145 // address if not. 146 147 // Unforunately, there is no InetAddress method for doing this, so 148 // we have to resort to hacking at the toString() encoding. 149 String s = value.toString(); 150 int i = s.indexOf('/'); 151 if (i > 0) { 152 // Host address is before the forward slash. 153 return s.substring(0, i); 154 } else { 155 return value.getHostAddress(); 156 } 157 } 158 159 160 161 /** 162 * {@inheritDoc} 163 */ 164 @Override 165 public <R, P> R accept(PropertyDefinitionVisitor<R, P> v, P p) { 166 return v.visitIPAddress(this, p); 167 } 168 169 170 171 /** 172 * {@inheritDoc} 173 */ 174 @Override 175 public <R, P> R accept(PropertyValueVisitor<R, P> v, InetAddress value, P p) { 176 return v.visitIPAddress(this, value, p); 177 } 178 179 180 181 /** 182 * {@inheritDoc} 183 */ 184 @Override 185 public int compare(InetAddress o1, InetAddress o2) { 186 return o1.getHostAddress().compareTo(o2.getHostAddress()); 187 } 188 }