Search in sources :

Example 71 with Attribute

use of javax.management.Attribute in project sling by apache.

the class JMXResourceProvider method listChildren.

/**
     * @see org.apache.sling.api.resource.ResourceProvider#listChildren(org.apache.sling.api.resource.Resource)
     */
public Iterator<Resource> listChildren(final Resource parent) {
    final PathInfo info = this.parse(parent.getPath());
    if (info != null) {
        if (info.isRoot || info.mbeanInfo == null) {
            // list all MBeans
            final Set<ObjectName> names = this.queryObjectNames(info.isRoot ? null : info.pathInfo);
            final Set<String> filteredNames = new HashSet<String>();
            final String prefix = (info.isRoot ? null : info.pathInfo + "/");
            for (final ObjectName name : names) {
                final String path = this.convertObjectNameToResourcePath(name);
                final String testName = (info.isRoot ? path : path.substring(prefix.length()));
                final int sep = testName.indexOf('/');
                if (sep == -1) {
                    filteredNames.add(":" + name.getCanonicalName());
                } else {
                    filteredNames.add(testName.substring(0, sep));
                }
            }
            final List<String> sortedNames = new ArrayList<String>(filteredNames);
            Collections.sort(sortedNames);
            final Iterator<String> iter = sortedNames.iterator();
            return new Iterator<Resource>() {

                private Resource next;

                {
                    seek();
                }

                private void seek() {
                    while (iter.hasNext() && this.next == null) {
                        final String name = iter.next();
                        if (name.startsWith(":")) {
                            try {
                                final ObjectName on = new ObjectName(name.substring(1));
                                final MBeanInfo info = mbeanServer.getMBeanInfo(on);
                                final String path = convertObjectNameToResourcePath(on);
                                final int sep = path.lastIndexOf('/');
                                this.next = new MBeanResource(mbeanServer, parent.getResourceResolver(), path, parent.getPath() + "/" + path.substring(sep + 1), info, on);
                            } catch (final IntrospectionException e) {
                            // ignore
                            } catch (final InstanceNotFoundException e) {
                            // ignore
                            } catch (final ReflectionException e) {
                            // ignore
                            } catch (final MalformedObjectNameException e) {
                            // ignore
                            }
                        } else {
                            this.next = new RootResource(parent.getResourceResolver(), parent.getPath() + '/' + name);
                        }
                    }
                }

                public boolean hasNext() {
                    return next != null;
                }

                public Resource next() {
                    if (next != null) {
                        final Resource rsrc = next;
                        this.next = null;
                        seek();
                        return rsrc;
                    }
                    throw new NoSuchElementException();
                }

                public void remove() {
                    throw new UnsupportedOperationException("remove");
                }
            };
        } else {
            if (info.pathInfo == null) {
                final MBeanResource parentResource;
                if (parent instanceof MBeanResource) {
                    parentResource = (MBeanResource) parent;
                } else {
                    parentResource = (MBeanResource) this.getResource(parent.getResourceResolver(), parent.getPath());
                }
                final List<Resource> list = new ArrayList<Resource>();
                list.add(new AttributesResource(parent.getResourceResolver(), parent.getPath() + "/mbean:attributes", parentResource));
                return list.iterator();
            } else if (info.pathInfo.equals("mbean:attributes")) {
                final AttributesResource parentResource;
                if (parent instanceof AttributesResource) {
                    parentResource = (AttributesResource) parent;
                } else {
                    parentResource = (AttributesResource) this.getResource(parent.getResourceResolver(), parent.getPath());
                }
                final MBeanResource parentMBeanResource = (MBeanResource) parentResource.getParent();
                final AttributeList result = parentMBeanResource.getAttributes();
                final MBeanAttributeInfo[] infos = info.mbeanInfo.getAttributes();
                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() {
                        throw new UnsupportedOperationException("remove");
                    }

                    public Resource next() {
                        final Attribute attr = (Attribute) iter.next();
                        return new AttributeResource(parent.getResourceResolver(), parent.getPath() + "/" + attr.getName(), infoMap.get(attr.getName()), attr.getValue(), parentResource);
                    }

                    public boolean hasNext() {
                        return iter.hasNext();
                    }
                };
            } else if (info.pathInfo.startsWith("mbean:attributes/")) {
                Resource checkParentResource = parent;
                if (!(checkParentResource instanceof AttributeResource) && !(checkParentResource instanceof MapResource)) {
                    checkParentResource = this.getResource(parent.getResourceResolver(), parent.getPath());
                }
                final AttributeResource parentResource;
                if (checkParentResource instanceof AttributeResource) {
                    parentResource = (AttributeResource) checkParentResource;
                } else {
                    parentResource = ((MapResource) checkParentResource).getAttributeResource();
                }
                final String attrPath = info.pathInfo.substring("mbean:attributes/".length());
                final int pos = attrPath.indexOf('/');
                final String subPath;
                if (pos == -1) {
                    subPath = null;
                } else {
                    subPath = attrPath.substring(pos + 1);
                }
                return parentResource.getChildren(parent.getPath(), subPath);
            }
        }
    }
    return null;
}
Also used : MBeanInfo(javax.management.MBeanInfo) Attribute(javax.management.Attribute) AttributeList(javax.management.AttributeList) ArrayList(java.util.ArrayList) IntrospectionException(javax.management.IntrospectionException) Iterator(java.util.Iterator) HashSet(java.util.HashSet) ReflectionException(javax.management.ReflectionException) MalformedObjectNameException(javax.management.MalformedObjectNameException) InstanceNotFoundException(javax.management.InstanceNotFoundException) Resource(org.apache.sling.api.resource.Resource) MBeanAttributeInfo(javax.management.MBeanAttributeInfo) ObjectName(javax.management.ObjectName) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) NoSuchElementException(java.util.NoSuchElementException)

Example 72 with Attribute

use of javax.management.Attribute in project tomee by apache.

the class DynamicMBeanClientTest method remoteGet.

@Test
public void remoteGet() throws Exception {
    assertEquals(0, remoteClient.getCounter());
    ManagementFactory.getPlatformMBeanServer().setAttribute(objectName, new Attribute("Counter", 5));
    assertEquals(5, remoteClient.getCounter());
}
Also used : Attribute(javax.management.Attribute) Test(org.junit.Test)

Example 73 with Attribute

use of javax.management.Attribute in project tomee by apache.

the class DynamicMBeanHandler method invoke.

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    final String methodName = method.getName();
    if (method.getDeclaringClass().equals(Object.class) && "toString".equals(methodName)) {
        return getClass().getSimpleName() + " Proxy";
    }
    if (method.getAnnotation(PreDestroy.class) != null) {
        return destroy();
    }
    final ConnectionInfo info = getConnectionInfo(method);
    final MBeanInfo infos = info.getMBeanInfo();
    if (methodName.startsWith("set") && methodName.length() > 3 && args != null && args.length == 1 && (Void.TYPE.equals(method.getReturnType()) || Void.class.equals(method.getReturnType()))) {
        final String attributeName = attributeName(infos, methodName, method.getParameterTypes()[0]);
        info.setAttribute(new Attribute(attributeName, args[0]));
        return null;
    } else if (methodName.startsWith("get") && (args == null || args.length == 0) && methodName.length() > 3) {
        final String attributeName = attributeName(infos, methodName, method.getReturnType());
        return info.getAttribute(attributeName);
    }
    // operation
    return info.invoke(methodName, args, getSignature(method));
}
Also used : MBeanInfo(javax.management.MBeanInfo) Attribute(javax.management.Attribute) PreDestroy(javax.annotation.PreDestroy)

Example 74 with Attribute

use of javax.management.Attribute in project tomee by apache.

the class JMXBeanCreator method create.

public <T> Object create() throws MBeanRegistrationException {
    final String code = (String) properties.remove("code");
    final String name = (String) properties.remove("name");
    final String iface = (String) properties.remove("interface");
    final String prefix = (String) properties.remove("prefix");
    requireNotNull(code);
    requireNotNull(name);
    requireNotNull(iface);
    try {
        final Class<? extends T> cls = (Class<? extends T>) Class.forName(code, true, Thread.currentThread().getContextClassLoader());
        final Class<T> ifaceCls = (Class<T>) Class.forName(iface, true, Thread.currentThread().getContextClassLoader());
        final T instance = (T) cls.newInstance();
        final StandardMBean mBean = new StandardMBean(instance, ifaceCls);
        for (String attributeName : properties.stringPropertyNames()) {
            final Object value = properties.remove(attributeName);
            if (prefix != null) {
                if (!attributeName.startsWith(prefix + ".")) {
                    continue;
                } else {
                    attributeName = attributeName.substring(prefix.length() + 1);
                }
            }
            final Class<?> targetType = findAttributeType(mBean.getMBeanInfo(), attributeName);
            final Object targetValue = Converter.convert(value, targetType, null);
            final Attribute attribute = new Attribute(attributeName, targetValue);
            mBean.setAttribute(attribute);
        }
        final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
        final ObjectName objectName = new ObjectName(name);
        mbs.registerMBean(instance, objectName);
        return instance;
    } catch (final Exception e) {
        e.printStackTrace();
        LOGGER.severe("Unable to register mbean " + e.getMessage());
        throw new MBeanRegistrationException(e);
    }
}
Also used : StandardMBean(javax.management.StandardMBean) Attribute(javax.management.Attribute) ObjectName(javax.management.ObjectName) MBeanServer(javax.management.MBeanServer)

Example 75 with Attribute

use of javax.management.Attribute in project tomee by apache.

the class Alternative method postConstruct.

@PostConstruct
public <T> void postConstruct() throws MBeanRegistrationException {
    final String name = properties.remove("name").toString();
    final String iface = properties.remove("interface").toString();
    final String prefix = properties.remove("prefix").toString();
    requireNotNull(name);
    requireNotNull(iface);
    try {
        final Class<T> ifaceCls = (Class<T>) Class.forName(iface, true, Thread.currentThread().getContextClassLoader());
        final StandardMBean mBean = new StandardMBean((T) this, ifaceCls);
        for (String attributeName : properties.stringPropertyNames()) {
            final Object value = properties.remove(attributeName);
            if (prefix != null) {
                if (!attributeName.startsWith(prefix + ".")) {
                    continue;
                } else {
                    attributeName = attributeName.substring(prefix.length() + 1);
                }
            }
            final Class<?> targetType = findAttributeType(mBean.getMBeanInfo(), attributeName);
            final Object targetValue = Converter.convert(value, targetType, null);
            final Attribute attribute = new Attribute(attributeName, targetValue);
            mBean.setAttribute(attribute);
        }
        final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
        final ObjectName objectName = new ObjectName(name);
        mbs.registerMBean(this, objectName);
    } catch (final Exception e) {
        LOGGER.severe("Unable to register mbean " + e.getMessage());
        throw new MBeanRegistrationException(e);
    }
}
Also used : StandardMBean(javax.management.StandardMBean) Attribute(javax.management.Attribute) MalformedObjectNameException(javax.management.MalformedObjectNameException) MBeanRegistrationException(org.superbiz.resource.jmx.factory.MBeanRegistrationException) InstanceNotFoundException(javax.management.InstanceNotFoundException) MBeanRegistrationException(org.superbiz.resource.jmx.factory.MBeanRegistrationException) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName) PostConstruct(javax.annotation.PostConstruct)

Aggregations

Attribute (javax.management.Attribute)157 ObjectName (javax.management.ObjectName)94 MBeanServer (javax.management.MBeanServer)56 AttributeList (javax.management.AttributeList)46 Test (org.junit.Test)38 ReflectionException (javax.management.ReflectionException)29 MBeanException (javax.management.MBeanException)25 HashMap (java.util.HashMap)23 InstanceNotFoundException (javax.management.InstanceNotFoundException)22 InvalidAttributeValueException (javax.management.InvalidAttributeValueException)21 AttributeNotFoundException (javax.management.AttributeNotFoundException)20 MBeanServerConnection (javax.management.MBeanServerConnection)15 MBeanInfo (javax.management.MBeanInfo)14 JMXConnector (javax.management.remote.JMXConnector)13 RuntimeOperationsException (javax.management.RuntimeOperationsException)12 List (java.util.List)10 BacklogTracerEventMessage (org.apache.camel.api.management.mbean.BacklogTracerEventMessage)10 IOException (java.io.IOException)9 InvocationTargetException (java.lang.reflect.InvocationTargetException)9 ArrayList (java.util.ArrayList)9