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.util.EnumSet; 035 036import org.opends.server.types.DN; 037import org.opends.server.types.DirectoryException; 038 039 040 041/** 042 * DN property definition. 043 */ 044public final class DNPropertyDefinition extends PropertyDefinition<DN> { 045 046 /** 047 * Optional base DN which all valid values must be immediately 048 * subordinate to. 049 */ 050 private final DN baseDN; 051 052 053 054 /** 055 * An interface for incrementally constructing DN property 056 * definitions. 057 */ 058 public static class Builder extends 059 AbstractBuilder<DN, DNPropertyDefinition> { 060 061 /** 062 * Optional base DN which all valid values must be immediately 063 * subordinate to. 064 */ 065 private DN baseDN; 066 067 068 069 /** Private constructor. */ 070 private Builder( 071 AbstractManagedObjectDefinition<?, ?> d, String propertyName) { 072 super(d, propertyName); 073 } 074 075 076 077 /** 078 * Set the base DN which all valid values must be immediately 079 * subordinate to. By default there is no based DN. 080 * 081 * @param baseDN 082 * The string representation of the base DN. 083 * @throws IllegalArgumentException 084 * If the provided string is not a valid DN string 085 * representation. 086 */ 087 public void setBaseDN(String baseDN) 088 throws IllegalArgumentException { 089 if (baseDN == null) { 090 setBaseDN((DN) null); 091 } else { 092 try { 093 setBaseDN(DN.valueOf(baseDN)); 094 } catch (DirectoryException e) { 095 throw new IllegalArgumentException(e); 096 } 097 } 098 } 099 100 101 102 /** 103 * Set the base DN which all valid values must be immediately 104 * subordinate to. By default there is no based DN. 105 * 106 * @param baseDN 107 * The base DN. 108 */ 109 public void setBaseDN(DN baseDN) { 110 this.baseDN = baseDN; 111 } 112 113 114 115 /** {@inheritDoc} */ 116 @Override 117 protected DNPropertyDefinition buildInstance( 118 AbstractManagedObjectDefinition<?, ?> d, String propertyName, 119 EnumSet<PropertyOption> options, 120 AdministratorAction adminAction, 121 DefaultBehaviorProvider<DN> defaultBehavior) { 122 return new DNPropertyDefinition(d, propertyName, options, 123 adminAction, defaultBehavior, baseDN); 124 } 125 } 126 127 128 129 /** 130 * Create a DN property definition builder. 131 * 132 * @param d 133 * The managed object definition associated with this 134 * property definition. 135 * @param propertyName 136 * The property name. 137 * @return Returns the new boolean property definition builder. 138 */ 139 public static Builder createBuilder( 140 AbstractManagedObjectDefinition<?, ?> d, String propertyName) { 141 return new Builder(d, propertyName); 142 } 143 144 145 146 /** Private constructor. */ 147 private DNPropertyDefinition( 148 AbstractManagedObjectDefinition<?, ?> d, String propertyName, 149 EnumSet<PropertyOption> options, 150 AdministratorAction adminAction, 151 DefaultBehaviorProvider<DN> defaultBehavior, DN baseDN) { 152 super(d, DN.class, propertyName, options, adminAction, defaultBehavior); 153 this.baseDN = baseDN; 154 } 155 156 157 158 /** 159 * Get the base DN which all valid values must be immediately 160 * subordinate to, or <code>null</code> if there is no based DN. 161 * 162 * @return Returns the base DN which all valid values must be 163 * immediately subordinate to. 164 */ 165 public DN getBaseDN() { 166 return baseDN; 167 } 168 169 170 171 /** {@inheritDoc} */ 172 @Override 173 public void validateValue(DN value) 174 throws PropertyException { 175 ifNull(value); 176 177 if (baseDN != null) { 178 DN parent = value.parent(); 179 180 if (parent == null) { 181 parent = DN.rootDN(); 182 } 183 184 if (!parent.equals(baseDN)) { 185 throw PropertyException.illegalPropertyValueException(this, value); 186 } 187 } 188 } 189 190 191 192 /** {@inheritDoc} */ 193 @Override 194 public DN decodeValue(String value) 195 throws PropertyException { 196 ifNull(value); 197 198 try { 199 DN dn = DN.valueOf(value); 200 validateValue(dn); 201 return dn; 202 } catch (DirectoryException | PropertyException e) { 203 throw PropertyException.illegalPropertyValueException(this, value); 204 } 205 } 206 207 208 209 /** {@inheritDoc} */ 210 @Override 211 public <R, P> R accept(PropertyDefinitionVisitor<R, P> v, P p) { 212 return v.visitDN(this, p); 213 } 214 215 216 217 /** {@inheritDoc} */ 218 @Override 219 public <R, P> R accept(PropertyValueVisitor<R, P> v, DN value, P p) { 220 return v.visitDN(this, value, p); 221 } 222 223 224 225 /** {@inheritDoc} */ 226 @Override 227 public int compare(DN o1, DN o2) { 228 return o1.compareTo(o2); 229 } 230}