src/jdk.jcmd/share/classes/sun/tools/jps/Arguments.java
author sundar
Tue, 20 Feb 2018 15:25:40 +0530
changeset 48917 37beaca49e63
parent 48543 7067fe4e054e
permissions -rw-r--r--
8194922: jlink --exclude-resources should never exclude module-info.class Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
48543
7067fe4e054e 8189102: All tools should support -?, -h and --help
goetz
parents: 47216
diff changeset
     2
 * Copyright (c) 2004, 2018, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.tools.jps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import sun.jvmstat.monitor.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * Class for processing command line arguments and providing method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * level access to the command line arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * @author Brian Doherty
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
public class Arguments {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private static final boolean debug = Boolean.getBoolean("jps.debug");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private static final boolean printStackTrace = Boolean.getBoolean(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
            "jps.printStackTrace");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private boolean help;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private boolean quiet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private boolean longPaths;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private boolean vmArgs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private boolean vmFlags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private boolean mainArgs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private String hostname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private HostIdentifier hostId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    public static void printUsage(PrintStream ps) {
48543
7067fe4e054e 8189102: All tools should support -?, -h and --help
goetz
parents: 47216
diff changeset
    55
      ps.println("usage: jps [--help]");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
      ps.println("       jps [-q] [-mlvV] [<hostid>]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
      ps.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
      ps.println("Definitions:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
      ps.println("    <hostid>:      <hostname>[:<port>]");
48543
7067fe4e054e 8189102: All tools should support -?, -h and --help
goetz
parents: 47216
diff changeset
    60
      ps.println("    -? -h --help -help: Print this help message and exit.");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public Arguments(String[] args) throws IllegalArgumentException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        int argc = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        if (args.length == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            if ((args[0].compareTo("-?") == 0)
48543
7067fe4e054e 8189102: All tools should support -?, -h and --help
goetz
parents: 47216
diff changeset
    68
                || (args[0].compareTo("-h")== 0)
7067fe4e054e 8189102: All tools should support -?, -h and --help
goetz
parents: 47216
diff changeset
    69
                || (args[0].compareTo("--help")== 0)
7067fe4e054e 8189102: All tools should support -?, -h and --help
goetz
parents: 47216
diff changeset
    70
                // -help: legacy.
7067fe4e054e 8189102: All tools should support -?, -h and --help
goetz
parents: 47216
diff changeset
    71
                || (args[0].compareTo("-help")== 0)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
              help = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
              return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        for (argc = 0; (argc < args.length) && (args[argc].startsWith("-"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                argc++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            String arg = args[argc];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            if (arg.compareTo("-q") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
              quiet = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            } else if (arg.startsWith("-")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                for (int j = 1; j < arg.length(); j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                    switch (arg.charAt(j)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                    case 'm':
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                        mainArgs = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                    case 'l':
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                        longPaths = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                    case 'v':
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                        vmArgs = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                    case 'V':
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                        vmFlags = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                    default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                        throw new IllegalArgumentException("illegal argument: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                                                           + args[argc]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                throw new IllegalArgumentException("illegal argument: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                                                   + args[argc]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        switch (args.length - argc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            hostname = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            hostname = args[args.length - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            throw new IllegalArgumentException("invalid argument count");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            hostId = new HostIdentifier(hostname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        } catch (URISyntaxException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            IllegalArgumentException iae =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                    new IllegalArgumentException("Malformed Host Identifier: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                                                 + hostname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            iae.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            throw iae;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    public boolean isDebug() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        return debug;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    public boolean printStackTrace() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        return printStackTrace;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    public boolean isHelp() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        return help;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    public boolean isQuiet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        return quiet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    public boolean showLongPaths() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        return longPaths;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    public boolean showVmArgs() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        return vmArgs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    public boolean showVmFlags() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        return vmFlags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    public boolean showMainArgs() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        return mainArgs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    public String hostname() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        return hostname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    public HostIdentifier hostId() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        return hostId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
}