1 /*
   2  * Copyright (c) 2016, 2018, 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 
  26 package jdk.jfr.internal.cmd;
  27 
  28 import java.nio.file.Files;
  29 import java.nio.file.Path;
  30 import java.util.ArrayList;
  31 import java.util.Collections;
  32 import java.util.Deque;
  33 import java.util.List;
  34 
  35 abstract class Command {
  36 
  37     private final static Command HELP = new HelpCommand();
  38     private final static List<Command> COMMANDS = createCommands();
  39 
  40     static void displayHelp() {
  41         System.out.println("Usage: java " + Execute.class.getName() + " <command> [<options>]");
  42         System.out.println();
  43         displayAvailableCommands();
  44     }
  45 
  46     static void displayAvailableCommands() {
  47         System.out.println("Available commands are:");
  48         System.out.println();
  49         boolean first = true;
  50         for (Command c : Command.COMMANDS) {
  51             if (!first) {
  52                 System.out.println();
  53             }
  54             System.out.println("  " + c.getName() + " " + c.getOptionSyntax());
  55             System.out.println("    " + c.getDescription());
  56             first = false;
  57         }
  58     }
  59 
  60     public static List<Command> getCommands() {
  61         return COMMANDS;
  62     }
  63 
  64     public static Command valueOf(String commandName) {
  65         for (Command command : COMMANDS) {
  66             if (command.getName().equals(commandName)) {
  67                 return command;
  68             }
  69         }
  70         return null;
  71     }
  72 
  73     abstract public String getOptionSyntax();
  74 
  75     abstract public String getName();
  76 
  77     abstract public String getDescription();
  78 
  79     abstract public void displayOptionUsage();
  80 
  81     abstract public void execute(Deque<String> options);
  82 
  83     final protected void userFailed(String message) {
  84         println();
  85         println(message);
  86         displayUsage();
  87         throw new IllegalArgumentException(message);
  88     }
  89 
  90     final protected void ensureMaxArgumentCount(Deque<String> options, int maxCount) {
  91         if (options.size() > maxCount) {
  92             userFailed("Too many arguments");
  93         }
  94     }
  95 
  96     final protected void ensureMinArgumentCount(Deque<String> options, int minCount) {
  97         if (options.size() < minCount) {
  98             userFailed("Too few arguments");
  99         }
 100     }
 101 
 102     final protected void ensureFileExist(Path file) {
 103         if (!Files.exists(file)) {
 104             userFailed("Could not find file " + file);
 105         }
 106     }
 107 
 108     final protected Path ensureFileDoesNotExist(Path file) {
 109         if (Files.exists(file)) {
 110             userFailed("File " + file + " already exists");
 111         }
 112         return file;
 113     }
 114 
 115     final protected void ensureJFRFile(Path path) {
 116         if (!path.toString().endsWith(".jfr")) {
 117             userFailed("Filename must end with .jfr");
 118         }
 119     }
 120 
 121     final protected void displayUsage() {
 122         String javaText = "java " + Execute.class.getName();
 123         println();
 124         println("Usage: " + javaText + " " + getName() + " " + getOptionSyntax());
 125         println();
 126         displayOptionUsage();
 127     }
 128 
 129     final protected void println() {
 130         System.out.println();
 131     }
 132 
 133     final protected void print(String text) {
 134         System.out.print(text);
 135     }
 136 
 137     final protected void println(String text) {
 138         System.out.println(text);
 139     }
 140 
 141     private static List<Command> createCommands() {
 142         List<Command> commands = new ArrayList<>();
 143         commands.add(new PrintCommand());
 144         commands.add(new SummaryCommand());
 145         commands.add(new ReconstructCommand());
 146         commands.add(new SplitCommand());
 147         commands.add(HELP);
 148         return Collections.unmodifiableList(commands);
 149     }
 150 }