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:com.atolcd.alfresco.audit.cmr.AbstractJMXInfoGet.java

protected MBeanServer getMBeanServer(final ObjectName objectName) {
    if (knownMBeanServersWithQuery.containsKey(objectName)) {
        return knownMBeanServersWithQuery.get(objectName);
    }// w  ww  .  j  a  v a  2 s  . c  o m

    // no cached instances, search high and low...
    MBeanServer mbeanServer = null;

    final List<MBeanServer> servers = MBeanServerFactory.findMBeanServer(null);
    for (int i = 0; mbeanServer == null && i < servers.size(); i++) {
        try {
            if (servers.get(i).getObjectInstance(objectName) != null) {
                mbeanServer = servers.get(i);
            }
        } catch (InstanceNotFoundException e) {
            // woops, not registered here...
            throw new WebScriptException("[DatabaseInfoGet] not registered here...");
        }
    }

    if (mbeanServer == null) {
        // oh well, most likely it is here then...
        mbeanServer = getPlatformMBeanServer();
    }
    knownMBeanServers.put(objectName, mbeanServer);
    return mbeanServer;
}

From source file:com.buschmais.maexo.test.server.factory.ServerFactoryTest.java

/**
 * Checks if we can find the same MBeanServer instances in the OSGi service
 * registry as delivered by/*from   w w  w.  ja  v  a2  s.c om*/
 * {@link MBeanServerFactory#findMBeanServer(String)}
 * 
 * @throws InvalidSyntaxException
 */
@SuppressWarnings("unchecked")
public void test_getServersFromFactoy() {
    List<MBeanServer> mbeanServers = MBeanServerFactory.findMBeanServer(null);
    assertFalse(mbeanServers.isEmpty());
    super.test_ServersAreRegistered(mbeanServers);
}

From source file:org.openehealth.coala.lifecycle.FacesAppListener.java

@Override
public void processEvent(SystemEvent event) throws AbortProcessingException {

    /*//w  ww.  ja v a2  s.c  om
     * STARTUP PHASE - nothing yet
     */
    if (event instanceof PostConstructApplicationEvent) {
        LOG.info("PostConstructApplicationEvent is called");
    }

    /*
     * SHUTDOWN PHASE - cleaning up PDQ/XDS ipf routes (mwiesner)
     */

    if (event instanceof PreDestroyApplicationEvent) {
        LOG.info("PreDestroyApplicationEvent is called");
        MBeanServer mbserver = null;

        ArrayList<MBeanServer> mbservers = MBeanServerFactory.findMBeanServer(null);

        if (mbservers.size() > 0) {
            mbserver = (MBeanServer) mbservers.get(0);
        }

        if (mbserver != null) {
            LOG.info("Found our MBean server instance...");
        } else {
            mbserver = MBeanServerFactory.createMBeanServer();
        }

        try {
            Set<ObjectInstance> mbeans = mbserver.queryMBeans(null, null);
            for (ObjectInstance mb : mbeans) {
                if (mb.getObjectName().getCanonicalName().contains("camelContext-pdq")) {
                    LOG.info("Successfully removed MBean: " + mb.getObjectName().getCanonicalName());
                    mbserver.unregisterMBean(mb.getObjectName());
                } else if (mb.getObjectName().getCanonicalName().contains("camelContext-xds")) {
                    LOG.info("Successfully removed MBean: " + mb.getObjectName().getCanonicalName());
                    mbserver.unregisterMBean(mb.getObjectName());
                }
            }
        } catch (InstanceNotFoundException infe) {
            LOG.warn("Ignoring to unregister pdq/xds camelContext, as it was not found!?");
        } catch (Throwable e) {
            LOG.error(e.getLocalizedMessage(), e);
        }
    }
}

From source file:org.eclipse.ecr.core.storage.sql.reload.RepositoryReloader.java

public static MBeanServer locateJBoss() {
    for (MBeanServer server : MBeanServerFactory.findMBeanServer(null)) {
        if (server.getDefaultDomain().equals("jboss")) {
            return server;
        }//from  w w w .j a  v  a  2  s.  co  m
    }
    return null;
}

From source file:com.flexive.shared.mbeans.MBeanHelper.java

private static List<MBeanServer> findMBeanServers() {
    final ArrayList<MBeanServer> servers = MBeanServerFactory.findMBeanServer(null);
    if (servers.isEmpty()) {
        LOG.error("No active JMX MBean servers found - creating a new one");
        servers.add(MBeanServerFactory.createMBeanServer());
    }//w  ww . j  a  v  a 2 s.  com
    return servers;
}

From source file:com.beginner.core.utils.ProjectUtil.java

/**
 * ?Web???(???)//  www . j a  v  a 2s  . c o m
 */
public static String getPort() {
    String result = "";
    try {
        MBeanServer mBeanServer = null;
        List<MBeanServer> mBeanServers = MBeanServerFactory.findMBeanServer(null);
        if (mBeanServers.size() > 0) {
            for (MBeanServer _mBeanServer : mBeanServers) {
                mBeanServer = _mBeanServer;
                break;
            }
        }
        if (mBeanServer == null) {
            throw new IllegalStateException("?JVM?MBeanServer.");
        }
        Set<ObjectName> objectNames = null;
        objectNames = mBeanServer.queryNames(new ObjectName("Catalina:type=Connector,*"), null);
        if (objectNames == null || objectNames.size() <= 0) {
            throw new IllegalStateException("?JVM?MBeanServer : "
                    + mBeanServer.getDefaultDomain() + " ??.");
        }
        for (ObjectName objectName : objectNames) {
            String protocol = (String) mBeanServer.getAttribute(objectName, "protocol");
            if (protocol.equals("HTTP/1.1")) {
                int port = (Integer) mBeanServer.getAttribute(objectName, "port");
                result = port + StringUtils.EMPTY;
            }
        }
    } catch (Exception e) {
        logger.error("?WEB?HTTP/1.1???", e);
    }
    return result;
}

From source file:org.mule.module.management.agent.YourKitProfilerAgent.java

public void initialise() throws InitialisationException {
    if (!isApiAvailable()) {
        logger.warn("Cannot find YourKit API. Profiler JMX Agent will be unregistered.");
        unregisterMeQuietly();//from  w  w  w.j ava 2  s  .  c om
        return;
    }

    final List servers = MBeanServerFactory.findMBeanServer(null);
    if (servers.isEmpty()) {
        throw new InitialisationException(ManagementMessages.noMBeanServerAvailable(), this);
    }

    try {
        mBeanServer = (MBeanServer) servers.get(0);

        profilerName = jmxSupport
                .getObjectName(jmxSupport.getDomainName(muleContext) + ":" + PROFILER_OBJECT_NAME);

        // unregister existing YourKit MBean first if required
        unregisterMBeansIfNecessary();
        mBeanServer.registerMBean(new YourKitProfilerService(), profilerName);
    } catch (Exception e) {
        throw new InitialisationException(CoreMessages.failedToStart(this.getName()), e, this);
    }
}

From source file:org.opennms.netmgt.eventd.adaptors.EventHandlerMBeanProxy.java

private List<MBeanServer> findMBeanServers() {
    // In java 1.5 this returns a generic ArrayList
    return MBeanServerFactory.findMBeanServer(null);
}

From source file:org.springframework.jmx.support.JmxUtils.java

/**
 * Attempt to find a locally running {@code MBeanServer}. Fails if no
 * {@code MBeanServer} can be found. Logs a warning if more than one
 * {@code MBeanServer} found, returning the first one from the list.
 * @param agentId the agent identifier of the MBeanServer to retrieve.
 * If this parameter is {@code null}, all registered MBeanServers are considered.
 * If the empty String is given, the platform MBeanServer will be returned.
 * @return the {@code MBeanServer} if found
 * @throws MBeanServerNotFoundException if no {@code MBeanServer} could be found
 * @see javax.management.MBeanServerFactory#findMBeanServer(String)
 *//*from  w  w w .ja  v a 2  s .c om*/
public static MBeanServer locateMBeanServer(@Nullable String agentId) throws MBeanServerNotFoundException {
    MBeanServer server = null;

    // null means any registered server, but "" specifically means the platform server
    if (!"".equals(agentId)) {
        List<MBeanServer> servers = MBeanServerFactory.findMBeanServer(agentId);
        if (!CollectionUtils.isEmpty(servers)) {
            // Check to see if an MBeanServer is registered.
            if (servers.size() > 1 && logger.isWarnEnabled()) {
                logger.warn("Found more than one MBeanServer instance"
                        + (agentId != null ? " with agent id [" + agentId + "]" : "")
                        + ". Returning first from list.");
            }
            server = servers.get(0);
        }
    }

    if (server == null && !StringUtils.hasLength(agentId)) {
        // Attempt to load the PlatformMBeanServer.
        try {
            server = ManagementFactory.getPlatformMBeanServer();
        } catch (SecurityException ex) {
            throw new MBeanServerNotFoundException("No specific MBeanServer found, "
                    + "and not allowed to obtain the Java platform MBeanServer", ex);
        }
    }

    if (server == null) {
        throw new MBeanServerNotFoundException("Unable to locate an MBeanServer instance"
                + (agentId != null ? " with agent id [" + agentId + "]" : ""));
    }

    if (logger.isDebugEnabled()) {
        logger.debug("Found MBeanServer: " + server);
    }
    return server;
}

From source file:org.mule.module.management.agent.WrapperManagerAgent.java

@Override
public void initialise() throws InitialisationException {
    try {//from  w  ww .j  av a2s . co m
        List<?> servers = MBeanServerFactory.findMBeanServer(null);
        if (servers.isEmpty()) {
            throw new InitialisationException(ManagementMessages.noMBeanServerAvailable(), this);
        }

        mBeanServer = (MBeanServer) servers.get(0);

        /*
          Perform an extra check ourselves. If 'wrapper.native_library' property has
          not been set, which is the case for embedded scenarios, don't even try to
          construct the wrapper manager bean, as it performs a number of checks internally
          and outputs a very verbose warning.
        */
        boolean launchedByWrapper;
        if (System.getProperty(WRAPPER_SYSTEM_PROPERTY_NAME) == null) {
            launchedByWrapper = false;
        }
        // Check if an external process registered a wrapper MBean under default name.
        else if (mBeanServer.isRegistered(jmxSupport.getObjectName(DEFAULT_WRAPPER_MBEAN_NAME))) {
            logger.info("Mule is embedded in a container already launched by a wrapper."
                    + "Duplicates will not be registered. Use the " + DEFAULT_WRAPPER_MBEAN_NAME + " MBean "
                    + "instead for control.");
            unregisterMeQuietly();
            return;
        } else {
            lazyInitWrapperManager();
            launchedByWrapper = wrapperManagerRef.get().isControlledByNativeWrapper();
        }

        if (!launchedByWrapper) {
            logger.info("This JVM hasn't been launched by the wrapper, the agent will not run.");
            unregisterMeQuietly();
            return;
        }

        final boolean containerMode = muleContext.getConfiguration().isContainerMode();
        if (containerMode) {
            // container mode, register mbean under Mule domain, no duplicates under each app's domain
            wrapperName = jmxSupport
                    .getObjectName(JmxSupport.DEFAULT_JMX_DOMAIN_PREFIX + ":" + WRAPPER_JMX_NAME);
            if (mBeanServer.isRegistered(wrapperName)) {
                // ignore duplicate invocations when running in Mule container mode
                return;
            }
        } else {
            // embedded case, use Mule's single domain
            wrapperName = jmxSupport
                    .getObjectName(jmxSupport.getDomainName(muleContext) + ":" + WRAPPER_JMX_NAME);
        }

        unregisterMBeansIfNecessary();
        mBeanServer.registerMBean(wrapperManagerRef.get(), wrapperName);
    } catch (InitialisationException iex) {
        // rethrow
        throw iex;
    } catch (Exception e) {
        throw new InitialisationException(CoreMessages.failedToStart("wrapper agent"), e, this);
    }
}