//"metrics:jmx:url=localhost\:4711:type=memorypool-usage	jmx:url=localhost\:4711:type=memory-usage	jmx:url=localhost\:4711:type=class-count	jmx:url=localhost\:4711:type=gc-time	jmx:url=localhost\:4711:type=compile-time"
package kg.apc.perfmon.metrics.jmx;

import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import javax.management.MBeanServerConnection;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import kg.apc.perfmon.metrics.MetricParams;

/**
 *
 * @author undera
 */
abstract public class AbstractJMXDataProvider {

    protected final Set beans;
    private final boolean isDiff;
    private long prevValue = 0;

    public AbstractJMXDataProvider(MBeanServerConnection mBeanServerConn, boolean diff) throws Exception {
        isDiff = diff;
        beans = getMXBeans(mBeanServerConn);
    }

    public static AbstractJMXDataProvider getProvider(MBeanServerConnection mBeanServerConn, String type) throws Exception {
        if (type.startsWith("type=")) {
            String sType = MetricParams.getParamValue(type);
            if (sType.startsWith("gc-")) {
                return new GCDataProvider(mBeanServerConn, true);
            } else if (sType.startsWith("class-")) {
                return new ClassesDataProvider(mBeanServerConn, false);
            } else if (sType.startsWith("compile-")) {
                return new CompilerDataProvider(mBeanServerConn, true);
            } else if (sType.startsWith("memorypool-")) {
                return new MemoryPoolDataProvider(mBeanServerConn, false);
            } else if (sType.startsWith("memory-")) {
                return new MemoryDataProvider(mBeanServerConn, false);
            }
        }
        throw new IllegalArgumentException("Can't define JMX type");
    }

    private Set getMXBeans(MBeanServerConnection mBeanServerConn) throws MalformedObjectNameException, NullPointerException, IOException {
        ObjectName gcAllObjectName = new ObjectName(getMXBeanType() + ",*");
        Set gcMXBeanObjectNames = mBeanServerConn.queryNames(gcAllObjectName, null);
        Iterator it = gcMXBeanObjectNames.iterator();
        Set res = new HashSet();
        while (it.hasNext()) {
            ObjectName on = (ObjectName) it.next();
            Object mxBean = ManagementFactory.newPlatformMXBeanProxy(mBeanServerConn, on.getCanonicalName(), getMXBeanClass());
            res.add(mxBean);
        }
        return res;
    }

    abstract protected String getMXBeanType();

    abstract protected Class getMXBeanClass();

    abstract protected long getValueFromBean(Object bean);

    public void getValue(StringBuffer res) {
        Iterator it = beans.iterator();
        long value = 0;
        while (it.hasNext()) {
            value += getValueFromBean(it.next());
        }

        if (isDiff) {
            if (prevValue == 0) {
                prevValue = value;
                value = 0;
            } else {
                long oldVal = value;
                value -= prevValue;
                prevValue = oldVal;
            }
        }

        res.append(Long.toString(value));
    }
}

@@ -1,224 +1,4 @@
-//%22metrics:jmx:url=localhost%5C:4711:type=memorypool-usage%09jmx:url=localhost%5C:4711:type=memory-usage%09jmx:url=localhost%5C:4711:type=class-count%09jmx:url=localhost%5C:4711:type=gc-time%09jmx:url=localhost%5C:4711:type=compile-time%22%0A
 pack
@@ -878,114 +878,8 @@
 th(%22
-type=%22)) %7B%0A            String sType = MetricParams.getParamValue(type);%0A            if (sType.startsWith(%22
 gc-%22
@@ -879,28 +879,24 @@
 h(%22gc-%22)) %7B%0A
-    
             
@@ -945,36 +945,32 @@
  true);%0A        
-    
 %7D else if (sType
@@ -956,34 +956,33 @@
      %7D else if (
-sT
+t
 ype.startsWith(%22
@@ -989,28 +989,24 @@
 class-%22)) %7B%0A
-    
             
@@ -1061,36 +1061,32 @@
 false);%0A        
-    
 %7D else if (sType
@@ -1072,34 +1072,33 @@
      %7D else if (
-sT
+t
 ype.startsWith(%22
@@ -1107,28 +1107,24 @@
 mpile-%22)) %7B%0A
-    
             
@@ -1179,36 +1179,32 @@
  true);%0A        
-    
 %7D else if (sType
@@ -1190,34 +1190,33 @@
      %7D else if (
-sT
+t
 ype.startsWith(%22
@@ -1228,28 +1228,24 @@
 ypool-%22)) %7B%0A
-    
             
@@ -1295,36 +1295,32 @@
 erConn, false);%0A
-    
         %7D else i
@@ -1326,10 +1326,9 @@
 if (
-sT
+t
 ype.
@@ -1348,28 +1348,24 @@
 emory-%22)) %7B%0A
-    
             
@@ -1419,30 +1419,16 @@
 false);%0A
-            %7D%0A
         

correct type param processing