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