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.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
37
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
48
49
50
51
52
53
54
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
66
67
68
69
70
71
72
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
100 }
101 }
102
103
104
105
106
107
108
109 public ContextSelectorAdminMBean getContextSelectorAdmin() {
110 return contextSelectorAdmin;
111 }
112
113
114
115
116
117
118
119 public List<LoggerContextAdminMBean> getLoggerContextAdmins() {
120 return new ArrayList<LoggerContextAdminMBean>(contextAdminList);
121 }
122
123
124
125
126
127 public void close() {
128 try {
129 connector.close();
130 } catch (final IOException e) {
131 e.printStackTrace();
132 }
133 }
134
135
136
137
138
139
140
141 public MBeanServerConnection getConnection() {
142 return connection;
143 }
144
145
146
147
148
149
150
151 public StatusLoggerAdminMBean getStatusLoggerAdmin() {
152 return statusLoggerAdmin;
153 }
154 }