Example usage for javax.management MBeanServer getAttribute

List of usage examples for javax.management MBeanServer getAttribute

Introduction

In this page you can find the example usage for javax.management MBeanServer getAttribute.

Prototype

public Object getAttribute(ObjectName name, String attribute)
        throws MBeanException, AttributeNotFoundException, InstanceNotFoundException, ReflectionException;

Source Link

Usage

From source file:ome.services.util.JvmSettingsCheck.java

public static long getPhysicalMemory() {
    try {//w  w  w. ja  v  a2s.c o m
        MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
        Object attribute = mBeanServer.getAttribute(new ObjectName("java.lang", "type", "OperatingSystem"),
                "TotalPhysicalMemorySize");
        return Long.valueOf(attribute.toString());
    } catch (Exception e) {
        log.debug("Failed to get physical memory", e);
        return -1;
    }
}

From source file:com.googlecode.psiprobe.tools.JmxTools.java

public static long getLongAttr(MBeanServer mBeanServer, ObjectName oName, String attrName) throws Exception {
    return ((Long) mBeanServer.getAttribute(oName, attrName)).longValue();
}

From source file:com.googlecode.psiprobe.tools.JmxTools.java

public static int getIntAttr(MBeanServer mBeanServer, ObjectName oName, String attrName) throws Exception {
    return ((Integer) mBeanServer.getAttribute(oName, attrName)).intValue();
}

From source file:com.googlecode.psiprobe.tools.JmxTools.java

public static Object getAttribute(MBeanServer mBeanServer, ObjectName oName, String attrName) throws Exception {
    try {/*from w ww  .  java  2s  .  c  om*/
        return mBeanServer.getAttribute(oName, attrName);
    } catch (AttributeNotFoundException e) {
        logger.error(oName + " does not have \"" + attrName + "\" attribute");
        return null;
    }
}

From source file:com.googlecode.psiprobe.tools.JmxTools.java

public static long getLongAttr(MBeanServer mBeanServer, ObjectName oName, String attrName, long defaultValue) {
    try {//from ww w  . ja  va 2s  . co m
        Object o = mBeanServer.getAttribute(oName, attrName);
        return o == null ? defaultValue : ((Long) o).longValue();
    } catch (Exception e) {
        return defaultValue;
    }
}

From source file:org.opendaylight.infrautils.diagstatus.MBeanUtils.java

@Nullable
public static Object getMBeanAttribute(String objName, String attribute) throws JMException {
    ObjectName objectName = new ObjectName(objName);
    MBeanServer platformMbeanServer = ManagementFactory.getPlatformMBeanServer();
    return platformMbeanServer.getAttribute(objectName, attribute);
}

From source file:com.netsteadfast.greenstep.util.HostUtils.java

public static int getHttpPort() {
    int port = 8080;
    MBeanServer mBeanServer = MBeanServerFactory.findMBeanServer(null).get(0);
    ObjectName name;//  www .  j  a  v  a2 s .  co  m
    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;
}

From source file:com.parallels.desktopcloud.ParallelsDesktopConnectorSlaveComputer.java

private static VMResources getHostResources(Channel ch) throws Exception {
    return ch.call(new MasterToSlaveCallable<VMResources, Exception>() {
        private long getHostPhysicalMemory() {
            try {
                MBeanServer server = ManagementFactory.getPlatformMBeanServer();
                Object attribute = server.getAttribute(new ObjectName("java.lang", "type", "OperatingSystem"),
                        "TotalPhysicalMemorySize");
                return (Long) attribute;
            } catch (JMException e) {
                LOGGER.log(Level.SEVERE, "Failed to get host RAM size: %s", e);
                return Long.MAX_VALUE;
            }/*  w  w w  .  j  ava 2 s  .  c  o m*/
        }

        @Override
        public VMResources call() throws Exception {
            int cpus = Runtime.getRuntime().availableProcessors();
            long ram = getHostPhysicalMemory();
            return new VMResources(cpus, ram);
        }
    });
}

From source file:org.apache.tajo.worker.WorkerHeartbeatService.java

public static int getTotalMemoryMB() {
    javax.management.MBeanServer mBeanServer = java.lang.management.ManagementFactory.getPlatformMBeanServer();
    long max = 0;
    Object maxObject = null;//from  w ww. j  ava  2s.co m
    try {
        javax.management.ObjectName osName = new javax.management.ObjectName("java.lang:type=OperatingSystem");
        if (!System.getProperty("java.vendor").startsWith("IBM")) {
            maxObject = mBeanServer.getAttribute(osName, "TotalPhysicalMemorySize");
        } else {
            maxObject = mBeanServer.getAttribute(osName, "TotalPhysicalMemory");
        }
    } catch (Throwable t) {
        LOG.error(t.getMessage(), t);
    }
    if (maxObject != null) {
        max = ((Long) maxObject).longValue();
    }
    return ((int) (max / (1024 * 1024)));
}

From source file:com.meltmedia.cadmium.deployer.JBossUtil.java

public static boolean isWarDeployed(String warName, Logger log) throws Exception {
    MBeanServer server = MBeanServerLocator.locateJBoss();
    Set<ObjectInstance> foundInstances = server
            .queryMBeans(new ObjectName("jboss.deployment:type=Deployment,id=\"*" + warName + "*\""), null);
    boolean found = false;
    if (foundInstances != null && foundInstances.size() > 0) {
        log.debug("MBean query returned: {} results.", foundInstances.size());
        for (ObjectInstance instance : foundInstances) {
            String simpleName = "" + server.getAttribute(instance.getObjectName(), "SimpleName");
            log.debug("Checking {} is {}", simpleName, warName);
            if (simpleName.equals(warName)) {
                found = true;/*from   w  w w. j av a2s.c o m*/
                String state = server.getAttribute(instance.getObjectName(), "State") + "";
                log.debug("Deployment state {}", state);
                if (state.equals("ERROR")) {
                    Object error = server.getAttribute(instance.getObjectName(), "Problem");
                    log.debug("Found problem: {}", error);
                    if (error instanceof Throwable) {
                        throw new Exception((Throwable) error);
                    } else {
                        throw new Exception(error.toString());
                    }
                } else if (state.equals("DEPLOYED")) {
                    return true;
                } else if (state.equals("UNDEPLOYED")) {
                    found = false;
                }
            }
        }
    }
    if (!found) {
        throw new NoDeploymentFoundException();
    }
    return false;
}