01: // THIS SOFTWARE IS PROVIDED BY SOFTARIS PTY.LTD. AND OTHER METABOSS
02: // CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
03: // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
04: // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTARIS PTY.LTD.
05: // OR OTHER METABOSS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
06: // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
07: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
08: // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
09: // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
10: // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
11: // EVEN IF SOFTARIS PTY.LTD. OR OTHER METABOSS CONTRIBUTORS ARE ADVISED OF THE
12: // POSSIBILITY OF SUCH DAMAGE.
13: //
14: // Copyright 2000-2005 © Softaris Pty.Ltd. All Rights Reserved.
15: package com.jamonapi.management;
16:
17: import javax.management.MBeanServer;
18: import javax.management.MalformedObjectNameException;
19: import javax.servlet.ServletContextEvent;
20:
21: import org.apache.commons.logging.Log;
22: import org.apache.commons.logging.LogFactory;
23:
24: /** This servlet context listener allows to register and unregister MonitorControllerMBean at
25: * the time when servlet context is created or destroyed. It should be configurred as the context listener
26: * in every web application where jamon is used and the MonitorControllerMBean is required. */
27: public class MonitorControllerServletContextListener implements
28: javax.servlet.ServletContextListener {
29: private static Log sLogger = LogFactory
30: .getLog(MonitorControllerServletContextListener.class);
31: private MBeanServer mMBeanServer = null;
32:
33: /** Creates a dedicated instance of the MonitorControllerMBean for this context.
34: * If one already exists - destroy it, because it can not be valid any more */
35: public void contextInitialized(ServletContextEvent sce) {
36: try {
37: String lServletContextName = sce.getServletContext()
38: .getServletContextName();
39: if (lServletContextName == null
40: || lServletContextName.length() == 0)
41: throw new MalformedObjectNameException(
42: "Servlet Context Name is empty. Unable to build unique ObjectName.");
43: // Possibly register MBean
44: MonitorControllerMBean.registerBean(lServletContextName);
45: } catch (MalformedObjectNameException e) {
46: // This is an unexpected situation. Report it as an error
47: sLogger
48: .error(
49: "Unable to create valid MBean Object Name (Caught an unexpected exception). The profiling management via managed beans has not been initialised properly and will not work.",
50: e);
51: }
52: }
53:
54: /** Destroy an instance for the MonitorControllerMBean which exists for this context */
55: public void contextDestroyed(ServletContextEvent sce) {
56: try {
57: String lServletContextName = sce.getServletContext()
58: .getServletContextName();
59: if (lServletContextName == null
60: || lServletContextName.length() == 0)
61: throw new MalformedObjectNameException(
62: "Servlet Context Name is empty. Unable to build unique ObjectName.");
63: // Possibly register MBean
64: MonitorControllerMBean.unregisterBean(lServletContextName);
65: } catch (MalformedObjectNameException e) {
66: // This is an unexpected situation. Report it as an error
67: sLogger
68: .error(
69: "Unable to create valid MBean Object Name (Caught an unexpected exception). The profiling management via managed beans has not been cleaned up properly.",
70: e);
71: }
72: }
73: }
|