2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 package org.apache.log4j.jmx;
20 import org.apache.log4j.Logger;
22 import javax.management.JMException;
23 import javax.management.MBeanServer;
24 import javax.management.MBeanServerFactory;
25 import javax.management.ObjectName;
26 import java.lang.reflect.InvocationTargetException;
27 import java.io.InterruptedIOException;
31 * Manages an instance of com.sun.jdmk.comm.HtmlAdapterServer which
32 * was provided for demonstration purposes in the
33 * Java Management Extensions Reference Implementation 1.2.1.
34 * This class is provided to maintain compatibility with earlier
35 * versions of log4j and use in new code is discouraged.
45 static Logger log = Logger.getLogger(Agent.class);
48 * Create new instance.
55 * Creates a new instance of com.sun.jdmk.comm.HtmlAdapterServer
59 * @return new instance.
61 private static Object createServer() {
62 Object newInstance = null;
64 newInstance = Class.forName(
65 "com.sun.jdmk.comm.HtmlAdapterServer").newInstance();
66 } catch (ClassNotFoundException ex) {
67 throw new RuntimeException(ex.toString());
68 } catch (InstantiationException ex) {
69 throw new RuntimeException(ex.toString());
70 } catch (IllegalAccessException ex) {
71 throw new RuntimeException(ex.toString());
77 * Invokes HtmlAdapterServer.start() using reflection.
80 * @param server instance of com.sun.jdmk.comm.HtmlAdapterServer.
82 private static void startServer(final Object server) {
84 server.getClass().getMethod("start", new Class[0]).
85 invoke(server, new Object[0]);
86 } catch(InvocationTargetException ex) {
87 Throwable cause = ex.getTargetException();
88 if (cause instanceof RuntimeException) {
89 throw (RuntimeException) cause;
90 } else if (cause != null) {
91 if (cause instanceof InterruptedException
92 || cause instanceof InterruptedIOException) {
93 Thread.currentThread().interrupt();
95 throw new RuntimeException(cause.toString());
97 throw new RuntimeException();
99 } catch(NoSuchMethodException ex) {
100 throw new RuntimeException(ex.toString());
101 } catch(IllegalAccessException ex) {
102 throw new RuntimeException(ex.toString());
108 * Starts instance of HtmlAdapterServer.
111 public void start() {
113 MBeanServer server = MBeanServerFactory.createMBeanServer();
114 Object html = createServer();
117 log.info("Registering HtmlAdaptorServer instance.");
118 server.registerMBean(html, new ObjectName("Adaptor:name=html,port=8082"));
119 log.info("Registering HierarchyDynamicMBean instance.");
120 HierarchyDynamicMBean hdm = new HierarchyDynamicMBean();
121 server.registerMBean(hdm, new ObjectName("log4j:hiearchy=default"));
122 } catch(JMException e) {
123 log.error("Problem while registering MBeans instances.", e);
125 } catch(RuntimeException e) {
126 log.error("Problem while registering MBeans instances.", e);