001: /*
002: * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/mbeans/GlobalResourcesLifecycleListener.java,v 1.3 2002/02/03 00:56:57 craigmcc Exp $
003: * $Revision: 1.3 $
004: * $Date: 2002/02/03 00:56:57 $
005: *
006: * ====================================================================
007: *
008: * The Apache Software License, Version 1.1
009: *
010: * Copyright (c) 2002 The Apache Software Foundation. All rights
011: * reserved.
012: *
013: * Redistribution and use in source and binary forms, with or without
014: * modification, are permitted provided that the following conditions
015: * are met:
016: *
017: * 1. Redistributions of source code must retain the above copyright
018: * notice, this list of conditions and the following disclaimer.
019: *
020: * 2. Redistributions in binary form must reproduce the above copyright
021: * notice, this list of conditions and the following disclaimer in
022: * the documentation and/or other materials provided with the
023: * distribution.
024: *
025: * 3. The end-user documentation included with the redistribution, if
026: * any, must include the following acknowlegement:
027: * "This product includes software developed by the
028: * Apache Software Foundation (http://www.apache.org/)."
029: * Alternately, this acknowlegement may appear in the software itself,
030: * if and wherever such third-party acknowlegements normally appear.
031: *
032: * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
033: * Foundation" must not be used to endorse or promote products derived
034: * from this software without prior written permission. For written
035: * permission, please contact apache@apache.org.
036: *
037: * 5. Products derived from this software may not be called "Apache"
038: * nor may "Apache" appear in their names without prior written
039: * permission of the Apache Group.
040: *
041: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
042: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
043: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
044: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
045: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
046: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
047: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
048: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
049: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
050: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
051: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
052: * SUCH DAMAGE.
053: * ====================================================================
054: *
055: * This software consists of voluntary contributions made by many
056: * individuals on behalf of the Apache Software Foundation. For more
057: * information on the Apache Software Foundation, please see
058: * <http://www.apache.org/>.
059: *
060: * [Additional notices, if required by prior licensing conditions]
061: *
062: */
063:
064: package org.apache.catalina.mbeans;
065:
066: import java.util.Iterator;
067: import javax.management.MBeanException;
068: import javax.management.MBeanServer;
069: import javax.management.ObjectName;
070: import javax.naming.Binding;
071: import javax.naming.Context;
072: import javax.naming.InitialContext;
073: import javax.naming.NamingEnumeration;
074: import javax.naming.NamingException;
075: import org.apache.catalina.Group;
076: import org.apache.catalina.Lifecycle;
077: import org.apache.catalina.LifecycleEvent;
078: import org.apache.catalina.LifecycleListener;
079: import org.apache.catalina.Role;
080: import org.apache.catalina.Server;
081: import org.apache.catalina.User;
082: import org.apache.catalina.UserDatabase;
083: import org.apache.catalina.core.StandardServer;
084: import org.apache.commons.modeler.ManagedBean;
085: import org.apache.commons.modeler.Registry;
086:
087: /**
088: * Implementation of <code>LifecycleListener</code> that instantiates the
089: * set of MBeans associated with global JNDI resources that are subject to
090: * management.
091: *
092: * @author Craig R. McClanahan
093: * @version $Revision: 1.3 $ $Date: 2002/02/03 00:56:57 $
094: * @since 4.1
095: */
096:
097: public class GlobalResourcesLifecycleListener implements
098: LifecycleListener {
099:
100: // ----------------------------------------------------- Instance Variables
101:
102: /**
103: * The owning Catalina component that we are attached to.
104: */
105: protected Lifecycle component = null;
106:
107: /**
108: * The configuration information registry for our managed beans.
109: */
110: protected static Registry registry = MBeanUtils.createRegistry();
111:
112: // ------------------------------------------------------------- Properties
113:
114: /**
115: * The debugging detail level for this component.
116: */
117: protected int debug = 0;
118:
119: public int getDebug() {
120: return (this .debug);
121: }
122:
123: public void setDebug(int debug) {
124: this .debug = debug;
125: }
126:
127: // ---------------------------------------------- LifecycleListener Methods
128:
129: /**
130: * Primary entry point for startup and shutdown events.
131: *
132: * @param event The event that has occurred
133: */
134: public void lifecycleEvent(LifecycleEvent event) {
135:
136: if (Lifecycle.START_EVENT.equals(event.getType())) {
137: component = event.getLifecycle();
138: createMBeans();
139: } else if (Lifecycle.STOP_EVENT.equals(event.getType())) {
140: destroyMBeans();
141: component = null;
142: }
143:
144: }
145:
146: // ------------------------------------------------------ Protected Methods
147:
148: /**
149: * Create the MBeans for the interesting global JNDI resources.
150: */
151: protected void createMBeans() {
152:
153: // Look up our global naming context
154: Context context = null;
155: try {
156: context = (Context) (new InitialContext()).lookup("java:/");
157: } catch (NamingException e) {
158: e.printStackTrace();
159: throw new IllegalStateException(
160: "No global naming context defined for server");
161: }
162:
163: // Recurse through the defined global JNDI resources context
164: try {
165: createMBeans("", context);
166: } catch (NamingException e) {
167: log("Exception processing Global JNDI Resources", e);
168: }
169:
170: }
171:
172: /**
173: * Create the MBeans for the interesting global JNDI resources in
174: * the specified naming context.
175: *
176: * @param prefix Prefix for complete object name paths
177: * @param context Context to be scanned
178: *
179: * @exception NamingException if a JNDI exception occurs
180: */
181: protected void createMBeans(String prefix, Context context)
182: throws NamingException {
183:
184: if (debug >= 1) {
185: log("Creating MBeans for Global JNDI Resources in Context '"
186: + prefix + "'");
187: }
188:
189: NamingEnumeration bindings = context.listBindings("");
190: while (bindings.hasMore()) {
191: Binding binding = (Binding) bindings.next();
192: String name = prefix + binding.getName();
193: Object value = context.lookup(binding.getName());
194: if (debug >= 2) {
195: log("Checking resource " + name);
196: }
197: if (value instanceof Context) {
198: createMBeans(name + "/", (Context) value);
199: } else if (value instanceof UserDatabase) {
200: try {
201: createMBeans(name, (UserDatabase) value);
202: } catch (Exception e) {
203: log("Exception creating UserDatabase MBeans for "
204: + name, e);
205: }
206: }
207: }
208:
209: }
210:
211: /**
212: * Create the MBeans for the specified UserDatabase and its contents.
213: *
214: * @param name Complete resource name of this UserDatabase
215: * @param database The UserDatabase to be processed
216: *
217: * @exception Exception if an exception occurs while creating MBeans
218: */
219: protected void createMBeans(String name, UserDatabase database)
220: throws Exception {
221:
222: // Create the MBean for the UserDatabase itself
223: if (debug >= 2) {
224: log("Creating UserDatabase MBeans for resource " + name);
225: log("Database=" + database);
226: }
227: if (MBeanUtils.createMBean(database) == null) {
228: throw new IllegalArgumentException(
229: "Cannot create UserDatabase MBean for resource "
230: + name);
231: }
232:
233: // Create the MBeans for each defined Role
234: Iterator roles = database.getRoles();
235: while (roles.hasNext()) {
236: Role role = (Role) roles.next();
237: if (debug >= 3) {
238: log(" Creating Role MBean for role " + role);
239: }
240: if (MBeanUtils.createMBean(role) == null) {
241: throw new IllegalArgumentException(
242: "Cannot create Role MBean for role " + role);
243: }
244: }
245:
246: // Create the MBeans for each defined Group
247: Iterator groups = database.getGroups();
248: while (groups.hasNext()) {
249: Group group = (Group) groups.next();
250: if (debug >= 3) {
251: log(" Creating Group MBean for group " + group);
252: }
253: if (MBeanUtils.createMBean(group) == null) {
254: throw new IllegalArgumentException(
255: "Cannot create Group MBean for group " + group);
256: }
257: }
258:
259: // Create the MBeans for each defined User
260: Iterator users = database.getUsers();
261: while (users.hasNext()) {
262: User user = (User) users.next();
263: if (debug >= 3) {
264: log(" Creating User MBean for user " + user);
265: }
266: if (MBeanUtils.createMBean(user) == null) {
267: throw new IllegalArgumentException(
268: "Cannot create User MBean for user " + user);
269: }
270: }
271:
272: }
273:
274: /**
275: * Destroy the MBeans for the interesting global JNDI resources.
276: */
277: protected void destroyMBeans() {
278:
279: if (debug >= 1) {
280: log("Destroying MBeans for Global JNDI Resources");
281: }
282:
283: }
284:
285: /**
286: * The destination for log messages.
287: */
288: protected java.io.PrintStream stream = System.out;
289:
290: /**
291: * Log a message.
292: *
293: * @param message The message to be logged
294: */
295: protected void log(String message) {
296:
297: /*
298: if (stream == System.out) {
299: try {
300: stream = new java.io.PrintStream
301: (new java.io.FileOutputStream("grll.log"));
302: } catch (Throwable t) {
303: ;
304: }
305: }
306: */
307:
308: stream.print("GlobalResourcesLifecycleListener: ");
309: stream.println(message);
310:
311: }
312:
313: /**
314: * Log a message and associated exception.
315: *
316: * @param message The message to be logged
317: * @param throwable The exception to be logged
318: */
319: protected void log(String message, Throwable throwable) {
320:
321: log(message);
322: throwable.printStackTrace(stream);
323:
324: }
325:
326: }
|