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  package org.apache.logging.log4j.core.jmx;
18  
19  import java.io.IOException;
20  import java.util.ArrayList;
21  import java.util.List;
22  import java.util.Set;
23  
24  import javax.management.JMX;
25  import javax.management.MBeanServerConnection;
26  import javax.management.MalformedObjectNameException;
27  import javax.management.ObjectName;
28  import javax.management.remote.JMXConnector;
29  
30  /**
31   * This class allows client-side code to perform operations on remote
32   * (server-side) MBeans via proxies.
33   */
34  public class Client {
35      private JMXConnector connector;
36      private MBeanServerConnection connection;
37      private StatusLoggerAdminMBean statusLoggerAdmin;
38      private ContextSelectorAdminMBean contextSelectorAdmin;
39      private List<LoggerContextAdminMBean> contextAdminList;
40  
41      /**
42       * Constructs a new {@code Client} object and creates proxies for all known
43       * remote MBeans.
44       * 
45       * @param connector used to create the MBean server connection through which
46       *            to communicate with the remote mbeans
47       * @throws MalformedObjectNameException if a problem occurred identifying
48       *             one of the remote mbeans
49       * @throws IOException if the connection failed
50       */
51      public Client(JMXConnector connector) throws MalformedObjectNameException,
52              IOException {
53          this.connector = Assert.isNotNull(connector, "JMXConnector");
54          this.connector.connect();
55          this.connection = connector.getMBeanServerConnection();
56          init();
57      }
58  
59      /**
60       * Constructs a new {@code Client} object and creates proxies for all known
61       * remote MBeans.
62       * 
63       * @param mBeanServerConnection the MBean server connection through which to
64       *            communicate with the remote mbeans
65       * @throws MalformedObjectNameException if a problem occurred identifying
66       *             one of the remote mbeans
67       * @throws IOException if the connection failed
68       */
69      public Client(MBeanServerConnection mBeanServerConnection)
70              throws MalformedObjectNameException, IOException {
71          this.connection = mBeanServerConnection;
72          init();
73      }
74  
75      private void init() throws MalformedObjectNameException, IOException {
76          statusLoggerAdmin = JMX.newMBeanProxy(connection, //
77                  new ObjectName(StatusLoggerAdminMBean.NAME), //
78                  StatusLoggerAdminMBean.class, true);
79  
80          contextSelectorAdmin = JMX.newMBeanProxy(connection, //
81                  new ObjectName(ContextSelectorAdminMBean.NAME), //
82                  ContextSelectorAdminMBean.class, false);
83  
84          contextAdminList = new ArrayList<LoggerContextAdminMBean>();
85          String pattern = String.format(LoggerContextAdminMBean.PATTERN, "*");
86          ObjectName search = new ObjectName(pattern);
87          Set<ObjectName> found = connection.queryNames(search, null);
88          for (ObjectName contextName : found) {
89              LoggerContextAdminMBean ctx = JMX.newMBeanProxy(connection, //
90                      contextName, //
91                      LoggerContextAdminMBean.class, false);
92              contextAdminList.add(ctx);
93  
94              // TODO Appenders, LoggerConfigs
95          }
96      }
97  
98      /**
99       * Returns a proxy that allows operations to be performed on the remote
100      * {@code ContextSelectorAdminMBean}.
101      * 
102      * @return a proxy to the remote {@code ContextSelectorAdminMBean}
103      */
104     public ContextSelectorAdminMBean getContextSelectorAdmin() {
105         return contextSelectorAdmin;
106     }
107 
108     /**
109      * Returns a list of proxies that allow operations to be performed on the
110      * remote {@code LoggerContextAdminMBean}s.
111      * 
112      * @return a list of proxies to the remote {@code LoggerContextAdminMBean}s
113      */
114     public List<LoggerContextAdminMBean> getLoggerContextAdmins() {
115         return new ArrayList<LoggerContextAdminMBean>(contextAdminList);
116     }
117 
118     /**
119      * Closes the client connection to its server. Any ongoing or new requests
120      * to the MBeanServerConnection will fail.
121      */
122     public void close() {
123         try {
124             connector.close();
125         } catch (IOException e) {
126             e.printStackTrace();
127         }
128     }
129 
130     /**
131      * Returns the MBean server connection through which to communicate with the
132      * remote mbeans.
133      * 
134      * @return the MBean server connection
135      */
136     public MBeanServerConnection getConnection() {
137         return connection;
138     }
139 
140     /**
141      * Returns a proxy that allows operations to be performed on the remote
142      * {@code StatusLoggerAdminMBean}.
143      * 
144      * @return a proxy to the remote {@code StatusLoggerAdminMBean}
145      */
146     public StatusLoggerAdminMBean getStatusLoggerAdmin() {
147         return statusLoggerAdmin;
148     }
149 }