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