Example usage for javax.management MBeanServerFactory findMBeanServer

List of usage examples for javax.management MBeanServerFactory findMBeanServer

Introduction

In this page you can find the example usage for javax.management MBeanServerFactory findMBeanServer.

Prototype

public synchronized static ArrayList<MBeanServer> findMBeanServer(String agentId) 

Source Link

Document

Return a list of registered MBeanServer objects.

Usage

From source file:Main.java

public static MBeanServer findMBeanServer() {
    ArrayList<MBeanServer> servers = MBeanServerFactory.findMBeanServer(null);
    if (servers == null || servers.isEmpty()) {
        return ManagementFactory.getPlatformMBeanServer();
    }/*w w w  .j  a v a  2s  .c  om*/

    return servers.get(0);
}

From source file:Main.java

/**
 * Resets MBeanServerFactory and ManagementFactory to a known consistent state.
 * This involves releasing all currently registered MBeanServers and resetting
 * the platformMBeanServer to null.//from   ww  w . ja v  a  2 s  . c o  m
 */
public static void resetMBeanServers() throws Exception {
    for (MBeanServer server : MBeanServerFactory.findMBeanServer(null)) {
        MBeanServerFactory.releaseMBeanServer(server);
    }
    Field field = ManagementFactory.class.getDeclaredField("platformMBeanServer");
    field.setAccessible(true);
    field.set(null, null);
}

From source file:org.rhq.plugins.jbossas.util.JBossMBeanUtility.java

public static MBeanServer getJBossMBeanServer() {
    List<MBeanServer> servers = MBeanServerFactory.findMBeanServer(null);
    MBeanServer jbossServer = null;
    for (MBeanServer server : servers) {
        if ("jboss".equals(server.getDefaultDomain())) {
            jbossServer = server;/* w w  w.  j a v  a2 s  .c om*/
        }
    }
    if (jbossServer == null) {
        jbossServer = ManagementFactory.getPlatformMBeanServer();
    }
    return jbossServer;
}

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;/*  w ww.  ja v  a2s  . c  om*/
    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:org.openamf.invoker.JMXServiceInvoker.java

public Object invokeService() throws ServiceInvocationException {

    Object serviceResult = null;/*ww  w .j  a  v  a  2 s.c o m*/
    try {
        List serverList = MBeanServerFactory.findMBeanServer(null);
        if (serverList == null || serverList.size() == 0) {
            throw new Exception("MBeanServers not found");
        }
        MBeanServer server = (MBeanServer) serverList.get(0);
        ObjectName objectName = new ObjectName(request.getServiceName());
        Object[] parameters = request.getParameters().toArray();
        String[] signature = new String[parameters.length];

        for (int i = 0; i < parameters.length; i++) {
            signature[i] = parameters[i].getClass().getName();
        }
        serviceResult = server.invoke(objectName, request.getServiceMethodName(), parameters, signature);
    } catch (Exception e) {
        throw new ServiceInvocationException(request, e);
    }

    return serviceResult;
}

From source file:com.googlecode.psiprobe.beans.JBossResourceResolverBean.java

public MBeanServer getMBeanServer() {
    for (Iterator it = MBeanServerFactory.findMBeanServer(null).iterator(); it.hasNext();) {
        MBeanServer server = (MBeanServer) it.next();
        if ("jboss".equals(server.getDefaultDomain()) || "DefaultDomain".equals(server.getDefaultDomain())) {
            return server;
        }//from  w  ww . jav  a2s.c o  m
    }
    return null;
}

From source file:org.jminix.server.DefaultLocalServerConnectionProvider.java

public List<MBeanServerConnection> getConnections() {
    List<MBeanServerConnection> result = new ArrayList<MBeanServerConnection>();

    // Make sure the platform server is instanciated
    try {//from   w w  w. j a v a 2s.  c  om
        ManagementFactory.getPlatformMBeanServer();
    } catch (SecurityException ex) {
        LogFactory.getLog(this.getClass()).warn("Not allowed to obtain the Java platform MBeanServer", ex);
    }

    try {
        List servers = MBeanServerFactory.findMBeanServer(null);

        LogFactory.getLog(this.getClass()).debug("Found " + servers.size() + " MBean servers");

        result.addAll(servers);
    } catch (Exception e) {
        LogFactory.getLog(this.getClass()).warn("Could not register the Java platform MBeanServer", e);
    }

    return result;
}

From source file:de.msg.terminfindung.common.jmx.TestJmxUeberwachung.java

@Test
public void testJmxUeberwachung() throws Exception {
    // SpringContext initialisieren und Komponente holen
    //Meldung meldung = (Meldung) SpringContextHolder.getBean("meldung");
    // Holen des MBeanServers
    ArrayList<MBeanServer> mBeanServerList = MBeanServerFactory.findMBeanServer(null);
    MBeanServer mBeanServer = mBeanServerList.get(0);
    // Lesen der gewnschten Information per JMX
    // de.bund.bva.isyfact.terminfindung:type=ServiceStatistik,name=&quot;Erstellung-Statistik&quot;
    Hashtable<String, String> table = new Hashtable<>();
    table.put("type", "ServiceStatistik");
    table.put("name", "\"Erstellung-Statistik\"");
    ObjectName testObjectName = new ObjectName("de.bund.bva.isyfact.terminfindung", table);
    String testAttributeName = "DurchschnittsDauerLetzteAufrufe";
    String result = mBeanServer.getAttribute(testObjectName, testAttributeName).toString();
    // Auswerten des Ergebnisses
    assertEquals("0", result);
    // Einen Anwendungsfall ausfhren
    contr.initialisiereModel(new ErstellenModel());
    result = mBeanServer.getAttribute(testObjectName, testAttributeName).toString();
    assertNotEquals("0", result);
}

From source file:com.googlecode.arit.mbeans.MBeanScanner.java

public void scanForResources(ResourceListener resourceEventListener) {
    ArrayList<MBeanServer> mbeanServers = MBeanServerFactory.findMBeanServer(null);
    for (MBeanServer mbs : mbeanServers) {
        MBeanAccessor mbeanAccessor = mbsInspector.inspect(mbs);
        if (mbeanAccessor == null) {
            String errorMsg = "Unable to inspect MBeanServer of type " + mbs.getClass().getName();
            LOG.error(errorMsg);/*from   ww w. j  av a 2s.  c  o m*/
            if (messages != null) {
                messages.addMessage(errorMsg);
            }
        } else {
            Set<ObjectName> mbeans;
            try {
                mbeans = mbs.queryNames(new ObjectName("*:*"), null);
            } catch (MalformedObjectNameException ex) {
                // We should never get here
                throw new Error(ex);
            }

            for (ObjectName name : mbeans) {
                Object mbean = mbeanAccessor.retrieve(name);
                SimpleResource<Object> mbeanResource = new SimpleResource<Object>(resourceType, mbean,
                        "MBean: " + name);
                mbeanResource.addClassloaderReference(mbean.getClass().getClassLoader(),
                        "Implementation class: " + mbean.getClass().getName());
                resourceEventListener.onResourceFound(mbeanResource);
            }
        }
    }
}

From source file:chronos.web.listener.ChronosServletListener.java

/**
 * @return MBeanServer//from   w  ww .  ja  va 2 s  .c o  m
 */
private MBeanServer findOrCreateChronosMBeanServer() {
    final ArrayList<MBeanServer> servers = MBeanServerFactory.findMBeanServer(null);
    logger.debug("Got " + servers.size() + " MBean servers");
    if (servers.size() == 0) {
        logger.debug("Creating new MBeanServer for \"chronos\"...");
        return MBeanServerFactory.createMBeanServer(CHRONOS);
    }

    final MBeanServer server = servers.get(0);
    logger.debug("Returning MBeanServer domain: " + server.getDefaultDomain());
    return server;
}