KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mortbay > jetty > servlet > jmx > WebApplicationContextMBean


1 // ========================================================================
2
// $Id: WebApplicationContextMBean.java,v 1.11 2005/08/13 00:01:27 gregwilkins Exp $
3
// Copyright 1999-2004 Mort Bay Consulting Pty. Ltd.
4
// ------------------------------------------------------------------------
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
// ========================================================================
15

16 package org.mortbay.jetty.servlet.jmx;
17
18 import java.util.HashMap JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import javax.management.MBeanException JavaDoc;
23 import javax.management.MBeanServer JavaDoc;
24 import javax.management.ObjectName JavaDoc;
25
26 import org.apache.commons.logging.Log;
27 import org.mortbay.log.LogFactory;
28 import org.mortbay.jetty.servlet.WebApplicationContext;
29 import org.mortbay.util.LifeCycleEvent;
30 import org.mortbay.util.LifeCycleListener;
31 import org.mortbay.util.LogSupport;
32
33 /* ------------------------------------------------------------ */
34 /** Web Application MBean.
35  * Note that while Web Applications are HttpContexts, the MBean is
36  * not derived from HttpContextMBean as they are managed differently.
37  *
38  * @version $Revision: 1.11 $
39  * @author Greg Wilkins (gregw)
40  */

41 public class WebApplicationContextMBean extends ServletHttpContextMBean
42 {
43     private static final Log log = LogFactory.getLog(WebApplicationContextMBean.class);
44     private WebApplicationContext _webappContext;
45     private Map JavaDoc _configurations = new HashMap JavaDoc();
46     
47     /* ------------------------------------------------------------ */
48     /** Constructor.
49      * @exception MBeanException
50      */

51     public WebApplicationContextMBean()
52         throws MBeanException JavaDoc
53     {}
54
55     /* ------------------------------------------------------------ */
56     protected void defineManagedResource()
57     {
58         super.defineManagedResource();
59
60         defineAttribute("displayName",false);
61         defineAttribute("defaultsDescriptor",true);
62         defineAttribute("WAR",true);
63         defineAttribute("extractWAR",true);
64         _webappContext=(WebApplicationContext)getManagedResource();
65         _webappContext.addEventListener(new LifeCycleListener()
66                 {
67
68                     public void lifeCycleStarting (LifeCycleEvent event)
69                     {}
70                     
71                     public void lifeCycleStarted (LifeCycleEvent event)
72                     {
73                         getConfigurations();
74                     }
75
76                     public void lifeCycleFailure (LifeCycleEvent event)
77                     {}
78
79                     public void lifeCycleStopping (LifeCycleEvent event)
80                     {}
81
82                     public void lifeCycleStopped (LifeCycleEvent event)
83                     {
84                         destroyConfigurations();
85                     }
86             
87                 });
88     }
89     
90     
91     /** postRegister
92      * Register mbeans for all of the jsr77 servlet stats
93      * @see javax.management.MBeanRegistration#postRegister(java.lang.Boolean)
94      */

95     public void postRegister(Boolean JavaDoc ok)
96     {
97         super.postRegister(ok);
98         getConfigurations();
99     }
100     
101     
102    
103     
104     
105     /**postDeregister
106      * Unregister mbeans we created for the Configuration objects.
107      * @see javax.management.MBeanRegistration#postDeregister()
108      */

109     public void postDeregister ()
110     {
111         destroyConfigurations ();
112         super.postDeregister();
113     }
114    
115     
116     /**getConfigurations
117      * Make mbeans for all of the Configurations applied to the
118      * WebApplicationContext
119      * @return
120      */

121     public ObjectName JavaDoc[] getConfigurations ()
122     {
123         return getComponentMBeans(_webappContext.getConfigurations(),_configurations);
124     }
125     
126     public void destroyConfigurations ()
127     {
128         MBeanServer JavaDoc mbeanServer = getMBeanServer();
129         Iterator JavaDoc itor = _configurations.values().iterator();
130         while (itor.hasNext())
131         {
132             try
133             {
134                 ObjectName JavaDoc o = (ObjectName JavaDoc)itor.next();
135                 log.debug("Unregistering: "+o);
136                 
137                 if (null!=mbeanServer)
138                     mbeanServer.unregisterMBean((ObjectName JavaDoc)o);
139             }
140             catch (Exception JavaDoc e)
141             {
142                 log.warn(LogSupport.EXCEPTION, e);
143             }
144         }
145         _configurations.clear();
146     }
147     
148     
149  
150     
151 }
152
Popular Tags