package de.is24.util.monitoring;

import org.apache.log4j.Logger;
import javax.management.MBeanInfo;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import java.lang.management.ManagementFactory;
import java.util.Set;


public class JMXTestHelper {
  private static final Logger LOGGER = Logger.getLogger(JMXTestHelper.class);
  private static final MBeanServer platformMBeanServer = ManagementFactory.getPlatformMBeanServer();


  public static boolean checkInApplicationMonitorJMXBeanRegistered() {
    return checkInApplicationMonitorJMXBeanRegistered("is24");
  }


  public static boolean checkInApplicationMonitorJMXBeanRegistered(String domain) {
    try {
      ObjectName inAppJMXName = new ObjectName(domain + ":name=InApplicationMonitor");

      Set<ObjectName> objectNames = platformMBeanServer.queryNames(inAppJMXName, null);
      LOGGER.info("checkInApplicationMonitorJMXBeanRegistered found " + objectNames.size() + " items");
      return objectNames.size() == 1;
    } catch (Exception e) {
      LOGGER.warn("oops", e);
      throw new RuntimeException(e);
    }

  }

  public static MBeanInfo getInApplicationMonitorMBeanInfo() {
    try {
      ObjectName inAppJMXName = new ObjectName("is24:name=InApplicationMonitor");

      MBeanInfo info = platformMBeanServer.getMBeanInfo(inAppJMXName);
      return info;

    } catch (Exception e) {
      LOGGER.warn("oops", e);
      throw new RuntimeException(e);
    }

  }

  public static Long getTimerValue(String timerName, String attribute) {
    try {
      ObjectName objectName = new ObjectName("is24:type=InApplicationMonitor,name=" + timerName);

      Long result = (Long) platformMBeanServer.getAttribute(objectName, attribute);
      return result;

    } catch (Exception e) {
      LOGGER.warn("oops", e);
      throw new RuntimeException(e);
    }

  }

  public static Long getCounterValue(String domain, String counterName) {
    try {
      ObjectName objectName = new ObjectName(domain + ":name=InApplicationMonitor");

      Long result = (Long) platformMBeanServer.getAttribute(objectName, counterName);
      return result;

    } catch (Exception e) {
      LOGGER.warn("oops", e);
      throw new RuntimeException(e);
    }

  }


  public static MBeanInfo getTimerMBeanInfo(String timerKey) {
    try {
      ObjectName inAppJMXName = new ObjectName("is24:type=InApplicationMonitor,name=" + timerKey);

      MBeanInfo info = platformMBeanServer.getMBeanInfo(inAppJMXName);
      return info;
    } catch (Exception e) {
      LOGGER.warn("oops", e);
      throw new RuntimeException(e);
    }

  }

}

@@ -55,24 +55,115 @@
 g4j.Logger;%0A
+import javax.management.InstanceNotFoundException;%0Aimport javax.management.MBeanException;%0A
 import javax
@@ -258,16 +258,61 @@
 ctName;%0A
+import javax.management.ReflectionException;%0A
 import j
@@ -2748,10 +2748,326 @@
 %7D%0A%0A  %7D%0A%0A
+%0A  public static Object invoke(ObjectName objectName, Object%5B%5D params, String%5B%5D signature)%0A                       throws InstanceNotFoundException, MBeanException, ReflectionException %7B%0A    Object result = platformMBeanServer.invoke(objectName, %22addStateValuesToGraphite%22, params, signature);%0A    return result;%0A  %7D%0A
 %7D%0A

add object method invocation