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

Jean-Noel Rouvignac
11.03.2013 ef98b739b780e41d9b40c5c392ebec62455a022c
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
/*
 * 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
 *
 *
 *      Copyright 2006-2008 Sun Microsystems, Inc.
 *      Portions copyright 2013 ForgeRock AS
 */
package org.opends.server.util.args;
 
import java.util.Iterator;
import java.util.LinkedList;
 
import org.opends.messages.Message;
import org.opends.messages.MessageBuilder;
 
import static org.opends.messages.UtilityMessages.*;
import static org.opends.server.util.StaticUtils.*;
 
/**
 * This class defines a generic argument that may be used in the argument list
 * for an application.  This is an abstract class that must be subclassed in
 * order to provide specific functionality.
 */
public abstract class Argument
{
  /**
   * Indicates whether this argument should be hidden in the usage information.
   */
  private boolean isHidden;
 
  /**
   * Indicates whether this argument may be specified more than once for
   * multiple values.
   */
  private boolean isMultiValued;
 
  /**
   * Indicates whether this argument was provided in the set of command-line
   * arguments.
   */
  private boolean isPresent;
 
  /** Indicates whether this argument is required to have a value. */
  private boolean isRequired;
 
  /** Indicates whether this argument requires a value. */
  private boolean needsValue;
 
  /** The single-character identifier for this argument. */
  private Character shortIdentifier;
 
  /** The unique ID of the description for this argument. */
  private Message description;
 
  /** The set of provided values for this argument. */
  private LinkedList<String> values;
 
  /** The default value for the argument if none other is provided. */
  private String defaultValue;
 
  /** The long identifier for this argument. */
  private String longIdentifier;
 
  /** The generic name that will be used to refer to this argument. */
  private String name;
 
  /** The name of the property that can be used to set the default value. */
  private String propertyName;
 
  /**
   * The value placeholder for this argument, which will be used in usage
   * information. It describes the format that must be used to specify the
   * values for this argument.
   */
  private Message valuePlaceholder;
 
  /**
   * Indicates whether this argument was provided in the set of properties found
   * is a properties file.
   */
  private boolean isValueSetByProperty;
 
 
  /**
   * Creates a new argument with the provided information.
   *
   * @param  name              The generic name that should be used to refer to
   *                           this argument.
   * @param  shortIdentifier   The single-character identifier for this
   *                           argument, or <CODE>null</CODE> if there is none.
   * @param  longIdentifier    The long identifier for this argument, or
   *                           <CODE>null</CODE> if there is none.
   * @param  isRequired        Indicates whether this argument must be specified
   *                           on the command line.
   * @param  isMultiValued     Indicates whether this argument may be specified
   *                           more than once to provide multiple values.
   * @param  needsValue        Indicates whether this argument requires a value.
   * @param  valuePlaceholder  The placeholder for the argument value that will
   *                           be displayed in usage information, or
   *                           <CODE>null</CODE> if this argument does not
   *                           require a value.
   * @param  defaultValue      The default value that should be used for this
   *                           argument if none is provided in a properties file
   *                           or on the command line.  This may be
   *                           <CODE>null</CODE> if there is no generic default.
   * @param  propertyName      The name of the property in a property file that
   *                           may be used to override the default value but
   *                           will be overridden by a command-line argument.
   * @param  description       Message for the description of this
   *                           argument.
   *
   * @throws  ArgumentException  If there is a problem with any of the
   *                             parameters used to create this argument.
   */
  protected Argument(String name, Character shortIdentifier,
                     String longIdentifier, boolean isRequired,
                     boolean isMultiValued, boolean needsValue,
                     Message valuePlaceholder, String defaultValue,
                     String propertyName,
                     Message description)
            throws ArgumentException
  {
    this.name             = name;
    this.shortIdentifier  = shortIdentifier;
    this.longIdentifier   = longIdentifier;
    this.isRequired       = isRequired;
    this.isMultiValued    = isMultiValued;
    this.needsValue       = needsValue;
    this.valuePlaceholder = valuePlaceholder;
    this.defaultValue     = defaultValue;
    this.propertyName     = propertyName;
    this.description      = description;
    this.isValueSetByProperty = false ;
 
    if (shortIdentifier == null && longIdentifier == null)
    {
      Message message = ERR_ARG_NO_IDENTIFIER.get(name);
      throw new ArgumentException(message);
    }
 
    if (needsValue && valuePlaceholder == null)
    {
      Message message = ERR_ARG_NO_VALUE_PLACEHOLDER.get(name);
      throw new ArgumentException(message);
    }
 
    values    = new LinkedList<String>();
    isPresent = false;
    isHidden  = false;
  }
 
 
 
  /**
   * Retrieves the generic name that will be used to refer to this argument.
   *
   * @return  The generic name that will be used to refer to this argument.
   */
  public String getName()
  {
    return name;
  }
 
 
 
  /**
   * Retrieves the single-character identifier that may be used to specify the
   * value of this argument.
   *
   * @return  The single-character identifier that may be used to specify the
   *          value of this argument, or <CODE>null</CODE> if there is none.
   */
  public Character getShortIdentifier()
  {
    return shortIdentifier;
  }
 
 
 
  /**
   * Retrieves the long (multi-character) identifier that may be used to specify
   * the value of this argument.
   *
   * @return  The long (multi-character) identifier that may be used to specify
   *          the value of this argument.
   */
  public String getLongIdentifier()
  {
    return longIdentifier;
  }
 
 
 
  /**
   * Indicates whether this argument is required to have at least one value.
   *
   * @return  <CODE>true</CODE> if this argument is required to have at least
   *          one value, or <CODE>false</CODE> if it does not need to have a
   *          value.
   */
  public boolean isRequired()
  {
    return isRequired;
  }
 
 
 
  /**
   * Specifies whether this argument is required to have at least one value.
   *
   * @param  isRequired  Indicates whether this argument is required to have at
   *                     least one value.
   */
  public void setRequired(boolean isRequired)
  {
    this.isRequired = isRequired;
  }
 
 
 
  /**
   * Indicates whether this argument is present in the parsed set of
   * command-line arguments.
   *
   * @return  <CODE>true</CODE> if this argument is present in the parsed set of
   *          command-line arguments, or <CODE>false</CODE> if not.
   */
  public boolean isPresent()
  {
    return isPresent;
  }
 
 
 
  /**
   * Specifies whether this argument is present in the parsed set of
   * command-line arguments.
   *
   * @param  isPresent  Indicates whether this argument is present in the set of
   *                    command-line arguments.
   */
  public void setPresent(boolean isPresent)
  {
    this.isPresent = isPresent;
  }
 
 
 
  /**
   * Indicates whether this argument should be hidden from the usage
   * information.
   *
   * @return  <CODE>true</CODE> if this argument should be hidden from the usage
   *          information, or <CODE>false</CODE> if not.
   */
  public boolean isHidden()
  {
    return isHidden;
  }
 
 
 
  /**
   * Specifies whether this argument should be hidden from the usage
   * information.
   *
   * @param  isHidden  Indicates whether this argument should be hidden from the
   *                   usage information.
   */
  public void setHidden(boolean isHidden)
  {
    this.isHidden = isHidden;
  }
 
 
 
  /**
   * Indicates whether this argument may be provided more than once on the
   * command line to specify multiple values.
   *
   * @return  <CODE>true</CODE> if this argument may be provided more than once
   *          on the command line to specify multiple values, or
   *          <CODE>false</CODE> if it may have at most one value.
   */
  public boolean isMultiValued()
  {
    return isMultiValued;
  }
 
 
 
  /**
   * Specifies whether this argument may be provided more than once on the
   * command line to specify multiple values.
   *
   * @param  isMultiValued  Indicates whether this argument may be provided more
   *                        than once on the command line to specify multiple
   *                        values.
   */
  public void setMultiValued(boolean isMultiValued)
  {
    this.isMultiValued = isMultiValued;
  }
 
 
 
  /**
   * Indicates whether a value must be provided with this argument if it is
   * present.
   *
   * @return  <CODE>true</CODE> if a value must be provided with the argument if
   *          it is present, or <CODE>false</CODE> if the argument does not take
   *          a value and the presence of the argument identifier itself is
   *          sufficient to convey the necessary information.
   */
  public boolean needsValue()
  {
    return needsValue;
  }
 
 
 
  /**
   * Specifies whether a value must be provided with this argument if it is
   * present.  If this is changed from <CODE>false</CODE> to <CODE>true</CODE>,
   * then a value placeholder must also be provided.
   *
   * @param  needsValue  Indicates whether a value must be provided with this
   *                     argument if it is present.
 
   */
  public void setNeedsValue(boolean needsValue)
  {
    this.needsValue = needsValue;
  }
 
 
 
  /**
   * Retrieves the value placeholder that will be displayed for this argument in
   * the generated usage information.
   *
   * @return  The value placeholder that will be displayed for this argument in
   *          the generated usage information, or <CODE>null</CODE> if there is
   *          none.
   */
  public Message getValuePlaceholder()
  {
    return valuePlaceholder;
  }
 
 
 
  /**
   * Specifies the value placeholder that will be displayed for this argument in
   * the generated usage information.  It may be <CODE>null</CODE> only if
   * <CODE>needsValue()</CODE> returns <CODE>false</CODE>.
   *
   * @param  valuePlaceholder  The value placeholder that will be displayed for
   *                           this argument in the generated usage information.
   */
  public void setValuePlaceholder(Message valuePlaceholder)
  {
    this.valuePlaceholder = valuePlaceholder;
  }
 
 
 
  /**
   * Retrieves the default value that will be used for this argument if it is
   * not specified on the command line and it is not set from a properties file.
   *
   * @return  The default value that will be used for this argument if it is not
   *          specified on the command line and it is not set from a properties
   *          file, or <CODE>null</CODE> if there is no default value.
   */
  public String getDefaultValue()
  {
    return defaultValue;
  }
 
 
 
  /**
   * Specifies the default value that will be used for this argument if it is
   * not specified on the command line and it is not set from a properties file.
   *
   * @param  defaultValue  The default value that will be used for this argument
   *                       if it is not specified on the command line and it is
   *                       not set from a properties file.
   */
  public void setDefaultValue(String defaultValue)
  {
    this.defaultValue = defaultValue;
  }
 
 
 
  /**
   * Retrieves the name of a property in a properties file that may be used to
   * set the default value for this argument if it is present.  A value read
   * from a properties file will override the default value returned from the
   * <CODE>getDefaultValue</CODE>, but the properties file value will be
   * overridden by a value supplied on the command line.
   *
   * @return  The name of a property in a properties file that may be used to
   *          set the default value for this argument if it is present.
   */
  public String getPropertyName()
  {
    return propertyName;
  }
 
  /**
   * Specifies the name of a property in a properties file that may be used to
   * set the default value for this argument if it is present.
   *
   * @param  propertyName  The name of a property in a properties file that may
   *                       be used to set the default value for this argument if
   *                       it is present.
   */
  public void setPropertyName(String propertyName)
  {
    this.propertyName = propertyName;
  }
 
  /**
   * Indicates whether this argument was provided in the set of
   * properties found is a properties file.
   *
   * @return <CODE>true</CODE> if this argument was provided in the
   *         set of properties found is a properties file, or
   *         <CODE>false</CODE> if not.
   */
  public boolean isValueSetByProperty()
  {
    return isValueSetByProperty;
  }
 
  /**
   * Specifies whether this argument was provided in the set of
   * properties found is a properties file.
   *
   * @param isValueSetByProperty
   *          Specify whether this argument was provided in the set
   *          of properties found is a properties file.
   */
  public void setValueSetByProperty(boolean isValueSetByProperty)
  {
    this.isValueSetByProperty = isValueSetByProperty;
  }
 
  /**
   * Retrieves the human-readable description for this argument.
   *
   * @return  The human-readable description for this argument.
   */
  public Message getDescription()
  {
    return description != null ? description : Message.EMPTY;
  }
 
  /**
   * Indicates whether this argument has at least one value.
   *
   * @return  <CODE>true</CODE> if this argument has at least one value, or
   *          <CODE>false</CODE> if it does not have any values.
   */
  public boolean hasValue()
  {
    return !values.isEmpty();
  }
 
  /**
   * Retrieves the string value for this argument.  If it has multiple values,
   * then the first will be returned.  If it does not have any values, then the
   * default value will be returned.
   *
   * @return  The string value for this argument, or <CODE>null</CODE> if there
   *          are no values and no default value has been given.
   */
  public String getValue()
  {
    if (values.isEmpty())
    {
      return defaultValue;
    }
    return values.getFirst();
  }
 
  /**
   * Retrieves the set of string values for this argument.
   *
   * @return  The set of string values for this argument.
   */
  public LinkedList<String> getValues()
  {
    return values;
  }
 
  /**
   * Retrieves the value of this argument as an integer.
   *
   * @return  The value of this argument as an integer.
   *
   * @throws  ArgumentException  If there are multiple values, or the value
   *                             cannot be parsed as an integer.
   */
  public int getIntValue()
         throws ArgumentException
  {
    if (values.isEmpty())
    {
      Message message = ERR_ARG_NO_INT_VALUE.get(name);
      throw new ArgumentException(message);
    }
 
    Iterator<String> iterator = values.iterator();
    String valueString = iterator.next();
 
    int intValue;
    try
    {
      intValue = Integer.parseInt(valueString);
    }
    catch (Exception e)
    {
      Message message = ERR_ARG_CANNOT_DECODE_AS_INT.get(valueString, name);
      throw new ArgumentException(message, e);
    }
 
    if (iterator.hasNext())
    {
      Message message = ERR_ARG_INT_MULTIPLE_VALUES.get(name);
      throw new ArgumentException(message);
    }
    return intValue;
  }
 
  /**
   * Retrieves the set of values for this argument as a list of integers.
   *
   * @return  A list of the integer representations of the values for this
   *          argument.
   *
   * @throws  ArgumentException  If any of the values cannot be parsed as an
   *                             integer.
   */
  public LinkedList<Integer> getIntValues()
         throws ArgumentException
  {
    LinkedList<Integer> intList = new LinkedList<Integer>();
 
    for (String valueString : values)
    {
      try
      {
        intList.add(Integer.valueOf(valueString));
      }
      catch (Exception e)
      {
        Message message = ERR_ARG_CANNOT_DECODE_AS_INT.get(valueString, name);
        throw new ArgumentException(message, e);
      }
    }
 
    return intList;
  }
 
 
 
  /**
   * Retrieves the value of this argument as a <CODE>Boolean</CODE>.
   *
   * @return  The value of this argument as a <CODE>Boolean</CODE>.
   *
   * @throws  ArgumentException  If this argument cannot be interpreted as a
   *                             Boolean value.
   */
  public boolean getBooleanValue()
         throws ArgumentException
  {
    if (values.isEmpty())
    {
      Message message = ERR_ARG_NO_BOOLEAN_VALUE.get(name);
      throw new ArgumentException(message);
    }
 
    Iterator<String> iterator = values.iterator();
    String valueString = toLowerCase(iterator.next());
 
    boolean booleanValue;
    if ("true".equals(valueString) || "yes".equals(valueString) ||
        "on".equals(valueString) || "1".equals(valueString))
    {
      booleanValue = true;
    }
    else if ("false".equals(valueString) || "no".equals(valueString) ||
             "off".equals(valueString) || "0".equals(valueString))
    {
      booleanValue = false;
    }
    else
    {
      Message message = ERR_ARG_CANNOT_DECODE_AS_BOOLEAN.get(valueString, name);
      throw new ArgumentException(message);
    }
 
    if (iterator.hasNext())
    {
      Message message = ERR_ARG_BOOLEAN_MULTIPLE_VALUES.get(name);
      throw new ArgumentException(message);
    }
    return booleanValue;
  }
 
 
 
  /**
   * Indicates whether the provided value is acceptable for use in this
   * argument.
   *
   * @param  valueString    The value for which to make the determination.
   * @param  invalidReason  A buffer into which the invalid reason may be
   *                        written if the value is not acceptable.
   *
   * @return  <CODE>true</CODE> if the value is acceptable, or
   *          <CODE>false</CODE> if it is not.
   */
  public abstract boolean valueIsAcceptable(String valueString,
                                            MessageBuilder invalidReason);
 
  /**
   * Adds a value to the set of values for this argument.  This should only be
   * called if the value is allowed by the <CODE>valueIsAcceptable</CODE>
   * method.
   *
   * @param  valueString  The string representation of the value to add to this
   *                      argument.
   */
  public void addValue(String valueString)
  {
    values.add(valueString);
  }
 
  /**
   * Clears the set of values assigned to this argument.
   */
  public void clearValues()
  {
    values.clear();
  }
 
  /** {@inheritDoc} */
  @Override
  public String toString()
  {
    final StringBuilder sb = new StringBuilder(getClass().getSimpleName());
    sb.append("[name=\"").append(this.name).append("\"");
    if (shortIdentifier != null || longIdentifier != null)
    {
      sb.append(", options=");
      if (shortIdentifier != null)
      {
        sb.append("-").append(shortIdentifier);
      }
      if (longIdentifier != null)
      {
        if (shortIdentifier != null)
        {
          sb.append(" ");
        }
        sb.append("--").append(longIdentifier);
      }
    }
    sb.append(", ").append(isRequired ? "required" : "optional");
    sb.append(", ").append(isMultiValued ? "multiValued" : "singleValued");
    sb.append(", defaultValue=\"").append(defaultValue)
      .append("\", valueFormat=\"").append(valuePlaceholder)
      .append("\", providedValues=\"").append(values)
      .append("\", description=\"").append(description)
      .append("\"]");
    return sb.toString();
  }
}