mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Gaetan Boismal
03.54.2016 e01f9041e7cb466dede5fa79bc2683c3296b5d0c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
/*
 * The contents of this file are subject to the terms of the Common Development and
 * Distribution License (the License). You may not use this file except in compliance with the
 * License.
 *
 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
 * specific language governing permission and limitations under the License.
 *
 * When distributing Covered Software, include this CDDL Header Notice in each file and include
 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
 * Header, with the fields enclosed by brackets [] replaced by your own identifying
 * information: "Portions Copyright [year] [name of copyright owner]".
 *
 * Copyright 2008 Sun Microsystems, Inc.
 * Portions Copyright 2015 ForgeRock AS.
 */
 
package org.forgerock.opendj.config;
 
import org.forgerock.util.Reject;
 
import java.util.Comparator;
import java.util.EnumSet;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.Set;
 
import org.forgerock.i18n.LocalizableMessage;
 
/**
 * An interface for querying generic property definition features.
 * <p>
 * Property definitions are analogous to ConfigAttributes in the current model
 * and will play a similar role. Eventually these will replace them.
 * <p>
 * Implementations <b>must</b> take care to implement the various comparison
 * methods.
 *
 * @param <T>
 *            The data-type of values of the property.
 */
public abstract class PropertyDefinition<T> implements Comparator<T>, Comparable<PropertyDefinition<?>> {
 
    /**
     * An interface for incrementally constructing property definitions.
     *
     * @param <T>
     *            The data-type of values of the property.
     * @param <D>
     *            The type of property definition constructed by this builder.
     */
    protected static abstract class AbstractBuilder<T, D extends PropertyDefinition<T>> {
 
        /** The administrator action. */
        private AdministratorAction adminAction;
 
        /** The default behavior provider. */
        private DefaultBehaviorProvider<T> defaultBehavior;
 
        /** The abstract managed object. */
        private final AbstractManagedObjectDefinition<?, ?> definition;
 
        /** The options applicable to this definition. */
        private final EnumSet<PropertyOption> options;
 
        /** The name of this property definition. */
        private final String propertyName;
 
        /**
         * Create a property definition builder.
         *
         * @param d
         *            The managed object definition associated with this
         *            property definition.
         * @param propertyName
         *            The property name.
         */
        protected AbstractBuilder(AbstractManagedObjectDefinition<?, ?> d, String propertyName) {
            this.definition = d;
            this.propertyName = propertyName;
            this.options = EnumSet.noneOf(PropertyOption.class);
            this.adminAction = new AdministratorAction(AdministratorAction.Type.NONE, d, propertyName);
            this.defaultBehavior = new UndefinedDefaultBehaviorProvider<>();
        }
 
        /**
         * Construct a property definition based on the properties of this
         * builder.
         *
         * @return The new property definition.
         */
        public final D getInstance() {
            return buildInstance(definition, propertyName, options, adminAction, defaultBehavior);
        }
 
        /**
         * Set the administrator action.
         *
         * @param adminAction
         *            The administrator action.
         */
        public final void setAdministratorAction(AdministratorAction adminAction) {
            Reject.ifNull(adminAction);
            this.adminAction = adminAction;
        }
 
        /**
         * Set the default behavior provider.
         *
         * @param defaultBehavior
         *            The default behavior provider.
         */
        public final void setDefaultBehaviorProvider(DefaultBehaviorProvider<T> defaultBehavior) {
            Reject.ifNull(defaultBehavior);
            this.defaultBehavior = defaultBehavior;
        }
 
        /**
         * Add a property definition option.
         *
         * @param option
         *            The property option.
         */
        public final void setOption(PropertyOption option) {
            Reject.ifNull(option);
            options.add(option);
        }
 
        /**
         * Build a property definition based on the properties of this builder.
         *
         * @param d
         *            The managed object definition associated with this
         *            property definition.
         * @param propertyName
         *            The property name.
         * @param options
         *            Options applicable to this definition.
         * @param adminAction
         *            The administrator action.
         * @param defaultBehavior
         *            The default behavior provider.
         * @return The new property definition.
         */
        protected abstract D buildInstance(AbstractManagedObjectDefinition<?, ?> d, String propertyName,
            EnumSet<PropertyOption> options, AdministratorAction adminAction,
            DefaultBehaviorProvider<T> defaultBehavior);
    }
 
    /** The administrator action. */
    private final AdministratorAction adminAction;
 
    /** The default behavior provider. */
    private final DefaultBehaviorProvider<T> defaultBehavior;
 
    /** The abstract managed object. */
    private final AbstractManagedObjectDefinition<?, ?> definition;
 
    /** Options applicable to this definition. */
    private final Set<PropertyOption> options;
 
    /** The property name. */
    private final String propertyName;
 
    /** The property value class. */
    private final Class<T> theClass;
 
    /**
     * Create a property definition.
     *
     * @param d
     *            The managed object definition associated with this property
     *            definition.
     * @param theClass
     *            The property value class.
     * @param propertyName
     *            The property name.
     * @param options
     *            Options applicable to this definition.
     * @param adminAction
     *            The administrator action.
     * @param defaultBehavior
     *            The default behavior provider.
     */
    protected PropertyDefinition(AbstractManagedObjectDefinition<?, ?> d, Class<T> theClass, String propertyName,
        EnumSet<PropertyOption> options, AdministratorAction adminAction, DefaultBehaviorProvider<T> defaultBehavior) {
        Reject.ifNull(d, theClass, propertyName, options, adminAction, defaultBehavior);
 
        this.definition = d;
        this.theClass = theClass;
        this.propertyName = propertyName;
        this.options = EnumSet.copyOf(options);
        this.adminAction = adminAction;
        this.defaultBehavior = defaultBehavior;
    }
 
    /**
     * Apply a visitor to this property definition.
     *
     * @param <R>
     *            The return type of the visitor's methods.
     * @param <P>
     *            The type of the additional parameters to the visitor's
     *            methods.
     * @param v
     *            The property definition visitor.
     * @param p
     *            Optional additional visitor parameter.
     * @return Returns a result as specified by the visitor.
     */
    public abstract <R, P> R accept(PropertyDefinitionVisitor<R, P> v, P p);
 
    /**
     * Apply a visitor to a property value associated with this property
     * definition.
     *
     * @param <R>
     *            The return type of the visitor's methods.
     * @param <P>
     *            The type of the additional parameters to the visitor's
     *            methods.
     * @param v
     *            The property value visitor.
     * @param value
     *            The property value.
     * @param p
     *            Optional additional visitor parameter.
     * @return Returns a result as specified by the visitor.
     */
    public abstract <R, P> R accept(PropertyValueVisitor<R, P> v, T value, P p);
 
    /**
     * Cast the provided value to the type associated with this property
     * definition.
     * <p>
     * This method only casts the object to the required type; it does not
     * validate the value once it has been cast. Subsequent validation should be
     * performed using the method {@link #validateValue(Object)}.
     * <p>
     * This method guarantees the following expression is always
     * <code>true</code>:
     *
     * <pre>
     *  PropertyDefinition d;
     *  x == d.cast(x);
     * </pre>
     *
     * @param object
     *            The property value to be cast (can be <code>null</code>).
     * @return Returns the property value cast to the correct type.
     * @throws ClassCastException
     *             If the provided property value did not have the correct type.
     */
    public final T castValue(Object object) {
        return theClass.cast(object);
    }
 
    /**
     * Compares two property values for order. Returns a negative integer, zero,
     * or a positive integer as the first argument is less than, equal to, or
     * greater than the second.
     * <p>
     * This default implementation normalizes both values using
     * {@link #normalizeValue(Object)} and then performs a case-sensitive string
     * comparison.
     *
     * @param o1
     *            the first object to be compared.
     * @param o2
     *            the second object to be compared.
     * @return a negative integer, zero, or a positive integer as the first
     *         argument is less than, equal to, or greater than the second.
     */
    public int compare(T o1, T o2) {
        Reject.ifNull(o1);
        Reject.ifNull(o2);
 
        String s1 = normalizeValue(o1);
        String s2 = normalizeValue(o2);
 
        return s1.compareTo(s2);
    }
 
    /**
     * Compares this property definition with the specified property definition
     * for order. Returns a negative integer, zero, or a positive integer if
     * this property definition is less than, equal to, or greater than the
     * specified property definition.
     * <p>
     * The ordering must be determined first from the property name and then
     * base on the underlying value type.
     *
     * @param o
     *            The reference property definition with which to compare.
     * @return Returns a negative integer, zero, or a positive integer if this
     *         property definition is less than, equal to, or greater than the
     *         specified property definition.
     */
    public final int compareTo(PropertyDefinition<?> o) {
        int rc = propertyName.compareTo(o.propertyName);
        if (rc == 0) {
            rc = theClass.getName().compareTo(o.theClass.getName());
        }
        return rc;
    }
 
    /**
     * Parse and validate a string representation of a property value.
     *
     * @param value
     *            The property string value (must not be <code>null</code>).
     * @return Returns the decoded property value.
     * @throws PropertyException
     *             If the property value string is invalid.
     */
    public abstract T decodeValue(String value);
 
    /**
     * Encode the provided property value into its string representation.
     * <p>
     * This default implementation simply returns invokes the
     * {@link Object#toString()} method on the provided value.
     *
     * @param value
     *            The property value (must not be <code>null</code>).
     * @return Returns the encoded property string value.
     * @throws PropertyException
     *             If the property value is invalid.
     */
    public String encodeValue(T value) {
        Reject.ifNull(value);
 
        return value.toString();
    }
 
    /**
     * Indicates whether some other object is &quot;equal to&quot; this property
     * definition. This method must obey the general contract of
     * <tt>Object.equals(Object)</tt>. Additionally, this method can return
     * <tt>true</tt> <i>only</i> if the specified Object is also a property
     * definition and it has the same name, as returned by {@link #getName()},
     * and also is deemed to be &quot;compatible&quot; with this property
     * definition. Compatibility means that the two property definitions share
     * the same underlying value type and provide similar comparator
     * implementations.
     *
     * @param o
     *            The reference object with which to compare.
     * @return Returns <code>true</code> only if the specified object is also a
     *         property definition and it has the same name and is compatible
     *         with this property definition.
     * @see java.lang.Object#equals(java.lang.Object)
     * @see java.lang.Object#hashCode()
     */
    @Override
    public final boolean equals(Object o) {
        if (this == o) {
            return true;
        } else if (o instanceof PropertyDefinition) {
            PropertyDefinition<?> other = (PropertyDefinition<?>) o;
            return propertyName.equals(other.propertyName)
                    && theClass.equals(other.theClass);
        } else {
            return false;
        }
    }
 
    /**
     * Get the administrator action associated with this property definition.
     * The administrator action describes any action which the administrator
     * must perform in order for changes to this property to take effect.
     *
     * @return Returns the administrator action associated with this property
     *         definition.
     */
    public final AdministratorAction getAdministratorAction() {
        return adminAction;
    }
 
    /**
     * Get the default behavior provider associated with this property
     * definition.
     *
     * @return Returns the default behavior provider associated with this
     *         property definition.
     */
    public final DefaultBehaviorProvider<T> getDefaultBehaviorProvider() {
        return defaultBehavior;
    }
 
    /**
     * Gets the optional description of this property definition in the default
     * locale.
     *
     * @return Returns the description of this property definition in the
     *         default locale, or <code>null</code> if there is no description.
     */
    public final LocalizableMessage getDescription() {
        return getDescription(Locale.getDefault());
    }
 
    /**
     * Gets the optional description of this property definition in the
     * specified locale.
     *
     * @param locale
     *            The locale.
     * @return Returns the description of this property definition in the
     *         specified locale, or <code>null</code> if there is no
     *         description.
     */
    public final LocalizableMessage getDescription(Locale locale) {
        ManagedObjectDefinitionI18NResource resource = ManagedObjectDefinitionI18NResource.getInstance();
        String property = "property." + propertyName + ".description";
        try {
            return resource.getMessage(definition, property, locale);
        } catch (MissingResourceException e) {
            return null;
        }
    }
 
    /**
     * Gets the managed object definition associated with this property
     * definition.
     *
     * @return Returns the managed object definition associated with this
     *         property definition.
     */
    public final AbstractManagedObjectDefinition<?, ?> getManagedObjectDefinition() {
        return definition;
    }
 
    /**
     * Get the name of the property.
     *
     * @return Returns the name of the property.
     */
    public final String getName() {
        return propertyName;
    }
 
    /**
     * Gets the synopsis of this property definition in the default locale.
     *
     * @return Returns the synopsis of this property definition in the default
     *         locale.
     */
    public final LocalizableMessage getSynopsis() {
        return getSynopsis(Locale.getDefault());
    }
 
    /**
     * Gets the synopsis of this property definition in the specified locale.
     *
     * @param locale
     *            The locale.
     * @return Returns the synopsis of this property definition in the specified
     *         locale.
     */
    public final LocalizableMessage getSynopsis(Locale locale) {
        ManagedObjectDefinitionI18NResource resource = ManagedObjectDefinitionI18NResource.getInstance();
        String property = "property." + propertyName + ".synopsis";
        return resource.getMessage(definition, property, locale);
    }
 
    /**
     * Returns a hash code value for this property definition. The hash code
     * should be derived from the property name and the type of values handled
     * by this property definition.
     *
     * @return Returns the hash code value for this property definition.
     */
    @Override
    public final int hashCode() {
        int rc = 17 + propertyName.hashCode();
        return 37 * rc + theClass.hashCode();
    }
 
    /**
     * Check if the specified option is set for this property definition.
     *
     * @param option
     *            The option to test.
     * @return Returns <code>true</code> if the option is set, or
     *         <code>false</code> otherwise.
     */
    public final boolean hasOption(PropertyOption option) {
        return options.contains(option);
    }
 
    /**
     * Get a normalized string representation of a property value. This can then
     * be used for comparisons and for generating hash-codes.
     * <p>
     * This method may throw an exception if the provided value is invalid.
     * However, applications should not assume that implementations of this
     * method will always validate a value. This task is the responsibility of
     * {@link #validateValue(Object)}.
     * <p>
     * This default implementation simply returns the string representation of
     * the provided value. Sub-classes might want to override this method if
     * this behavior is insufficient (for example, a string property definition
     * might strip white-space and convert characters to lower-case).
     *
     * @param value
     *            The property value to be normalized.
     * @return Returns the normalized property value.
     * @throws PropertyException
     *             If the property value is invalid.
     */
    public String normalizeValue(T value) {
        Reject.ifNull(value);
 
        return encodeValue(value);
    }
 
    /**
     * Returns a string representation of this property definition.
     *
     * @return Returns a string representation of this property definition.
     * @see Object#toString()
     */
    @Override
    public final String toString() {
        StringBuilder builder = new StringBuilder();
        toString(builder);
        return builder.toString();
    }
 
    /**
     * Append a string representation of the property definition to the provided
     * string builder.
     * <p>
     * This simple implementation just outputs the propertyName of the property
     * definition. Sub-classes should override this method to provide more
     * complete string representations.
     *
     * @param builder
     *            The string builder where the string representation should be
     *            appended.
     */
    public void toString(StringBuilder builder) {
        builder.append(propertyName);
    }
 
    /**
     * Determine if the provided property value is valid according to this
     * property definition.
     *
     * @param value
     *            The property value (must not be <code>null</code>).
     * @throws PropertyException
     *             If the property value is invalid.
     */
    public abstract void validateValue(T value);
 
    /**
     * Performs any run-time initialization required by this property
     * definition. This may include resolving managed object paths and property
     * names.
     *
     * @throws Exception
     *             If this property definition could not be initialized.
     */
    protected void initialize() throws Exception {
        // No implementation required.
    }
}