/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
*
* Portions Copyright Apache Software Foundation.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can obtain
* a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
* or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
* Sun designates this particular file as subject to the "Classpath" exception
* as provided by Sun in the GPL Version 2 section of the License file that
* accompanied this code. If applicable, add the following below the License
* Header, with the fields enclosed by brackets [] replaced by your own
* identifying information: "Portions Copyrighted [year]
* [name of copyright owner]"
*
* Contributor(s):
*
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package org.apache.catalina.mbeans;
import java.util.Iterator;
import javax.naming.Binding;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.OperationNotSupportedException;
import org.apache.catalina.Group;
import org.apache.catalina.Lifecycle;
import org.apache.catalina.LifecycleEvent;
import org.apache.catalina.LifecycleListener;
import org.apache.catalina.Role;
import org.apache.catalina.User;
import org.apache.catalina.UserDatabase;
import com.sun.org.apache.commons.modeler.Registry;
import com.sun.org.apache.commons.logging.Log;
import com.sun.org.apache.commons.logging.LogFactory;
/**
* Implementation of <code>LifecycleListener</code> that instantiates the
* set of MBeans associated with global JNDI resources that are subject to
* management.
*
* @author Craig R. McClanahan
* @version $Revision: 1.5 $ $Date: 2007/05/05 05:32:09 $
* @since 4.1
*/
public class GlobalResourcesLifecycleListener
implements LifecycleListener {
private static Log log = LogFactory.getLog(GlobalResourcesLifecycleListener.class);
// ----------------------------------------------------- Instance Variables
/**
* The owning Catalina component that we are attached to.
*/
protected Lifecycle component = null;
/**
* The configuration information registry for our managed beans.
*/
protected static final Registry registry = MBeanUtils.createRegistry();
// ------------------------------------------------------------- Properties
/**
* The debugging detail level for this component.
*/
protected int debug = 0;
public int getDebug() {
return (this.debug);
}
public void setDebug(int debug) {
this.debug = debug;
}
// ---------------------------------------------- LifecycleListener Methods
/**
* Primary entry point for startup and shutdown events.
*
* @param event The event that has occurred
*/
public void lifecycleEvent(LifecycleEvent event) {
if (Lifecycle.START_EVENT.equals(event.getType())) {
component = event.getLifecycle();
createMBeans();
} else if (Lifecycle.STOP_EVENT.equals(event.getType())) {
destroyMBeans();
component = null;
}
}
// ------------------------------------------------------ Protected Methods
/**
* Create the MBeans for the interesting global JNDI resources.
*/
protected void createMBeans() {
// 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);
}
}
/**
* 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
*/
protected void createMBeans(String prefix, Context context)
throws NamingException {
if (debug >= 1) {
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 (debug >= 2) {
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);
}
}
/**
* 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
*/
protected void createMBeans(String name, UserDatabase database)
throws Exception {
// Create the MBean for the UserDatabase itself
if (debug >= 2) {
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 (debug >= 3) {
log.error(" 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 (debug >= 3) {
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 (debug >= 3) {
log.debug(" Creating User MBean for user " + user);
}
if (MBeanUtils.createMBean(user) == null) {
throw new IllegalArgumentException
("Cannot create User MBean for user " + user);
}
}
}
/**
* Destroy the MBeans for the interesting global JNDI resources.
*/
protected void destroyMBeans() {
if (debug >= 1) {
log.debug("Destroying MBeans for Global JNDI Resources");
}
}
/**
* Log a message.
*
* @param message The message to be logged
*/
protected void log(String message) {
log.info(message);
}
/**
* Log a message and associated exception.
*
* @param message The message to be logged
* @param throwable The exception to be logged
*/
protected void log(String message, Throwable throwable) {
log.info(message, throwable);
}
}
|