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

neil_a_wilson
05.46.2007 8f5c232ab629b7aa83c5a1d4eff9648874a0c9ca
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
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at
 * trunk/opends/resource/legal-notices/OpenDS.LICENSE
 * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at
 * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
 * add the following below this CDDL HEADER, with the fields enclosed
 * by brackets "[]" replaced with your own identifying information:
 *      Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 *
 *      Portions Copyright 2007 Sun Microsystems, Inc.
 */
package org.opends.server.tools.dsconfig;
 
 
 
import static org.opends.server.loggers.debug.DebugLogger.*;
import static org.opends.server.messages.MessageHandler.*;
import static org.opends.server.messages.ToolMessages.*;
import static org.opends.server.tools.ToolConstants.*;
import static org.opends.server.util.ServerConstants.*;
import static org.opends.server.util.StaticUtils.*;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.Reader;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;
 
import org.opends.server.admin.AbstractManagedObjectDefinition;
import org.opends.server.admin.AttributeTypePropertyDefinition;
import org.opends.server.admin.ClassLoaderProvider;
import org.opends.server.admin.ClassPropertyDefinition;
import org.opends.server.admin.PropertyException;
import org.opends.server.admin.Tag;
import org.opends.server.admin.client.ManagedObjectDecodingException;
import org.opends.server.admin.client.ManagementContext;
import org.opends.server.core.DirectoryServer;
import org.opends.server.loggers.debug.DebugTracer;
import org.opends.server.tools.ClientException;
import org.opends.server.types.DebugLogLevel;
import org.opends.server.types.InitializationException;
import org.opends.server.types.NullOutputStream;
import org.opends.server.util.PasswordReader;
import org.opends.server.util.StaticUtils;
import org.opends.server.util.args.ArgumentException;
import org.opends.server.util.args.BooleanArgument;
import org.opends.server.util.args.SubCommand;
import org.opends.server.util.args.SubCommandArgumentParser;
 
 
 
/**
 * This class provides a command-line tool which enables
 * administrators to configure the Directory Server.
 */
public final class DSConfig {
 
  /**
   * The tracer object for the debug logger.
   */
  private static final DebugTracer TRACER = getTracer();
 
 
 
  /**
   * Provides the command-line arguments to the main application for
   * processing.
   *
   * @param args
   *          The set of command-line arguments provided to this
   *          program.
   */
  public static void main(String[] args) {
    DSConfig app = new DSConfig(System.in, System.out, System.err,
        new LDAPManagementContextFactory());
    // Only initialize the client environment when run as a standalone
    // application.
    try {
      app.initializeClientEnvironment();
    } catch (InitializationException e) {
      // TODO: is this ok as an error message?
      System.err.println(wrapText(e.getMessage(), MAX_LINE_WIDTH));
      System.exit(1);
    }
 
    // Run the application.
    int exitCode = app.run(args);
    if (exitCode != 0) {
      System.exit(filterExitCode(exitCode));
    }
  }
 
  // Flag indicating whether or not the application environment has
  // already been initialized.
  private boolean environmentInitialized = false;
 
  // The error stream which this application should use.
  private final PrintStream err;
 
  // The factory which the application should use to retrieve its
  // management context.
  private final ManagementContextFactory factory;
 
  // Flag indicating whether or not the global arguments have
  // already been initialized.
  private boolean globalArgumentsInitialized = false;
 
  // Mapping of sub-commands to their implementations;
  private final Map<SubCommand, SubCommandHandler> handlers =
    new HashMap<SubCommand, SubCommandHandler>();
 
  // The input stream reader which this application should use.
  private final BufferedReader in;
 
  // The argument which should be used to request interactive
  // behavior.
  private BooleanArgument interactiveArgument;
 
  // The output stream which this application should use.
  private final PrintStream out;
 
  // The command-line argument parser.
  private final SubCommandArgumentParser parser;
 
  // The argument which should be used to request quiet output.
  private BooleanArgument quietArgument;
 
  // The argument which should be used to request script-friendly
  // output.
  private BooleanArgument scriptFriendlyArgument;
 
  // The argument which should be used to request usage information.
  private BooleanArgument showUsageArgument;
 
  // Flag indicating whether or not the sub-commands have
  // already been initialized.
  private boolean subCommandsInitialized = false;
 
  // The argument which should be used to request verbose output.
  private BooleanArgument verboseArgument;
 
 
 
  /**
   * Creates a new dsconfig application instance.
   *
   * @param in
   *          The application input stream.
   * @param out
   *          The application output stream.
   * @param err
   *          The application error stream.
   * @param factory
   *          The factory which this application instance should use
   *          for obtaining management contexts.
   */
  public DSConfig(InputStream in, OutputStream out, OutputStream err,
      ManagementContextFactory factory) {
    this.parser = new SubCommandArgumentParser(this.getClass().getName(),
        getMessage(MSGID_CONFIGDS_TOOL_DESCRIPTION), false);
 
    if (in != null) {
      this.in = new BufferedReader(new InputStreamReader(in));
    } else {
      this.in = new BufferedReader(new Reader() {
 
        @Override
        public void close() throws IOException {
          // Do nothing.
        }
 
 
 
        @Override
        public int read(char[] cbuf, int off, int len) throws IOException {
          return -1;
        }
 
      });
    }
 
    if (out != null) {
      this.out = new PrintStream(out);
    } else {
      this.out = NullOutputStream.printStream();
    }
 
    if (err != null) {
      this.err = new PrintStream(err);
    } else {
      this.err = NullOutputStream.printStream();
    }
 
    this.factory = factory;
  }
 
 
 
  /**
   * Interactively confirms whether a user wishes to perform an
   * action. If the application is non-interactive, then the action is
   * granted by default.
   *
   * @param prompt
   *          The prompt describing the action.
   * @return Returns <code>true</code> if the user wishes the action
   *         to be performed, or <code>false</code> if they refused,
   *         or if an exception occurred.
   */
  public boolean confirmAction(String prompt) {
    if (!isInteractive()) {
      return true;
    }
 
    String yes = Messages.getString("general.yes");
    String no = Messages.getString("general.no");
    String errMsg = Messages.getString("general.confirm.error");
    String error = String.format(errMsg, yes, no);
    prompt = prompt + String.format(" (%s / %s): ", yes, no);
 
    while (true) {
      String response;
      try {
        response = readLineOfInput(prompt);
      } catch (Exception e) {
        return false;
      }
 
      if (response == null) {
        // End of input.
        return false;
      }
 
      response = response.toLowerCase().trim();
      if (response.length() == 0) {
        // Empty input.
        err.println(wrapText(error, MAX_LINE_WIDTH));
      } else if (no.startsWith(response)) {
        return false;
      } else if (yes.startsWith(response)) {
        return true;
      } else {
        // Try again...
        err.println(wrapText(error, MAX_LINE_WIDTH));
      }
    }
  }
 
 
 
  /**
   * Displays a message to the error stream.
   *
   * @param msg
   *          The message.
   */
  public void displayMessage(String msg) {
    err.println(wrapText(msg, MAX_LINE_WIDTH));
  }
 
 
 
  /**
   * Displays a message to the error stream if verbose mode is
   * enabled.
   *
   * @param msg
   *          The verbose message.
   */
  public void displayVerboseMessage(String msg) {
    if (isVerbose()) {
      err.println(wrapText(msg, MAX_LINE_WIDTH));
    }
  }
 
 
 
  /**
   * Initializes core APIs for use when dsconfig will be run as a
   * standalone application.
   *
   * @throws InitializationException
   *           If the core APIs could not be initialized.
   */
  public void initializeClientEnvironment() throws InitializationException {
    if (environmentInitialized == false) {
      // TODO: do we need to do this?
      DirectoryServer.bootstrapClient();
 
      // Bootstrap definition classes.
      ClassLoaderProvider.getInstance().enable();
 
      // Switch off class name validation in client.
      ClassPropertyDefinition.setAllowClassValidation(false);
 
      // Switch off attribute type name validation in client.
      AttributeTypePropertyDefinition.setCheckSchema(false);
 
      environmentInitialized = true;
    }
  }
 
 
 
  /**
   * Indicates whether or not the user has requested interactive
   * behavior.
   *
   * @return Returns <code>true</code> if the user has requested
   *         interactive behavior.
   */
  public boolean isInteractive() {
    return interactiveArgument.isPresent();
  }
 
 
 
  /**
   * Indicates whether or not the user has requested quiet output.
   *
   * @return Returns <code>true</code> if the user has requested
   *         quiet output.
   */
  public boolean isQuiet() {
    return quietArgument.isPresent();
  }
 
 
 
  /**
   * Indicates whether or not the user has requested script-friendly
   * output.
   *
   * @return Returns <code>true</code> if the user has requested
   *         script-friendly output.
   */
  public boolean isScriptFriendly() {
    return scriptFriendlyArgument.isPresent();
  }
 
 
 
  /**
   * Indicates whether or not the user has requested verbose output.
   *
   * @return Returns <code>true</code> if the user has requested
   *         verbose output.
   */
  public boolean isVerbose() {
    return verboseArgument.isPresent();
  }
 
 
 
  /**
   * Interactively retrieves a line of input from the console.
   *
   * @param prompt
   *          The prompt.
   * @return Returns the line of input, or <code>null</code> if the
   *         end of input has been reached.
   * @throws Exception
   *           If the line of input could not be retrieved for some
   *           reason.
   */
  public String readLineOfInput(String prompt) throws Exception {
    err.print(wrapText(prompt, MAX_LINE_WIDTH));
    return in.readLine();
  }
 
 
 
  /**
   * Interactively retrieves a password from the console.
   *
   * @param prompt
   *          The password prompt.
   * @return Returns the password.
   * @throws Exception
   *           If the password could not be retrieved for some reason.
   */
  public String readPassword(String prompt) throws Exception {
    err.print(wrapText(prompt, MAX_LINE_WIDTH));
    char[] pwChars = PasswordReader.readPassword();
    return new String(pwChars);
  }
 
 
 
  /**
   * Gets the management context which sub-commands should use in
   * order to manage the directory server.
   *
   * @return Returns the management context which sub-commands should
   *         use in order to manage the directory server.
   * @throws ArgumentException
   *           If a management context related argument could not be
   *           parsed successfully.
   * @throws ClientException
   *           If the management context could not be created.
   */
  ManagementContext getManagementContext() throws ArgumentException,
      ClientException {
    return factory.getManagementContext(this);
  }
 
 
 
  /**
   * Registers the global arguments with the argument parser.
   *
   * @throws ArgumentException
   *           If a global argument could not be registered.
   */
  private void initializeGlobalArguments() throws ArgumentException {
    if (globalArgumentsInitialized == false) {
      verboseArgument = new BooleanArgument("verbose", 'v', "verbose",
          MSGID_DESCRIPTION_VERBOSE);
 
      quietArgument = new BooleanArgument("quiet", 'q', "quiet",
          MSGID_DESCRIPTION_QUIET);
 
      scriptFriendlyArgument = new BooleanArgument("script-friendly", 's',
          "script-friendly", MSGID_DESCRIPTION_SCRIPT_FRIENDLY);
 
      interactiveArgument = new BooleanArgument("interactive", 'i',
          "interactive", MSGID_DESCRIPTION_INTERACTIVE);
 
      showUsageArgument = new BooleanArgument("showUsage", OPTION_SHORT_HELP,
          OPTION_LONG_HELP, MSGID_DSCFG_DESCRIPTION_SHOW_GROUP_USAGE_SUMMARY);
 
      // Register the global arguments.
      parser.addGlobalArgument(showUsageArgument);
      parser.setUsageArgument(showUsageArgument, out);
      parser.addGlobalArgument(verboseArgument);
      parser.addGlobalArgument(quietArgument);
      parser.addGlobalArgument(scriptFriendlyArgument);
      parser.addGlobalArgument(interactiveArgument);
 
      // Register any global arguments required by the management
      // context factory.
      factory.registerGlobalArguments(parser);
 
      globalArgumentsInitialized = true;
    }
  }
 
 
 
  /**
   * Registers the sub-commands with the argument parser. This method
   * uses the administration framework introspection APIs to determine
   * the overall structure of the command-line.
   *
   * @throws ArgumentException
   *           If a sub-command could not be created.
   */
  private void initializeSubCommands() throws ArgumentException {
    if (subCommandsInitialized == false) {
      Comparator<SubCommand> c = new Comparator<SubCommand>() {
 
        public int compare(SubCommand o1, SubCommand o2) {
          return o1.getName().compareTo(o2.getName());
        }
      };
 
      SubCommandBuilder builder = new SubCommandBuilder();
      Map<Tag, SortedSet<SubCommand>> groups =
        new TreeMap<Tag, SortedSet<SubCommand>>();
      SortedSet<SubCommand> allSubCommands = new TreeSet<SubCommand>(c);
      for (SubCommandHandler handler : builder.getSubCommandHandlers(parser)) {
        SubCommand sc = handler.getSubCommand();
 
        handlers.put(sc, handler);
        allSubCommands.add(sc);
 
        // Add the sub-command to its groups.
        for (Tag tag : handler.getTags()) {
          SortedSet<SubCommand> group = groups.get(tag);
          if (group == null) {
            group = new TreeSet<SubCommand>(c);
            groups.put(tag, group);
          }
          group.add(sc);
        }
      }
 
      // Register the usage arguments.
      for (Map.Entry<Tag, SortedSet<SubCommand>> group : groups.entrySet()) {
        Tag tag = group.getKey();
        SortedSet<SubCommand> subCommands = group.getValue();
 
        String option = OPTION_LONG_HELP + "-" + tag.getName();
        String synopsis = tag.getSynopsis().toLowerCase();
        BooleanArgument arg = new BooleanArgument(option, null, option,
            MSGID_DSCFG_DESCRIPTION_SHOW_GROUP_USAGE, synopsis);
 
        parser.addGlobalArgument(arg);
        parser.setUsageGroupArgument(arg, subCommands);
      }
 
      // Register the --help-all argument.
      String option = OPTION_LONG_HELP + "-all";
      BooleanArgument arg = new BooleanArgument(option, null, option,
          MSGID_DSCFG_DESCRIPTION_SHOW_GROUP_USAGE_ALL);
 
      parser.addGlobalArgument(arg);
      parser.setUsageGroupArgument(arg, allSubCommands);
 
      subCommandsInitialized = true;
    }
  }
 
 
 
  /**
   * Parses the provided command-line arguments and makes the
   * appropriate changes to the Directory Server configuration.
   *
   * @param args
   *          The command-line arguments provided to this program.
   * @return The exit code from the configuration processing. A
   *         nonzero value indicates that there was some kind of
   *         problem during the configuration processing.
   */
  private int run(String[] args) {
    // Register global arguments and sub-commands.
    try {
      initializeGlobalArguments();
      initializeSubCommands();
    } catch (ArgumentException e) {
      int msgID = MSGID_CANNOT_INITIALIZE_ARGS;
      String message = getMessage(msgID, e.getMessage());
      err.println(wrapText(message, MAX_LINE_WIDTH));
      return 1;
    }
 
    // Parse the command-line arguments provided to this program.
    try {
      parser.parseArguments(args);
    } catch (ArgumentException ae) {
      int msgID = MSGID_ERROR_PARSING_ARGS;
      String message = getMessage(msgID, ae.getMessage());
      err.println(wrapText(message, MAX_LINE_WIDTH));
      err.println();
      err.println(parser.getHelpUsageReference());
      return 1;
    }
 
    // If the usage/version argument was provided, then we don't need
    // to do anything else.
    if (parser.usageOrVersionDisplayed()) {
      return 0;
    }
 
    // Make sure that we have a sub-command.
    if (parser.getSubCommand() == null) {
      int msgID = MSGID_ERROR_PARSING_ARGS;
      String message = getMessage(msgID,
          getMessage(MSGID_DSCFG_ERROR_MISSING_SUBCOMMAND));
      err.println(wrapText(message, MAX_LINE_WIDTH));
      err.println();
      err.println(parser.getHelpUsageReference());
      return 1;
    }
 
    if (quietArgument.isPresent() && verboseArgument.isPresent()) {
      int msgID = MSGID_TOOL_CONFLICTING_ARGS;
      String message = getMessage(msgID, quietArgument.getLongIdentifier(),
          verboseArgument.getLongIdentifier());
      err.println(wrapText(message, MAX_LINE_WIDTH));
      err.println();
      err.println(parser.getHelpUsageReference());
      return 1;
    }
 
    if (quietArgument.isPresent() && interactiveArgument.isPresent()) {
      int msgID = MSGID_TOOL_CONFLICTING_ARGS;
      String message = getMessage(msgID, quietArgument.getLongIdentifier(),
          interactiveArgument.getLongIdentifier());
      err.println(wrapText(message, MAX_LINE_WIDTH));
      err.println();
      err.println(parser.getHelpUsageReference());
      return 1;
    }
 
    if (scriptFriendlyArgument.isPresent() && verboseArgument.isPresent()) {
      int msgID = MSGID_TOOL_CONFLICTING_ARGS;
      String message = getMessage(msgID, scriptFriendlyArgument
          .getLongIdentifier(), verboseArgument.getLongIdentifier());
      err.println(wrapText(message, MAX_LINE_WIDTH));
      err.println();
      err.println(parser.getHelpUsageReference());
      return 1;
    }
 
    // Make sure that management context's arguments are valid.
    try {
      factory.validateGlobalArguments();
    } catch (ArgumentException e) {
      err.println(wrapText(e.getMessage(), MAX_LINE_WIDTH));
      return 1;
    }
 
    // Retrieve the sub-command implementation and run it.
    SubCommandHandler handler = handlers.get(parser.getSubCommand());
    try {
      return handler.run(this, out, err);
    } catch (ArgumentException e) {
      err.println(wrapText(e.getMessage(), MAX_LINE_WIDTH));
      return 1;
    } catch (ClientException e) {
      // If the client exception was caused by a decoding exception
      // then we should display the causes.
      err.println(wrapText(e.getMessage(), MAX_LINE_WIDTH));
 
      Throwable cause = e.getCause();
      if (cause instanceof ManagedObjectDecodingException) {
        // FIXME: use a table.
        ManagedObjectDecodingException de =
          (ManagedObjectDecodingException) cause;
 
        err.println();
        for (PropertyException pe : de.getCauses()) {
          AbstractManagedObjectDefinition<?, ?> d = de
              .getPartialManagedObject().getManagedObjectDefinition();
          ArgumentException ae = ArgumentExceptionFactory
              .adaptPropertyException(pe, d);
          err.println(wrapText(" * " + ae.getMessage(), MAX_LINE_WIDTH));
        }
        err.println();
      }
 
      return 1;
    } catch (Exception e) {
      if (debugEnabled()) {
        TRACER.debugCaught(DebugLogLevel.ERROR, e);
      }
      err.println(wrapText(StaticUtils.stackTraceToString(e), MAX_LINE_WIDTH));
      return 1;
    }
  }
}