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

Jean-Noël Rouvignac
19.36.2016 93dc3520b26d74dadbdad265182d9beaa9145dc4
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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
/*
 * 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-2009 Sun Microsystems, Inc.
 * Portions Copyright 2014-2016 ForgeRock AS.
 */
package org.forgerock.opendj.config.client.spi;
 
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
 
import org.forgerock.i18n.LocalizableMessage;
import org.forgerock.opendj.config.AbstractManagedObjectDefinition;
import org.forgerock.opendj.config.Configuration;
import org.forgerock.opendj.config.ConfigurationClient;
import org.forgerock.opendj.config.Constraint;
import org.forgerock.opendj.config.PropertyException;
import org.forgerock.opendj.config.DefaultManagedObject;
import org.forgerock.opendj.config.DefinitionDecodingException;
import org.forgerock.opendj.config.InstantiableRelationDefinition;
import org.forgerock.opendj.config.ManagedObjectAlreadyExistsException;
import org.forgerock.opendj.config.ManagedObjectDefinition;
import org.forgerock.opendj.config.ManagedObjectNotFoundException;
import org.forgerock.opendj.config.ManagedObjectPath;
import org.forgerock.opendj.config.OptionalRelationDefinition;
import org.forgerock.opendj.config.PropertyDefinition;
import org.forgerock.opendj.config.PropertyOption;
import org.forgerock.opendj.config.RelationDefinition;
import org.forgerock.opendj.config.RelationDefinitionVisitor;
import org.forgerock.opendj.config.SetRelationDefinition;
import org.forgerock.opendj.config.SingletonRelationDefinition;
import org.forgerock.opendj.config.DefinitionDecodingException.Reason;
import org.forgerock.opendj.config.client.ClientConstraintHandler;
import org.forgerock.opendj.config.client.ConcurrentModificationException;
import org.forgerock.opendj.config.client.IllegalManagedObjectNameException;
import org.forgerock.opendj.config.client.ManagedObject;
import org.forgerock.opendj.config.client.ManagedObjectDecodingException;
import org.forgerock.opendj.config.client.ManagementContext;
import org.forgerock.opendj.config.client.MissingMandatoryPropertiesException;
import org.forgerock.opendj.config.client.OperationRejectedException;
import org.forgerock.opendj.config.client.OperationRejectedException.OperationType;
import org.forgerock.opendj.ldap.LdapException;
 
/**
 * An abstract managed object implementation.
 *
 * @param <T>
 *            The type of client configuration represented by the client managed
 *            object.
 */
public abstract class AbstractManagedObject<T extends ConfigurationClient> implements ManagedObject<T> {
 
    /** Creates any default managed objects associated with a relation definition. */
    private final class DefaultManagedObjectFactory implements RelationDefinitionVisitor<Void, Void> {
 
        /** Possible exceptions. */
        private ManagedObjectAlreadyExistsException moaee;
        private MissingMandatoryPropertiesException mmpe;
        private ConcurrentModificationException cme;
        private OperationRejectedException ore;
        private LdapException ere;
 
        @Override
        public <C extends ConfigurationClient, S extends Configuration> Void visitInstantiable(
            InstantiableRelationDefinition<C, S> rd, Void p) {
            for (String name : rd.getDefaultManagedObjectNames()) {
                DefaultManagedObject<? extends C, ? extends S> dmo = rd.getDefaultManagedObject(name);
                ManagedObjectDefinition<? extends C, ? extends S> d = dmo.getManagedObjectDefinition();
                ManagedObject<? extends C> child;
                try {
                    child = createChild(rd, d, name, null);
                } catch (IllegalManagedObjectNameException e) {
                    // This should not happen.
                    throw new RuntimeException(e);
                }
                createDefaultManagedObject(d, child, dmo);
            }
            return null;
        }
 
        @Override
        public <C extends ConfigurationClient, S extends Configuration> Void visitOptional(
            OptionalRelationDefinition<C, S> rd, Void p) {
            if (rd.getDefaultManagedObject() != null) {
                DefaultManagedObject<? extends C, ? extends S> dmo = rd.getDefaultManagedObject();
                ManagedObjectDefinition<? extends C, ? extends S> d = dmo.getManagedObjectDefinition();
                ManagedObject<? extends C> child = createChild(rd, d, null);
                createDefaultManagedObject(d, child, dmo);
            }
            return null;
        }
 
        @Override
        public <C extends ConfigurationClient, S extends Configuration> Void visitSingleton(
            SingletonRelationDefinition<C, S> rd, Void p) {
            // Do nothing - not possible to create singletons
            // dynamically.
            return null;
        }
 
        @Override
        public <C extends ConfigurationClient, S extends Configuration> Void visitSet(SetRelationDefinition<C, S> rd,
            Void p) {
            for (String name : rd.getDefaultManagedObjectNames()) {
                DefaultManagedObject<? extends C, ? extends S> dmo = rd.getDefaultManagedObject(name);
                ManagedObjectDefinition<? extends C, ? extends S> d = dmo.getManagedObjectDefinition();
                ManagedObject<? extends C> child = createChild(rd, d, null);
                createDefaultManagedObject(d, child, dmo);
            }
            return null;
        }
 
        /** Create the child managed object. */
        private void createDefaultManagedObject(ManagedObjectDefinition<?, ?> d, ManagedObject<?> child,
            DefaultManagedObject<?, ?> dmo) {
            for (PropertyDefinition<?> pd : d.getAllPropertyDefinitions()) {
                setPropertyValues(child, pd, dmo);
            }
 
            try {
                child.commit();
            } catch (ManagedObjectAlreadyExistsException e) {
                moaee = e;
            } catch (MissingMandatoryPropertiesException e) {
                mmpe = e;
            } catch (ConcurrentModificationException e) {
                cme = e;
            } catch (OperationRejectedException e) {
                ore = e;
            } catch (LdapException e) {
                ere = e;
            }
        }
 
        /**
         * Creates the default managed objects associated with the provided
         * relation definition.
         *
         * @param rd
         *            The relation definition.
         */
        private void createDefaultManagedObjects(RelationDefinition<?, ?> rd) throws LdapException,
            ConcurrentModificationException, MissingMandatoryPropertiesException,
            ManagedObjectAlreadyExistsException, OperationRejectedException {
            rd.accept(this, null);
 
            if (ere != null) {
                throw ere;
            } else if (cme != null) {
                throw cme;
            } else if (mmpe != null) {
                throw mmpe;
            } else if (moaee != null) {
                throw moaee;
            } else if (ore != null) {
                throw ore;
            }
        }
 
        /** Set property values. */
        private <P> void setPropertyValues(ManagedObject<?> mo, PropertyDefinition<P> pd,
            DefaultManagedObject<?, ?> dmo) {
            mo.setPropertyValues(pd, dmo.getPropertyValues(pd));
        }
    }
 
    /** The managed object definition associated with this managed object. */
    private final ManagedObjectDefinition<T, ? extends Configuration> definition;
 
    /**
     * Indicates whether or not this managed object exists on the server
     * (false means the managed object is new and has not been
     * committed).
     */
    private boolean existsOnServer;
 
    /** Optional naming property definition. */
    private final PropertyDefinition<?> namingPropertyDefinition;
 
    /** The path associated with this managed object. */
    private ManagedObjectPath<T, ? extends Configuration> path;
 
    /** The managed object's properties. */
    private final PropertySet properties;
 
    /**
     * Creates a new abstract managed object.
     *
     * @param d
     *            The managed object's definition.
     * @param path
     *            The managed object's path.
     * @param properties
     *            The managed object's properties.
     * @param existsOnServer
     *            Indicates whether or not the managed object exists on the
     *            server (false means the managed object is new and has not been
     *            committed).
     * @param namingPropertyDefinition
     *            Optional naming property definition.
     */
    protected AbstractManagedObject(ManagedObjectDefinition<T, ? extends Configuration> d,
        ManagedObjectPath<T, ? extends Configuration> path, PropertySet properties, boolean existsOnServer,
        PropertyDefinition<?> namingPropertyDefinition) {
        this.definition = d;
        this.path = path;
        this.properties = properties;
        this.existsOnServer = existsOnServer;
        this.namingPropertyDefinition = namingPropertyDefinition;
    }
 
    @Override
    public final void commit() throws ManagedObjectAlreadyExistsException, MissingMandatoryPropertiesException,
        ConcurrentModificationException, OperationRejectedException, LdapException {
        // First make sure all mandatory properties are defined.
        List<PropertyException> exceptions = new LinkedList<>();
 
        for (PropertyDefinition<?> pd : definition.getAllPropertyDefinitions()) {
            Property<?> p = getProperty(pd);
            if (pd.hasOption(PropertyOption.MANDATORY) && p.getEffectiveValues().isEmpty()) {
                exceptions.add(PropertyException.propertyIsMandatoryException(pd));
            }
        }
 
        if (!exceptions.isEmpty()) {
            throw new MissingMandatoryPropertiesException(definition.getUserFriendlyName(), exceptions,
                !existsOnServer);
        }
 
        // Now enforce any constraints.
        List<LocalizableMessage> messages = new LinkedList<>();
        boolean isAcceptable = true;
        ManagementContext context = getDriver().getManagementContext();
 
        for (Constraint constraint : definition.getAllConstraints()) {
            for (ClientConstraintHandler handler : constraint.getClientConstraintHandlers()) {
                if (existsOnServer) {
                    if (!handler.isModifyAcceptable(context, this, messages)) {
                        isAcceptable = false;
                    }
                } else {
                    if (!handler.isAddAcceptable(context, this, messages)) {
                        isAcceptable = false;
                    }
                }
            }
            if (!isAcceptable) {
                break;
            }
        }
 
        if (!isAcceptable) {
            if (existsOnServer) {
                throw new OperationRejectedException(OperationType.MODIFY, definition.getUserFriendlyName(), messages);
            } else {
                throw new OperationRejectedException(OperationType.CREATE, definition.getUserFriendlyName(), messages);
            }
        }
 
        // Commit the managed object.
        if (existsOnServer) {
            modifyExistingManagedObject();
        } else {
            addNewManagedObject();
        }
 
        // Make all pending property values active.
        properties.commit();
 
        // If the managed object was created make sure that any default
        // subordinate managed objects are also created.
        if (!existsOnServer) {
            DefaultManagedObjectFactory factory = new DefaultManagedObjectFactory();
            for (RelationDefinition<?, ?> rd : definition.getAllRelationDefinitions()) {
                factory.createDefaultManagedObjects(rd);
            }
 
            existsOnServer = true;
        }
    }
 
    @Override
    public final <C extends ConfigurationClient, S extends Configuration, C1 extends C> ManagedObject<C1> createChild(
        InstantiableRelationDefinition<C, S> r, ManagedObjectDefinition<C1, ? extends S> d, String name,
        Collection<PropertyException> exceptions) throws IllegalManagedObjectNameException {
        validateRelationDefinition(r);
 
        // Empty names are not allowed.
        if (name.trim().length() == 0) {
            throw new IllegalManagedObjectNameException(name);
        }
 
        // If the relation uses a naming property definition then it must
        // be a valid value.
        PropertyDefinition<?> pd = r.getNamingPropertyDefinition();
        if (pd != null) {
            try {
                pd.decodeValue(name);
            } catch (PropertyException e) {
                throw new IllegalManagedObjectNameException(name, pd);
            }
        }
 
        ManagedObjectPath<C1, ? extends S> childPath = path.child(r, d, name);
        return createNewManagedObject(d, childPath, pd, name, exceptions);
    }
 
    @Override
    public final <C extends ConfigurationClient, S extends Configuration, C1 extends C> ManagedObject<C1> createChild(
        OptionalRelationDefinition<C, S> r, ManagedObjectDefinition<C1, ? extends S> d,
        Collection<PropertyException> exceptions) {
        validateRelationDefinition(r);
        ManagedObjectPath<C1, ? extends S> childPath = path.child(r, d);
        return createNewManagedObject(d, childPath, null, null, exceptions);
    }
 
    @Override
    public final <C extends ConfigurationClient, S extends Configuration, C1 extends C> ManagedObject<C1> createChild(
        SetRelationDefinition<C, S> r, ManagedObjectDefinition<C1, ? extends S> d,
        Collection<PropertyException> exceptions) {
        validateRelationDefinition(r);
 
        ManagedObjectPath<C1, ? extends S> childPath = path.child(r, d);
        return createNewManagedObject(d, childPath, null, null, exceptions);
    }
 
    @Override
    public final <C extends ConfigurationClient, S extends Configuration> ManagedObject<? extends C> getChild(
        InstantiableRelationDefinition<C, S> r, String name) throws DefinitionDecodingException,
        ManagedObjectDecodingException, ManagedObjectNotFoundException, ConcurrentModificationException,
        LdapException {
        validateRelationDefinition(r);
        ensureThisManagedObjectExists();
        Driver ctx = getDriver();
        return ctx.getManagedObject(path.child(r, name));
    }
 
    @Override
    public final <C extends ConfigurationClient, S extends Configuration> ManagedObject<? extends C> getChild(
        OptionalRelationDefinition<C, S> r) throws DefinitionDecodingException, ManagedObjectDecodingException,
        ManagedObjectNotFoundException, ConcurrentModificationException, LdapException {
        validateRelationDefinition(r);
        ensureThisManagedObjectExists();
        Driver ctx = getDriver();
        return ctx.getManagedObject(path.child(r));
    }
 
    @Override
    public final <C extends ConfigurationClient, S extends Configuration> ManagedObject<? extends C> getChild(
        SingletonRelationDefinition<C, S> r) throws DefinitionDecodingException, ManagedObjectDecodingException,
        ManagedObjectNotFoundException, ConcurrentModificationException, LdapException {
        validateRelationDefinition(r);
        ensureThisManagedObjectExists();
        Driver ctx = getDriver();
        return ctx.getManagedObject(path.child(r));
    }
 
    @Override
    public final <C extends ConfigurationClient, S extends Configuration> ManagedObject<? extends C> getChild(
        SetRelationDefinition<C, S> r, String name) throws DefinitionDecodingException,
        ManagedObjectDecodingException, ManagedObjectNotFoundException, ConcurrentModificationException,
        LdapException {
        validateRelationDefinition(r);
        ensureThisManagedObjectExists();
        Driver ctx = getDriver();
 
        AbstractManagedObjectDefinition<C, S> d = r.getChildDefinition();
        AbstractManagedObjectDefinition<? extends C, ? extends S> cd;
 
        try {
            cd = d.getChild(name);
        } catch (IllegalArgumentException e) {
            // Unrecognized definition name - report this as a decoding
            // exception.
            throw new DefinitionDecodingException(d, Reason.WRONG_TYPE_INFORMATION);
        }
 
        return ctx.getManagedObject(path.child(r, cd));
    }
 
    @Override
    public final T getConfiguration() {
        return definition.createClientConfiguration(this);
    }
 
    @Override
    public final ManagedObjectDefinition<T, ? extends Configuration> getManagedObjectDefinition() {
        return definition;
    }
 
    @Override
    public final ManagedObjectPath<T, ? extends Configuration> getManagedObjectPath() {
        return path;
    }
 
    @Override
    public final <P> SortedSet<P> getPropertyDefaultValues(PropertyDefinition<P> pd) {
        return new TreeSet<>(getProperty(pd).getDefaultValues());
    }
 
    @Override
    public final <P> P getPropertyValue(PropertyDefinition<P> pd) {
        Set<P> values = getProperty(pd).getEffectiveValues();
        if (!values.isEmpty()) {
            return values.iterator().next();
        }
        return null;
    }
 
    @Override
    public final <P> SortedSet<P> getPropertyValues(PropertyDefinition<P> pd) {
        return new TreeSet<>(getProperty(pd).getEffectiveValues());
    }
 
    @Override
    public final <C extends ConfigurationClient, S extends Configuration> boolean hasChild(
        OptionalRelationDefinition<C, S> r) throws ConcurrentModificationException, LdapException {
        validateRelationDefinition(r);
        Driver ctx = getDriver();
        try {
            return ctx.managedObjectExists(path.child(r));
        } catch (ManagedObjectNotFoundException e) {
            throw new ConcurrentModificationException();
        }
    }
 
    @Override
    public final boolean isPropertyPresent(PropertyDefinition<?> pd) {
        return !getProperty(pd).isEmpty();
    }
 
    @Override
    public final <C extends ConfigurationClient, S extends Configuration> String[] listChildren(
        InstantiableRelationDefinition<C, S> r) throws ConcurrentModificationException, LdapException {
        return listChildren(r, r.getChildDefinition());
    }
 
    @Override
    public final <C extends ConfigurationClient, S extends Configuration> String[] listChildren(
        InstantiableRelationDefinition<C, S> r, AbstractManagedObjectDefinition<? extends C, ? extends S> d)
            throws ConcurrentModificationException, LdapException {
        validateRelationDefinition(r);
        Driver ctx = getDriver();
        try {
            return ctx.listManagedObjects(path, r, d);
        } catch (ManagedObjectNotFoundException e) {
            throw new ConcurrentModificationException();
        }
    }
 
    @Override
    public final <C extends ConfigurationClient, S extends Configuration> String[] listChildren(
        SetRelationDefinition<C, S> r) throws ConcurrentModificationException, LdapException {
        return listChildren(r, r.getChildDefinition());
    }
 
    @Override
    public final <C extends ConfigurationClient, S extends Configuration> String[] listChildren(
        SetRelationDefinition<C, S> r, AbstractManagedObjectDefinition<? extends C, ? extends S> d)
            throws ConcurrentModificationException, LdapException {
        validateRelationDefinition(r);
        Driver ctx = getDriver();
        try {
            return ctx.listManagedObjects(path, r, d);
        } catch (ManagedObjectNotFoundException e) {
            throw new ConcurrentModificationException();
        }
    }
 
    @Override
    public final <C extends ConfigurationClient, S extends Configuration> void removeChild(
        InstantiableRelationDefinition<C, S> r, String name) throws ManagedObjectNotFoundException,
        OperationRejectedException, ConcurrentModificationException, LdapException {
        validateRelationDefinition(r);
        Driver ctx = getDriver();
        boolean found;
 
        try {
            found = ctx.deleteManagedObject(path, r, name);
        } catch (ManagedObjectNotFoundException e) {
            throw new ConcurrentModificationException();
        }
 
        if (!found) {
            throw new ManagedObjectNotFoundException();
        }
    }
 
    @Override
    public final <C extends ConfigurationClient, S extends Configuration> void removeChild(
        OptionalRelationDefinition<C, S> r) throws ManagedObjectNotFoundException, OperationRejectedException,
        ConcurrentModificationException, LdapException {
        validateRelationDefinition(r);
        Driver ctx = getDriver();
        boolean found;
 
        try {
            found = ctx.deleteManagedObject(path, r);
        } catch (ManagedObjectNotFoundException e) {
            throw new ConcurrentModificationException();
        }
 
        if (!found) {
            throw new ManagedObjectNotFoundException();
        }
    }
 
    @Override
    public final <C extends ConfigurationClient, S extends Configuration> void removeChild(
        SetRelationDefinition<C, S> r, String name) throws ManagedObjectNotFoundException,
        OperationRejectedException, ConcurrentModificationException, LdapException {
        validateRelationDefinition(r);
        Driver ctx = getDriver();
        boolean found;
 
        try {
            found = ctx.deleteManagedObject(path, r, name);
        } catch (ManagedObjectNotFoundException e) {
            throw new ConcurrentModificationException();
        }
 
        if (!found) {
            throw new ManagedObjectNotFoundException();
        }
    }
 
    @Override
    public final <P> void setPropertyValue(PropertyDefinition<P> pd, P value) {
        if (value != null) {
            setPropertyValues(pd, Collections.singleton(value));
        } else {
            setPropertyValues(pd, Collections.<P> emptySet());
        }
    }
 
    @Override
    public final <P> void setPropertyValues(PropertyDefinition<P> pd, Collection<P> values) {
        if (pd.hasOption(PropertyOption.MONITORING)) {
            throw PropertyException.propertyIsReadOnlyException(pd);
        }
 
        if (existsOnServer && pd.hasOption(PropertyOption.READ_ONLY)) {
            throw PropertyException.propertyIsReadOnlyException(pd);
        }
 
        properties.setPropertyValues(pd, values);
 
        // If this is a naming property then update the name.
        if (pd.equals(namingPropertyDefinition)) {
            // The property must be single-valued and mandatory.
            String newName = pd.encodeValue(values.iterator().next());
            path = path.rename(newName);
        }
    }
 
    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
 
        builder.append("{ TYPE=");
        builder.append(definition.getName());
        builder.append(", PATH=\"");
        builder.append(path);
        builder.append('\"');
        for (PropertyDefinition<?> pd : definition.getAllPropertyDefinitions()) {
            builder.append(", ");
            builder.append(pd.getName());
            builder.append('=');
            builder.append(getPropertyValues(pd));
        }
        builder.append(" }");
 
        return builder.toString();
    }
 
    /**
     * Adds this new managed object.
     *
     * @throws ManagedObjectAlreadyExistsException
     *             If the managed object cannot be added to the server because
     *             it already exists.
     * @throws ConcurrentModificationException
     *             If the managed object's parent has been removed by another
     *             client.
     * @throws OperationRejectedException
     *             If the managed object cannot be added due to some client-side
     *             or server-side constraint which cannot be satisfied.
     * @throws LdapException
     *             If any other error occurs.
     */
    protected abstract void addNewManagedObject() throws LdapException, OperationRejectedException,
        ConcurrentModificationException, ManagedObjectAlreadyExistsException;
 
    /**
     * Gets the management context driver associated with this managed object.
     *
     * @return Returns the management context driver associated with this
     *         managed object.
     */
    protected abstract Driver getDriver();
 
    /**
     * Gets the naming property definition associated with this managed object.
     *
     * @return Returns the naming property definition associated with this
     *         managed object, or <code>null</code> if this managed object does
     *         not have a naming property.
     */
    protected final PropertyDefinition<?> getNamingPropertyDefinition() {
        return namingPropertyDefinition;
    }
 
    /**
     * Gets the property associated with the specified property definition.
     *
     * @param <P>
     *            The underlying type of the property.
     * @param pd
     *            The Property definition.
     * @return Returns the property associated with the specified property
     *         definition.
     * @throws IllegalArgumentException
     *             If this property provider does not recognize the requested
     *             property definition.
     */
    protected final <P> Property<P> getProperty(PropertyDefinition<P> pd) {
        return properties.getProperty(pd);
    }
 
    /**
     * Applies changes made to this managed object.
     *
     * @throws ConcurrentModificationException
     *             If this managed object has been removed from the server by
     *             another client.
     * @throws OperationRejectedException
     *             If the managed object cannot be added due to some client-side
     *             or server-side constraint which cannot be satisfied.
     * @throws LdapException
     *             If any other error occurs.
     */
    protected abstract void modifyExistingManagedObject() throws ConcurrentModificationException,
        OperationRejectedException, LdapException;
 
    /**
     * Creates a new managed object.
     *
     * @param <M>
     *            The type of client configuration represented by the client
     *            managed object.
     * @param d
     *            The managed object's definition.
     * @param path
     *            The managed object's path.
     * @param properties
     *            The managed object's properties.
     * @param existsOnServer
     *            Indicates whether or not the managed object exists on the
     *            server (false means the managed object is new and has not been
     *            committed).
     * @param namingPropertyDefinition
     *            Optional naming property definition.
     * @return Returns the new managed object.
     */
    protected abstract <M extends ConfigurationClient> ManagedObject<M> newInstance(ManagedObjectDefinition<M, ?> d,
        ManagedObjectPath<M, ?> path, PropertySet properties, boolean existsOnServer,
        PropertyDefinition<?> namingPropertyDefinition);
 
    /** Creates a new managed object with no active values, just default values. */
    private <M extends ConfigurationClient, P> ManagedObject<M> createNewManagedObject(
        ManagedObjectDefinition<M, ?> d, ManagedObjectPath<M, ?> p, PropertyDefinition<P> namingPropertyDefinition,
        String name, Collection<PropertyException> exceptions) {
        PropertySet childProperties = new PropertySet();
        for (PropertyDefinition<?> pd : d.getAllPropertyDefinitions()) {
            try {
                createProperty(childProperties, p, pd);
            } catch (PropertyException e) {
                // Add the exception if requested.
                if (exceptions != null) {
                    exceptions.add(e);
                }
            }
        }
 
        // Set the naming property if there is one.
        if (namingPropertyDefinition != null) {
            P value = namingPropertyDefinition.decodeValue(name);
            childProperties.setPropertyValues(namingPropertyDefinition, Collections.singleton(value));
        }
 
        return newInstance(d, p, childProperties, false, namingPropertyDefinition);
    }
 
    /** Create an empty property. */
    private <P> void createProperty(PropertySet properties, ManagedObjectPath<?, ?> p, PropertyDefinition<P> pd) {
        try {
            Driver context = getDriver();
            Collection<P> defaultValues = context.findDefaultValues(p, pd, true);
            properties.addProperty(pd, defaultValues, Collections.<P> emptySet());
        } catch (PropertyException e) {
            // Make sure that we have still created the property.
            properties.addProperty(pd, Collections.<P> emptySet(), Collections.<P> emptySet());
            throw e;
        }
    }
 
    /** Makes sure that this managed object exists. */
    private void ensureThisManagedObjectExists() throws ConcurrentModificationException, LdapException {
        if (!path.isEmpty()) {
            Driver ctx = getDriver();
 
            try {
                if (!ctx.managedObjectExists(path)) {
                    throw new ConcurrentModificationException();
                }
            } catch (ManagedObjectNotFoundException e) {
                throw new ConcurrentModificationException();
            }
        }
    }
 
    /** Validate that a relation definition belongs to this managed object. */
    private void validateRelationDefinition(RelationDefinition<?, ?> rd) {
        ManagedObjectDefinition<T, ?> d = getManagedObjectDefinition();
        RelationDefinition<?, ?> tmp = d.getRelationDefinition(rd.getName());
        if (tmp != rd) {
            throw new IllegalArgumentException("The relation " + rd.getName() + " is not associated with a "
                + d.getName());
        }
    }
 
}