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