Example usage for javax.management ObjectName toString

List of usage examples for javax.management ObjectName toString

Introduction

In this page you can find the example usage for javax.management ObjectName toString.

Prototype

@Override
public String toString() 

Source Link

Document

Returns a string representation of the object name.

Usage

From source file:com.continuent.tungsten.common.jmx.JmxManager.java

/**
 * Server helper method to register a JMX MBean. MBeans are registered by a
 * combination of their MBean interface and the custom mbeanName argument.
 * The mbeanName permits multiple mBeans to be registered under the same
 * name./*from   w  w  w  .j a va 2  s .  c o m*/
 * 
 * @param mbeanInterface The MBean interface this instance implements
 * @throws ServerRuntimeException
 */
public static void unregisterMBean(Class<?> mbeanInterface) {
    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    try {
        ObjectName name = generateMBeanObjectName(mbeanInterface);
        if (mbs.isRegistered(name)) {
            logger.info("Unregistering mbean: " + name.toString());
            mbs.unregisterMBean(name);
        } else {
            logger.warn("Ignoring attempt to unregister unknown mbean: " + name.toString());
        }
    } catch (Exception e) {
        throw new ServerRuntimeException("Unable to unregister mbean: interface=" + mbeanInterface, e);

    }
}

From source file:com.continuent.tungsten.common.jmx.JmxManager.java

/**
 * Server helper method to register a JMX MBean. MBeans are registered by a
 * combination of their MBean interface and the custom mbeanName argument.
 * The mbeanName permits multiple mBeans to be registered under the same
 * name./*w ww . ja v a  2  s .  c  o  m*/
 * 
 * @param mbeanInterface The MBean interface this instance implements
 * @param mbeanName A custom name for this MBean
 * @throws ServerRuntimeException
 */
public static void unregisterMBean(Class<?> mbeanInterface, String mbeanName) {
    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    try {
        ObjectName name = generateMBeanObjectName(mbeanInterface.getName(), mbeanName);
        if (mbs.isRegistered(name)) {
            logger.info("Unregistering mbean: " + name.toString());
            mbs.unregisterMBean(name);
        } else {
            logger.warn("Ignoring attempt to unregister unknown mbean: " + name.toString());
        }
    } catch (Exception e) {
        throw new ServerRuntimeException(
                "Unable to unregister mbean: interface=" + mbeanInterface + " name=" + mbeanName, e);

    }
}

From source file:org.apache.hadoop.hdfs.tools.DataNodeAdmin.java

/**
 * Gets the FSDataset mbean//w  w w  .  j  av a  2  s  . co m
 * @return FSDataset mbean
 * @throws IOException
 */
private FSDatasetMBean getDatasetMBean() throws IOException {
    Set<ObjectName> names = new TreeSet<ObjectName>(mbsc.queryNames(null, null));
    ObjectName fsDataSetObject = null;
    for (ObjectName name : names) {
        if (name.toString().contains("FSDatasetState"))
            fsDataSetObject = name;
    }
    return JMX.newMBeanProxy(mbsc, fsDataSetObject, FSDatasetMBean.class, true);
}

From source file:org.apache.geode.admin.jmx.internal.MBeanUtil.java

/**
 * Ensures that an MBean is registered for the specified <code>ManagedResource</code>. If an MBean
 * cannot be found in the <code>MBeanServer</code>, then this creates and registers a
 * <code>ModelMBean</code>. State changing callbacks into the <code>ManagedResource</code> will
 * also be made.//from www  . j  av a2 s  . c om
 *
 * @param resource the ManagedResource to create a managing MBean for
 *
 * @return The object name of the MBean that manages the ManagedResource
 *
 * @see ManagedResource#setModelMBean
 */
static ObjectName ensureMBeanIsRegistered(ManagedResource resource) {
    try {
        ObjectName objName = ObjectName.getInstance(resource.getMBeanName());
        synchronized (MBeanUtil.class) {
            if (mbeanServer != null && !mbeanServer.isRegistered(objName)) {
                return createMBean(resource);
            }
        }
        raiseOnFailure(mbeanServer.isRegistered(objName),
                LocalizedStrings.MBeanUtil_COULDNT_FIND_MBEAN_REGISTERED_WITH_OBJECTNAME_0
                        .toLocalizedString(new Object[] { objName.toString() }));
        return objName;
    } catch (java.lang.Exception e) {
        throw new RuntimeAdminException(e);
    }
}

From source file:org.apache.streams.jackson.DatumStatusCounterDeserializer.java

@Override
public DatumStatusCounterBroadcast deserialize(JsonParser jsonParser,
        DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
    try {//from w w  w  .jav a  2  s.  c  om
        MBeanServer server = ManagementFactory.getPlatformMBeanServer();

        DatumStatusCounterBroadcast datumStatusCounterBroadcast = new DatumStatusCounterBroadcast();
        JsonNode attributes = jsonParser.getCodec().readTree(jsonParser);

        ObjectName name = new ObjectName(attributes.get("canonicalName").asText());
        MBeanInfo info = server.getMBeanInfo(name);
        datumStatusCounterBroadcast.setName(name.toString());

        for (MBeanAttributeInfo attribute : Arrays.asList(info.getAttributes())) {
            try {
                switch (attribute.getName()) {
                case "Failed":
                    datumStatusCounterBroadcast
                            .setFailed((boolean) server.getAttribute(name, attribute.getName()));
                    break;
                case "Passed":
                    datumStatusCounterBroadcast
                            .setPassed((boolean) server.getAttribute(name, attribute.getName()));
                    break;
                }
            } catch (Exception e) {
                LOGGER.error("Exception trying to deserialize DatumStatusCounterBroadcast object: {}", e);
            }
        }

        return datumStatusCounterBroadcast;
    } catch (Exception e) {
        LOGGER.error("Exception trying to deserialize DatumStatusCounterBroadcast object: {}", e);
        return null;
    }
}

From source file:org.apache.streams.jackson.MemoryUsageDeserializer.java

@Override
public MemoryUsageBroadcast deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
        throws IOException, JsonProcessingException {
    try {//from ww w. java  2  s.c o  m
        MBeanServer server = ManagementFactory.getPlatformMBeanServer();

        MemoryUsageBroadcast memoryUsageBroadcast = new MemoryUsageBroadcast();
        JsonNode attributes = jsonParser.getCodec().readTree(jsonParser);

        ObjectName name = new ObjectName(attributes.get("canonicalName").asText());
        MBeanInfo info = server.getMBeanInfo(name);
        memoryUsageBroadcast.setName(name.toString());

        for (MBeanAttributeInfo attribute : Arrays.asList(info.getAttributes())) {
            switch (attribute.getName()) {
            case "Verbose":
                memoryUsageBroadcast.setVerbose((boolean) server.getAttribute(name, attribute.getName()));
                break;
            case "ObjectPendingFinalizationCount":
                memoryUsageBroadcast.setObjectPendingFinalizationCount(
                        Long.parseLong(server.getAttribute(name, attribute.getName()).toString()));
                break;
            case "HeapMemoryUsage":
                memoryUsageBroadcast.setHeapMemoryUsage(
                        (Long) ((CompositeDataSupport) server.getAttribute(name, attribute.getName()))
                                .get("used"));
                break;
            case "NonHeapMemoryUsage":
                memoryUsageBroadcast.setNonHeapMemoryUsage(
                        (Long) ((CompositeDataSupport) server.getAttribute(name, attribute.getName()))
                                .get("used"));
                break;
            }
        }

        return memoryUsageBroadcast;
    } catch (Exception e) {
        LOGGER.error("Exception trying to deserialize MemoryUsageDeserializer object: {}", e);
        return null;
    }
}

From source file:org.alfresco.solr.servlet.Solr4X509ServletFilter.java

private int getHttpsPort() {
    try {/*from w  w w .jav a2  s. c  om*/
        MBeanServer mBeanServer = MBeanServerFactory.findMBeanServer(null).get(0);
        QueryExp query = Query.eq(Query.attr("Scheme"), Query.value("https"));
        Set<ObjectName> objectNames = mBeanServer.queryNames(null, query);

        if (objectNames != null && objectNames.size() > 0) {
            for (ObjectName objectName : objectNames) {
                String name = objectName.toString();
                if (name.indexOf("port=") > -1) {
                    String[] parts = name.split("port=");
                    String port = parts[1];
                    try {
                        int portNum = Integer.parseInt(port);
                        return portNum;
                    } catch (NumberFormatException e) {
                        logger.error("Error parsing https port:" + port);
                        return -1;
                    }
                }
            }
        }
    } catch (Throwable t) {
        logger.error("Error getting https port:", t);
    }

    return -1;
}

From source file:org.apache.streams.jackson.ThroughputQueueDeserializer.java

@Override
public ThroughputQueueBroadcast deserialize(JsonParser jsonParser,
        DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
    try {/*from w  ww  . ja va  2s  . c om*/
        MBeanServer server = ManagementFactory.getPlatformMBeanServer();

        ThroughputQueueBroadcast throughputQueueBroadcast = new ThroughputQueueBroadcast();
        JsonNode attributes = jsonParser.getCodec().readTree(jsonParser);

        ObjectName name = new ObjectName(attributes.get("canonicalName").asText());
        MBeanInfo info = server.getMBeanInfo(name);
        throughputQueueBroadcast.setName(name.toString());

        for (MBeanAttributeInfo attribute : Arrays.asList(info.getAttributes())) {
            try {
                switch (attribute.getName()) {
                case "CurrentSize":
                    throughputQueueBroadcast
                            .setCurrentSize((long) server.getAttribute(name, attribute.getName()));
                    break;
                case "AvgWait":
                    throughputQueueBroadcast
                            .setAvgWait((double) server.getAttribute(name, attribute.getName()));
                    break;
                case "MaxWait":
                    throughputQueueBroadcast.setMaxWait((long) server.getAttribute(name, attribute.getName()));
                    break;
                case "Removed":
                    throughputQueueBroadcast.setRemoved((long) server.getAttribute(name, attribute.getName()));
                    break;
                case "Added":
                    throughputQueueBroadcast.setAdded((long) server.getAttribute(name, attribute.getName()));
                    break;
                case "Throughput":
                    throughputQueueBroadcast
                            .setThroughput((double) server.getAttribute(name, attribute.getName()));
                    break;
                }
            } catch (Exception e) {
                LOGGER.error("Exception while trying to deserialize ThroughputQueueBroadcast object: {}", e);
            }
        }

        return throughputQueueBroadcast;
    } catch (Exception e) {
        return null;
    }
}

From source file:org.apache.streams.jackson.StreamsTaskCounterDeserializer.java

@Override
public StreamsTaskCounterBroadcast deserialize(JsonParser jsonParser,
        DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
    try {// w  ww.java2s. c  o  m
        MBeanServer server = ManagementFactory.getPlatformMBeanServer();

        StreamsTaskCounterBroadcast streamsTaskCounterBroadcast = new StreamsTaskCounterBroadcast();
        JsonNode attributes = jsonParser.getCodec().readTree(jsonParser);

        ObjectName name = new ObjectName(attributes.get("canonicalName").asText());
        MBeanInfo info = server.getMBeanInfo(name);
        streamsTaskCounterBroadcast.setName(name.toString());

        for (MBeanAttributeInfo attribute : Arrays.asList(info.getAttributes())) {
            try {
                switch (attribute.getName()) {
                case "ErrorRate":
                    streamsTaskCounterBroadcast
                            .setErrorRate((double) server.getAttribute(name, attribute.getName()));
                    break;
                case "NumEmitted":
                    streamsTaskCounterBroadcast
                            .setNumEmitted((long) server.getAttribute(name, attribute.getName()));
                    break;
                case "NumReceived":
                    streamsTaskCounterBroadcast
                            .setNumReceived((long) server.getAttribute(name, attribute.getName()));
                    break;
                case "NumUnhandledErrors":
                    streamsTaskCounterBroadcast
                            .setNumUnhandledErrors((long) server.getAttribute(name, attribute.getName()));
                    break;
                case "AvgTime":
                    streamsTaskCounterBroadcast
                            .setAvgTime((double) server.getAttribute(name, attribute.getName()));
                    break;
                case "MaxTime":
                    streamsTaskCounterBroadcast
                            .setMaxTime((long) server.getAttribute(name, attribute.getName()));
                    break;
                }
            } catch (Exception e) {
                LOGGER.error("Exception while trying to deserialize StreamsTaskCounterBroadcast object: {}", e);
            }
        }

        return streamsTaskCounterBroadcast;
    } catch (Exception e) {
        LOGGER.error("Exception while trying to deserialize StreamsTaskCounterBroadcast object: {}", e);
        return null;
    }
}

From source file:catalina.mbeans.GroupMBean.java

/**
 * Return the MBean Names of all authorized roles for this group.
 *///from  www  .  j  ava2s. c o  m
public String[] getRoles() {

    Group group = (Group) this.resource;
    ArrayList results = new ArrayList();
    Iterator roles = group.getRoles();
    while (roles.hasNext()) {
        Role role = null;
        try {
            role = (Role) roles.next();
            ObjectName oname = MBeanUtils.createObjectName(managed.getDomain(), role);
            results.add(oname.toString());
        } catch (MalformedObjectNameException e) {
            throw new IllegalArgumentException("Cannot create object name for role " + role);
        }
    }
    return ((String[]) results.toArray(new String[results.size()]));

}