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

maximthomas
28.38.2022 4a7bed046945673c3d30356a09fd309111238c32
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
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
/*
 * 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 2007-2008 Sun Microsystems, Inc.
 * Portions Copyright 2011-2016 ForgeRock AS.
 */
package org.forgerock.opendj.config.dsconfig;
 
import static com.forgerock.opendj.cli.CliMessages.*;
import static com.forgerock.opendj.cli.Utils.*;
import static com.forgerock.opendj.dsconfig.DsconfigMessages.*;
 
import java.io.PrintStream;
import java.util.Collection;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
 
import org.forgerock.i18n.LocalizableMessage;
import org.forgerock.i18n.LocalizableMessageBuilder;
import org.forgerock.opendj.config.AbsoluteInheritedDefaultBehaviorProvider;
import org.forgerock.opendj.config.AbstractManagedObjectDefinition;
import org.forgerock.opendj.config.AdministratorAction;
import org.forgerock.opendj.config.AggregationPropertyDefinition;
import org.forgerock.opendj.config.AliasDefaultBehaviorProvider;
import org.forgerock.opendj.config.DefaultBehaviorProviderVisitor;
import org.forgerock.opendj.config.DefinedDefaultBehaviorProvider;
import org.forgerock.opendj.config.EnumPropertyDefinition;
import org.forgerock.opendj.config.PropertyDefinition;
import org.forgerock.opendj.config.PropertyDefinitionUsageBuilder;
import org.forgerock.opendj.config.PropertyDefinitionVisitor;
import org.forgerock.opendj.config.PropertyOption;
import org.forgerock.opendj.config.RelativeInheritedDefaultBehaviorProvider;
import org.forgerock.opendj.config.StringPropertyDefinition;
import org.forgerock.opendj.config.Tag;
import org.forgerock.opendj.config.UndefinedDefaultBehaviorProvider;
import org.forgerock.opendj.config.client.ManagedObject;
 
import com.forgerock.opendj.cli.ArgumentException;
import com.forgerock.opendj.cli.BooleanArgument;
import com.forgerock.opendj.cli.ClientException;
import com.forgerock.opendj.cli.ConsoleApplication;
import com.forgerock.opendj.cli.MenuResult;
import com.forgerock.opendj.cli.StringArgument;
import com.forgerock.opendj.cli.SubCommand;
import com.forgerock.opendj.cli.SubCommandArgumentParser;
import com.forgerock.opendj.cli.TableBuilder;
import com.forgerock.opendj.cli.TablePrinter;
import com.forgerock.opendj.cli.TextTablePrinter;
 
/**
 * A sub-command handler which is used to display help about managed objects and their properties.
 * <p>
 * This sub-command implements the help-properties sub-command.
 */
final class HelpSubCommandHandler extends SubCommandHandler {
 
    /** This class is used to print the default behavior of a property. */
    private static class DefaultBehaviorPrinter {
 
        /**
         * The default behavior printer visitor implementation.
         *
         * @param <T>
         *            The property type.
         */
        private static class DefaultVisitor<T> implements
                DefaultBehaviorProviderVisitor<T, LocalizableMessage, PropertyDefinition<T>> {
 
            @Override
            public LocalizableMessage visitAbsoluteInherited(AbsoluteInheritedDefaultBehaviorProvider<T> d,
                    PropertyDefinition<T> p) {
                return INFO_DSCFG_HELP_FIELD_INHERITED_ABS.get(d.getPropertyName(), d.getManagedObjectPath()
                        .getRelationDefinition().getUserFriendlyName());
            }
 
            @Override
            public LocalizableMessage visitAlias(AliasDefaultBehaviorProvider<T> d, PropertyDefinition<T> p) {
                return d.getSynopsis();
            }
 
            @Override
            public LocalizableMessage visitDefined(DefinedDefaultBehaviorProvider<T> d, PropertyDefinition<T> p) {
                LocalizableMessageBuilder builder = new LocalizableMessageBuilder();
                PropertyValuePrinter printer = new PropertyValuePrinter(null, null, false);
                boolean isFirst = true;
                for (String s : d.getDefaultValues()) {
                    if (!isFirst) {
                        builder.append(", ");
                    }
 
                    T value = p.decodeValue(s);
                    builder.append(printer.print(p, value));
                }
 
                return builder.toMessage();
            }
 
            @Override
            public LocalizableMessage visitRelativeInherited(RelativeInheritedDefaultBehaviorProvider<T> d,
                    PropertyDefinition<T> p) {
                if (d.getRelativeOffset() == 0) {
                    return INFO_DSCFG_HELP_FIELD_INHERITED_THIS.get(d.getPropertyName(), d.getManagedObjectDefinition()
                            .getUserFriendlyName());
                } else {
                    return INFO_DSCFG_HELP_FIELD_INHERITED_PARENT.get(d.getPropertyName(), d
                            .getManagedObjectDefinition().getUserFriendlyName());
                }
            }
 
            @Override
            public LocalizableMessage visitUndefined(UndefinedDefaultBehaviorProvider<T> d, PropertyDefinition<T> p) {
                return INFO_DSCFG_HELP_FIELD_UNDEFINED.get();
            }
        }
 
        /** Create a new default behavior printer. */
        public DefaultBehaviorPrinter() {
            // No implementation required.
        }
 
        /**
         * Get a user-friendly description of a property's default behavior.
         *
         * @param <T>
         *            The type of the property definition.
         * @param pd
         *            The property definition.
         * @return Returns the user-friendly description of a property's default behavior.
         */
        public <T> LocalizableMessage print(PropertyDefinition<T> pd) {
            DefaultVisitor<T> v = new DefaultVisitor<>();
            return pd.getDefaultBehaviorProvider().accept(v, pd);
        }
    }
 
    /** This class is used to print detailed syntax information about a property. */
    private static class SyntaxPrinter {
 
        /** The syntax printer visitor implementation. */
        private static final class Visitor extends PropertyDefinitionVisitor<Void, PrintStream> {
 
            /** Private constructor. */
            private Visitor() {
                // No implementation required.
            }
 
            @Override
            public <E extends Enum<E>> Void visitEnum(EnumPropertyDefinition<E> d, PrintStream p) {
                displayUsage(p, INFO_DSCFG_HELP_FIELD_ENUM.get());
                p.println();
 
                TableBuilder builder = new TableBuilder();
                boolean isFirst = true;
                for (E value : EnumSet.<E> allOf(d.getEnumClass())) {
                    if (!isFirst) {
                        builder.startRow();
                    }
 
                    builder.startRow();
                    builder.appendCell();
                    builder.appendCell();
                    builder.appendCell(value.toString());
                    builder.appendCell(HEADING_SEPARATOR);
                    builder.appendCell(d.getValueSynopsis(value));
 
                    isFirst = false;
                }
 
                TextTablePrinter factory = new TextTablePrinter(p);
                factory.setDisplayHeadings(false);
                factory.setColumnWidth(0, HEADING_WIDTH);
                factory.setColumnWidth(1, HEADING_SEPARATOR.length());
                factory.setColumnWidth(4, 0);
                factory.setPadding(0);
 
                builder.print(factory);
 
                return null;
            }
 
            @Override
            public Void visitString(StringPropertyDefinition d, PrintStream p) {
                PropertyDefinitionUsageBuilder usageBuilder = new PropertyDefinitionUsageBuilder(false);
 
                TableBuilder builder = new TableBuilder();
                builder.startRow();
                builder.appendCell(INFO_DSCFG_HELP_HEADING_SYNTAX.get());
                builder.appendCell(HEADING_SEPARATOR);
                builder.appendCell(usageBuilder.getUsage(d));
 
                if (d.getPattern() != null) {
                    builder.startRow();
                    builder.startRow();
                    builder.appendCell();
                    builder.appendCell();
                    builder.appendCell(d.getPatternSynopsis());
                }
 
                TextTablePrinter factory = new TextTablePrinter(p);
                factory.setDisplayHeadings(false);
                factory.setColumnWidth(0, HEADING_WIDTH);
                factory.setColumnWidth(2, 0);
                factory.setPadding(0);
 
                builder.print(factory);
 
                return null;
            }
 
            @Override
            public <T> Void visitUnknown(PropertyDefinition<T> d, PrintStream p) {
                PropertyDefinitionUsageBuilder usageBuilder = new PropertyDefinitionUsageBuilder(true);
                displayUsage(p, usageBuilder.getUsage(d));
 
                return null;
            }
 
            /** Common usage. */
            private void displayUsage(PrintStream p, LocalizableMessage usage) {
                TableBuilder builder = new TableBuilder();
                builder.startRow();
                builder.appendCell(INFO_DSCFG_HELP_HEADING_SYNTAX.get());
                builder.appendCell(HEADING_SEPARATOR);
                builder.appendCell(usage);
 
                TextTablePrinter factory = new TextTablePrinter(p);
                factory.setDisplayHeadings(false);
                factory.setColumnWidth(0, HEADING_WIDTH);
                factory.setColumnWidth(2, 0);
                factory.setPadding(0);
 
                builder.print(factory);
            }
        }
 
        /** The private implementation. */
        private final Visitor pimpl;
 
        /**
         * Creates a new syntax printer which can be used to print detailed syntax information about a property.
         */
        public SyntaxPrinter() {
            this.pimpl = new Visitor();
        }
 
        /**
         * Print detailed syntax information about a property definition.
         *
         * @param out
         *            The output stream.
         * @param pd
         *            The property definition.
         */
        public void print(PrintStream out, PropertyDefinition<?> pd) {
            pd.accept(pimpl, out);
        }
    }
 
    /** Strings used in property help. */
    private static final String HEADING_SEPARATOR = " : ";
    /** Width of biggest heading (need to be careful of I18N). */
    private static final int HEADING_WIDTH;
 
    /** The value for the long option category. */
    private static final String OPTION_DSCFG_LONG_CATEGORY = "category";
    /** The value for the long option inherited. */
    private static final String OPTION_DSCFG_LONG_INHERITED = "inherited";
    /** The value for the long option type. */
    private static final String OPTION_DSCFG_LONG_TYPE = "type";
    /** The value for the short option category. */
    private static final Character OPTION_DSCFG_SHORT_CATEGORY = 'c';
    /** The value for the short option inherited. */
    private static final Character OPTION_DSCFG_SHORT_INHERITED = null;
    /** The value for the short option type. */
    private static final Character OPTION_DSCFG_SHORT_TYPE = 't';
 
    static {
        int tmp = INFO_DSCFG_HELP_HEADING_SYNTAX.get().length();
        tmp = Math.max(tmp, INFO_DSCFG_HELP_HEADING_DEFAULT.get().length());
        tmp = Math.max(tmp, INFO_DSCFG_HELP_HEADING_MULTI_VALUED.get().length());
        tmp = Math.max(tmp, INFO_DSCFG_HELP_HEADING_MANDATORY.get().length());
        tmp = Math.max(tmp, INFO_DSCFG_HELP_HEADING_READ_ONLY.get().length());
        HEADING_WIDTH = tmp;
    }
 
    /**
     * Creates a new help-properties sub-command.
     *
     * @param parser
     *            The sub-command argument parser.
     * @return Returns the new help-properties sub-command.
     * @throws ArgumentException
     *             If the sub-command could not be created successfully.
     */
    public static HelpSubCommandHandler create(SubCommandArgumentParser parser) throws ArgumentException {
        return new HelpSubCommandHandler(parser);
    }
 
    /**
     * Displays detailed help about a single component to the specified output stream.
     *
     * @param app
     *            The application console.
     * @param mo
     *            The managed object.
     * @param c
     *            The collection of properties to be displayed.
     */
    public static void displaySingleComponent(ConsoleApplication app, ManagedObject<?> mo,
            Collection<PropertyDefinition<?>> c) {
        String ufn = mo.getManagedObjectPath().getName();
        if (ufn == null) {
            ufn = mo.getManagedObjectDefinition().getUserFriendlyName().toString();
        }
        // Display the title.
        app.println(INFO_DSCFG_HELP_HEADING_COMPONENT.get(ufn));
 
        final AbstractManagedObjectDefinition<?, ?> d = mo.getManagedObjectDefinition();
 
        app.println();
        app.println(d.getSynopsis());
        if (d.getDescription() != null) {
            app.println();
            app.println(d.getDescription());
        }
 
        app.println();
        app.println();
        displayPropertyOptionKey(app);
 
        app.println();
        app.println();
 
        // Headings.
        final TableBuilder builder = new TableBuilder();
 
        builder.appendHeading(INFO_DSCFG_HEADING_PROPERTY_NAME.get());
        builder.appendHeading(INFO_DSCFG_HEADING_PROPERTY_OPTIONS.get());
        builder.appendHeading(INFO_DSCFG_HEADING_PROPERTY_SYNTAX.get());
 
        // Sort keys.
        builder.addSortKey(0);
 
        // Output summary of each property.
        for (final PropertyDefinition<?> pd : c) {
            // Display the property.
            builder.startRow();
 
            // Display the property name.
            builder.appendCell(pd.getName());
 
            // Display the options.
            builder.appendCell(getPropertyOptionSummary(pd));
 
            // Display the syntax.
            final PropertyDefinitionUsageBuilder v = new PropertyDefinitionUsageBuilder(false);
            builder.appendCell(v.getUsage(pd));
        }
 
        builder.print(new TextTablePrinter(app.getErrorStream()));
    }
 
    /**
     * Displays detailed help about a single property to the specified output stream.
     *
     * @param app
     *            The application console.
     * @param d
     *            The managed object definition.
     * @param name
     *            The name of the property definition.
     */
    public static void displayVerboseSingleProperty(ConsoleApplication app, AbstractManagedObjectDefinition<?, ?> d,
            String name) {
        PropertyDefinition<?> pd = d.getPropertyDefinition(name);
 
        // Display the title.
        app.println(INFO_DSCFG_HELP_HEADING_PROPERTY.get(name));
 
        // Display the property synopsis and description.
        app.println();
        app.println(pd.getSynopsis(), 4);
 
        if (pd.getDescription() != null) {
            app.println();
            app.println(pd.getDescription(), 4);
        }
 
        if (pd instanceof AggregationPropertyDefinition) {
            AggregationPropertyDefinition<?, ?> apd = (AggregationPropertyDefinition<?, ?>) pd;
            if (apd.getSourceConstraintSynopsis() != null) {
                app.println();
                app.println(apd.getSourceConstraintSynopsis(), 4);
            }
        }
 
        // Display the syntax.
        app.println();
        SyntaxPrinter syntaxPrinter = new SyntaxPrinter();
        syntaxPrinter.print(app.getErrorStream(), pd);
 
        // Display remaining information in a table.
        app.println();
        TableBuilder builder = new TableBuilder();
 
        // Display the default behavior.
        DefaultBehaviorPrinter defaultPrinter = new DefaultBehaviorPrinter();
 
        builder.startRow();
        builder.appendCell(INFO_DSCFG_HELP_HEADING_DEFAULT.get());
        builder.appendCell(HEADING_SEPARATOR);
        builder.appendCell(defaultPrinter.print(pd));
 
        // Display options.
        builder.startRow();
        builder.appendCell(INFO_DSCFG_HELP_HEADING_ADVANCED.get());
        builder.appendCell(HEADING_SEPARATOR);
        builder.appendCell(hasOptionYN(pd, PropertyOption.ADVANCED));
 
        builder.startRow();
        builder.appendCell(INFO_DSCFG_HELP_HEADING_MULTI_VALUED.get());
        builder.appendCell(HEADING_SEPARATOR);
        builder.appendCell(hasOptionYN(pd, PropertyOption.MULTI_VALUED));
 
        builder.startRow();
        builder.appendCell(INFO_DSCFG_HELP_HEADING_MANDATORY.get());
        builder.appendCell(HEADING_SEPARATOR);
        builder.appendCell(hasOptionYN(pd, PropertyOption.MANDATORY));
 
        builder.startRow();
        builder.appendCell(INFO_DSCFG_HELP_HEADING_READ_ONLY.get());
        builder.appendCell(HEADING_SEPARATOR);
        if (pd.hasOption(PropertyOption.MONITORING)) {
            builder.appendCell(INFO_DSCFG_HELP_FIELD_MONITORING.get());
        } else if (pd.hasOption(PropertyOption.READ_ONLY)) {
            builder.appendCell(INFO_DSCFG_HELP_FIELD_READ_ONLY.get(d.getUserFriendlyName()));
        } else {
            builder.appendCell(INFO_GENERAL_NO.get());
        }
 
        TextTablePrinter factory = new TextTablePrinter(app.getErrorStream());
        factory.setDisplayHeadings(false);
        factory.setColumnWidth(0, HEADING_WIDTH);
        factory.setColumnWidth(2, 0);
        factory.setPadding(0);
        builder.print(factory);
 
        // Administrator action.
        LocalizableMessage synopsis = getSynopsis(d, pd);
        if (synopsis != null) {
            app.println();
            app.println(synopsis);
        }
    }
 
    private static LocalizableMessage hasOptionYN(PropertyDefinition<?> pd, PropertyOption option) {
        return pd.hasOption(option) ? INFO_GENERAL_YES.get() : INFO_GENERAL_NO.get();
    }
 
    private static LocalizableMessage getSynopsis(AbstractManagedObjectDefinition<?, ?> d, PropertyDefinition<?> pd) {
        AdministratorAction action = pd.getAdministratorAction();
        LocalizableMessage synopsis = action.getSynopsis();
        if (synopsis != null) {
            return synopsis;
        }
 
        switch (action.getType()) {
        case COMPONENT_RESTART:
            return INFO_DSCFG_HELP_FIELD_COMPONENT_RESTART.get(d.getUserFriendlyName());
        case SERVER_RESTART:
            return INFO_DSCFG_HELP_FIELD_SERVER_RESTART.get();
        default:
            return null;
        }
    }
 
    /** Displays the property option summary key. */
    private static void displayPropertyOptionKey(ConsoleApplication app) {
        LocalizableMessageBuilder builder;
 
        app.println(INFO_DSCFG_HELP_DESCRIPTION_OPTION.get());
        app.println();
 
        builder = new LocalizableMessageBuilder();
        builder.append(" r -- ");
        builder.append(INFO_DSCFG_HELP_DESCRIPTION_READ.get());
        app.println(builder.toMessage());
 
        builder = new LocalizableMessageBuilder();
        builder.append(" w -- ");
        builder.append(INFO_DSCFG_HELP_DESCRIPTION_WRITE.get());
        app.println(builder.toMessage());
 
        builder = new LocalizableMessageBuilder();
        builder.append(" m -- ");
        builder.append(INFO_DSCFG_HELP_DESCRIPTION_MANDATORY.get());
        app.println(builder.toMessage());
 
        builder = new LocalizableMessageBuilder();
        builder.append(" s -- ");
        builder.append(INFO_DSCFG_HELP_DESCRIPTION_SINGLE_VALUED.get());
        app.println(builder.toMessage());
 
        builder = new LocalizableMessageBuilder();
        builder.append(" a -- ");
        builder.append(INFO_DSCFG_HELP_DESCRIPTION_ADMIN_ACTION.get());
        app.println(builder.toMessage());
    }
 
    /** Compute the options field. */
    private static String getPropertyOptionSummary(PropertyDefinition<?> pd) {
        StringBuilder b = new StringBuilder();
 
        if (pd.hasOption(PropertyOption.MONITORING) || pd.hasOption(PropertyOption.READ_ONLY)) {
            b.append("r-");
        } else {
            b.append("rw");
        }
 
        if (pd.hasOption(PropertyOption.MANDATORY)) {
            b.append('m');
        } else {
            b.append('-');
        }
 
        if (pd.hasOption(PropertyOption.MULTI_VALUED)) {
            b.append('-');
        } else {
            b.append('s');
        }
 
        AdministratorAction action = pd.getAdministratorAction();
        if (action.getType() != AdministratorAction.Type.NONE) {
            b.append('a');
        } else {
            b.append('-');
        }
        return b.toString();
    }
 
    /** The sub-command associated with this handler. */
    private final SubCommand subCommand;
    /** The argument which should be used to specify the category of managed object to be retrieved. */
    private final StringArgument categoryArgument;
    /** A table listing all the available types of managed object indexed on their parent type. */
    private final Map<String, Map<String, AbstractManagedObjectDefinition<?, ?>>> categoryMap = new TreeMap<>();
 
    /** The argument which should be used to display inherited properties. */
    private final BooleanArgument inheritedModeArgument;
    /** A table listing all the available types of managed object indexed on their tag(s). */
    private final Map<Tag, Map<String, AbstractManagedObjectDefinition<?, ?>>> tagMap = new HashMap<>();
    /** The argument which should be used to specify the sub-type of managed object to be retrieved. */
    private final StringArgument typeArgument;
 
    /** Private constructor. */
    private HelpSubCommandHandler(SubCommandArgumentParser parser) throws ArgumentException {
        // Create the sub-command.
        String name = "list-properties";
        LocalizableMessage desc = INFO_DSCFG_DESCRIPTION_SUBCMD_HELPPROP.get();
        this.subCommand = new SubCommand(parser, name, false, 0, 0, null, desc);
 
        categoryArgument =
                StringArgument.builder(OPTION_DSCFG_LONG_CATEGORY)
                        .shortIdentifier(OPTION_DSCFG_SHORT_CATEGORY)
                        .description(INFO_DSCFG_DESCRIPTION_HELP_CATEGORY.get())
                        .valuePlaceholder(INFO_CATEGORY_PLACEHOLDER.get())
                        .buildAndAddToSubCommand(subCommand);
        typeArgument =
                StringArgument.builder(OPTION_DSCFG_LONG_TYPE)
                        .shortIdentifier(OPTION_DSCFG_SHORT_TYPE)
                        .description(INFO_DSCFG_DESCRIPTION_HELP_TYPE.get())
                        .valuePlaceholder(INFO_TYPE_PLACEHOLDER.get())
                        .buildAndAddToSubCommand(subCommand);
        inheritedModeArgument =
                BooleanArgument.builder(OPTION_DSCFG_LONG_INHERITED)
                        .shortIdentifier(OPTION_DSCFG_SHORT_INHERITED)
                        .description(INFO_DSCFG_DESCRIPTION_HELP_INHERITED.get())
                        .buildAndAddToSubCommand(subCommand);
 
        // Register common arguments.
        registerPropertyNameArgument(this.subCommand);
 
        setCommandBuilderUseful(false);
    }
 
    @Override
    public SubCommand getSubCommand() {
        return subCommand;
    }
 
    /**
     * Registers a managed object definition with this help properties sub-command.
     *
     * @param d
     *            The managed object definition.
     */
    public void registerManagedObjectDefinition(AbstractManagedObjectDefinition<?, ?> d) {
        // Determine the definition's base name.
        AbstractManagedObjectDefinition<?, ?> parent = d;
        while (!parent.getParent().isTop()) {
            parent = parent.getParent();
        }
 
        String baseName = parent.getName();
        String typeName = null;
        if (parent == d) {
            // This was a top-level definition.
            typeName = DSConfig.GENERIC_TYPE;
        } else {
            // For the type name we shorten it, if possible, by stripping
            // off the trailing part of the name which matches the
            // base-type.
            String suffix = "-" + baseName;
            typeName = d.getName();
            if (typeName.endsWith(suffix)) {
                typeName = typeName.substring(0, typeName.length() - suffix.length());
            }
        }
 
        // Get the sub-type mapping, creating it if necessary.
        Map<String, AbstractManagedObjectDefinition<?, ?>> subTypes = categoryMap.get(baseName);
        if (subTypes == null) {
            subTypes = new TreeMap<>();
            categoryMap.put(baseName, subTypes);
        }
 
        subTypes.put(typeName, d);
 
        // Get the tag mapping, creating it if necessary.
        for (Tag tag : d.getAllTags()) {
            subTypes = tagMap.get(tag);
            if (subTypes == null) {
                subTypes = new TreeMap<>();
                tagMap.put(tag, subTypes);
            }
            subTypes.put(typeName, d);
        }
    }
 
    @Override
    public MenuResult<Integer> run(ConsoleApplication app, LDAPManagementContextFactory factory)
            throws ArgumentException, ClientException {
 
        String categoryName = categoryArgument.getValue();
        String typeName = typeArgument.getValue();
        Tag tag = null;
        Set<String> propertyNames = getPropertyNames();
 
        // Reset the command builder
        getCommandBuilder().clearArguments();
 
        // Update the command builder.
        updateCommandBuilderWithSubCommand();
 
        List<AbstractManagedObjectDefinition<?, ?>> dlist = new LinkedList<>();
        AbstractManagedObjectDefinition<?, ?> tmp = null;
 
        if (categoryName != null) {
            // User requested a category of components.
            Map<String, AbstractManagedObjectDefinition<?, ?>> subTypes = categoryMap.get(categoryName);
 
            if (subTypes == null) {
                // Try a tag-base look-up.
                try {
                    tag = Tag.valueOf(categoryName);
                } catch (IllegalArgumentException e) {
                    throw ArgumentExceptionFactory.unknownCategory(categoryName);
                }
 
                subTypes = tagMap.get(tag);
                if (subTypes == null) {
                    throw ArgumentExceptionFactory.unknownCategory(categoryName);
                }
            } else {
                // Cache the generic definition for improved errors later on.
                tmp = subTypes.get(DSConfig.GENERIC_TYPE);
            }
 
            if (typeName != null) {
                AbstractManagedObjectDefinition<?, ?> d = subTypes.get(typeName);
                if (d == null) {
                    throw ArgumentExceptionFactory.unknownTypeForCategory(typeName, categoryName);
                }
                dlist.add(d);
 
                // Cache the generic definition for improved errors later on.
                tmp = d;
            } else {
                dlist.addAll(subTypes.values());
            }
        } else if (typeName != null) {
            // User requested just the sub-type which could appear in
            // multiple categories.
            boolean isFound = false;
 
            for (Map<String, AbstractManagedObjectDefinition<?, ?>> subTypes : categoryMap.values()) {
                AbstractManagedObjectDefinition<?, ?> d = subTypes.get(typeName);
                if (d != null) {
                    dlist.add(d);
                    isFound = true;
                }
            }
 
            if (!isFound) {
                throw ArgumentExceptionFactory.unknownTypeForCategory(typeName, categoryName);
            }
        } else {
            // User did not specify a category nor a sub-type.
            for (Map<String, AbstractManagedObjectDefinition<?, ?>> subTypes : categoryMap.values()) {
                dlist.addAll(subTypes.values());
            }
        }
 
        // Validate property names.
        if (dlist.size() == 1) {
            // Cache the generic definition for improved errors later on.
            tmp = dlist.get(0);
        }
 
        for (String propertyName : propertyNames) {
            boolean isFound = false;
 
            for (AbstractManagedObjectDefinition<?, ?> d : dlist) {
                try {
                    d.getPropertyDefinition(propertyName);
                    isFound = true;
                } catch (IllegalArgumentException e) {
                    // Ignore for now.
                }
            }
 
            if (!isFound) {
                if (tmp != null) {
                    throw ArgumentExceptionFactory.unknownProperty(tmp, propertyName);
                } else {
                    throw ArgumentExceptionFactory.unknownProperty(propertyName);
                }
            }
        }
 
        // Output everything to the output stream.
        if (!app.isVerbose()) {
            displayNonVerbose(app, categoryName, typeName, tag, propertyNames);
        } else {
            displayVerbose(app, categoryName, typeName, tag, propertyNames);
        }
 
        return MenuResult.success(0);
    }
 
    /** Output property summary table. */
    private void displayNonVerbose(ConsoleApplication app, String categoryName, String typeName, Tag tag,
            Set<String> propertyNames) {
        if (!app.isScriptFriendly()) {
            displayPropertyOptionKey(app);
            app.println();
            app.println();
        }
 
        // Headings.
        TableBuilder builder = new TableBuilder();
 
        builder.appendHeading(INFO_DSCFG_HEADING_COMPONENT_NAME.get());
        builder.appendHeading(INFO_DSCFG_HEADING_COMPONENT_TYPE.get());
        builder.appendHeading(INFO_DSCFG_HEADING_PROPERTY_NAME.get());
        builder.appendHeading(INFO_DSCFG_HEADING_PROPERTY_OPTIONS.get());
        builder.appendHeading(INFO_DSCFG_HEADING_PROPERTY_SYNTAX.get());
 
        // Sort keys.
        builder.addSortKey(0);
        builder.addSortKey(1);
        builder.addSortKey(2);
 
        // Generate the table content.
        for (String category : categoryMap.keySet()) {
            // Skip if this is the wrong category.
            if (categoryName != null && !categoryName.equals(category)) {
                continue;
            }
 
            // Process the sub-types.
            Map<String, AbstractManagedObjectDefinition<?, ?>> subTypes = categoryMap.get(category);
            for (String type : subTypes.keySet()) {
                // Skip if this is the wrong sub-type.
                if (typeName != null && !typeName.equals(type)) {
                    continue;
                }
 
                // Display help for each property.
                AbstractManagedObjectDefinition<?, ?> mod = subTypes.get(type);
                if (cannotDisplay(app, mod, tag)) {
                    continue;
                }
 
                Set<PropertyDefinition<?>> pds = getPropertyDefinitions(mod);
                for (PropertyDefinition<?> pd : pds) {
                    if (cannotDisplay(app, pd, propertyNames)) {
                        continue;
                    }
 
                    // Display the property:
                    // - component category and type,
                    // - property name, options
                    // - syntax
                    builder.startRow();
                    builder.appendCell(category);
                    builder.appendCell(type);
                    builder.appendCell(pd.getName());
                    builder.appendCell(getPropertyOptionSummary(pd));
                    PropertyDefinitionUsageBuilder v = new PropertyDefinitionUsageBuilder(false);
                    builder.appendCell(v.getUsage(pd));
                }
            }
        }
 
        TablePrinter printer;
        if (app.isScriptFriendly()) {
            printer = createScriptFriendlyTablePrinter(app.getOutputStream());
        } else {
            printer = new TextTablePrinter(app.getOutputStream());
        }
        builder.print(printer);
    }
 
    /** Display detailed help on managed objects and their properties. */
    private void displayVerbose(ConsoleApplication app, String categoryName, String typeName, Tag tag,
            Set<String> propertyNames) {
        // Construct line used to separate consecutive sections.
        LocalizableMessage c1 = buildLine('=', MAX_LINE_WIDTH);
        LocalizableMessage c2 = buildLine('-', MAX_LINE_WIDTH);
 
        // Display help for each managed object.
        boolean isFirstManagedObject = true;
        for (String category : categoryMap.keySet()) {
            // Skip if this is the wrong category.
            if (categoryName != null && !categoryName.equals(category)) {
                continue;
            }
 
            // Process the sub-types.
            Map<String, AbstractManagedObjectDefinition<?, ?>> subTypes = categoryMap.get(category);
            for (String type : subTypes.keySet()) {
                // Skip if this is the wrong sub-type.
                if (typeName != null && !typeName.equals(type)) {
                    continue;
                }
 
                // Display help for each property.
                AbstractManagedObjectDefinition<?, ?> mod = subTypes.get(type);
                if (cannotDisplay(app, mod, tag)) {
                    continue;
                }
 
                boolean isFirstProperty = true;
                Set<PropertyDefinition<?>> pds = getPropertyDefinitions(mod);
                for (PropertyDefinition<?> pd : pds) {
                    if (cannotDisplay(app, pd, propertyNames)) {
                        continue;
                    }
 
                    if (isFirstProperty) {
                        // User has requested properties relating to this managed
                        // object definition, so display the summary of the managed object.
                        if (!isFirstManagedObject) {
                            app.println();
                            app.println(c1);
                            app.println();
                        } else {
                            isFirstManagedObject = false;
                        }
 
                        // Display the title.
                        app.println(INFO_DSCFG_HELP_HEADING_COMPONENT.get(mod.getUserFriendlyName()));
 
                        app.println();
                        app.println(mod.getSynopsis());
                        if (mod.getDescription() != null) {
                            app.println();
                            app.println(mod.getDescription());
                        }
                    }
 
                    app.println();
                    app.println(c2);
                    app.println();
 
                    displayVerboseSingleProperty(app, mod, pd.getName());
                    isFirstProperty = false;
                }
            }
        }
    }
 
    private LocalizableMessage buildLine(char c, int length) {
        LocalizableMessageBuilder mb = new LocalizableMessageBuilder();
        for (int i = 0; i < length; i++) {
            mb.append(c);
        }
        return mb.toMessage();
    }
 
    private Set<PropertyDefinition<?>> getPropertyDefinitions(AbstractManagedObjectDefinition<?, ?> mod) {
        Set<PropertyDefinition<?>> pds = new TreeSet<>();
        if (inheritedModeArgument.isPresent()) {
            pds.addAll(mod.getAllPropertyDefinitions());
        } else {
            pds.addAll(mod.getPropertyDefinitions());
 
            // The list will still contain overridden properties.
            if (mod.getParent() != null) {
                pds.removeAll(mod.getParent().getAllPropertyDefinitions());
            }
        }
        return pds;
    }
}