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