incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r1506445 - /sling/trunk/contrib/extensions/healthcheck/hc-core/src/main/java/org/apache/sling/hc/util/RuleDynamicMBean.java
Date Wed, 24 Jul 2013 08:49:53 GMT
Author: bdelacretaz
Date: Wed Jul 24 08:49:53 2013
New Revision: 1506445

URL: http://svn.apache.org/r1506445
Log:
SLING-2977 - return log data as JMX TabularData

Modified:
    sling/trunk/contrib/extensions/healthcheck/hc-core/src/main/java/org/apache/sling/hc/util/RuleDynamicMBean.java

Modified: sling/trunk/contrib/extensions/healthcheck/hc-core/src/main/java/org/apache/sling/hc/util/RuleDynamicMBean.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/healthcheck/hc-core/src/main/java/org/apache/sling/hc/util/RuleDynamicMBean.java?rev=1506445&r1=1506444&r2=1506445&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/healthcheck/hc-core/src/main/java/org/apache/sling/hc/util/RuleDynamicMBean.java
(original)
+++ sling/trunk/contrib/extensions/healthcheck/hc-core/src/main/java/org/apache/sling/hc/util/RuleDynamicMBean.java
Wed Jul 24 08:49:53 2013
@@ -18,8 +18,9 @@
 package org.apache.sling.hc.util;
 
 import java.io.Serializable;
-import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 import javax.management.Attribute;
 import javax.management.AttributeList;
@@ -30,6 +31,15 @@ import javax.management.MBeanAttributeIn
 import javax.management.MBeanException;
 import javax.management.MBeanInfo;
 import javax.management.ReflectionException;
+import javax.management.openmbean.CompositeDataSupport;
+import javax.management.openmbean.CompositeType;
+import javax.management.openmbean.OpenDataException;
+import javax.management.openmbean.OpenMBeanAttributeInfoSupport;
+import javax.management.openmbean.OpenType;
+import javax.management.openmbean.SimpleType;
+import javax.management.openmbean.TabularData;
+import javax.management.openmbean.TabularDataSupport;
+import javax.management.openmbean.TabularType;
 
 import org.apache.sling.hc.api.EvaluationResult;
 import org.apache.sling.hc.api.Rule;
@@ -47,6 +57,31 @@ public class RuleDynamicMBean implements
     public static final String RULE_OK_ATTRIBUTE_NAME = "ok";
     public static final String LOG_ATTRIBUTE_NAME = "log";
     
+    private static CompositeType LOG_ROW_TYPE;
+    private static TabularType LOG_TABLE_TYPE;
+    
+    public static final String INDEX_COLUMN = "index";
+    public static final String LEVEL_COLUMN = "level";
+    public static final String MESSAGE_COLUMN = "message";
+    
+    static {
+        try {
+            // Define the log row and table types
+            LOG_ROW_TYPE = new CompositeType(
+                    "LogLine",
+                    "A line in the Rule log",
+                    new String [] { INDEX_COLUMN, LEVEL_COLUMN, MESSAGE_COLUMN },
+                    new String [] { "log line index", "log level", "log message"},
+                    new OpenType[] { SimpleType.INTEGER, SimpleType.STRING, SimpleType.STRING
}
+                    );
+            final String [] indexes = { INDEX_COLUMN };
+            LOG_TABLE_TYPE = new TabularType("LogTable", "Rule log messages", LOG_ROW_TYPE,
indexes);
+        } catch(Exception ignore) {
+            // row or table type will be null if this happens
+        }
+    }
+
+    
     public RuleDynamicMBean(Rule r) {
         beanName = r.toString();
         rule = r;
@@ -58,7 +93,7 @@ public class RuleDynamicMBean implements
         if(RULE_OK_ATTRIBUTE_NAME.equals(attribute)) {
             return !rule.evaluate().anythingToReport();
         } else if(LOG_ATTRIBUTE_NAME.equals(attribute)) {
-            return logList(rule.evaluate());
+            return logData(rule.evaluate());
         } else {
             final Object o = rule.getInfo().get(attribute);
             if(o == null) {
@@ -68,12 +103,21 @@ public class RuleDynamicMBean implements
         }
     }
     
-    private List<String> logList(EvaluationResult result) {
-        final List<String> list = new ArrayList<String>();
-        for(EvaluationResult.LogMessage msg : result.getLogMessages()) {
-            list.add(msg.getLevel() + " " + msg.getMessage());
+    private TabularData logData(EvaluationResult er) {
+        final TabularDataSupport result = new TabularDataSupport(LOG_TABLE_TYPE);
+        int i=0;
+        for(EvaluationResult.LogMessage msg : er.getLogMessages()) {
+            final Map<String, Object> data = new HashMap<String, Object>();
+            data.put(INDEX_COLUMN, i++);
+            data.put(LEVEL_COLUMN, msg.getLevel().toString());
+            data.put(MESSAGE_COLUMN, msg.getMessage());
+            try {
+                result.put(new CompositeDataSupport(LOG_ROW_TYPE, data));
+            } catch(OpenDataException ode) {
+                throw new IllegalStateException("OpenDataException while creating log data",
ode);
+            }
         }
-        return list;
+        return result;
     }
 
     @Override
@@ -94,7 +138,8 @@ public class RuleDynamicMBean implements
         final MBeanAttributeInfo[] attrs = new MBeanAttributeInfo[rule.getInfo().size() +
2];
         int i=0;
         attrs[i++] = new MBeanAttributeInfo(RULE_OK_ATTRIBUTE_NAME, Boolean.class.getName(),
"The rule value", true, false, false);
-        attrs[i++] = new MBeanAttributeInfo(LOG_ATTRIBUTE_NAME, String[][].class.getName(),
"The rule log", true, false, false);
+        attrs[i++] = new OpenMBeanAttributeInfoSupport(LOG_ATTRIBUTE_NAME, "The rule log",
LOG_TABLE_TYPE, true, false, false);
+        
         for(String key : rule.getInfo().keySet()) {
             attrs[i++] = new MBeanAttributeInfo(key, List.class.getName(), "Description of
" + key, true, false, false);
         }



Mime
View raw message