FileDocCategorySizeDatePackage
GlobalResourcesLifecycleListener.javaAPI DocApache Tomcat 6.0.148135Fri Jul 20 04:20:34 BST 2007org.apache.catalina.mbeans

GlobalResourcesLifecycleListener

public class GlobalResourcesLifecycleListener extends Object implements org.apache.catalina.LifecycleListener
Implementation of LifecycleListener that instantiates the set of MBeans associated with global JNDI resources that are subject to management.
author
Craig R. McClanahan
version
$Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
since
4.1

Fields Summary
private static org.apache.juli.logging.Log
log
protected org.apache.catalina.Lifecycle
component
The owning Catalina component that we are attached to.
protected static org.apache.tomcat.util.modeler.Registry
registry
The configuration information registry for our managed beans.
Constructors Summary
Methods Summary
protected voidcreateMBeans()
Create the MBeans for the interesting global JNDI resources.


        // Look up our global naming context
        Context context = null;
        try {
            context = (Context) (new InitialContext()).lookup("java:/");
        } catch (NamingException e) {
            log.error("No global naming context defined for server");
            return;
        }

        // Recurse through the defined global JNDI resources context
        try {
            createMBeans("", context);
        } catch (NamingException e) {
            log.error("Exception processing Global JNDI Resources", e);
        }

    
protected voidcreateMBeans(java.lang.String prefix, javax.naming.Context context)
Create the MBeans for the interesting global JNDI resources in the specified naming context.

param
prefix Prefix for complete object name paths
param
context Context to be scanned
exception
NamingException if a JNDI exception occurs


        if (log.isDebugEnabled()) {
            log.debug("Creating MBeans for Global JNDI Resources in Context '" +
                prefix + "'");
        }

        try {
            NamingEnumeration bindings = context.listBindings("");
            while (bindings.hasMore()) {
                Binding binding = (Binding) bindings.next();
                String name = prefix + binding.getName();
                Object value = context.lookup(binding.getName());
                if (log.isDebugEnabled()) {
                    log.debug("Checking resource " + name);
                }
                if (value instanceof Context) {
                    createMBeans(name + "/", (Context) value);
                } else if (value instanceof UserDatabase) {
                    try {
                        createMBeans(name, (UserDatabase) value);
                    } catch (Exception e) {
                        log.error("Exception creating UserDatabase MBeans for " + name,
                                e);
                    }
                }
            }
        } catch( RuntimeException ex) {
            log.error("RuntimeException " + ex);
        } catch( OperationNotSupportedException ex) {
            log.error("Operation not supported " + ex);
        }

    
protected voidcreateMBeans(java.lang.String name, org.apache.catalina.UserDatabase database)
Create the MBeans for the specified UserDatabase and its contents.

param
name Complete resource name of this UserDatabase
param
database The UserDatabase to be processed
exception
Exception if an exception occurs while creating MBeans


        // Create the MBean for the UserDatabase itself
        if (log.isDebugEnabled()) {
            log.debug("Creating UserDatabase MBeans for resource " + name);
            log.debug("Database=" + database);
        }
        if (MBeanUtils.createMBean(database) == null) {
            throw new IllegalArgumentException
                ("Cannot create UserDatabase MBean for resource " + name);
        }

        // Create the MBeans for each defined Role
        Iterator roles = database.getRoles();
        while (roles.hasNext()) {
            Role role = (Role) roles.next();
            if (log.isDebugEnabled()) {
                log.debug("  Creating Role MBean for role " + role);
            }
            if (MBeanUtils.createMBean(role) == null) {
                throw new IllegalArgumentException
                    ("Cannot create Role MBean for role " + role);
            }
        }

        // Create the MBeans for each defined Group
        Iterator groups = database.getGroups();
        while (groups.hasNext()) {
            Group group = (Group) groups.next();
            if (log.isDebugEnabled()) {
                log.debug("  Creating Group MBean for group " + group);
            }
            if (MBeanUtils.createMBean(group) == null) {
                throw new IllegalArgumentException
                    ("Cannot create Group MBean for group " + group);
            }
        }

        // Create the MBeans for each defined User
        Iterator users = database.getUsers();
        while (users.hasNext()) {
            User user = (User) users.next();
            if (log.isDebugEnabled()) {
                log.debug("  Creating User MBean for user " + user);
            }
            if (MBeanUtils.createMBean(user) == null) {
                throw new IllegalArgumentException
                    ("Cannot create User MBean for user " + user);
            }
        }

    
protected voiddestroyMBeans()
Destroy the MBeans for the interesting global JNDI resources.


        if (log.isDebugEnabled()) {
            log.debug("Destroying MBeans for Global JNDI Resources");
        }

    
public voidlifecycleEvent(org.apache.catalina.LifecycleEvent event)
Primary entry point for startup and shutdown events.

param
event The event that has occurred



    // ---------------------------------------------- LifecycleListener Methods


                        
        

        if (Lifecycle.START_EVENT.equals(event.getType())) {
            component = event.getLifecycle();
            createMBeans();
        } else if (Lifecycle.STOP_EVENT.equals(event.getType())) {
            destroyMBeans();
            component = null;
        }