KickJava   Java API By Example, From Geeks To Geeks.

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


1 // ========================================================================
2
// $Id: HttpContextMBean.java,v 1.17 2005/08/13 00:01:26 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.MBeanException JavaDoc;
21 import javax.management.MBeanServer JavaDoc;
22 import javax.management.ObjectName JavaDoc;
23
24 import org.apache.commons.logging.Log;
25 import org.mortbay.log.LogFactory;
26 import org.mortbay.http.HttpContext;
27 import org.mortbay.util.LifeCycleEvent;
28 import org.mortbay.util.LifeCycleListener;
29 import org.mortbay.util.LogSupport;
30 import org.mortbay.util.jmx.LifeCycleMBean;
31
32
33 /* ------------------------------------------------------------ */
34 /**
35  *
36  * @version $Revision: 1.17 $
37  * @author Greg Wilkins (gregw)
38  */

39 public class HttpContextMBean extends LifeCycleMBean
40 {
41     private static Log log = LogFactory.getLog(HttpContextMBean.class);
42
43     private HttpContext _httpContext;
44     private HashMap JavaDoc _rlMap=new HashMap JavaDoc(3);
45     private HashMap JavaDoc _handlerMap = new HashMap JavaDoc();
46
47     /* ------------------------------------------------------------ */
48     /** Constructor.
49      * @exception MBeanException
50      */

51     public HttpContextMBean()
52         throws MBeanException JavaDoc
53     {}
54
55     /* ------------------------------------------------------------ */
56     protected void defineManagedResource()
57     {
58         super.defineManagedResource();
59
60         defineAttribute("virtualHosts");
61         defineAttribute("hosts");
62         defineAttribute("contextPath");
63
64         defineAttribute("handlers",READ_ONLY,ON_MBEAN);
65         defineAttribute("requestLog",READ_ONLY,ON_MBEAN);
66         
67         defineAttribute("classPath");
68
69         defineAttribute("realm");
70         defineAttribute("realmName");
71
72         defineAttribute("redirectNullPath");
73         defineAttribute("resourceBase");
74         defineAttribute("maxCachedFileSize");
75         defineAttribute("maxCacheSize");
76         defineOperation("flushCache",
77                         IMPACT_ACTION);
78         defineOperation("getResource",
79                         new String JavaDoc[] {STRING},
80                         IMPACT_ACTION);
81
82         defineAttribute("welcomeFiles");
83         defineOperation("addWelcomeFile",
84                         new String JavaDoc[] {STRING},
85                         IMPACT_INFO);
86         defineOperation("removeWelcomeFile",
87                         new String JavaDoc[] {STRING},
88                         IMPACT_INFO);
89
90         defineAttribute("mimeMap");
91         defineOperation("setMimeMapping",new String JavaDoc[] {STRING,STRING},IMPACT_ACTION);
92
93         
94         defineAttribute("statsOn");
95         defineAttribute("statsOnMs");
96         defineOperation("statsReset",IMPACT_ACTION);
97         defineAttribute("requests");
98         defineAttribute("requestsActive");
99         defineAttribute("requestsActiveMax");
100         defineAttribute("responses1xx");
101         defineAttribute("responses2xx");
102         defineAttribute("responses3xx");
103         defineAttribute("responses4xx");
104         defineAttribute("responses5xx");
105
106         defineOperation("stop",new String JavaDoc[] {"java.lang.Boolean.TYPE"},IMPACT_ACTION);
107
108         defineOperation("destroy",
109                         IMPACT_ACTION);
110
111         defineOperation("setInitParameter",
112                         new String JavaDoc[] {STRING,STRING},
113                         IMPACT_ACTION);
114         defineOperation("getInitParameter",
115                         new String JavaDoc[] {STRING},
116                         IMPACT_INFO);
117         defineOperation("getInitParameterNames",
118                         NO_PARAMS,
119                         IMPACT_INFO);
120
121         defineOperation("setAttribute",new String JavaDoc[] {STRING,OBJECT},IMPACT_ACTION);
122         defineOperation("getAttribute",new String JavaDoc[] {STRING},IMPACT_INFO);
123         defineOperation("getAttributeNames",NO_PARAMS,IMPACT_INFO);
124         defineOperation("removeAttribute",new String JavaDoc[] {STRING},IMPACT_ACTION);
125
126         defineOperation("addHandler",new String JavaDoc[] {"org.mortbay.http.HttpHandler"},IMPACT_ACTION);
127         defineOperation("addHandler",new String JavaDoc[] {INT,"org.mortbay.http.HttpHandler"},IMPACT_ACTION);
128         defineOperation("removeHandler",new String JavaDoc[] {INT},IMPACT_ACTION);
129
130
131         _httpContext=(HttpContext)getManagedResource();
132         
133         _httpContext.addEventListener(new LifeCycleListener()
134                 {
135
136                     public void lifeCycleStarting (LifeCycleEvent event)
137                     {}
138
139                     public void lifeCycleStarted (LifeCycleEvent event)
140                     {
141                         getHandlers();
142                     }
143
144                     public void lifeCycleFailure (LifeCycleEvent event)
145                     {}
146
147                     public void lifeCycleStopping (LifeCycleEvent event)
148                     {}
149
150                     public void lifeCycleStopped (LifeCycleEvent event)
151                     {
152                         destroyHandlers();
153                     }
154             
155                 });
156     }
157
158
159     /* ------------------------------------------------------------ */
160     protected ObjectName JavaDoc newObjectName(MBeanServer JavaDoc server)
161     {
162         ObjectName JavaDoc oName=super.newObjectName(server);
163         String JavaDoc context=_httpContext.getContextPath();
164         if (context.length()==0)
165             context="/";
166         try{oName=new ObjectName JavaDoc(oName+",context="+context);}
167         catch(Exception JavaDoc e){log.warn(LogSupport.EXCEPTION,e);}
168         return oName;
169     }
170
171     /* ------------------------------------------------------------ */
172     public void postRegister(Boolean JavaDoc ok)
173     {
174         super.postRegister(ok);
175         if (ok.booleanValue())
176             getHandlers();
177     }
178
179     /* ------------------------------------------------------------ */
180     public void postDeregister()
181     {
182         _httpContext=null;
183         destroyComponentMBeans(_handlerMap);
184         super.postDeregister();
185     }
186
187     /* ------------------------------------------------------------ */
188     public ObjectName JavaDoc[] getHandlers()
189     {
190         return getComponentMBeans(_httpContext.getHandlers(),_handlerMap);
191     }
192     
193   
194     public void destroyHandlers()
195     {
196         destroyComponentMBeans(_handlerMap);
197     }
198
199     /* ------------------------------------------------------------ */
200     public ObjectName JavaDoc getRequestLog()
201     {
202         Object JavaDoc o = _httpContext.getRequestLog();
203         if (o==null)
204             return null;
205         
206         ObjectName JavaDoc[] on=getComponentMBeans(new Object JavaDoc[]{o},_rlMap);
207         if (on.length>0)
208             return on[0];
209         return null;
210     }
211
212 }
213
214
215
Popular Tags