Search in sources :

Example 51 with InstanceNotFoundException

use of javax.management.InstanceNotFoundException 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 52 with InstanceNotFoundException

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

the class MBeanResource method getAttributes.

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;
}
Also used : ReflectionException(javax.management.ReflectionException) AttributeList(javax.management.AttributeList) InstanceNotFoundException(javax.management.InstanceNotFoundException) MBeanAttributeInfo(javax.management.MBeanAttributeInfo)

Example 53 with InstanceNotFoundException

use of javax.management.InstanceNotFoundException in project bamboobsc by billchen198318.

the class HostUtils method getHttpPort.

public static int getHttpPort() {
    int port = 8080;
    MBeanServer mBeanServer = MBeanServerFactory.findMBeanServer(null).get(0);
    ObjectName name;
    try {
        name = new ObjectName("Catalina", "type", "Server");
        try {
            Server server = (Server) mBeanServer.getAttribute(name, "managedResource");
            Service[] services = server.findServices();
            for (Service service : services) {
                for (Connector connector : service.findConnectors()) {
                    ProtocolHandler protocolHandler = connector.getProtocolHandler();
                    if (protocolHandler instanceof Http11Protocol || protocolHandler instanceof Http11AprProtocol || protocolHandler instanceof Http11NioProtocol) {
                        port = connector.getPort();
                    }
                }
            }
        } catch (AttributeNotFoundException e) {
            e.printStackTrace();
        } catch (InstanceNotFoundException e) {
            e.printStackTrace();
        } catch (MBeanException e) {
            e.printStackTrace();
        } catch (ReflectionException e) {
            e.printStackTrace();
        }
    } catch (MalformedObjectNameException e) {
        e.printStackTrace();
    }
    return port;
}
Also used : Connector(org.apache.catalina.connector.Connector) ReflectionException(javax.management.ReflectionException) AttributeNotFoundException(javax.management.AttributeNotFoundException) MalformedObjectNameException(javax.management.MalformedObjectNameException) Http11NioProtocol(org.apache.coyote.http11.Http11NioProtocol) MBeanServer(javax.management.MBeanServer) Server(org.apache.catalina.Server) InstanceNotFoundException(javax.management.InstanceNotFoundException) Service(org.apache.catalina.Service) Http11AprProtocol(org.apache.coyote.http11.Http11AprProtocol) ObjectName(javax.management.ObjectName) ProtocolHandler(org.apache.coyote.ProtocolHandler) Http11Protocol(org.apache.coyote.http11.Http11Protocol) MBeanException(javax.management.MBeanException) MBeanServer(javax.management.MBeanServer)

Example 54 with InstanceNotFoundException

use of javax.management.InstanceNotFoundException in project cdap by caskdata.

the class OperationalStatsService method shutDown.

@Override
protected void shutDown() throws Exception {
    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    for (Map.Entry<OperationalExtensionId, OperationalStats> entry : operationalStatsLoader.getAll().entrySet()) {
        OperationalStats operationalStats = entry.getValue();
        ObjectName objectName = getObjectName(operationalStats);
        if (objectName == null) {
            LOG.warn("Found an operational extension with null service name and stat type while unregistering - {}. " + "Ignoring this extension.", operationalStats.getClass().getName());
            continue;
        }
        try {
            mbs.unregisterMBean(objectName);
        } catch (InstanceNotFoundException e) {
            LOG.warn("MBean {} not found while un-registering. Ignoring.", objectName);
        } catch (MBeanRegistrationException e) {
            LOG.warn("Error while un-registering MBean {}.", e);
        }
        operationalStats.destroy();
    }
    LOG.info("Successfully shutdown operational stats service.");
}
Also used : InstanceNotFoundException(javax.management.InstanceNotFoundException) Map(java.util.Map) MBeanRegistrationException(javax.management.MBeanRegistrationException) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName)

Example 55 with InstanceNotFoundException

use of javax.management.InstanceNotFoundException in project ddf by codice.

the class QueryMonitor method registerMbean.

private void registerMbean() {
    try {
        objectName = new ObjectName(QueryMonitor.class.getName() + ":service=querymonitor");
        mBeanServer = ManagementFactory.getPlatformMBeanServer();
    } catch (MalformedObjectNameException e) {
        LOGGER.info("Unable to create Query Monitor Configuration MBean.", e);
    }
    if (mBeanServer == null) {
        return;
    }
    try {
        try {
            mBeanServer.registerMBean(this, objectName);
            LOGGER.debug("Registered Query Monitor Configuration MBean under object name: {}", objectName.toString());
        } catch (InstanceAlreadyExistsException e) {
            // Try to remove and re-register
            mBeanServer.unregisterMBean(objectName);
            mBeanServer.registerMBean(this, objectName);
            LOGGER.debug("Re-registered Query Monitor Configuration MBean");
        }
    } catch (MBeanRegistrationException | InstanceNotFoundException | InstanceAlreadyExistsException | NotCompliantMBeanException e) {
        LOGGER.info("Could not register MBean [{}].", objectName.toString(), e);
    }
}
Also used : MalformedObjectNameException(javax.management.MalformedObjectNameException) NotCompliantMBeanException(javax.management.NotCompliantMBeanException) InstanceAlreadyExistsException(javax.management.InstanceAlreadyExistsException) InstanceNotFoundException(javax.management.InstanceNotFoundException) MBeanRegistrationException(javax.management.MBeanRegistrationException) ObjectName(javax.management.ObjectName)

Aggregations

InstanceNotFoundException (javax.management.InstanceNotFoundException)102 ObjectName (javax.management.ObjectName)59 ReflectionException (javax.management.ReflectionException)44 MBeanException (javax.management.MBeanException)32 MalformedObjectNameException (javax.management.MalformedObjectNameException)28 MBeanRegistrationException (javax.management.MBeanRegistrationException)25 InstanceAlreadyExistsException (javax.management.InstanceAlreadyExistsException)19 MBeanServer (javax.management.MBeanServer)17 IOException (java.io.IOException)16 AttributeNotFoundException (javax.management.AttributeNotFoundException)16 Attribute (javax.management.Attribute)15 IntrospectionException (javax.management.IntrospectionException)14 NotCompliantMBeanException (javax.management.NotCompliantMBeanException)14 AttributeList (javax.management.AttributeList)12 ObjectInstance (javax.management.ObjectInstance)12 MBeanInfo (javax.management.MBeanInfo)11 InvalidAttributeValueException (javax.management.InvalidAttributeValueException)10 RuntimeOperationsException (javax.management.RuntimeOperationsException)9 ArrayList (java.util.ArrayList)7 MBeanAttributeInfo (javax.management.MBeanAttributeInfo)7