Exemplo n.º 1
0
 private void deploy() {
   List startList = new ArrayList();
   Iterator iter = dirMap.entrySet().iterator();
   try {
     while (iter.hasNext() && !shutdown) {
       Map.Entry entry = (Map.Entry) iter.next();
       File f = (File) entry.getKey();
       QEntry qentry = (QEntry) entry.getValue();
       long deployed = qentry.getDeployed();
       if (deployed == 0) {
         if (deploy(f)) {
           if (qentry.isQBean()) startList.add(qentry.getInstance());
           qentry.setDeployed(f.lastModified());
         } else {
           // deploy failed, clean up.
           iter.remove();
         }
       } else if (deployed != f.lastModified()) {
         undeploy(f);
         iter.remove();
         loader.forceNewClassLoaderOnNextScan();
       }
     }
     iter = startList.iterator();
     while (iter.hasNext()) {
       start((ObjectInstance) iter.next());
     }
   } catch (Exception e) {
     log.error("deploy", e);
   }
 }
  private static void checkGarbageCollectionNotificationInfoContent(
      GarbageCollectionNotificationInfo notif) throws Exception {
    System.out.println("GC notification for " + notif.getGcName());
    System.out.print("Action: " + notif.getGcAction());
    System.out.println(" Cause: " + notif.getGcCause());
    GcInfo info = notif.getGcInfo();
    System.out.print("GC Info #" + info.getId());
    System.out.print(" start:" + info.getStartTime());
    System.out.print(" end:" + info.getEndTime());
    System.out.println(" (" + info.getDuration() + "ms)");
    Map<String, MemoryUsage> usage = info.getMemoryUsageBeforeGc();

    List<String> pnames = new ArrayList<String>();
    for (Map.Entry entry : usage.entrySet()) {
      String poolname = (String) entry.getKey();
      pnames.add(poolname);
      MemoryUsage busage = (MemoryUsage) entry.getValue();
      MemoryUsage ausage = (MemoryUsage) info.getMemoryUsageAfterGc().get(poolname);
      if (ausage == null) {
        throw new RuntimeException("After Gc Memory does not exist" + " for " + poolname);
      }
      System.out.println("Usage for pool " + poolname);
      System.out.println("   Before GC: " + busage);
      System.out.println("   After GC: " + ausage);
    }

    // check if memory usage for all memory pools are returned
    List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
    for (MemoryPoolMXBean p : pools) {
      if (!pnames.contains(p.getName())) {
        throw new RuntimeException(
            "GcInfo does not contain " + "memory usage for pool " + p.getName());
      }
    }
  }
  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");
      }
    }
  }
Exemplo n.º 4
0
  private void undeploy() {
    Object[] set = dirMap.entrySet().toArray();
    int l = set.length;

    while (l-- > 0) {
      Map.Entry entry = (Map.Entry) set[l];
      File f = (File) entry.getKey();
      undeploy(f);
    }
  }
Exemplo n.º 5
0
 private void checkModified() {
   Iterator iter = dirMap.entrySet().iterator();
   while (iter.hasNext()) {
     Map.Entry entry = (Map.Entry) iter.next();
     File f = (File) entry.getKey();
     QEntry qentry = (QEntry) entry.getValue();
     if (qentry.isQBean() && qentry.isQPersist()) {
       ObjectName name = qentry.getObjectName();
       if (getState(name) == QBean.STARTED && isModified(name)) {
         qentry.setDeployed(persist(f, name));
       }
     }
   }
 }
Exemplo n.º 6
0
  @Nullable
  public String getMapping() {
    final StringBuilder sb = new StringBuilder();

    for (Map.Entry<Pattern, ScopeMapping> patternAndMapping : _patternToMapping.entrySet()) {
      if (patternAndMapping.getKey() != null) {
        if (sb.length() > 0) {
          sb.append(",\n");
        }
        sb.append(patternAndMapping.getKey())
            .append('>')
            .append(patternAndMapping.getValue().getDefaultName());
      }
    }

    return sb.length() > 0 ? sb.toString() : null;
  }
Exemplo n.º 7
0
 @Nullable
 protected ScopeMapping getMappingFor(@Nonnull String path) {
   ScopeMapping mapping;
   synchronized (_pathToMappingCache) {
     mapping = _pathToMappingCache.get(path);
   }
   if (mapping == null) {
     for (Map.Entry<Pattern, ScopeMapping> patternAndScope : _patternToMapping.entrySet()) {
       final Pattern pattern = patternAndScope.getKey();
       if (pattern != null && pattern.matcher(path).matches()) {
         mapping = patternAndScope.getValue();
         break;
       }
     }
     synchronized (_pathToMappingCache) {
       _pathToMappingCache.put(path, mapping);
     }
   }
   return mapping;
 }
 /**
  * Quickie init test of the static initializer and print out of asynch response mappings
  *
  * @param args none
  */
 public static void main(String[] args) {
   SimpleLogger.info("Initialized");
   for (Map.Entry<Byte, Method> entry : keyToMethod.entrySet()) {
     Method am = keyToAsynchMethod.get(entry.getKey());
     SimpleLogger.info("\t", entry.getValue().getName(), " --> ", am.getName());
   }
   StringBuilder b = new StringBuilder("Mappings");
   for (Map.Entry<Byte, Method> entry : keyToMethod.entrySet()) {
     b.append("\n\t[")
         .append(entry.getKey())
         .append("]")
         .append("   --")
         .append(entry.getValue().getName())
         .append("--   ")
         .append("[")
         .append(methodToKey.get(entry.getValue()))
         .append("]");
   }
   SimpleLogger.info(b);
 }
  public Map<ObjectName, Result> process(JmxInvokerArguments arguments) throws IOException {

    String pid =
        (arguments.pid == null) ? Files2.readFile(arguments.pidFile, "US-ASCII") : arguments.pid;
    pid = pid.replace("\n", "").trim();

    ObjectName on = arguments.objectName;

    String[] op = arguments.operation;

    String operationName = op == null || op.length == 0 ? null : op[0];
    String[] operationArguments =
        op == null || op.length < 2 ? new String[0] : Arrays.copyOfRange(op, 1, op.length);

    String[] attr = arguments.attribute;
    String attributeName = attr == null || attr.length == 0 ? null : attr[0];
    String attributeValue = attr == null || attr.length < 2 ? null : attr[1];

    MBeanServerConnection mbeanServer = connectToMbeanServer(pid);

    Map<ObjectName, Result> results = new TreeMap<ObjectName, Result>();

    Set<ObjectName> objectNames = mbeanServer.queryNames(on, null);
    if (objectNames.isEmpty()) {
      logger.warn("No mbean found for ObjectName {}", on);
    }

    for (ObjectName objectName : objectNames) {
      Result result;
      try {
        if (operationName != null) {
          result = invokeOperation(mbeanServer, objectName, operationName, operationArguments);
        } else if (attributeName != null) {
          result = invokeAttribute(mbeanServer, objectName, attributeName, attributeValue);
        } else if (arguments.describeMbeans) {
          result = describeMbean(mbeanServer, objectName);
        } else if (arguments.listMbeans) {
          result = new Result(objectName, objectName.toString(), objectName.toString());
        } else {
          throw new CmdLineException(
              arguments.cmdLineParser, "NO SEARCH_MBEANS OR OPERATION OR ATTRIBUTE DEFINED");
        }
      } catch (Exception e) {
        StringWriter sw = new StringWriter();
        e.printStackTrace(new PrintWriter(sw));
        String msg = "## EXCEPTION ##\n" + sw.toString();
        result = new Result(objectName, msg, msg);
      }

      results.put(objectName, result);
    }

    logger.info("INVOCATION RESULT");
    logger.info("#################");
    logger.info("JVM pid: {}", pid);
    logger.info("Searched object-name: {}", on);
    if (operationName != null) {
      logger.info("Invoke operation {}{}", operationName, Arrays.asList(operationArguments));
    } else if (attributeValue == null) {
      logger.info("Get attribute {}", attributeName);
    } else {
      logger.info("Set attribute {}: {}", attributeName, attributeValue);
    }
    for (Map.Entry<ObjectName, Result> entry : results.entrySet()) {
      logger.info("{}", entry.getKey());
      logger.info("\t{}", entry.getValue());
    }

    return results;
  }