1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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.JMException;
25 import javax.management.JMX;
26 import javax.management.MBeanServerConnection;
27 import javax.management.MalformedObjectNameException;
28 import javax.management.ObjectName;
29 import javax.management.remote.JMXConnector;
30
31 import org.apache.logging.log4j.core.jmx.LoggerContextAdminMBean;
32 import org.apache.logging.log4j.core.jmx.Server;
33 import org.apache.logging.log4j.core.jmx.StatusLoggerAdminMBean;
34 import org.apache.logging.log4j.core.util.Assert;
35
36
37
38
39
40 public class Client {
41 private JMXConnector connector;
42 private final MBeanServerConnection connection;
43
44
45
46
47
48
49
50
51
52
53
54 public Client(final JMXConnector connector) throws MalformedObjectNameException, IOException {
55 this.connector = Assert.requireNonNull(connector, "JMXConnector");
56 this.connector.connect();
57 this.connection = connector.getMBeanServerConnection();
58 init();
59 }
60
61
62
63
64
65
66
67
68
69
70
71 public Client(final MBeanServerConnection mBeanServerConnection) throws MalformedObjectNameException, IOException {
72 this.connection = mBeanServerConnection;
73 init();
74 }
75
76 private void init() throws MalformedObjectNameException, IOException {
77 }
78
79 private Set<ObjectName> find(final String pattern) throws JMException, IOException {
80 final ObjectName search = new ObjectName(String.format(pattern, "*"));
81 final Set<ObjectName> result = connection.queryNames(search, null);
82 return result;
83 }
84
85
86
87
88
89
90
91
92
93 public List<LoggerContextAdminMBean> getLoggerContextAdmins() throws JMException, IOException {
94 final List<LoggerContextAdminMBean> result = new ArrayList<LoggerContextAdminMBean>();
95 final Set<ObjectName> contextNames = find(LoggerContextAdminMBean.PATTERN);
96 for (final ObjectName contextName : contextNames) {
97 result.add(getLoggerContextAdmin(contextName));
98 }
99 return result;
100 }
101
102 public LoggerContextAdminMBean getLoggerContextAdmin(final ObjectName name) {
103 final LoggerContextAdminMBean ctx = JMX.newMBeanProxy(connection,
104 name,
105 LoggerContextAdminMBean.class, false);
106 return ctx;
107 }
108
109
110
111
112
113 public void close() {
114 try {
115 connector.close();
116 } catch (final IOException e) {
117 e.printStackTrace();
118 }
119 }
120
121
122
123
124
125
126
127 public MBeanServerConnection getConnection() {
128 return connection;
129 }
130
131
132
133
134
135
136
137
138
139
140 public StatusLoggerAdminMBean getStatusLoggerAdmin(final String contextName)
141 throws MalformedObjectNameException, IOException {
142 final String pattern = StatusLoggerAdminMBean.PATTERN;
143 final String mbean = String.format(pattern, Server.escape(contextName));
144 final ObjectName search = new ObjectName(mbean);
145 final Set<ObjectName> result = connection.queryNames(search, null);
146 if (result.size() == 0) {
147 return null;
148 }
149 if (result.size() > 1) {
150 System.err.println("WARN: multiple status loggers found for " + contextName + ": " + result);
151 }
152 final StatusLoggerAdminMBean proxy = JMX.newMBeanProxy(connection,
153 result.iterator().next(),
154 StatusLoggerAdminMBean.class, true);
155 return proxy;
156 }
157
158
159
160
161
162
163
164
165
166 public boolean isLoggerContext(final ObjectName mbeanName) {
167 return Server.DOMAIN.equals(mbeanName.getDomain())
168 && mbeanName.getKeyPropertyList().containsKey("type")
169 && mbeanName.getKeyPropertyList().size() == 1;
170 }
171
172
173
174
175
176
177
178
179
180 public ObjectName getStatusLoggerObjectName(final ObjectName loggerContextObjName) {
181 if (!isLoggerContext(loggerContextObjName)) {
182 throw new IllegalArgumentException("Not a LoggerContext: " + loggerContextObjName);
183 }
184 final String cxtName = loggerContextObjName.getKeyProperty("type");
185 final String name = String.format(StatusLoggerAdminMBean.PATTERN, cxtName);
186 try {
187 return new ObjectName(name);
188 } catch (final MalformedObjectNameException ex) {
189 throw new IllegalStateException(name, ex);
190 }
191 }
192 }