KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mortbay > http > jmx > HttpServerMBean


1 // ========================================================================
2
// $Id: HttpServerMBean.java,v 1.16 2005/12/04 11:43:21 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.http.jmx;
17
18 import java.util.HashMap JavaDoc;
19
20 import javax.management.InstanceNotFoundException JavaDoc;
21 import javax.management.MBeanException JavaDoc;
22 import javax.management.ObjectName JavaDoc;
23 import javax.management.modelmbean.InvalidTargetObjectTypeException JavaDoc;
24 import javax.management.modelmbean.ModelMBean JavaDoc;
25
26 import org.apache.commons.logging.Log;
27 import org.mortbay.log.LogFactory;
28 import org.mortbay.http.HttpServer;
29 import org.mortbay.http.Version;
30 import org.mortbay.util.ComponentEvent;
31 import org.mortbay.util.ComponentListener;
32 import org.mortbay.util.LogSupport;
33 import org.mortbay.util.jmx.LifeCycleMBean;
34 import org.mortbay.util.jmx.ModelMBeanImpl;
35
36 /* ------------------------------------------------------------ */
37 /** HttpServer MBean.
38  * This Model MBean class provides the mapping for HttpServer
39  * management methods. It also registers itself as a membership
40  * listener of the HttpServer, so it can create and destroy MBean
41  * wrappers for listeners and contexts.
42  *
43  * @version $Revision: 1.16 $
44  * @author Greg Wilkins (gregw)
45  */

46 public class HttpServerMBean extends LifeCycleMBean
47     implements ComponentListener
48 {
49     private static Log log = LogFactory.getLog(HttpServerMBean.class);
50
51     private HttpServer _httpServer;
52     private HashMap JavaDoc _mbeanMap = new HashMap JavaDoc();
53
54     /* ------------------------------------------------------------ */
55     /** Constructor.
56      * @exception MBeanException
57      * @exception InstanceNotFoundException
58      */

59     protected HttpServerMBean(HttpServer httpServer)
60         throws MBeanException JavaDoc, InstanceNotFoundException JavaDoc
61     {
62         _httpServer=httpServer;
63         _httpServer.addEventListener(this);
64         try{super.setManagedResource(_httpServer,"objectReference");}
65         catch(InvalidTargetObjectTypeException JavaDoc e){log.warn(LogSupport.EXCEPTION,e);}
66     }
67
68     /* ------------------------------------------------------------ */
69     /** Constructor.
70      * @exception MBeanException
71      * @exception InstanceNotFoundException
72      */

73     public HttpServerMBean()
74         throws MBeanException JavaDoc, InstanceNotFoundException JavaDoc
75     {
76         this(new HttpServer());
77     }
78     
79     /* ------------------------------------------------------------ */
80     public void setManagedResource(Object JavaDoc o,String JavaDoc s)
81         throws MBeanException JavaDoc, InstanceNotFoundException JavaDoc, InvalidTargetObjectTypeException JavaDoc
82     {
83         if (o!=null)
84             ((HttpServer)o).addEventListener(this);
85         super.setManagedResource(o,s);
86     }
87
88     /* ------------------------------------------------------------ */
89     protected void defineManagedResource()
90     {
91         super.defineManagedResource();
92         
93         defineAttribute("listeners",READ_ONLY);
94         defineAttribute("contexts",READ_ONLY);
95         defineAttribute("version",READ_ONLY,ON_MBEAN);
96         defineAttribute("components",READ_ONLY,ON_MBEAN);
97         defineAttribute("requestLog");
98         
99         defineAttribute("trace");
100
101         
102         defineOperation("addListener",new String JavaDoc[]{"java.lang.String"},IMPACT_ACTION);
103         defineOperation("addListener",new String JavaDoc[]{"org.mortbay.util.InetAddrPort"},IMPACT_ACTION);
104         defineOperation("addListener",new String JavaDoc[]{"org.mortbay.http.HttpListener"},IMPACT_ACTION);
105         defineOperation("removeListener",new String JavaDoc[]{"org.mortbay.http.HttpListener"},IMPACT_ACTION);
106         defineOperation("addContext",new String JavaDoc[]{"org.mortbay.http.HttpContext"},IMPACT_ACTION);
107         defineOperation("removeContext",new String JavaDoc[]{"org.mortbay.http.HttpContext"},IMPACT_ACTION);
108         defineOperation("addContext",new String JavaDoc[]{"java.lang.String"},IMPACT_ACTION);
109         defineOperation("addContext",new String JavaDoc[]{"java.lang.String","java.lang.String"},IMPACT_ACTION);
110         
111         defineAttribute("requestsPerGC");
112         
113         defineAttribute("statsOn");
114         defineAttribute("statsOnMs");
115         defineOperation("statsReset",IMPACT_ACTION);
116         defineAttribute("connections");
117         defineAttribute("connectionsOpen");
118         defineAttribute("connectionsOpenMin");
119         defineAttribute("connectionsOpenMax");
120         defineAttribute("connectionsDurationAve");
121         defineAttribute("connectionsDurationMin");
122         defineAttribute("connectionsDurationMax");
123         defineAttribute("connectionsDurationTotal");
124         defineAttribute("connectionsRequestsAve");
125         defineAttribute("connectionsRequestsMin");
126         defineAttribute("connectionsRequestsMax");
127         defineAttribute("errors");
128         defineAttribute("requests");
129         defineAttribute("requestsActive");
130         defineAttribute("requestsActiveMin");
131         defineAttribute("requestsActiveMax");
132         defineAttribute("requestsDurationAve");
133         defineAttribute("requestsDurationMin");
134         defineAttribute("requestsDurationMax");
135         
136         defineOperation("stop",new String JavaDoc[]{"java.lang.Boolean.TYPE"},IMPACT_ACTION);
137         defineOperation("save",new String JavaDoc[]{"java.lang.String"},IMPACT_ACTION);
138         defineOperation("destroy",IMPACT_ACTION);
139     }
140     
141     /* ------------------------------------------------------------ */
142     public synchronized void addComponent(ComponentEvent event)
143     {
144         try
145         {
146             if(log.isDebugEnabled())log.debug("Component added "+event);
147             Object JavaDoc o=event.getComponent();
148             
149             ModelMBean JavaDoc mbean=ModelMBeanImpl.mbeanFor(o);
150             if (mbean==null)
151                 log.warn("No MBean for "+o);
152             else
153             {
154                 ObjectName JavaDoc oName=null;
155                 if (mbean instanceof ModelMBeanImpl)
156                 {
157                     ((ModelMBeanImpl)mbean).setBaseObjectName(getObjectName().toString());
158                     oName=
159                         getMBeanServer().registerMBean(mbean,null).getObjectName();
160                 }
161                 else
162                 {
163                     oName=uniqueObjectName(getMBeanServer(),
164                                            o,
165                                            getObjectName().toString());
166                     oName=getMBeanServer().registerMBean(mbean,oName)
167                         .getObjectName();
168                 }
169                 Holder holder = new Holder(oName,mbean);
170                 _mbeanMap.put(o,holder);
171             }
172         }
173         catch(Exception JavaDoc e)
174         {
175             log.warn(LogSupport.EXCEPTION,e);
176         }
177     }
178
179     /* ------------------------------------------------------------ */
180     public String JavaDoc getVersion()
181     {
182         return Version.getDetail();
183     }
184     
185     
186     /* ------------------------------------------------------------ */
187     public ObjectName JavaDoc[] getComponents()
188     {
189         Holder[] h=(Holder[])_mbeanMap.values().toArray(new Holder[_mbeanMap.size()]);
190         ObjectName JavaDoc[] on = new ObjectName JavaDoc[h.length];
191         for (int i=0;i<on.length;i++)
192             on[i]=h[i].oName;
193         return on;
194     }
195     
196     
197     /* ------------------------------------------------------------ */
198     public synchronized void removeComponent(ComponentEvent event)
199     {
200         if(log.isDebugEnabled())log.debug("Component removed "+event);
201         
202         try
203         {
204             Object JavaDoc o=event.getComponent();
205             Holder holder=(Holder)_mbeanMap.remove(o);
206             if (holder!=null)
207                 getMBeanServer().unregisterMBean(holder.oName);
208             else if (o==_httpServer)
209                 getMBeanServer().unregisterMBean(this.getObjectName());
210         }
211         catch(Exception JavaDoc e)
212         {
213             log.warn(LogSupport.EXCEPTION,e);
214         }
215     }
216     
217     /* ------------------------------------------------------------ */
218     /**
219      * @param ok
220      */

221     public void postRegister(Boolean JavaDoc ok)
222     {
223         super.postRegister(ok);
224     }
225     
226     /* ------------------------------------------------------------ */
227     public void postDeregister()
228     {
229         _httpServer.removeEventListener(this);
230         _httpServer=null;
231         if (_mbeanMap!=null)
232             _mbeanMap.clear();
233         _mbeanMap=null;
234         
235         super.postDeregister();
236     }
237
238     /* ------------------------------------------------------------ */
239     /* ------------------------------------------------------------ */
240     private static class Holder
241     {
242         Holder(ObjectName JavaDoc oName,Object JavaDoc mbean){ this.oName=oName; this.mbean=mbean;}
243         ObjectName JavaDoc oName;
244         Object JavaDoc mbean;
245     }
246 }
247
248
Popular Tags