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 53b4e4e4f42d841fe5c98b04e24aaf8e4b8b79d7 Author: Carsten Ziegeler <[email protected]> AuthorDate: Tue Oct 22 16:17:12 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@1534693 13f79535-47bb-0310-9956-ffa450edef68 --- .../sling/jmx/provider/impl/AttributeResource.java | 31 +++++++----- .../jmx/provider/impl/AttributesResource.java | 10 +++- .../jmx/provider/impl/JMXResourceProvider.java | 58 +++++++++++++++------- .../sling/jmx/provider/impl/MBeanResource.java | 54 +++++++++++--------- 4 files changed, 98 insertions(+), 55 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 9e7c849..2967d01 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 @@ -32,8 +32,6 @@ import java.util.Set; import java.util.TreeMap; import javax.management.MBeanAttributeInfo; -import javax.management.MBeanServer; -import javax.management.ObjectName; import javax.management.openmbean.CompositeData; import javax.management.openmbean.CompositeType; import javax.management.openmbean.TabularData; @@ -56,18 +54,25 @@ public class AttributeResource extends AbstractResource { private final MBeanAttributeInfo info; - private final MBeanServer server; + private final Object attrValue; - private final ObjectName on; + private final AttributesResource parent; - public AttributeResource(final MBeanServer server, - final ObjectName on, - final ResourceResolver resolver, final String p, final MBeanAttributeInfo mai) { + public AttributeResource(final ResourceResolver resolver, + final String path, + final MBeanAttributeInfo info, + final Object value, + final AttributesResource parent) { this.resourceResolver = resolver; - this.path = p; - this.info = mai; - this.on = on; - this.server = server; + this.path = path; + this.info = info; + this.attrValue = value; + this.parent = parent; + } + + @Override + public Resource getParent() { + return this.parent; } /** @@ -127,7 +132,7 @@ public class AttributeResource extends AbstractResource { result.put(Constants.PROP_TYPE, info.getType()); try { - final Object value = server.getAttribute(this.on, info.getName()); + final Object value = attrValue; if ( value != null ) { if ( value.getClass().isArray() ) { final int length = Array.getLength(value); @@ -190,7 +195,7 @@ public class AttributeResource extends AbstractResource { private Map<String, Object> convertData() { try { - final Object value = server.getAttribute(this.on, info.getName()); + final Object value = attrValue; if ( value instanceof TabularData ) { return convertObject((TabularData)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 fbfb1e5..b3d65f6 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 @@ -22,6 +22,7 @@ import java.util.HashMap; import java.util.Map; import org.apache.sling.api.resource.AbstractResource; +import org.apache.sling.api.resource.Resource; import org.apache.sling.api.resource.ResourceMetadata; import org.apache.sling.api.resource.ResourceResolver; import org.apache.sling.api.resource.ValueMap; @@ -35,10 +36,17 @@ public class AttributesResource extends AbstractResource { private final ResourceMetadata metadata = new ResourceMetadata(); - public AttributesResource(final ResourceResolver resolver, final String p) { + private final MBeanResource parent; + + public AttributesResource(final ResourceResolver resolver, final String p, final MBeanResource parent) { this.resourceResolver = resolver; this.path = p; + this.parent = parent; + } + @Override + public Resource getParent() { + return this.parent; } /** 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 602fd35..9d17f3f 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 @@ -23,8 +23,8 @@ import java.lang.management.ManagementFactory; import java.net.URLDecoder; import java.net.URLEncoder; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collections; +import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; @@ -33,6 +33,8 @@ import java.util.NoSuchElementException; import java.util.Set; import java.util.TreeMap; +import javax.management.Attribute; +import javax.management.AttributeList; import javax.management.InstanceNotFoundException; import javax.management.IntrospectionException; import javax.management.MBeanAttributeInfo; @@ -52,6 +54,7 @@ import org.apache.felix.scr.annotations.Service; import org.apache.sling.api.resource.Resource; import org.apache.sling.api.resource.ResourceProvider; import org.apache.sling.api.resource.ResourceResolver; +import org.apache.sling.api.resource.ResourceUtil; import org.apache.sling.commons.osgi.PropertiesUtil; @Component @@ -145,9 +148,14 @@ public class JMXResourceProvider implements ResourceProvider { return new MBeanResource(this.mbeanServer, resourceResolver, this.convertObjectNameToResourcePath(info.objectName), path, info.mbeanInfo, info.objectName); } if ( info.pathInfo.equals("mbean:attributes") ) { - return new AttributesResource(resourceResolver, path); + final MBeanResource parent = (MBeanResource)resourceResolver.getResource(ResourceUtil.getParent(path)); + return new AttributesResource(resourceResolver, path, parent); } if ( info.pathInfo.startsWith("mbean:attributes/") ) { + final AttributesResource parent = (AttributesResource)resourceResolver.getResource(ResourceUtil.getParent(path)); + final MBeanResource parentMBeanResource = (MBeanResource) parent.getParent(); + final AttributeList result = parentMBeanResource.getAttributes(); + final String attrPath = info.pathInfo.substring("mbean:attributes/".length()); final int pos = attrPath.indexOf('/'); final String attrName; @@ -161,7 +169,15 @@ public class JMXResourceProvider implements ResourceProvider { } for(final MBeanAttributeInfo mai : info.mbeanInfo.getAttributes()) { if ( mai.getName().equals(attrName) ) { - final AttributeResource rsrc = new AttributeResource(mbeanServer, info.objectName, resourceResolver, path, mai); + final Iterator iter = result.iterator(); + Object value = null; + while ( iter.hasNext() && value == null ) { + final Attribute a = (Attribute) iter.next(); + if ( a.getName().equals(attrName) ) { + value = a.getValue(); + } + } + final AttributeResource rsrc = new AttributeResource(resourceResolver, path, mai, value, parent); if ( subPath != null ) { return rsrc.getChildResource(subPath); } @@ -264,16 +280,24 @@ public class JMXResourceProvider implements ResourceProvider { public void remove() { throw new UnsupportedOperationException("remove"); } - }; + }; } else { if ( info.pathInfo == null ) { + final MBeanResource parentResource = (MBeanResource)parent; final List<Resource> list = new ArrayList<Resource>(); - list.add(new AttributesResource(parent.getResourceResolver(), parent.getPath() + "/mbean:attributes")); + list.add(new AttributesResource(parent.getResourceResolver(), parent.getPath() + "/mbean:attributes", parentResource)); return list.iterator(); } else if ( info.pathInfo.equals("mbean:attributes") ) { + final AttributesResource parentResource = (AttributesResource)parent; + final MBeanResource parentMBeanResource = (MBeanResource)parentResource.getParent(); + final AttributeList result = parentMBeanResource.getAttributes(); + final MBeanAttributeInfo[] infos = info.mbeanInfo.getAttributes(); - final List<MBeanAttributeInfo> list = Arrays.asList(infos); - final Iterator<MBeanAttributeInfo> iter = list.iterator(); + final Map<String, MBeanAttributeInfo> infoMap = new HashMap<String, MBeanAttributeInfo>(); + for(final MBeanAttributeInfo i : infos) { + infoMap.put(i.getName(), i); + } + final Iterator iter = result.iterator(); return new Iterator<Resource>() { public void remove() { @@ -281,8 +305,12 @@ public class JMXResourceProvider implements ResourceProvider { } public Resource next() { - final MBeanAttributeInfo mai = iter.next(); - return new AttributeResource(mbeanServer, info.objectName, parent.getResourceResolver(), parent.getPath() + "/" + mai.getName(), mai); + final Attribute attr = (Attribute)iter.next(); + return new AttributeResource(parent.getResourceResolver(), + parent.getPath() + "/" + attr.getName(), + infoMap.get(attr.getName()), + attr, + parentResource); } public boolean hasNext() { @@ -290,23 +318,17 @@ public class JMXResourceProvider implements ResourceProvider { } }; } else if ( info.pathInfo.startsWith("mbean:attributes/") ) { + final AttributeResource parentResource = (AttributeResource)parent; final String attrPath = info.pathInfo.substring("mbean:attributes/".length()); final int pos = attrPath.indexOf('/'); - final String attrName; final String subPath; if ( pos == -1 ) { - attrName = attrPath; subPath = null; } else { - attrName = attrPath.substring(0, pos); subPath = attrPath.substring(pos + 1); } - for(final MBeanAttributeInfo mai : info.mbeanInfo.getAttributes()) { - if ( mai.getName().equals(attrName) ) { - final AttributeResource rsrc = new AttributeResource(mbeanServer, info.objectName, parent.getResourceResolver(), parent.getPath(), mai); - return rsrc.getChildren(subPath); - } - } + + return parentResource.getChildren(subPath); } } 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 e062612..78c9e5a 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 @@ -54,6 +54,8 @@ public class MBeanResource extends AbstractResource { /** The mbean server. */ private final MBeanServer mbeanServer; + private volatile AttributeList attributeList; + public MBeanResource(final MBeanServer mbeanServer, final ResourceResolver resolver, final String resourceType, @@ -73,6 +75,28 @@ public class MBeanResource extends AbstractResource { } } + public AttributeList getAttributes() { + if ( this.attributeList == null ) { + final MBeanAttributeInfo[] infos = info.getAttributes(); + final String[] names = new String[infos.length]; + int index = 0; + for(final MBeanAttributeInfo i : infos) { + names[index] = i.getName(); + index++; + } + try { + this.attributeList = mbeanServer.getAttributes(objectName, names); + } catch (InstanceNotFoundException e) { + // ignore + this.attributeList = new AttributeList(); + } catch (ReflectionException e) { + // ignore + this.attributeList = new AttributeList(); + } + } + return this.attributeList; + } + /** * @see org.apache.sling.api.resource.Resource#getPath() */ @@ -128,30 +152,14 @@ public class MBeanResource extends AbstractResource { result.put(Constants.PROP_CLASSNAME, this.info.getClassName()); result.put(Constants.PROP_OBJECTNAME, this.objectName.getCanonicalName()); - final MBeanAttributeInfo[] attribs = this.info.getAttributes(); - final String[] names = new String[attribs.length]; - int index = 0; - for(final MBeanAttributeInfo i : attribs) { - names[index] = i.getName(); - index++; - } - AttributeList values = null; - try { - values = this.mbeanServer.getAttributes(this.objectName, names); - if ( values != null ) { - final Iterator iter = values.iterator(); - while ( iter.hasNext() ) { - final Attribute a = (Attribute)iter.next(); - final Object value = a.getValue(); - if ( value != null ) { - result.put(a.getName(), value); - } - } + final AttributeList values = this.getAttributes(); + final Iterator iter = values.iterator(); + while ( iter.hasNext() ) { + final Attribute a = (Attribute)iter.next(); + final Object value = a.getValue(); + if ( value != null ) { + result.put(a.getName(), value); } - } catch (final InstanceNotFoundException e) { - // ignore - } catch (final ReflectionException e) { - // ignore } return result; -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
