KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jk > common > JkMX


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.jk.common;
18
19
20 import org.apache.jk.core.JkHandler;
21
22 import javax.management.MBeanServer JavaDoc;
23 import javax.management.ObjectName JavaDoc;
24 import javax.management.Attribute JavaDoc;
25 import javax.management.MBeanServerFactory JavaDoc;
26 import java.io.IOException JavaDoc;
27
28 /**
29  * Load the HTTP or RMI adapters for MX4J and JMXRI.
30  *
31  * Add "mx.enabled=true" in jk2.properties to enable it.
32  * You could also select http and/or jrmp protocol,
33  * with mx.httpPort, mx.httpHost, mxjrmpPort and mx.jrmpPort.
34  * <p />
35  * If you run into an error message like
36  * "SystemId Unknown; Line #12; Column #81; Cannot add attribute name after
37  * child nodes or before an element is produced. Attribute will be ignored."
38  * after setting mx.enabled to true, you probably need a newer version
39  * of Xalan. See the RELEASE-NOTES document section on XML Parsers for
40  * more information.
41  *
42  */

43 public class JkMX extends JkHandler
44 {
45     MBeanServer JavaDoc mserver;
46     private boolean enabled=false;
47     private boolean log4jEnabled=true;
48     private int httpport=-1;
49     private String JavaDoc httphost="localhost";
50     private String JavaDoc authmode="none";
51     private String JavaDoc authuser=null;
52     private String JavaDoc authpassword=null;
53     private int jrmpport=-1;
54     private String JavaDoc jrmphost="localhost";
55     private boolean useXSLTProcessor = true;
56
57     public JkMX() {
58     }
59
60     /* -------------------- Public methods -------------------- */
61
62     /** Enable the MX4J adapters (new way)
63      */

64     public void setEnabled(boolean b) {
65         enabled=b;
66     }
67         
68     public boolean getEnabled() {
69         return enabled;
70     }
71         
72     /** Enable the Log4j MBean)
73      */

74     public void setLog4jEnabled(boolean b) {
75         log4jEnabled=b;
76     }
77         
78     public boolean getLog4jEnabled() {
79         return log4jEnabled;
80     }
81
82     /** Enable the MX4J adapters (old way, compatible)
83      */

84     public void setPort(int i) {
85         enabled=(i != -1);
86     }
87         
88     public int getPort() {
89         return ((httpport != -1) ? httpport : jrmpport);
90     }
91
92     /** Enable the MX4J HTTP internal adapter
93      */

94     public void setHttpPort( int i ) {
95         httpport=i;
96     }
97
98     public int getHttpPort() {
99         return httpport;
100     }
101
102     public void setHttpHost(String JavaDoc host ) {
103         this.httphost=host;
104     }
105
106     public String JavaDoc getHttpHost() {
107         return httphost;
108     }
109
110     public void setAuthMode(String JavaDoc mode) {
111         authmode=mode;
112     }
113
114     public String JavaDoc getAuthMode() {
115         return authmode;
116     }
117
118     public void setAuthUser(String JavaDoc user) {
119         authuser=user;
120     }
121
122     public String JavaDoc getAuthUser() {
123         return authuser;
124     }
125
126     public void setAuthPassword(String JavaDoc password) {
127         authpassword=password;
128     }
129
130     public String JavaDoc getAuthPassword() {
131         return authpassword;
132     }
133
134     /** Enable the MX4J JRMP internal adapter
135      */

136     public void setJrmpPort( int i ) {
137         jrmpport=i;
138     }
139
140     public int getJrmpPort() {
141         return jrmpport;
142     }
143
144     public void setJrmpHost(String JavaDoc host ) {
145         this.jrmphost=host;
146     }
147
148     public String JavaDoc getJrmpHost() {
149         return jrmphost;
150     }
151
152     public boolean getUseXSLTProcessor() {
153         return useXSLTProcessor;
154     }
155
156     public void setUseXSLTProcessor(boolean uxsltp) {
157         useXSLTProcessor = uxsltp;
158     }
159
160     /* ==================== Start/stop ==================== */
161     ObjectName JavaDoc httpServerName=null;
162     ObjectName JavaDoc jrmpServerName=null;
163
164     /** Initialize the worker. After this call the worker will be
165      * ready to accept new requests.
166      */

167     public void loadAdapter() throws IOException JavaDoc {
168         boolean httpAdapterLoaded = false;
169         boolean jrmpAdapterLoaded = false;
170         
171         if ((httpport != -1) && classExists("mx4j.adaptor.http.HttpAdaptor")) {
172             try {
173                 httpServerName = registerObject("mx4j.adaptor.http.HttpAdaptor",
174                                                 "Http:name=HttpAdaptor");
175
176                         
177                 if( httphost!=null )
178                     mserver.setAttribute(httpServerName, new Attribute JavaDoc("Host", httphost));
179                 mserver.setAttribute(httpServerName, new Attribute JavaDoc("Port", new Integer JavaDoc(httpport)));
180
181                 if( "none".equals(authmode) || "basic".equals(authmode) || "digest".equals(authmode) )
182                     mserver.setAttribute(httpServerName, new Attribute JavaDoc("AuthenticationMethod", authmode));
183
184                 if( authuser!=null && authpassword!=null )
185                     mserver.invoke(httpServerName, "addAuthorization",
186                         new Object JavaDoc[] {
187                             authuser,
188                             authpassword},
189                         new String JavaDoc[] { "java.lang.String", "java.lang.String" });
190
191                 if(useXSLTProcessor) {
192                     ObjectName JavaDoc processorName = registerObject("mx4j.adaptor.http.XSLTProcessor",
193                                                           "Http:name=XSLTProcessor");
194                     mserver.setAttribute(httpServerName, new Attribute JavaDoc("ProcessorName", processorName));
195                 }
196
197                 // starts the server
198
mserver.invoke(httpServerName, "start", null, null);
199
200                 log.info( "Started MX4J console on host " + httphost + " at port " + httpport);
201                 
202                 httpAdapterLoaded = true;
203
204             } catch( Throwable JavaDoc t ) {
205                 httpServerName=null;
206                 log.error( "Can't load the MX4J http adapter ", t );
207             }
208         }
209
210         if ((httpport != -1) && (!httpAdapterLoaded) && classExists("mx4j.tools.adaptor.http.HttpAdaptor")) {
211             try {
212                 httpServerName = registerObject("mx4j.tools.adaptor.http.HttpAdaptor",
213                                                 "Http:name=HttpAdaptor");
214
215                         
216                 if( httphost!=null )
217                     mserver.setAttribute(httpServerName, new Attribute JavaDoc("Host", httphost));
218                 mserver.setAttribute(httpServerName, new Attribute JavaDoc("Port", new Integer JavaDoc(httpport)));
219
220                 if( "none".equals(authmode) || "basic".equals(authmode) || "digest".equals(authmode) )
221                     mserver.setAttribute(httpServerName, new Attribute JavaDoc("AuthenticationMethod", authmode));
222
223                 if( authuser!=null && authpassword!=null )
224                     mserver.invoke(httpServerName, "addAuthorization",
225                         new Object JavaDoc[] {
226                             authuser,
227                             authpassword},
228                         new String JavaDoc[] { "java.lang.String", "java.lang.String" });
229
230                if(useXSLTProcessor) {
231                     ObjectName JavaDoc processorName = registerObject("mx4j.tools.adaptor.http.XSLTProcessor",
232                                                           "Http:name=XSLTProcessor");
233                     mserver.setAttribute(httpServerName, new Attribute JavaDoc("ProcessorName", processorName));
234         }
235                 // starts the server
236
mserver.invoke(httpServerName, "start", null, null);
237                 if(log.isInfoEnabled())
238                     log.info( "Started MX4J console on host " + httphost + " at port " + httpport);
239                 
240                 httpAdapterLoaded = true;
241
242             } catch( Throwable JavaDoc t ) {
243                 httpServerName=null;
244                 log.error( "Can't load the MX4J http adapter ", t );
245             }
246         }
247
248         if ((jrmpport != -1) && classExists("mx4j.tools.naming.NamingService")) {
249             try {
250                 jrmpServerName = registerObject("mx4j.tools.naming.NamingService",
251                                                 "Naming:name=rmiregistry");
252                 mserver.setAttribute(jrmpServerName, new Attribute JavaDoc("Port",
253                                                      new Integer JavaDoc(jrmpport)));
254                 mserver.invoke(jrmpServerName, "start", null, null);
255                 if(log.isInfoEnabled())
256                     log.info( "Creating " + jrmpServerName );
257
258                 // Create the JRMP adaptor
259
ObjectName JavaDoc adaptor = registerObject("mx4j.adaptor.rmi.jrmp.JRMPAdaptor",
260                                                     "Adaptor:protocol=jrmp");
261
262
263                 mserver.setAttribute(adaptor, new Attribute JavaDoc("JNDIName", "jrmp"));
264
265                 mserver.invoke( adaptor, "putNamingProperty",
266                         new Object JavaDoc[] {
267                             javax.naming.Context.INITIAL_CONTEXT_FACTORY,
268                             "com.sun.jndi.rmi.registry.RegistryContextFactory"},
269                         new String JavaDoc[] { "java.lang.Object", "java.lang.Object" });
270
271                 String JavaDoc jrpmurl = "rmi://" + jrmphost + ":" + Integer.toString(jrmpport) ;
272                                         
273                 mserver.invoke( adaptor, "putNamingProperty",
274                         new Object JavaDoc[] {
275                             javax.naming.Context.PROVIDER_URL,
276                             jrpmurl},
277                         new String JavaDoc[] { "java.lang.Object", "java.lang.Object" });
278
279                 // Registers the JRMP adaptor in JNDI and starts it
280
mserver.invoke(adaptor, "start", null, null);
281                 if(log.isInfoEnabled())
282                     log.info( "Creating " + adaptor + " on host " + jrmphost + " at port " + jrmpport);
283
284                 jrmpAdapterLoaded = true;
285
286             } catch( Exception JavaDoc ex ) {
287                 jrmpServerName = null;
288                 log.error( "MX4j RMI adapter not loaded: " + ex.toString());
289             }
290         }
291
292         if ((httpport != -1) && (! httpAdapterLoaded) && classExists("com.sun.jdmk.comm.HtmlAdaptorServer")) {
293             try {
294                 httpServerName=registerObject("com.sun.jdmk.comm.HtmlAdaptorServer",
295                                               "Adaptor:name=html,port=" + httpport);
296                 if(log.isInfoEnabled())
297                     log.info("Registering the JMX_RI html adapter " + httpServerName + " at port " + httpport);
298
299                 mserver.setAttribute(httpServerName,
300                                      new Attribute JavaDoc("Port", new Integer JavaDoc(httpport)));
301
302                 mserver.invoke(httpServerName, "start", null, null);
303
304                 httpAdapterLoaded = true;
305             } catch( Throwable JavaDoc t ) {
306                 httpServerName = null;
307                 log.error( "Can't load the JMX_RI http adapter " + t.toString() );
308             }
309         }
310
311         if ((!httpAdapterLoaded) && (!jrmpAdapterLoaded))
312             log.warn( "No adaptors were loaded but mx.enabled was defined.");
313
314     }
315
316     public void destroy() {
317         try {
318             if(log.isInfoEnabled())
319                 log.info("Stoping JMX ");
320
321             if( httpServerName!=null ) {
322                 mserver.invoke(httpServerName, "stop", null, null);
323             }
324             if( jrmpServerName!=null ) {
325                 mserver.invoke(jrmpServerName, "stop", null, null);
326             }
327         } catch( Throwable JavaDoc t ) {
328             log.error( "Destroy error" + t );
329         }
330     }
331
332     public void init() throws IOException JavaDoc {
333         try {
334             mserver = getMBeanServer();
335
336             if( enabled ) {
337                 loadAdapter();
338             }
339             if( log4jEnabled) {
340                 try {
341                     registerObject("org.apache.log4j.jmx.HierarchyDynamicMBean" ,
342                                    "log4j:hierarchy=default");
343                     if(log.isInfoEnabled())
344                          log.info("Registering the JMX hierarchy for Log4J ");
345                 } catch( Throwable JavaDoc t ) {
346                     if(log.isInfoEnabled())
347                         log.info("Can't enable log4j mx: ",t);
348                 }
349             }
350         } catch( Throwable JavaDoc t ) {
351             log.error( "Init error", t );
352         }
353     }
354
355     public void addHandlerCallback( JkHandler w ) {
356     }
357
358     MBeanServer JavaDoc getMBeanServer() {
359         MBeanServer JavaDoc server;
360         if( MBeanServerFactory.findMBeanServer(null).size() > 0 ) {
361             server=(MBeanServer JavaDoc)MBeanServerFactory.findMBeanServer(null).get(0);
362         } else {
363             server=MBeanServerFactory.createMBeanServer();
364         }
365         return (server);
366     }
367
368
369     private static boolean classExists(String JavaDoc className) {
370         try {
371             Thread.currentThread().getContextClassLoader().loadClass(className);
372             return true;
373         } catch(Throwable JavaDoc e) {
374             if (log.isInfoEnabled())
375                 log.info( "className [" + className + "] does not exist");
376             return false;
377         }
378     }
379
380     private ObjectName JavaDoc registerObject(String JavaDoc className, String JavaDoc oName)
381         throws Exception JavaDoc {
382         Class JavaDoc c = Class.forName(className);
383         Object JavaDoc o = c.newInstance();
384         ObjectName JavaDoc objN = new ObjectName JavaDoc(oName);
385         mserver.registerMBean(o, objN);
386         return objN;
387     }
388
389     private static org.apache.commons.logging.Log log=
390         org.apache.commons.logging.LogFactory.getLog( JkMX.class );
391
392
393 }
394
395
Popular Tags