001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 * 
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 * 
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.log4j.jmx;
019
020import org.apache.log4j.Logger;
021
022import javax.management.JMException;
023import javax.management.MBeanServer;
024import javax.management.MBeanServerFactory;
025import javax.management.ObjectName;
026import java.lang.reflect.InvocationTargetException;
027import java.io.InterruptedIOException;
028
029
030/**
031 * Manages an instance of com.sun.jdmk.comm.HtmlAdapterServer which
032 * was provided for demonstration purposes in the
033 * Java Management Extensions Reference Implementation 1.2.1.
034 * This class is provided to maintain compatibility with earlier
035 * versions of log4j and use in new code is discouraged.
036 *
037 * @deprecated
038 */
039public class Agent {
040
041    /**
042     * Diagnostic logger.
043     * @deprecated
044     */
045  static Logger log = Logger.getLogger(Agent.class);
046
047    /**
048     * Create new instance.
049     * @deprecated
050     */
051  public Agent() {
052  }
053
054    /**
055     * Creates a new instance of com.sun.jdmk.comm.HtmlAdapterServer
056     * using reflection.
057     *
058     * @since 1.2.16
059     * @return new instance.
060     */
061  private static Object createServer() {
062      Object newInstance = null;
063      try {
064        newInstance = Class.forName(
065                "com.sun.jdmk.comm.HtmlAdapterServer").newInstance();
066      } catch (ClassNotFoundException ex) {
067          throw new RuntimeException(ex.toString());
068      } catch (InstantiationException ex) {
069          throw new RuntimeException(ex.toString());
070      } catch (IllegalAccessException ex) {
071          throw new RuntimeException(ex.toString());
072      }
073      return newInstance;
074  }
075
076    /**
077     * Invokes HtmlAdapterServer.start() using reflection.
078     *
079     * @since 1.2.16
080     * @param server instance of com.sun.jdmk.comm.HtmlAdapterServer.
081     */
082  private static void startServer(final Object server) {
083      try {
084          server.getClass().getMethod("start", new Class[0]).
085                  invoke(server, new Object[0]);
086      } catch(InvocationTargetException ex) {
087          Throwable cause = ex.getTargetException();
088          if (cause instanceof RuntimeException) {
089              throw (RuntimeException) cause;
090          } else if (cause != null) {
091              if (cause instanceof InterruptedException
092                      || cause instanceof InterruptedIOException) {
093                  Thread.currentThread().interrupt();
094              }
095              throw new RuntimeException(cause.toString());
096          } else {
097              throw new RuntimeException();
098          }
099      } catch(NoSuchMethodException ex) {
100          throw new RuntimeException(ex.toString());
101      } catch(IllegalAccessException ex) {
102        throw new RuntimeException(ex.toString());
103    }
104  }
105
106
107    /**
108     * Starts instance of HtmlAdapterServer.
109     * @deprecated
110      */
111  public void start() {
112
113    MBeanServer server = MBeanServerFactory.createMBeanServer();
114    Object html = createServer();
115
116    try {
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);
124      return;
125    } catch(RuntimeException e) {
126      log.error("Problem while registering MBeans instances.", e);
127      return;
128    }
129    startServer(html);
130  }
131}