Example usage for javax.management MBeanServer isRegistered

List of usage examples for javax.management MBeanServer isRegistered

Introduction

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

Prototype

public boolean isRegistered(ObjectName name);

Source Link

Usage

From source file:org.jboss.web.tomcat.tc5.Tomcat5.java

public void stopService() throws Exception {

    String objectNameS = catalinaDomain + ":type=server";
    ObjectName objectName = new ObjectName(objectNameS);

    server.invoke(objectName, "stop", new Object[] {}, new String[] {});

    server.invoke(objectName, "destroy", new Object[] {}, new String[] {});

    server.unregisterMBean(objectName);/* w  w w.  j  a v  a2  s . c om*/

    MBeanServer server2 = server;

    // deregister with MainDeployer
    mainDeployer.removeDeployer(thisProxy);

    // Unregister any remaining jboss.web or Catalina MBeans
    ObjectName queryObjectName = new ObjectName(catalinaDomain + ":*");
    Iterator iterator = server2.queryMBeans(queryObjectName, null).iterator();
    while (iterator.hasNext()) {
        ObjectInstance oi = (ObjectInstance) iterator.next();
        ObjectName toRemove = oi.getObjectName();
        // Exception: Don't unregister the service right now
        if (!"WebServer".equals(toRemove.getKeyProperty("service"))) {
            if (server2.isRegistered(toRemove)) {
                server2.unregisterMBean(toRemove);
            }
        }
    }
    queryObjectName = new ObjectName("Catalina:*");
    iterator = server2.queryMBeans(queryObjectName, null).iterator();
    while (iterator.hasNext()) {
        ObjectInstance oi = (ObjectInstance) iterator.next();
        ObjectName name = oi.getObjectName();
        server2.unregisterMBean(name);
    }

}

From source file:io.fabric8.api.jmx.FabricManager.java

public void registerMBeanServer(MBeanServer mbeanServer) {
    try {//from  www.j  av  a2s  .  c o m
        ObjectName name = getObjectName();
        if (!mbeanServer.isRegistered(name)) {
            mbeanServer.registerMBean(this, name);
        }
    } catch (Exception e) {
        LOG.warn("An error occured during mbean server registration: " + e, e);
    }
}

From source file:org.red5.server.tomcat.TomcatLoader.java

/**
 * Starts a web application and its red5 (spring) component. This is
 * basically a stripped down version of init().
 * /*from   ww  w. ja  va 2 s  .c  o m*/
 * @return true on success
 */
public boolean startWebApplication(String applicationName) {
    log.info("Starting Tomcat - Web application");
    boolean result = false;

    //get a reference to the current threads classloader
    final ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();

    log.debug("Webapp root: {}", webappFolder);

    // application directory
    String contextName = '/' + applicationName;

    Container ctx = null;

    if (webappFolder == null) {
        // Use default webapps directory
        webappFolder = System.getProperty("red5.root") + "/webapps";
    }
    System.setProperty("red5.webapp.root", webappFolder);
    log.info("Application root: {}", webappFolder);

    // scan for additional webapp contexts

    // Root applications directory
    File appDirBase = new File(webappFolder);

    // check if the context already exists for the host
    if ((ctx = host.findChild(contextName)) == null) {
        log.debug("Context did not exist in host");
        String webappContextDir = FileUtil.formatPath(appDirBase.getAbsolutePath(), applicationName);
        log.debug("Webapp context directory (full path): {}", webappContextDir);
        // set the newly created context as the current container
        ctx = addContext(contextName, webappContextDir);
    } else {
        log.debug("Context already exists in host");
    }

    final ServletContext servletContext = ((Context) ctx).getServletContext();
    log.debug("Context initialized: {}", servletContext.getContextPath());

    String prefix = servletContext.getRealPath("/");
    log.debug("Path: {}", prefix);

    try {
        Loader cldr = ctx.getLoader();
        log.debug("Loader delegate: {} type: {}", cldr.getDelegate(), cldr.getClass().getName());
        if (cldr instanceof WebappLoader) {
            log.debug("WebappLoader class path: {}", ((WebappLoader) cldr).getClasspath());
        }
        final ClassLoader webClassLoader = cldr.getClassLoader();
        log.debug("Webapp classloader: {}", webClassLoader);

        // get the (spring) config file path
        final String contextConfigLocation = servletContext.getInitParameter("contextConfigLocation") == null
                ? defaultSpringConfigLocation
                : servletContext.getInitParameter("contextConfigLocation");
        log.debug("Spring context config location: {}", contextConfigLocation);

        // get the (spring) parent context key
        final String parentContextKey = servletContext.getInitParameter("parentContextKey") == null
                ? defaultParentContextKey
                : servletContext.getInitParameter("parentContextKey");
        log.debug("Spring parent context key: {}", parentContextKey);

        //set current threads classloader to the webapp classloader
        Thread.currentThread().setContextClassLoader(webClassLoader);

        //create a thread to speed-up application loading
        Thread thread = new Thread("Launcher:" + servletContext.getContextPath()) {
            @SuppressWarnings("cast")
            public void run() {
                //set current threads classloader to the webapp classloader
                Thread.currentThread().setContextClassLoader(webClassLoader);

                // create a spring web application context
                XmlWebApplicationContext appctx = new XmlWebApplicationContext();
                appctx.setClassLoader(webClassLoader);
                appctx.setConfigLocations(new String[] { contextConfigLocation });

                // check for red5 context bean
                ApplicationContext parentAppCtx = null;

                if (applicationContext.containsBean(defaultParentContextKey)) {
                    parentAppCtx = (ApplicationContext) applicationContext.getBean(defaultParentContextKey);
                } else {
                    log.warn("{} bean was not found in context: {}", defaultParentContextKey,
                            applicationContext.getDisplayName());
                    // lookup context loader and attempt to get what we need from it
                    if (applicationContext.containsBean("context.loader")) {
                        ContextLoader contextLoader = (ContextLoader) applicationContext
                                .getBean("context.loader");
                        parentAppCtx = contextLoader.getContext(defaultParentContextKey);
                    } else {
                        log.debug("Context loader was not found, trying JMX");
                        MBeanServer mbs = JMXFactory.getMBeanServer();
                        // get the ContextLoader from jmx
                        ObjectName oName = JMXFactory.createObjectName("type", "ContextLoader");
                        ContextLoaderMBean proxy = null;
                        if (mbs.isRegistered(oName)) {
                            proxy = (ContextLoaderMBean) MBeanServerInvocationHandler.newProxyInstance(mbs,
                                    oName, ContextLoaderMBean.class, true);
                            log.debug("Context loader was found");
                            parentAppCtx = proxy.getContext(defaultParentContextKey);
                        } else {
                            log.warn("Context loader was not found");
                        }
                    }
                }
                if (log.isDebugEnabled()) {
                    if (appctx.getParent() != null) {
                        log.debug("Parent application context: {}", appctx.getParent().getDisplayName());
                    }
                }

                appctx.setParent(parentAppCtx);

                appctx.setServletContext(servletContext);
                // set the root webapp ctx attr on the each
                // servlet context so spring can find it later
                servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
                        appctx);
                appctx.refresh();
            }
        };
        thread.setDaemon(true);
        thread.start();

        result = true;
    } catch (Throwable t) {
        log.error("Error setting up context: {} due to: {}", servletContext.getContextPath(), t.getMessage());
        t.printStackTrace();
    } finally {
        //reset the classloader
        Thread.currentThread().setContextClassLoader(originalClassLoader);
    }

    return result;
}

From source file:org.jboss.web.tomcat.service.session.JBossCacheCluster.java

/**
 * Registers this object and the tree cache (if we created it) with JMX.
 *//*from w  ww . j av  a 2 s  .  c  o m*/
private void registerMBeans() {
    try {
        MBeanServer server = getMBeanServer();

        String domain;
        if (container instanceof ContainerBase) {
            domain = ((ContainerBase) container).getDomain();
        } else {
            domain = server.getDefaultDomain();
        }

        String name = ":type=Cluster";
        if (container instanceof Host) {
            name += ",host=" + container.getName();
        } else if (container instanceof Engine) {
            name += ",engine=" + container.getName();
        }

        ObjectName clusterName = new ObjectName(domain + name);

        if (server.isRegistered(clusterName)) {
            log.warn("MBean " + clusterName + " already registered");
        } else {
            this.objectName = clusterName;
            server.registerMBean(this, objectName);
        }

    } catch (Exception ex) {
        log.error(ex.getMessage(), ex);
    }
}

From source file:io.fabric8.api.jmx.FabricManager.java

public void unregisterMBeanServer(MBeanServer mbeanServer) {
    if (mbeanServer != null) {
        try {/*from w  w  w. j a v  a 2  s . co  m*/
            ObjectName name = getObjectName();
            if (mbeanServer.isRegistered(name)) {
                mbeanServer.unregisterMBean(name);
            }
        } catch (Exception e) {
            LOG.warn("An error occured during mbean server registration: " + e, e);
        }
    }
}

From source file:io.fabric8.core.jmx.FabricManager.java

public void registerMBeanServer(ShutdownTracker shutdownTracker, MBeanServer mbeanServer) {
    try {//from  w  w w.j  a v a 2  s  .co  m
        ObjectName name = getObjectName();
        if (!mbeanServer.isRegistered(name)) {
            StandardMBean mbean = new StandardMBean(this, FabricManagerMBean.class);
            mbeanServer.registerMBean(mbean, name);
        }
    } catch (Exception e) {
        LOG.warn("An error occured during mbean server registration: " + e, e);
    }
}

From source file:org.jboss.web.tomcat.tc5.session.JBossCacheCluster.java

/**
 * Registers this object and the tree cache (if we created it) with JMX.
 *///w w  w  .  j  av  a 2  s  .  c o  m
private void registerMBeans() {
    try {
        MBeanServer server = getMBeanServer();

        String domain;
        if (container instanceof ContainerBase) {
            domain = ((ContainerBase) container).getDomain();
        } else {
            domain = server.getDefaultDomain();
        }

        String name = ":type=Cluster";
        if (container instanceof Host) {
            name += ",host=" + container.getName();
        } else if (container instanceof Engine) {
            name += ",engine=" + container.getName();
        }

        ObjectName clusterName = new ObjectName(domain + name);

        if (server.isRegistered(clusterName)) {
            log.warn("MBean " + clusterName + " already registered");
        } else {
            this.objectName = clusterName;
            server.registerMBean(this, objectName);
        }

        if (treeCacheLocal) {
            // Register the treeCache
            ObjectName treeCacheName = new ObjectName(treeCacheObjectName);
            server.registerMBean(getTreeCache(), treeCacheName);
        }

    } catch (Exception ex) {
        log.error(ex.getMessage(), ex);
    }
}

From source file:org.red5.server.scope.Scope.java

protected void unregisterJMX() {
    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    if (oName != null && mbs.isRegistered(oName)) {
        try {/*from w w w.ja v a  2s .c  o m*/
            mbs.unregisterMBean(oName);
        } catch (Exception e) {
            log.warn("Exception unregistering: {}", oName, e);
        }
        oName = null;
    }
}

From source file:com.brienwheeler.lib.jmx.logging.Log4jMBeanExporter.java

private synchronized int registerMBeansInternal() {
    // protect against JMX invocation while shutting down
    if (shutdown)
        return 0;

    MBeanServer server = JmxUtils.locateMBeanServer();

    HierarchyDynamicMBean hdm;//from w  w  w . j  av a  2  s.c om
    synchronized (this.getClass()) {
        boolean usedNew = hierarchyDynamicMBean.compareAndSet(null, new HierarchyDynamicMBean());
        hdm = hierarchyDynamicMBean.get();
        if (usedNew) {
            try {
                ObjectName mbo = new ObjectName(LOG4J_HIERARCHY_DEFAULT);
                server.registerMBean(hdm, mbo);
                registeredHierarchy = true;
                // Add the root logger to the Hierarchy MBean
                hdm.addLoggerMBean(Logger.getRootLogger().getName());
            } catch (Exception e) {
                log.error("Error initializing Log4jMBeanExporter", e);
                return 0;
            }
        }
    }

    // Get each logger from the Log4J Repository and add it to
    // the Hierarchy MBean created above.
    LoggerRepository r = LogManager.getLoggerRepository();

    Enumeration<?> loggers = r.getCurrentLoggers();
    int count = 0;

    while (loggers.hasMoreElements()) {
        String name = ((Logger) loggers.nextElement()).getName();
        // this name definition copied from HierarchyDynamicMBean
        ObjectName objectName;
        try {
            objectName = new ObjectName("log4j", "logger", name);
        } catch (Exception e) {
            log.error("Error creating JMX name for " + name, e);
            continue;
        }

        if (!server.isRegistered(objectName)) {
            if (log.isDebugEnabled()) {
                log.debug("[contextInitialized]: Registering " + name);
            }
            registeredNames.add(hdm.addLoggerMBean(name));
            count++;
        }
    }

    log.debug("registered " + count + " new Log4j MBeans");
    return count;
}

From source file:org.red5.server.scope.Scope.java

protected void registerJMX() {
    // register with jmx
    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    try {/*www .j av  a 2 s. c  o  m*/
        String cName = this.getClass().getName();
        if (cName.indexOf('.') != -1) {
            cName = cName.substring(cName.lastIndexOf('.')).replaceFirst("[\\.]", "");
        }
        oName = new ObjectName(String.format("org.red5.server:type=%s,name=%s", cName, name));
        // don't reregister
        if (!mbs.isRegistered(oName)) {
            mbs.registerMBean(new StandardMBean(this, ScopeMXBean.class, true), oName);
        }
    } catch (Exception e) {
        log.warn("Error on jmx registration", e);
    }
}