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:47 UTC

[sling-org-apache-sling-jmx-provider] 03/09: SLING-3200 : Avoid duplicated requests to mbeans when creating resources

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

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

commit caba701b5007414fe4b17343fe9399c852f2e214
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Wed Oct 23 08:01:59 2013 +0000

    SLING-3200 : Avoid duplicated requests to mbeans when creating resources
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/contrib/extensions/jmxprovider@1534947 13f79535-47bb-0310-9956-ffa450edef68
---
 .../sling/jmx/provider/impl/AttributeResource.java | 53 ++++++++++++----------
 .../jmx/provider/impl/JMXResourceProvider.java     | 12 +++--
 .../sling/jmx/provider/impl/MapResource.java       | 12 ++++-
 3 files changed, 50 insertions(+), 27 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 2967d01..eabf9d2 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
@@ -28,6 +28,7 @@ import java.util.Iterator;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.NoSuchElementException;
 import java.util.Set;
 import java.util.TreeMap;
 
@@ -188,24 +189,22 @@ public class AttributeResource extends AbstractResource {
                 current = (Map<String, Object>)child;
             }
 
-            return new MapResource(this.getResourceResolver(), this.getPath(), current);
+            return new MapResource(this.getResourceResolver(), this.getPath(), current, this);
         }
         return null;
     }
 
-    private Map<String, Object> convertData() {
-        try {
-            final Object value = attrValue;
+    private volatile Map<String, Object> convertedValue;
 
-            if ( value instanceof TabularData ) {
-                return convertObject((TabularData)value);
-            } else if ( value instanceof CompositeData ) {
-                return convertObject((CompositeData)value);
+    private Map<String, Object> convertData() {
+        if ( convertedValue == null ) {
+            if ( attrValue instanceof TabularData ) {
+                convertedValue = convertObject((TabularData)attrValue);
+            } else if ( attrValue instanceof CompositeData ) {
+                convertedValue = convertObject((CompositeData)attrValue);
             }
-        } catch (final Exception ignore) {
-            // ignore and return null
         }
-        return null;
+        return convertedValue;
     }
 
     private Map<String, Object> convertObject(final TabularData td) {
@@ -292,7 +291,7 @@ public class AttributeResource extends AbstractResource {
         return result;
     }
 
-    public Iterator<Resource> getChildren(String subPath) {
+    public Iterator<Resource> getChildren(final String parentPath, final String subPath) {
         final Map<String, Object> childStructure = this.convertData();
         if ( childStructure != null ) {
             Map<String, Object> current = childStructure;
@@ -309,13 +308,6 @@ public class AttributeResource extends AbstractResource {
                     current = (Map<String, Object>)child;
                 }
             }
-            final Iterator<Map.Entry<String, Object>> removeIter = current.entrySet().iterator();
-            while ( removeIter.hasNext() ) {
-                final Map.Entry<String, Object> c = removeIter.next();
-                if ( !(c.getValue() instanceof Map) ) {
-                    removeIter.remove();
-                }
-            }
             if ( current.size() == 0 ) {
                 return null;
             }
@@ -323,18 +315,33 @@ public class AttributeResource extends AbstractResource {
 
             return new Iterator<Resource>() {
 
+                private Map.Entry<String, Object> next = this.seek();
+
+                private Map.Entry<String, Object> seek() {
+                    while ( childIter.hasNext() ) {
+                        final Map.Entry<String, Object> c = childIter.next();
+                        if ( c.getValue() instanceof Map ) {
+                            return c;
+                        }
+                    }
+                    return null;
+                }
+
                 public void remove() {
                     throw new UnsupportedOperationException("remove");
                 }
 
                 public Resource next() {
-                    final Map.Entry<String, Object> props = childIter.next();
-
-                    return new MapResource(getResourceResolver(), getPath() + '/' + props.getKey(), (Map)props.getValue());
+                    final Map.Entry<String, Object> props = next;
+                    if ( props == null ) {
+                        throw new NoSuchElementException();
+                    }
+                    next = seek();
+                    return new MapResource(getResourceResolver(), parentPath + '/' + props.getKey(), (Map)props.getValue(), AttributeResource.this);
                 }
 
                 public boolean hasNext() {
-                    return childIter.hasNext();
+                    return next != null;
                 }
             };
         }
diff --git a/src/main/java/org/apache/sling/jmx/provider/impl/JMXResourceProvider.java b/src/main/java/org/apache/sling/jmx/provider/impl/JMXResourceProvider.java
index 9d17f3f..2eb7b46 100644
--- a/src/main/java/org/apache/sling/jmx/provider/impl/JMXResourceProvider.java
+++ b/src/main/java/org/apache/sling/jmx/provider/impl/JMXResourceProvider.java
@@ -309,7 +309,7 @@ public class JMXResourceProvider implements ResourceProvider {
                             return new AttributeResource(parent.getResourceResolver(),
                                     parent.getPath() + "/" + attr.getName(),
                                     infoMap.get(attr.getName()),
-                                    attr,
+                                    attr.getValue(),
                                     parentResource);
                         }
 
@@ -318,7 +318,13 @@ public class JMXResourceProvider implements ResourceProvider {
                         }
                     };
                 } else if ( info.pathInfo.startsWith("mbean:attributes/") ) {
-                    final AttributeResource parentResource = (AttributeResource)parent;
+                    final AttributeResource parentResource;
+                    if ( parent instanceof AttributeResource ) {
+                        parentResource = (AttributeResource)parent;
+                    } else {
+                        parentResource = ((MapResource)parent).getAttributeResource();
+                    }
+
                     final String attrPath = info.pathInfo.substring("mbean:attributes/".length());
                     final int pos = attrPath.indexOf('/');
                     final String subPath;
@@ -328,7 +334,7 @@ public class JMXResourceProvider implements ResourceProvider {
                         subPath = attrPath.substring(pos + 1);
                     }
 
-                    return parentResource.getChildren(subPath);
+                    return parentResource.getChildren(parent.getPath(), subPath);
 
                 }
             }
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 bdecaf6..596f4e0 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
@@ -38,7 +38,13 @@ public class MapResource extends AbstractResource {
 
     private final ResourceMetadata metadata = new ResourceMetadata();
 
-    public MapResource(final ResourceResolver resolver, final String path, final Map<String, Object> props) {
+    private final AttributeResource parent;
+
+    public MapResource(final ResourceResolver resolver,
+            final String path,
+            final Map<String, Object> props,
+            final AttributeResource parent) {
+        this.parent = parent;
         this.resolver = resolver;
         this.path = path;
         this.properties = new HashMap<String, Object>();
@@ -49,6 +55,10 @@ public class MapResource extends AbstractResource {
         }
     }
 
+    public AttributeResource getAttributeResource() {
+        return this.parent;
+    }
+
     /**
      * @see org.apache.sling.api.resource.Resource#getPath()
      */

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