public static void main(String[] args) throws Exception {

    long timeBeforeInMillis = System.currentTimeMillis();

    JmxInvokerArguments arguments = new JmxInvokerArguments();
    CmdLineParser parser = new CmdLineParser(arguments);
    try {
      parser.parseArgument(args);
      arguments.cmdLineParser = parser;
      if (Strings2.isEmpty(arguments.pid) && arguments.pidFile == null) {
        throw new CmdLineException(parser, "Options --pid and --pid-file can NOT be both null");
      } else if (!Strings2.isEmpty(arguments.pid) && arguments.pidFile != null) {
        throw new CmdLineException(parser, "Options --pid and --pid-file can NOT be both defined");
      } else if ((arguments.attribute == null || arguments.attribute.length == 0)
          && (arguments.operation == null || arguments.operation.length == 0)
          && arguments.listMbeans == false
          && arguments.describeMbeans == false) {
        throw new CmdLineException(
            parser,
            "Option --attribute or --operation or --list-mbeans or --describe-mbeans must be defined");
      } else if ((arguments.attribute != null && arguments.attribute.length > 0)
          && (arguments.operation != null && arguments.operation.length > 0)) {
        throw new CmdLineException(
            parser, "Options --attribute and --operation can NOT be both defined");
      }

      String logLevel;
      if (arguments.superVerbose) {
        logLevel = "TRACE";
      } else if (arguments.verbose) {
        logLevel = "DEBUG";
      } else {
        logLevel = "WARN";
      }
      System.setProperty(SimpleLogger.DEFAULT_LOG_LEVEL_KEY, logLevel);

      Map<ObjectName, Result> results = new JmxInvoker().process(arguments);
      for (Map.Entry<ObjectName, Result> entry : results.entrySet()) {
        System.out.println(entry.getValue().description);
      }
    } catch (CmdLineException e) {
      System.err.println("INVALID INVOCATION: " + e.getMessage());
      System.err.println("Arguments: " + Strings2.join(args, " "));
      System.err.println("Usage:");
      parser.printUsage(System.err);
      throw e;
    } catch (Exception e) {
      System.err.println("INVALID INVOCATION: " + e.getMessage());
      System.err.println("Arguments: " + Strings2.join(args, " "));
      e.printStackTrace();
      throw e;
    } finally {
      if (arguments.verbose || arguments.superVerbose) {
        System.out.println("Duration: " + (System.currentTimeMillis() - timeBeforeInMillis) + "ms");
      }
    }
  }
Esempio n. 2
0
  private static void checkMap() throws Exception {
    // Add new system properties
    System.setProperty(KEY1, VALUE1);
    System.setProperty(KEY2, VALUE2);

    TabularData props1 = (TabularData) server.getAttribute(runtime, "SystemProperties");

    String value1 = getProperty(props1, KEY1);
    if (value1 == null || !value1.equals(VALUE1)) {
      throw new RuntimeException(
          "TEST FAILED: "
              + KEY1
              + " property found"
              + " with value = "
              + value1
              + " but expected to be "
              + VALUE1);
    }

    String value2 = getProperty(props1, KEY2);
    if (value2 == null || !value2.equals(VALUE2)) {
      throw new RuntimeException(
          "TEST FAILED: "
              + KEY2
              + " property found"
              + " with value = "
              + value2
              + " but expected to be "
              + VALUE2);
    }

    String value3 = getProperty(props1, KEY3);
    if (value3 != null) {
      throw new RuntimeException(
          "TEST FAILED: " + KEY3 + " property found" + " but should not exist");
    }
  }
 private static void setSystemPropertyIfNotDefined(String systemPropertyName, String value) {
   if (!System.getProperties().contains(systemPropertyName))
     System.setProperty(systemPropertyName, value);
 }