Line 0
Link Here
|
|
|
1 |
package org.apache.log4j.jmx; |
2 |
|
3 |
import java.util.ArrayList; |
4 |
import java.util.List; |
5 |
import java.util.Map; |
6 |
|
7 |
import javax.management.MBeanException; |
8 |
import javax.management.MBeanOperationInfo; |
9 |
import javax.management.MBeanParameterInfo; |
10 |
import javax.management.RuntimeOperationsException; |
11 |
import javax.management.modelmbean.ModelMBeanAttributeInfo; |
12 |
import javax.management.modelmbean.ModelMBeanInfo; |
13 |
import javax.management.modelmbean.ModelMBeanOperationInfo; |
14 |
|
15 |
/** |
16 |
* Model MBean which makes the methods of <code>LoggerManager</code> available per JMX. |
17 |
* @author Stefan Fleiter |
18 |
* @see LoggerManager |
19 |
*/ |
20 |
public class LoggerManagerModelMBean extends AbstractModelMBean { |
21 |
|
22 |
/** |
23 |
* Constructor. |
24 |
* @throws MBeanException user Exception of MBean |
25 |
* @throws RuntimeOperationsException Runtime Exceptions in the MBeanServer |
26 |
*/ |
27 |
public LoggerManagerModelMBean() throws MBeanException, RuntimeOperationsException { |
28 |
this(new LoggerManager()); |
29 |
} |
30 |
|
31 |
/** |
32 |
* Constructor to use an own instance of LoggerManager to represent per JMX. |
33 |
* @param loggerManager Instance of LoggerManager to represent as MBean |
34 |
* @throws MBeanException user Exception of MBean |
35 |
* @throws RuntimeOperationsException Runtime Exceptions in the MBeanServer |
36 |
*/ |
37 |
public LoggerManagerModelMBean(LoggerManager loggerManager) throws MBeanException, RuntimeOperationsException { |
38 |
super(loggerManager); |
39 |
setModelMBeanInfo(getModelMBeanInfo()); |
40 |
try { |
41 |
setManagedResource(loggerManager, "ObjectReference"); |
42 |
} catch (Exception e) { |
43 |
throw new RuntimeException(e); |
44 |
} |
45 |
} |
46 |
|
47 |
/** |
48 |
* Creates the ModelMBeanOperationInfos for this ModelMBean, extracted for easy extendability. |
49 |
* @return Array of ModelMBeanOperationInfo describing the methods available per JMX |
50 |
*/ |
51 |
protected ModelMBeanOperationInfo[] createModelMBeanOperationInfos() { |
52 |
ModelMBeanOperationInfo getLogConfiguration = createOperationInfo( |
53 |
"getLogConfiguration", |
54 |
"Return the levels of all explicitly configured loggers", |
55 |
new MBeanParameterInfo[0], |
56 |
Map.class, |
57 |
ModelMBeanOperationInfo.INFO |
58 |
); |
59 |
ModelMBeanOperationInfo getConfiguredLoggers = createOperationInfo( |
60 |
"getConfiguredLoggers", |
61 |
"Return the names of the explicitly configured loggers", |
62 |
new MBeanParameterInfo[0], |
63 |
String[].class, |
64 |
ModelMBeanOperationInfo.INFO |
65 |
); |
66 |
|
67 |
|
68 |
ModelMBeanOperationInfo getRootLogLevel = createOperationInfo( |
69 |
"getRootLogLevel", |
70 |
"Return the level of the root logger", |
71 |
new MBeanParameterInfo[0], |
72 |
String.class, |
73 |
MBeanOperationInfo.INFO |
74 |
); |
75 |
ModelMBeanOperationInfo setRootLogLevel = createOperationInfo( |
76 |
"setRootLogLevel", |
77 |
"Set the level of the root logger", |
78 |
new MBeanParameterInfo[] { |
79 |
new MBeanParameterInfo( |
80 |
"level", |
81 |
String.class.getName(), |
82 |
"New level of root logger (INHERITED to inherit level from next ancestor)" |
83 |
), |
84 |
|
85 |
}, |
86 |
void.class, |
87 |
ModelMBeanOperationInfo.ACTION |
88 |
); |
89 |
|
90 |
ModelMBeanOperationInfo getLogLevel = createOperationInfo( |
91 |
"getLogLevel", |
92 |
"Return the level explicitly set at the specified logger, null if this logger INHERITS its level", |
93 |
new MBeanParameterInfo[] { |
94 |
new MBeanParameterInfo( |
95 |
"logger", |
96 |
String.class.getName(), |
97 |
"Name of logger to query level" |
98 |
), |
99 |
}, |
100 |
String.class, |
101 |
MBeanOperationInfo.INFO |
102 |
); |
103 |
ModelMBeanOperationInfo getEffectiveLogLevel = createOperationInfo( |
104 |
"getEffectiveLogLevel", |
105 |
"Return the effective level of the specified logger " + |
106 |
"(the explicitly specified level or the level of the next ancestor)", |
107 |
new MBeanParameterInfo[] { |
108 |
new MBeanParameterInfo( |
109 |
"logger", |
110 |
String.class.getName(), |
111 |
"Name of logger to query level" |
112 |
), |
113 |
}, |
114 |
String.class, |
115 |
MBeanOperationInfo.INFO |
116 |
); |
117 |
ModelMBeanOperationInfo setLogLevel = createOperationInfo( |
118 |
"setLogLevel", |
119 |
"Set the level of the specified logger", |
120 |
new MBeanParameterInfo[] { |
121 |
new MBeanParameterInfo( |
122 |
"logger", |
123 |
String.class.getName(), |
124 |
"name of logger to modify level" |
125 |
|
126 |
), |
127 |
new MBeanParameterInfo( |
128 |
"level", |
129 |
String.class.getName(), |
130 |
"new level of specified logger (INHERITED to inherit level from next ancestor)" |
131 |
), |
132 |
}, |
133 |
void.class, |
134 |
MBeanOperationInfo.ACTION |
135 |
); |
136 |
|
137 |
ModelMBeanOperationInfo rollbackLogConfiguration = createOperationInfo( |
138 |
"rollbackLogConfiguration", |
139 |
"Resets all loggers to its' states before calling actions in this MBean", |
140 |
new MBeanParameterInfo[0], |
141 |
void.class, |
142 |
MBeanOperationInfo.ACTION |
143 |
); |
144 |
|
145 |
|
146 |
ModelMBeanOperationInfo[] modelMBeanOperationInfos = new ModelMBeanOperationInfo[] { |
147 |
getLogConfiguration, getConfiguredLoggers, |
148 |
getRootLogLevel, setRootLogLevel, |
149 |
getLogLevel, getEffectiveLogLevel, setLogLevel, |
150 |
}; |
151 |
|
152 |
// dynamically register rollbackLogConfiguration, but only if our LoggerManager has that method |
153 |
List modelMBeanOperations = new ArrayList(); |
154 |
try { |
155 |
getDelegate().getClass().getMethod("rollbackLogConfiguration", null); |
156 |
modelMBeanOperations.add(rollbackLogConfiguration); |
157 |
} catch (NoSuchMethodException expected) { |
158 |
// nothing to do |
159 |
} |
160 |
for (int i = 0; i < modelMBeanOperationInfos.length; ++i) { |
161 |
modelMBeanOperations.add(modelMBeanOperationInfos[i]); |
162 |
} |
163 |
modelMBeanOperationInfos = (ModelMBeanOperationInfo[]) modelMBeanOperations.toArray( |
164 |
new ModelMBeanOperationInfo[modelMBeanOperations.size()]); |
165 |
return modelMBeanOperationInfos; |
166 |
} |
167 |
|
168 |
/** |
169 |
* {@inheritDoc} |
170 |
*/ |
171 |
protected ModelMBeanAttributeInfo[] createModelMBeanAttributeInfos() { |
172 |
ModelMBeanAttributeInfo rootLoggerAttribute = createAttributeInfo( |
173 |
"rootLogger", |
174 |
"Level of root logger", |
175 |
String.class, |
176 |
true, |
177 |
true, |
178 |
false, |
179 |
"getRootLogLevel", |
180 |
"setRootLogLevel" |
181 |
); |
182 |
|
183 |
ModelMBeanAttributeInfo configuredLoggersAttribute = createAttributeInfo( |
184 |
"configuredLoggers", |
185 |
"Names of all loggers with explicitly set log levels", |
186 |
String[].class, |
187 |
true, |
188 |
false, |
189 |
false, |
190 |
"getConfiguredLoggers", |
191 |
null |
192 |
); |
193 |
|
194 |
ModelMBeanAttributeInfo logConfigurationAttribute = createAttributeInfo( |
195 |
"logConfiguration", |
196 |
"Name of all explicitly configured loggers with their log level", |
197 |
Map.class, |
198 |
true, |
199 |
false, |
200 |
false, |
201 |
"getLogConfiguration", |
202 |
null |
203 |
); |
204 |
|
205 |
return new ModelMBeanAttributeInfo[] { |
206 |
rootLoggerAttribute, |
207 |
configuredLoggersAttribute, |
208 |
logConfigurationAttribute, |
209 |
}; |
210 |
} |
211 |
|
212 |
/** |
213 |
* Constructs the ModelMBeanInfo for this Model MBean. |
214 |
* @return ModelMBeanInfo instance |
215 |
* @throws MBeanException user Exception of MBean |
216 |
* @throws RuntimeOperationsException Runtime Exceptions in the MBeanServer |
217 |
*/ |
218 |
protected ModelMBeanInfo getModelMBeanInfo() throws RuntimeOperationsException, MBeanException { |
219 |
Class clazz = getDelegate().getClass(); |
220 |
String displayName = "Log4jManager"; |
221 |
String description = "Model MBean for Manager Logger Configuration of Log4j"; |
222 |
String mbeanName = "log4j:manager"; |
223 |
return createModelMBeanInfo(displayName, description, clazz, mbeanName); |
224 |
} |
225 |
|
226 |
/** |
227 |
* Calls <code>LoggerManager#destroy()</code>. |
228 |
* @see LoggerManager#destroy() |
229 |
* {@inheritDoc}} |
230 |
*/ |
231 |
public void postDeregister() { |
232 |
((LoggerManager) getDelegate()).destroy(); |
233 |
super.postDeregister(); |
234 |
} |
235 |
|
236 |
|
237 |
} |
0 |
+ Id |
238 |
+ Id |