File: ArgumentParser.java

package info (click to toggle)
openjdk-11-jre-dcevm 11.0.15%2B1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 221,420 kB
  • sloc: java: 1,363,561; cpp: 967,919; ansic: 69,480; xml: 62,475; sh: 8,106; asm: 3,475; python: 1,667; javascript: 943; makefile: 397; sed: 172; perl: 114
file content (507 lines) | stat: -rw-r--r-- 16,951 bytes parent folder | download | duplicates (10)
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
/*
 * Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

package nsk.share;

import java.util.*;

import nsk.share.test.StressOptions;
import nsk.share.test.Stresser;

/**
 * Parser for JDI test's command-line arguments.
 * <p>
 * Test's command line may contain two kind of arguments, namely:
 * <ul>
 * <li> options for ArgumentParser
 * <li> other arguments for the test itself
 * </ul>
 * <p>
 * We call <i>raw arguments</i> the <code>args[]</code> array
 * passed to the test's method <code>main(String&nbsp;args[])</code>.
 * ArgumentParser instance initialized with raw arguments serves to parse
 * these two kinds of arguments. Use <code>ArgumentParser(args[])</code>
 * constructor, or <code>setRawArguments(args[])</code> method
 * to initialize a ArgumentParser instance with particular raw arguments.
 * <p>
 * Arguments, started with ``<code>-</code>'' symbol are called <i>options</i>.
 * They are recognized by ArgumentParser and are used by support classes
 * (such as Log, Binder, etc.).
 * These options should be specified in the following general form:
 * <ul>
 * <li> <code>-option=<i>value</i></code>
 * </ul>
 * or
 * <ul>
 * <li> <code>-option <i>value</i></code>
 * </ul>
 * List of the recognized options with their values may be obtained by
 * invoking method <code>getOptions()</code> that returnes
 * a <code>Properties</code> object with options values.
 * It is not recommended to get options value directly. An appropriate methods
 * such as <code>verbose()</code>, <code>getArch()</code>, etc. should be used
 * instead.
 * Options may appear in the test command line in any order.
 * <p>
 * All the other arguments of command line are called <i>test arguments</i>
 * (or simply <i>arguments</i>). These arguments should be handled by test itself.
 * Full list of the test arguments in the same order as they appears in the command line
 * may be obtained by invoking method <code>getArguments()</code>.
 * <p>
 * Following is the list of basic options accepted by AgrumentParser:
 * <ul>
 * <li> <code>-arch=</code>&lt;<i>${ARCH}</i>&gt; -
 *   architecture name
 * <li> <code>-waittime=</code>&lt;<i>minutes</i>&gt; -
 *   timeout in minutes for waiting events or so
 * <li> <code>-verbose</code> -
 *   verbose Log mode (default is quiet)
 * <li> <code>-trace.time</code> -
 *   prefix log messages with timestamps (default is no)
 * </ul>
 * Also AgrumentParser supports following stress options (see nsk.share.test.StressOptions for details):
 * <ul>
 * <li> <code>-stressTime</code>
 * <li> <code>-stressIterationsFactor</code>
 * <li> <code>-stressThreadsFactor</code>
 * <li> <code>-stressDebug</code>
 * </ul>
 * <p>
 * Note, that the tests from the particular subsuites have its own argument handlers
 * wich accepts additional options. See <code>jpda.DebugeeArgumentHandler</code>,
 * <code>jdi.ArgumentHandler</code>, <code>jdwp.ArgumentHandler</code>.
 *
 * @see #setRawArguments(String[])
 * @see #getRawArguments()
 * @see #getArguments()
 * @see #getOptions()
 *
 * @see nsk.share.jpda.DebugeeArgumentHandler
 * @see nsk.share.jdwp.ArgumentHandler
 * @see nsk.share.jdi.ArgumentHandler
 * @see nsk.share.jvmti.ArgumentHandler
 * @see nsk.monitoring.share.ArgumentHandler
 */
public class ArgumentParser {

    /**
     * Raw array of command-line arguments.
     *
     * @see #setRawArguments(String[])
     * @see #getRawArguments()
     */
    protected String rawArguments[] = null;

    /**
     * Refined arguments -- raw arguments but options.
     *
     * @see #options
     * @see #getArguments()
     */
    protected String arguments[] = null;

    /**
     * Recognized options for ArgumentParser class.
     *
     * @see #arguments
     * @see #getOptions()
     */
    protected Properties options = new Properties();

    /**
     * Make new ArgumentParser object with default values of otions.
     * This constructor is used only to obtain default values of options.
     *
     * @see #setRawArguments(String[])
     */
    protected ArgumentParser() {
        String[] args = new String[0];
        setRawArguments(args);
    }

    /**
     * Keep a copy of raw command-line arguments and parse them;
     * but throw an exception on parsing error.
     *
     * @param  args  Array of the raw command-line arguments.
     *
     * @throws  BadOption  If option values are invalid.
     *
     * @see #setRawArguments(String[])
     * @see BadOption
     */
    public ArgumentParser(String args[]) {
        setRawArguments(args);
    }

    /**
     * Return a copy of the raw command-line arguments kept by
     * this ArgumentParser instance.
     *
     * @throws  NullPointerException  If raw arguments were not
     *                                set for this instance.
     *
     * @see #setRawArguments(String[])
     */
    public String[] getRawArguments() {
        return (String[]) rawArguments.clone();
    }

    /**
     * Return given raw command-line argument.
     *
     * @param index index of argument
     * @return value of raw argument
     */
    public String getRawArgument(int index) {
            return rawArguments[index];
    }

    /**
     * Return refined array of test arguments (only those of the raw
     * arguments which are not recognized as options for ArgumentParser).
     *
     * <p>Note, that sintax of test arguments was not checked;
     * while syntax of arguments describing ArgumentParser's options
     * was checked while raw arguments were set to this ArgumentParser
     * instance.
     *
     * @throws  NullPointerException  If raw arguments were not
     *                                set for this instance.
     *
     * @see #setRawArguments(String[])
     * @see #getOptions()
     */
    public String[] getArguments() {
        return (String[]) arguments.clone();
    }

    /**
     * Return list of recognized otions with their values in the form of
     * <code>Properties</code> object.
     * If no options has been recognized, this list will be empty.
     *
     * @see #setRawArguments(String[])
     * @see #getArguments()
     */
    public Properties getOptions() {
        return (Properties) options.clone();
    }

    /**
     * Join specified arguments into one line using given quoting
     * and separator symbols.
     *
     * @param args Array of the command-line arguments
     * @param quote Symbol used to quote each argument
     * @param separator Symbol used as separator between argumnets
     * @return Single line with arguments
     */
    static public String joinArguments(String args[], String quote, String separator) {
        if (args.length <= 0) {
            return "";
        }
        String line = quote + args[0] + quote;
        for (int i = 1; i < args.length; i++) {
            line += separator + quote + args[i] + quote;
        }
        return line;
    }

    /**
     * Join specified arguments into one line using given quoting symbol
     * and space as a separator symbol.
     *
     * @param args Array of the command-line arguments
     * @param quote Symbol used to quote each argument
     * @return Single line with arguments
     */
    static public String joinArguments(String args[], String quote) {
        return joinArguments(args, quote, " ");
    }

    /**
     * Keep a copy of command-line arguments and parse them;
     * but throw an exception on parsing error.
     *
     * @param  args  Array of the raw command-line arguments.
     *
     * @throws  BadOption  If an option has invalid value.
     *
     * @see #getRawArguments()
     * @see #getArguments()
     */
    public void setRawArguments(String args[]) {
        this.rawArguments = (String[]) args.clone();
        parseArguments();
    }

    /**
     * Add or replace given option value in options list and in raw arguments list.
     * Use specified <code>rawPrefix</code> while adding to raw arguments list.
     *
     * @see #getRawArguments()
     * @see #getOptions()
     */
    public void setOption(String rawPrefix, String name, String value) {
        String prefix = rawPrefix + name + "=";
        String arg = prefix + value;

        options.setProperty(name, value);

        int length = rawArguments.length;
        boolean found = false;
        for (int i = 0; i < length; i++) {
            if (rawArguments[i].startsWith(prefix)) {
                found = true;
                rawArguments[i] = arg;
                break;
            }
        }

        if (!found) {
            String[] newRawArguments = new String[length + 1];
            for (int i = 0; i < length; i++) {
                newRawArguments[i] = rawArguments[i];
            }
            newRawArguments[length] = arg;
            rawArguments = newRawArguments;
        }
    }

    /**
     * Return current architecture name from ArgumentParser's
     * options.
     *
     * <p>Note that null string is returning if test argument
     * <code>-arch</code> has not been set.
     *
     * @see #setRawArguments(String[])
     */
    public String getArch() {
        return options.getProperty("arch");
    }

    /**
     * Timeout (in minutes) for test's critical section like:
     * (a) awaiting for an event, or conversly (b) making sure
     * that there is no unexpected event.
     *
     * <p>By default, <i>2</i> minutes is returned if option
     * <code>-waittime</code> is not set with command line.
     *
     * @see TimeoutHandler
     */
    public int getWaitTime() {
        String val = options.getProperty("waittime", "2");
        int minutes;
        try {
            minutes = Integer.parseInt(val);
        } catch (NumberFormatException e) {
            throw new TestBug("Not integer value of \"waittime\" argument: " + val);
        }
        return minutes;
    }

    /**
     * Return boolean value of current Log mode:
     * <ul>
     * <li><i>true</i> if Log mode is verbose.
     * <li><i>false</i> otherwise.
     *
     * <p>Note that default Log mode is quiet if test argument
     * <code>-verbose</code> has not been set.
     *
     * @see #setRawArguments(String[])
     */
    public boolean verbose() {
        return options.getProperty("verbose") != null;
    }

    /**
     * Return boolean value of setting of timestamp for log messages:
     * <ul>
     * <li><i>true</i> if Log messages are timestamp'ed.
     * <li><i>false</i> otherwise.
     *
     * <p>Note that by default Log messages won't be timestamp'ed until
     * <code>-trace.time</code> has not been set.
     *
     * @see #setRawArguments(String[])
     */
    public boolean isTimestamp() {
        return options.getProperty("trace.time") != null;
    }

    /**
     * Return level of printing tracing mesages for debugging purpose.
     * Level <i>0</i> means no tracing messages at all.
     *
     * <p>Note that by default no tracing messages will be printed out
     * until <code>-trace.level</code> has not been set.
     *
     * @see #setRawArguments(String[])
     */
    public int getTraceLevel() {
        String value = options.getProperty("trace.level", Integer.toString(Log.TraceLevel.DEFAULT));
        try {
            int level = Integer.parseInt(value);
            return level;
        } catch (NumberFormatException e) {
            throw new Failure("Not integer value of -trace.level option: " + value);
        }
    }

    /**
     * Parse arguments from rawArgumnets, extact recognized options,
     * check legality of options values options and store non-option
     * arguments.
     *
     * @throws  NullPointerException  If raw arguments were not set
     *                                for this ArgumentParser instance.
     * @throws  BadOption If Option name is not accepted or
     *                    option has illegal value.
     *
     * @see #setRawArguments(String[])
     * @see #checkOption(String, String)
     * @see #checkOptions()
     */
    protected void parseArguments() {
        String selected[] = new String [rawArguments.length];
        Properties properties = new Properties();
        int count = 0;
        for (int i=0; i<rawArguments.length; i++) {
            String argument = rawArguments[i];
            if (argument.startsWith("-")) {
                int pos = argument.indexOf("=", 1);
                String option, value;
                if (pos < 0) {
                    option = argument.substring(1);
                    if (i + 1 < rawArguments.length && !rawArguments[i + 1].startsWith("-")) {
                        value = rawArguments[i + 1];
                        ++i;
                    } else
                        value = "";
                } else {
                    option = argument.substring(1, pos);
                    value = argument.substring(pos + 1);
                }
                if (!checkOption(option, value)) {
                    throw new BadOption("Unrecognized command line option: " + argument);
                }
                properties.setProperty(option, value);
            } else {
                selected[count++] = rawArguments[i];
            }
        }
        // Strip away the dummy tail of the selected[] array:
        arguments = new String [count];
        System.arraycopy(selected,0,arguments,0,count);
        options = properties;
        checkOptions();
    }

    public StressOptions getStressOptions() {
        return new StressOptions(rawArguments);
    }

    /**
     * Check if the specified option is allowed and has legal value.
     * <p>
     * Derived classes for hadling test arguments in particular sub-suites
     * override this method to allow to accept sub-suite specific options.
     * However, they should invoke this method of the base class to enshure
     * that the basic options will be accepted too.
     *
     * @return <i>true</i> if option is allowed and has legel value
     *         <i>false</I> if option is unknown
     *
     * @throws  BadOption  If value of the allowed option is illegal.
     *
     * @see #setRawArguments(String[])
     * @see #parseArguments()
     */
    protected boolean checkOption(String option, String value) {

        // accept arguments of nsk.share.test.StressOptions
        if (StressOptions.isValidStressOption(option))
            return true;

        // options with any string value
        if (option.equals("arch")) {
            return true;
        }

        // options with positive integer value
        if (option.equals("waittime")
            || option.equals("trace.level")) {
            try {
                int number = Integer.parseInt(value);
                if (number < 0) {
                    throw new BadOption(option + ": value must be a positive integer");
                }
            } catch (NumberFormatException e) {
                throw new BadOption(option + ": value must be an integer");
            }
            return true;
        }

        // options without any value
        if (option.equals("verbose")
            || option.equals("vbs")
            || option.equals("trace.time")) {
            if (!(value == null || value.length() <= 0)) {
                throw new BadOption(option + ": no value must be specified");
            }
            return true;
        }

        return false;
    }

    /**
     * Check that the value of all options are not inconsistent.
     * This method is invoked by <code>parseArguments()</code>
     *
     * @throws  BadOption  If value of the options are inconsistent
     *
     * @see #parseArguments()
     */
    protected void checkOptions() {
        // do nothing
    }

    /**
     * Thrown if invalid option or option value is found.
     */
    public static class BadOption extends IllegalArgumentException {
        /**
         * Explain the reason.
         *
         * @param  message   Printing message.
         */
        public BadOption(String message) {
            super(message);
        }
    }
}