Agent xref

View Javadoc

    1   /*
    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
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
    10   *
    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.
    16   */
    17
    18  package org.apache.log4j.jmx;
    19
    20  import org.apache.log4j.Logger;
    21
    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;
    28
    29
    30  /**
    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.
    36   *
    37   * @deprecated
    38   */
    39  public class Agent {
    40
    41      /**
    42       * Diagnostic logger.
    43       * @deprecated
    44       */
    45    static Logger log = Logger.getLogger(Agent.class);
    46
    47      /**
    48       * Create new instance.
    49       * @deprecated
    50       */
    51    public Agent() {
    52    }
    53
    54      /**
    55       * Creates a new instance of com.sun.jdmk.comm.HtmlAdapterServer
    56       * using reflection.
    57       *
    58       * @since 1.2.16
    59       * @return new instance.
    60       */
    61    private static Object createServer() {
    62        Object newInstance = null;
    63        try {
    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());
    72        }
    73        return newInstance;
    74    }
    75
    76      /**
    77       * Invokes HtmlAdapterServer.start() using reflection.
    78       *
    79       * @since 1.2.16
    80       * @param server instance of com.sun.jdmk.comm.HtmlAdapterServer.
    81       */
    82    private static void startServer(final Object server) {
    83        try {
    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();
    94                }
    95                throw new RuntimeException(cause.toString());
    96            } else {
    97                throw new RuntimeException();
    98            }
    99        } 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 }