001: /*
002: * Copyright 2002,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.catalina.mbeans;
018:
019: import java.util.Iterator;
020: import javax.naming.Binding;
021: import javax.naming.Context;
022: import javax.naming.InitialContext;
023: import javax.naming.NamingEnumeration;
024: import javax.naming.NamingException;
025: import javax.naming.OperationNotSupportedException;
026: import org.apache.catalina.Group;
027: import org.apache.catalina.Lifecycle;
028: import org.apache.catalina.LifecycleEvent;
029: import org.apache.catalina.LifecycleListener;
030: import org.apache.catalina.Role;
031: import org.apache.catalina.User;
032: import org.apache.catalina.UserDatabase;
033: import org.apache.commons.modeler.Registry;
034: import org.apache.commons.logging.Log;
035: import org.apache.commons.logging.LogFactory;
036:
037: /**
038: * Implementation of <code>LifecycleListener</code> that instantiates the
039: * set of MBeans associated with global JNDI resources that are subject to
040: * management.
041: *
042: * @author Craig R. McClanahan
043: * @version $Revision: 1.5 $ $Date: 2004/06/09 20:38:16 $
044: * @since 4.1
045: */
046:
047: public class GlobalResourcesLifecycleListener implements
048: LifecycleListener {
049: private static Log log = LogFactory
050: .getLog(GlobalResourcesLifecycleListener.class);
051:
052: // ----------------------------------------------------- Instance Variables
053:
054: /**
055: * The owning Catalina component that we are attached to.
056: */
057: protected Lifecycle component = null;
058:
059: /**
060: * The configuration information registry for our managed beans.
061: */
062: protected static Registry registry = MBeanUtils.createRegistry();
063:
064: // ------------------------------------------------------------- Properties
065:
066: /**
067: * The debugging detail level for this component.
068: */
069: protected int debug = 0;
070:
071: public int getDebug() {
072: return (this .debug);
073: }
074:
075: public void setDebug(int debug) {
076: this .debug = debug;
077: }
078:
079: // ---------------------------------------------- LifecycleListener Methods
080:
081: /**
082: * Primary entry point for startup and shutdown events.
083: *
084: * @param event The event that has occurred
085: */
086: public void lifecycleEvent(LifecycleEvent event) {
087:
088: if (Lifecycle.START_EVENT.equals(event.getType())) {
089: component = event.getLifecycle();
090: createMBeans();
091: } else if (Lifecycle.STOP_EVENT.equals(event.getType())) {
092: destroyMBeans();
093: component = null;
094: }
095:
096: }
097:
098: // ------------------------------------------------------ Protected Methods
099:
100: /**
101: * Create the MBeans for the interesting global JNDI resources.
102: */
103: protected void createMBeans() {
104:
105: // Look up our global naming context
106: Context context = null;
107: try {
108: context = (Context) (new InitialContext()).lookup("java:/");
109: } catch (NamingException e) {
110: log.error("No global naming context defined for server");
111: return;
112: }
113:
114: // Recurse through the defined global JNDI resources context
115: try {
116: createMBeans("", context);
117: } catch (NamingException e) {
118: log.error("Exception processing Global JNDI Resources", e);
119: }
120:
121: }
122:
123: /**
124: * Create the MBeans for the interesting global JNDI resources in
125: * the specified naming context.
126: *
127: * @param prefix Prefix for complete object name paths
128: * @param context Context to be scanned
129: *
130: * @exception NamingException if a JNDI exception occurs
131: */
132: protected void createMBeans(String prefix, Context context)
133: throws NamingException {
134:
135: if (debug >= 1) {
136: log
137: .debug("Creating MBeans for Global JNDI Resources in Context '"
138: + prefix + "'");
139: }
140:
141: try {
142: NamingEnumeration bindings = context.listBindings("");
143: while (bindings.hasMore()) {
144: Binding binding = (Binding) bindings.next();
145: String name = prefix + binding.getName();
146: Object value = context.lookup(binding.getName());
147: if (debug >= 2) {
148: log.debug("Checking resource " + name);
149: }
150: if (value instanceof Context) {
151: createMBeans(name + "/", (Context) value);
152: } else if (value instanceof UserDatabase) {
153: try {
154: createMBeans(name, (UserDatabase) value);
155: } catch (Exception e) {
156: log.error(
157: "Exception creating UserDatabase MBeans for "
158: + name, e);
159: }
160: }
161: }
162: } catch (RuntimeException ex) {
163: log.error("RuntimeException " + ex);
164: } catch (OperationNotSupportedException ex) {
165: log.error("Operation not supported " + ex);
166: }
167:
168: }
169:
170: /**
171: * Create the MBeans for the specified UserDatabase and its contents.
172: *
173: * @param name Complete resource name of this UserDatabase
174: * @param database The UserDatabase to be processed
175: *
176: * @exception Exception if an exception occurs while creating MBeans
177: */
178: protected void createMBeans(String name, UserDatabase database)
179: throws Exception {
180:
181: // Create the MBean for the UserDatabase itself
182: if (debug >= 2) {
183: log.debug("Creating UserDatabase MBeans for resource "
184: + name);
185: log.debug("Database=" + database);
186: }
187: if (MBeanUtils.createMBean(database) == null) {
188: throw new IllegalArgumentException(
189: "Cannot create UserDatabase MBean for resource "
190: + name);
191: }
192:
193: // Create the MBeans for each defined Role
194: Iterator roles = database.getRoles();
195: while (roles.hasNext()) {
196: Role role = (Role) roles.next();
197: if (debug >= 3) {
198: log.debug(" Creating Role MBean for role " + role);
199: }
200: if (MBeanUtils.createMBean(role) == null) {
201: throw new IllegalArgumentException(
202: "Cannot create Role MBean for role " + role);
203: }
204: }
205:
206: // Create the MBeans for each defined Group
207: Iterator groups = database.getGroups();
208: while (groups.hasNext()) {
209: Group group = (Group) groups.next();
210: if (debug >= 3) {
211: log.debug(" Creating Group MBean for group " + group);
212: }
213: if (MBeanUtils.createMBean(group) == null) {
214: throw new IllegalArgumentException(
215: "Cannot create Group MBean for group " + group);
216: }
217: }
218:
219: // Create the MBeans for each defined User
220: Iterator users = database.getUsers();
221: while (users.hasNext()) {
222: User user = (User) users.next();
223: if (debug >= 3) {
224: log.debug(" Creating User MBean for user " + user);
225: }
226: if (MBeanUtils.createMBean(user) == null) {
227: throw new IllegalArgumentException(
228: "Cannot create User MBean for user " + user);
229: }
230: }
231:
232: }
233:
234: /**
235: * Destroy the MBeans for the interesting global JNDI resources.
236: */
237: protected void destroyMBeans() {
238:
239: if (debug >= 1) {
240: log.debug("Destroying MBeans for Global JNDI Resources");
241: }
242:
243: }
244:
245: /**
246: * Log a message.
247: *
248: * @param message The message to be logged
249: */
250: protected void log(String message) {
251: log.info(message);
252: }
253:
254: /**
255: * Log a message and associated exception.
256: *
257: * @param message The message to be logged
258: * @param throwable The exception to be logged
259: */
260: protected void log(String message, Throwable throwable) {
261: log.info(message, throwable);
262: }
263:
264: }
|