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