|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| package org.apache.hivemind.management.log4j; |
|
16 |
| |
|
17 |
| import java.util.ArrayList; |
|
18 |
| import java.util.Enumeration; |
|
19 |
| import java.util.List; |
|
20 |
| |
|
21 |
| import javax.management.Attribute; |
|
22 |
| import javax.management.AttributeNotFoundException; |
|
23 |
| import javax.management.InvalidAttributeValueException; |
|
24 |
| import javax.management.MBeanAttributeInfo; |
|
25 |
| import javax.management.MBeanConstructorInfo; |
|
26 |
| import javax.management.MBeanException; |
|
27 |
| import javax.management.MBeanInfo; |
|
28 |
| import javax.management.MBeanNotificationInfo; |
|
29 |
| import javax.management.MBeanOperationInfo; |
|
30 |
| import javax.management.MBeanParameterInfo; |
|
31 |
| import javax.management.Notification; |
|
32 |
| import javax.management.NotificationListener; |
|
33 |
| import javax.management.ObjectName; |
|
34 |
| import javax.management.ReflectionException; |
|
35 |
| import javax.management.RuntimeOperationsException; |
|
36 |
| |
|
37 |
| import org.apache.hivemind.management.mbeans.AbstractDynamicMBean; |
|
38 |
| import org.apache.log4j.Appender; |
|
39 |
| import org.apache.log4j.Level; |
|
40 |
| import org.apache.log4j.Logger; |
|
41 |
| import org.apache.log4j.helpers.OptionConverter; |
|
42 |
| import org.apache.log4j.jmx.AppenderDynamicMBean; |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| public class LoggerMBean extends AbstractDynamicMBean implements NotificationListener |
|
53 |
| { |
|
54 |
| |
|
55 |
| private MBeanConstructorInfo[] _constructors = new MBeanConstructorInfo[0]; |
|
56 |
| |
|
57 |
| private MBeanOperationInfo[] _operations = new MBeanOperationInfo[1]; |
|
58 |
| |
|
59 |
| private List _attributes = new ArrayList(); |
|
60 |
| |
|
61 |
| private String _className = this.getClass().getName(); |
|
62 |
| |
|
63 |
| private String _description = "This MBean acts as a management facade for a org.apache.log4j.Logger instance."; |
|
64 |
| |
|
65 |
| |
|
66 |
| private static Logger _log = Logger.getLogger(LoggerMBean.class); |
|
67 |
| |
|
68 |
| |
|
69 |
| private Logger _logger; |
|
70 |
| |
|
71 |
9
| public LoggerMBean(Logger logger)
|
|
72 |
| { |
|
73 |
9
| this._logger = logger;
|
|
74 |
9
| buildDynamicMBeanInfo();
|
|
75 |
| } |
|
76 |
| |
|
77 |
0
| public void handleNotification(Notification notification, Object handback)
|
|
78 |
| { |
|
79 |
0
| _log.debug("Received notification: " + notification.getType());
|
|
80 |
0
| registerAppenderMBean((Appender) notification.getUserData());
|
|
81 |
| |
|
82 |
| } |
|
83 |
| |
|
84 |
9
| private void buildDynamicMBeanInfo()
|
|
85 |
| { |
|
86 |
9
| _attributes.add(new MBeanAttributeInfo("name", "java.lang.String",
|
|
87 |
| "The name of this Logger.", true, false, false)); |
|
88 |
| |
|
89 |
9
| _attributes.add(new MBeanAttributeInfo("priority", "java.lang.String",
|
|
90 |
| "The priority of this logger.", true, true, false)); |
|
91 |
| |
|
92 |
9
| MBeanParameterInfo[] params = new MBeanParameterInfo[2];
|
|
93 |
9
| params[0] = new MBeanParameterInfo("class_name", "java.lang.String",
|
|
94 |
| "add an appender to this logger"); |
|
95 |
9
| params[1] = new MBeanParameterInfo("appender_name", "java.lang.String",
|
|
96 |
| "name of the appender"); |
|
97 |
| |
|
98 |
9
| _operations[0] = new MBeanOperationInfo("addAppender", "addAppender(): add an appender",
|
|
99 |
| params, "void", MBeanOperationInfo.ACTION); |
|
100 |
| } |
|
101 |
| |
|
102 |
0
| protected Logger getLogger()
|
|
103 |
| { |
|
104 |
0
| return _logger;
|
|
105 |
| } |
|
106 |
| |
|
107 |
9
| public MBeanInfo getMBeanInfo()
|
|
108 |
| { |
|
109 |
9
| MBeanAttributeInfo[] attribs = new MBeanAttributeInfo[_attributes.size()];
|
|
110 |
9
| _attributes.toArray(attribs);
|
|
111 |
| |
|
112 |
9
| MBeanInfo mb = new MBeanInfo(_className, _description, attribs, _constructors, _operations,
|
|
113 |
| new MBeanNotificationInfo[0]); |
|
114 |
| |
|
115 |
9
| return mb;
|
|
116 |
| } |
|
117 |
| |
|
118 |
0
| public Object invoke(String operationName, Object params[], String signature[])
|
|
119 |
| throws MBeanException, ReflectionException |
|
120 |
| { |
|
121 |
| |
|
122 |
0
| if (operationName.equals("addAppender"))
|
|
123 |
| { |
|
124 |
0
| addAppender((String) params[0], (String) params[1]);
|
|
125 |
0
| return "Hello world.";
|
|
126 |
| } |
|
127 |
| |
|
128 |
0
| return null;
|
|
129 |
| } |
|
130 |
| |
|
131 |
17
| public Object getAttribute(String attributeName) throws AttributeNotFoundException,
|
|
132 |
| MBeanException, ReflectionException |
|
133 |
| { |
|
134 |
| |
|
135 |
| |
|
136 |
17
| if (attributeName == null)
|
|
137 |
| { |
|
138 |
0
| throw new RuntimeOperationsException(new IllegalArgumentException(
|
|
139 |
| "Attribute name cannot be null"), "Cannot invoke a getter of " + _className |
|
140 |
| + " with null attribute name"); |
|
141 |
| } |
|
142 |
| |
|
143 |
| |
|
144 |
17
| if (attributeName.equals("name"))
|
|
145 |
| { |
|
146 |
17
| return _logger.getName();
|
|
147 |
| } |
|
148 |
0
| else if (attributeName.equals("priority"))
|
|
149 |
| { |
|
150 |
0
| Level l = _logger.getLevel();
|
|
151 |
0
| if (l == null)
|
|
152 |
0
| return null;
|
|
153 |
| |
|
154 |
0
| return l.toString();
|
|
155 |
| } |
|
156 |
0
| else if (attributeName.startsWith("appender="))
|
|
157 |
| { |
|
158 |
0
| try
|
|
159 |
| { |
|
160 |
0
| return new ObjectName("log4j:" + attributeName);
|
|
161 |
| } |
|
162 |
| catch (Exception e) |
|
163 |
| { |
|
164 |
0
| _log.error("Could not create ObjectName" + attributeName);
|
|
165 |
| } |
|
166 |
| } |
|
167 |
| |
|
168 |
| |
|
169 |
0
| throw (new AttributeNotFoundException("Cannot find " + attributeName + " attribute in "
|
|
170 |
| + _className)); |
|
171 |
| |
|
172 |
| } |
|
173 |
| |
|
174 |
0
| void addAppender(String appenderClass, String appenderName)
|
|
175 |
| { |
|
176 |
0
| _log.debug("addAppender called with " + appenderClass + ", " + appenderName);
|
|
177 |
0
| Appender appender = (Appender) OptionConverter.instantiateByClassName(
|
|
178 |
| appenderClass, |
|
179 |
| org.apache.log4j.Appender.class, |
|
180 |
| null); |
|
181 |
0
| appender.setName(appenderName);
|
|
182 |
0
| _logger.addAppender(appender);
|
|
183 |
| |
|
184 |
| } |
|
185 |
| |
|
186 |
0
| public void setAttribute(Attribute attribute) throws AttributeNotFoundException,
|
|
187 |
| InvalidAttributeValueException, MBeanException, ReflectionException |
|
188 |
| { |
|
189 |
| |
|
190 |
| |
|
191 |
0
| if (attribute == null)
|
|
192 |
| { |
|
193 |
0
| throw new RuntimeOperationsException(new IllegalArgumentException(
|
|
194 |
| "Attribute cannot be null"), "Cannot invoke a setter of " + _className |
|
195 |
| + " with null attribute"); |
|
196 |
| } |
|
197 |
0
| String name = attribute.getName();
|
|
198 |
0
| Object value = attribute.getValue();
|
|
199 |
| |
|
200 |
0
| if (name == null)
|
|
201 |
| { |
|
202 |
0
| throw new RuntimeOperationsException(new IllegalArgumentException(
|
|
203 |
| "Attribute name cannot be null"), "Cannot invoke the setter of " + _className |
|
204 |
| + " with null attribute name"); |
|
205 |
| } |
|
206 |
| |
|
207 |
0
| if (name.equals("priority"))
|
|
208 |
| { |
|
209 |
0
| if (value instanceof String)
|
|
210 |
| { |
|
211 |
0
| String s = (String) value;
|
|
212 |
0
| Level p = _logger.getLevel();
|
|
213 |
0
| if (s.equalsIgnoreCase("NULL"))
|
|
214 |
| { |
|
215 |
0
| p = null;
|
|
216 |
| } |
|
217 |
| else |
|
218 |
| { |
|
219 |
0
| p = OptionConverter.toLevel(s, p);
|
|
220 |
| } |
|
221 |
0
| _logger.setLevel(p);
|
|
222 |
| } |
|
223 |
| } |
|
224 |
| else |
|
225 |
| { |
|
226 |
0
| throw (new AttributeNotFoundException("Attribute " + name + " not found in "
|
|
227 |
| + this.getClass().getName())); |
|
228 |
| } |
|
229 |
| } |
|
230 |
| |
|
231 |
4
| void appenderMBeanRegistration()
|
|
232 |
| { |
|
233 |
4
| Enumeration enumeration = _logger.getAllAppenders();
|
|
234 |
4
| while (enumeration.hasMoreElements())
|
|
235 |
| { |
|
236 |
0
| Appender appender = (Appender) enumeration.nextElement();
|
|
237 |
0
| registerAppenderMBean(appender);
|
|
238 |
| } |
|
239 |
| } |
|
240 |
| |
|
241 |
| |
|
242 |
| |
|
243 |
| |
|
244 |
| |
|
245 |
| |
|
246 |
0
| void registerAppenderMBean(Appender appender)
|
|
247 |
| { |
|
248 |
0
| String name = appender.getName();
|
|
249 |
0
| _log.debug("Adding AppenderMBean for appender named " + name);
|
|
250 |
0
| ObjectName objectName = null;
|
|
251 |
0
| try
|
|
252 |
| { |
|
253 |
0
| objectName = new ObjectName("log4j", "appender", name);
|
|
254 |
| |
|
255 |
0
| if (!getMBeanServer().isRegistered(objectName))
|
|
256 |
| { |
|
257 |
0
| AppenderDynamicMBean appenderMBean = new AppenderDynamicMBean(appender);
|
|
258 |
0
| getMBeanServer().registerMBean(appenderMBean, objectName);
|
|
259 |
| |
|
260 |
0
| _attributes.add(new MBeanAttributeInfo("appender=" + name,
|
|
261 |
| "javax.management.ObjectName", "The " + name + " appender.", true, true, |
|
262 |
| false)); |
|
263 |
| } |
|
264 |
| |
|
265 |
| } |
|
266 |
| catch (Exception e) |
|
267 |
| { |
|
268 |
0
| _log.error("Could not add appenderMBean for [" + name + "].", e);
|
|
269 |
| } |
|
270 |
| } |
|
271 |
| |
|
272 |
4
| public void postRegister(java.lang.Boolean registrationDone)
|
|
273 |
| { |
|
274 |
4
| appenderMBeanRegistration();
|
|
275 |
| } |
|
276 |
| } |