1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
32
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
43
44
45
46
47
48
49
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
61
62
63
64
65
66
67
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
95 }
96 }
97
98
99
100
101
102
103
104 public ContextSelectorAdminMBean getContextSelectorAdmin() {
105 return contextSelectorAdmin;
106 }
107
108
109
110
111
112
113
114 public List<LoggerContextAdminMBean> getLoggerContextAdmins() {
115 return new ArrayList<LoggerContextAdminMBean>(contextAdminList);
116 }
117
118
119
120
121
122 public void close() {
123 try {
124 connector.close();
125 } catch (IOException e) {
126 e.printStackTrace();
127 }
128 }
129
130
131
132
133
134
135
136 public MBeanServerConnection getConnection() {
137 return connection;
138 }
139
140
141
142
143
144
145
146 public StatusLoggerAdminMBean getStatusLoggerAdmin() {
147 return statusLoggerAdmin;
148 }
149 }