You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 09:50:33 UTC

[sling-org-apache-sling-jmx-provider] 08/11: SLING-2999 : JMX Resource Provider - add sorting of tabular data

This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to annotated tag org.apache.sling.jmx.provider-0.5.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-jmx-provider.git

commit ecc0c2079087fc5be5d09c8b9bfb5ac648b47e5e
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Tue Aug 20 06:48:59 2013 +0000

    SLING-2999 : JMX Resource Provider - add sorting of tabular data
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/contrib/extensions/jmxprovider@1515718 13f79535-47bb-0310-9956-ffa450edef68
---
 .../sling/jmx/provider/impl/AttributeResource.java | 77 ++++++++++++++--------
 .../jmx/provider/impl/AttributesResource.java      |  6 +-
 .../apache/sling/jmx/provider/impl/Constants.java  | 48 ++++++++++++++
 .../sling/jmx/provider/impl/MBeanResource.java     | 12 ++--
 .../sling/jmx/provider/impl/MapResource.java       |  4 +-
 .../sling/jmx/provider/impl/RootResource.java      |  6 +-
 6 files changed, 113 insertions(+), 40 deletions(-)

diff --git a/src/main/java/org/apache/sling/jmx/provider/impl/AttributeResource.java b/src/main/java/org/apache/sling/jmx/provider/impl/AttributeResource.java
index 1bf47a7..9e7c849 100644
--- a/src/main/java/org/apache/sling/jmx/provider/impl/AttributeResource.java
+++ b/src/main/java/org/apache/sling/jmx/provider/impl/AttributeResource.java
@@ -19,10 +19,14 @@
 package org.apache.sling.jmx.provider.impl;
 
 import java.lang.reflect.Array;
+import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Collections;
+import java.util.Comparator;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.LinkedHashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import java.util.TreeMap;
@@ -77,7 +81,7 @@ public class AttributeResource extends AbstractResource {
      * @see org.apache.sling.api.resource.Resource#getResourceType()
      */
     public String getResourceType() {
-        return "sling:mbeanattribute";
+        return Constants.TYPE_ATTRIBUTE;
     }
 
     /**
@@ -112,15 +116,15 @@ public class AttributeResource extends AbstractResource {
 
     private Map<String, Object> getPropertiesMap() {
         final Map<String, Object> result = new HashMap<String, Object>();
-        result.put(ResourceResolver.PROPERTY_RESOURCE_TYPE, this.getResourceType());
+        result.put(Constants.PROP_RESOURCE_TYPE, this.getResourceType());
         if ( this.getResourceSuperType() != null ) {
-            result.put("sling:resourceSuperType", this.getResourceSuperType());
+            result.put(Constants.PROP_RESOURCE_SUPER_TYPE, this.getResourceSuperType());
         }
 
         if ( info.getDescription() != null ) {
-            result.put("mbean:description", info.getDescription());
+            result.put(Constants.PROP_DESCRIPTION, info.getDescription());
         }
-        result.put("mbean:type", info.getType());
+        result.put(Constants.PROP_TYPE, info.getType());
 
         try {
             final Object value = server.getAttribute(this.on, info.getName());
@@ -132,13 +136,13 @@ public class AttributeResource extends AbstractResource {
                         final Object o = Array.get(value, i);
                         values[i] = convert(o);
                     }
-                    result.put("mbean:value", values);
+                    result.put(Constants.PROP_VALUE, values);
                 } else if (value instanceof TabularData) {
                     // Nothing to do, value is child resource
                 } else if (value instanceof CompositeData) {
                     // Nothing to do, value is child resource
                 } else {
-                    result.put("mbean:value", convert(value));
+                    result.put(Constants.PROP_VALUE, convert(value));
                 }
             }
         } catch (final Exception ignore) {
@@ -202,17 +206,38 @@ public class AttributeResource extends AbstractResource {
     private Map<String, Object> convertObject(final TabularData td) {
         final TabularType type = td.getTabularType();
         final Map<String, Object> result = new HashMap<String, Object>();
-        result.put("sling:resourceSuperType", "mbean:attributes");
-        result.put("sling:resourceType", type.getTypeName());
+        result.put(Constants.PROP_RESOURCE_SUPER_TYPE, Constants.TYPE_ATTRIBUTES);
+        result.put(Constants.PROP_RESOURCE_TYPE, type.getTypeName());
 
         final Map<String, Map<String, Object>> rows = new LinkedHashMap<String, Map<String, Object>>();
-        int index = 1;
-        // TODO - use index values
-        for(final CompositeData data : (Collection<CompositeData>)td.values()) {
-            rows.put(String.valueOf(index), convertObject(data));
-            index++;
+        int rowIndex = 1;
+        @SuppressWarnings("unchecked")
+        final List<CompositeData> values = new ArrayList<CompositeData>((Collection<CompositeData>)td.values());
+        Collections.sort(values, new Comparator<CompositeData>() {
+
+            public int compare(final CompositeData o1, final CompositeData o2) {
+                for(final String name : type.getIndexNames()) {
+                    final Object value1 = o1.get(name);
+                    final Object value2 = o2.get(name);
+                    final int result;
+                    if ( value1 instanceof Comparable ) {
+                        result = ((Comparable)value1).compareTo(value2);
+                    } else {
+                        result = value1.toString().compareTo(value2.toString());
+                    }
+                    if ( result != 0 ) {
+                        return result;
+                    }
+                }
+                return 0;
+            }
+
+        });
+        for(final CompositeData data : values) {
+            rows.put(String.valueOf(rowIndex), convertObject(data));
+            rowIndex++;
         }
-        result.put("mbean:value", rows);
+        result.put(Constants.RSRC_VALUE, rows);
 
         return result;
     }
@@ -220,24 +245,24 @@ public class AttributeResource extends AbstractResource {
     private Map<String, Object> convertObject(final CompositeData cd) {
         final CompositeType type = cd.getCompositeType();
         final Map<String, Object> result = new HashMap<String, Object>();
-        result.put("sling:resourceSuperType", "mbean:attributes");
-        result.put("sling:resourceType", type.getTypeName());
+        result.put(Constants.PROP_RESOURCE_SUPER_TYPE, Constants.TYPE_ATTRIBUTES);
+        result.put(Constants.PROP_RESOURCE_TYPE, type.getTypeName());
 
         final Map<String, Object> attrMap = new TreeMap<String, Object>();
-        attrMap.put("sling:resourceType", "mbean:attributes");
-        result.put("mbean:attributes", attrMap);
+        attrMap.put(Constants.PROP_RESOURCE_TYPE, Constants.TYPE_ATTRIBUTES);
+        result.put(Constants.RSRC_ATTRIBUTES, attrMap);
 
         final Set<String> names = type.keySet();
         for(final String name : names) {
             final Map<String, Object> dataMap = new HashMap<String, Object>();
             attrMap.put(name, dataMap);
             dataMap.put(ResourceResolver.PROPERTY_RESOURCE_TYPE, type.getType(name));
-            dataMap.put("sling:resourceSuperType", "mbean:attributes");
+            dataMap.put(Constants.PROP_RESOURCE_SUPER_TYPE, Constants.TYPE_ATTRIBUTE);
 
             if ( type.getDescription() != null ) {
-                dataMap.put("mbean:description", type.getDescription());
+                dataMap.put(Constants.PROP_DESCRIPTION, type.getDescription());
             }
-            dataMap.put("mbean:type", type.getType(name));
+            dataMap.put(Constants.PROP_TYPE, type.getType(name).getTypeName());
 
             final Object value = cd.get(name);
             if ( value != null ) {
@@ -248,13 +273,13 @@ public class AttributeResource extends AbstractResource {
                         final Object o = Array.get(value, i);
                         values[i] = convert(o);
                     }
-                    dataMap.put("mbean:value", values);
+                    dataMap.put(Constants.PROP_VALUE, values);
                 } else if (value instanceof TabularData) {
-                    dataMap.put("mbean:value", convertObject((TabularData)value));
+                    dataMap.put(Constants.RSRC_VALUE, convertObject((TabularData)value));
                 } else if (value instanceof CompositeData) {
-                    dataMap.put("mbean:value", convertObject((CompositeData)value));
+                    dataMap.put(Constants.RSRC_VALUE, convertObject((CompositeData)value));
                 } else {
-                    dataMap.put("mbean:value", convert(value));
+                    dataMap.put(Constants.PROP_VALUE, convert(value));
                 }
             }
         }
diff --git a/src/main/java/org/apache/sling/jmx/provider/impl/AttributesResource.java b/src/main/java/org/apache/sling/jmx/provider/impl/AttributesResource.java
index 4214569..fbfb1e5 100644
--- a/src/main/java/org/apache/sling/jmx/provider/impl/AttributesResource.java
+++ b/src/main/java/org/apache/sling/jmx/provider/impl/AttributesResource.java
@@ -52,7 +52,7 @@ public class AttributesResource extends AbstractResource {
      * @see org.apache.sling.api.resource.Resource#getResourceType()
      */
     public String getResourceType() {
-        return "sling:mbeanattributes";
+        return Constants.TYPE_ATTRIBUTES;
     }
 
     /**
@@ -87,9 +87,9 @@ public class AttributesResource extends AbstractResource {
 
     private Map<String, Object> getPropertiesMap() {
         final Map<String, Object> result = new HashMap<String, Object>();
-        result.put(ResourceResolver.PROPERTY_RESOURCE_TYPE, this.getResourceType());
+        result.put(Constants.PROP_RESOURCE_TYPE, this.getResourceType());
         if ( this.getResourceSuperType() != null ) {
-            result.put("sling:resourceSuperType", this.getResourceSuperType());
+            result.put(Constants.PROP_RESOURCE_SUPER_TYPE, this.getResourceSuperType());
         }
 
         return result;
diff --git a/src/main/java/org/apache/sling/jmx/provider/impl/Constants.java b/src/main/java/org/apache/sling/jmx/provider/impl/Constants.java
new file mode 100644
index 0000000..8bccecb
--- /dev/null
+++ b/src/main/java/org/apache/sling/jmx/provider/impl/Constants.java
@@ -0,0 +1,48 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.jmx.provider.impl;
+
+public abstract class Constants {
+
+    public static final String RSRC_ATTRIBUTES = "mbean:attributes";
+
+    public static final String RSRC_VALUE = "mbean:value";
+
+    public static final String PROP_VALUE = "mbean:value";
+
+    public static final String PROP_RESOURCE_TYPE = "sling:resourceType";
+
+    public static final String PROP_RESOURCE_SUPER_TYPE = "sling:resourceType";
+
+    public static final String PROP_TYPE = "mbean:type";
+
+    public static final String PROP_DESCRIPTION = "mbean:description";
+
+    public static final String PROP_CLASSNAME = "mbean:className";
+
+    public static final String PROP_OBJECTNAME = "mbean:objectName";
+
+    public static final String TYPE_ATTRIBUTES = "sling:mbeanattributes";
+
+    public static final String TYPE_ATTRIBUTE = "sling:mbeanattribute";
+
+    public static final String TYPE_MBEAN = "sling:mbean";
+
+    public static final String TYPE_MBEANS = "sling:mbeans";
+}
diff --git a/src/main/java/org/apache/sling/jmx/provider/impl/MBeanResource.java b/src/main/java/org/apache/sling/jmx/provider/impl/MBeanResource.java
index dc64816..1c778d9 100644
--- a/src/main/java/org/apache/sling/jmx/provider/impl/MBeanResource.java
+++ b/src/main/java/org/apache/sling/jmx/provider/impl/MBeanResource.java
@@ -79,7 +79,7 @@ public class MBeanResource extends AbstractResource {
      * @see org.apache.sling.api.resource.Resource#getResourceSuperType()
      */
     public String getResourceSuperType() {
-        return "sling:mbean";
+        return Constants.TYPE_MBEAN;
     }
 
     /**
@@ -107,14 +107,14 @@ public class MBeanResource extends AbstractResource {
 
     private Map<String, Object> getPropertiesMap() {
         final Map<String, Object> result = new HashMap<String, Object>();
-        result.put(ResourceResolver.PROPERTY_RESOURCE_TYPE, this.getResourceType());
-        result.put("sling:resourceSuperType", this.getResourceSuperType());
+        result.put(Constants.PROP_RESOURCE_TYPE, this.getResourceType());
+        result.put(Constants.PROP_RESOURCE_SUPER_TYPE, this.getResourceSuperType());
 
         if ( this.info.getDescription() != null ) {
-            result.put("mbean:description", this.info.getDescription());
+            result.put(Constants.PROP_DESCRIPTION, this.info.getDescription());
         }
-        result.put("mbean:className", this.info.getClassName());
-        result.put("mbean:objectName", this.objectName.getCanonicalName());
+        result.put(Constants.PROP_CLASSNAME, this.info.getClassName());
+        result.put(Constants.PROP_OBJECTNAME, this.objectName.getCanonicalName());
 
         return result;
     }
diff --git a/src/main/java/org/apache/sling/jmx/provider/impl/MapResource.java b/src/main/java/org/apache/sling/jmx/provider/impl/MapResource.java
index 920c366..bdecaf6 100644
--- a/src/main/java/org/apache/sling/jmx/provider/impl/MapResource.java
+++ b/src/main/java/org/apache/sling/jmx/provider/impl/MapResource.java
@@ -60,14 +60,14 @@ public class MapResource extends AbstractResource {
      * @see org.apache.sling.api.resource.Resource#getResourceType()
      */
     public String getResourceType() {
-        return (String)properties.get("sling:resourceType");
+        return (String)properties.get(Constants.PROP_RESOURCE_TYPE);
     }
 
     /**
      * @see org.apache.sling.api.resource.Resource#getResourceSuperType()
      */
     public String getResourceSuperType() {
-        return (String)properties.get("sling:resourceSuperType");
+        return (String)properties.get(Constants.PROP_RESOURCE_SUPER_TYPE);
     }
 
     /**
diff --git a/src/main/java/org/apache/sling/jmx/provider/impl/RootResource.java b/src/main/java/org/apache/sling/jmx/provider/impl/RootResource.java
index 7d1ee38..34fd064 100644
--- a/src/main/java/org/apache/sling/jmx/provider/impl/RootResource.java
+++ b/src/main/java/org/apache/sling/jmx/provider/impl/RootResource.java
@@ -52,7 +52,7 @@ public class RootResource extends AbstractResource {
      * @see org.apache.sling.api.resource.Resource#getResourceType()
      */
     public String getResourceType() {
-        return "sling:mbeans";
+        return Constants.TYPE_MBEANS;
     }
 
     /**
@@ -87,9 +87,9 @@ public class RootResource extends AbstractResource {
 
     private Map<String, Object> getPropertiesMap() {
         final Map<String, Object> result = new HashMap<String, Object>();
-        result.put(ResourceResolver.PROPERTY_RESOURCE_TYPE, this.getResourceType());
+        result.put(Constants.PROP_RESOURCE_TYPE, this.getResourceType());
         if ( this.getResourceSuperType() != null ) {
-            result.put("sling:resourceSuperType", this.getResourceSuperType());
+            result.put(Constants.PROP_RESOURCE_SUPER_TYPE, this.getResourceSuperType());
         }
 
         return result;

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.