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.Enumeration; |
18 |
| import java.util.Iterator; |
19 |
| import java.util.List; |
20 |
| |
21 |
| import javax.management.InstanceAlreadyExistsException; |
22 |
| import javax.management.JMException; |
23 |
| import javax.management.MBeanAttributeInfo; |
24 |
| import javax.management.MBeanOperationInfo; |
25 |
| import javax.management.MBeanParameterInfo; |
26 |
| import javax.management.MBeanRegistration; |
27 |
| import javax.management.MBeanServer; |
28 |
| import javax.management.ObjectName; |
29 |
| |
30 |
| import mx4j.AbstractDynamicMBean; |
31 |
| |
32 |
| import org.apache.hivemind.ApplicationRuntimeException; |
33 |
| import org.apache.hivemind.management.ObjectNameBuilder; |
34 |
| import org.apache.hivemind.util.StringUtils; |
35 |
| import org.apache.log4j.LogManager; |
36 |
| import org.apache.log4j.Logger; |
37 |
| import org.apache.log4j.helpers.OptionConverter; |
38 |
| import org.apache.log4j.jmx.LoggerDynamicMBean; |
39 |
| import org.apache.log4j.spi.LoggerRepository; |
40 |
| import org.apache.oro.text.regex.MalformedPatternException; |
41 |
| import org.apache.oro.text.regex.Pattern; |
42 |
| import org.apache.oro.text.regex.Perl5Compiler; |
43 |
| import org.apache.oro.text.regex.Perl5Matcher; |
44 |
| |
45 |
| |
46 |
| |
47 |
| |
48 |
| |
49 |
| |
50 |
| |
51 |
| |
52 |
| |
53 |
| |
54 |
| |
55 |
| public class LogManagementMBean extends AbstractDynamicMBean implements MBeanRegistration, |
56 |
| LogManagement |
57 |
| { |
58 |
| private static final String OBJECT_NAME_TYPE = "logger"; |
59 |
| |
60 |
| private static final char WILDCARD = '*'; |
61 |
| |
62 |
| private static Logger logger = Logger.getLogger(LogManagementMBean.class); |
63 |
| |
64 |
| private ObjectNameBuilder _objectNameBuilder; |
65 |
| |
66 |
| private LoggerRepository _loggerRepository; |
67 |
| |
68 |
| private MBeanServer _mbeanserver; |
69 |
| |
70 |
| private List _loggerContributions; |
71 |
| |
72 |
2
| public LogManagementMBean(ObjectNameBuilder objectNameBuilder, List loggerContributions)
|
73 |
| { |
74 |
2
| _objectNameBuilder = objectNameBuilder;
|
75 |
2
| _loggerRepository = LogManager.getLoggerRepository();
|
76 |
2
| _loggerContributions = loggerContributions;
|
77 |
| } |
78 |
| |
79 |
1
| protected MBeanAttributeInfo[] createMBeanAttributeInfo()
|
80 |
| { |
81 |
1
| return new MBeanAttributeInfo[]
|
82 |
| { new MBeanAttributeInfo("Threshold", String.class.getName(), |
83 |
| "The \"threshold\" state of the logger hierarchy.", true, true, false) }; |
84 |
| } |
85 |
| |
86 |
1
| protected MBeanOperationInfo[] createMBeanOperationInfo()
|
87 |
| { |
88 |
1
| MBeanParameterInfo parameterInfo[] = new MBeanParameterInfo[1];
|
89 |
1
| parameterInfo[0] = new MBeanParameterInfo("loggerPattern", "java.lang.String",
|
90 |
| "Name of the Logger. Use * as wildcard"); |
91 |
1
| return new MBeanOperationInfo[]
|
92 |
| { new MBeanOperationInfo("addLoggerMBean", "Adds a MBean for a single Logger or " |
93 |
| + "a group of Loggers", parameterInfo, "void", 1) }; |
94 |
| } |
95 |
| |
96 |
2
| public ObjectName preRegister(MBeanServer mbeanserver, ObjectName objectname)
|
97 |
| { |
98 |
2
| _mbeanserver = mbeanserver;
|
99 |
2
| return objectname;
|
100 |
| } |
101 |
| |
102 |
2
| public void postRegister(Boolean registrationDone)
|
103 |
| { |
104 |
2
| addConfiguredLoggerMBeans();
|
105 |
| } |
106 |
| |
107 |
1
| public void preDeregister() throws Exception
|
108 |
| { |
109 |
| } |
110 |
| |
111 |
1
| public void postDeregister()
|
112 |
| { |
113 |
| } |
114 |
| |
115 |
0
| public String getThreshold()
|
116 |
| { |
117 |
0
| return _loggerRepository.getThreshold().toString();
|
118 |
| } |
119 |
| |
120 |
0
| public void setThreshold(String threshold)
|
121 |
| { |
122 |
0
| OptionConverter.toLevel(threshold, _loggerRepository.getThreshold());
|
123 |
| |
124 |
0
| _loggerRepository.setThreshold(threshold);
|
125 |
| } |
126 |
| |
127 |
| |
128 |
| |
129 |
| |
130 |
0
| public void addLoggerMBean(String loggerPattern)
|
131 |
| { |
132 |
0
| boolean hasWildcard = loggerPattern.indexOf(WILDCARD) >= 0;
|
133 |
0
| if (hasWildcard)
|
134 |
| { |
135 |
0
| addLoggerMBeansForPattern(loggerPattern);
|
136 |
| } |
137 |
| else |
138 |
| { |
139 |
0
| Logger log = LogManager.getLogger(loggerPattern);
|
140 |
0
| addLoggerMBean(log);
|
141 |
| } |
142 |
| } |
143 |
| |
144 |
| |
145 |
| |
146 |
| |
147 |
| |
148 |
| |
149 |
| |
150 |
| |
151 |
9
| protected ObjectName addLoggerMBean(Logger log)
|
152 |
| { |
153 |
9
| String name = log.getName();
|
154 |
9
| ObjectName objectname = null;
|
155 |
9
| try
|
156 |
| { |
157 |
9
| LoggerDynamicMBean loggerMBean = new LoggerDynamicMBean(log);
|
158 |
9
| objectname = getObjectNameBuilder().createObjectName(name, OBJECT_NAME_TYPE);
|
159 |
9
| _mbeanserver.registerMBean(loggerMBean, objectname);
|
160 |
| } |
161 |
| catch (InstanceAlreadyExistsException exception) |
162 |
| { |
163 |
| |
164 |
0
| log.warn("MBean for Logger " + log.getName() + " already exists");
|
165 |
| } |
166 |
| catch (JMException exception) |
167 |
| { |
168 |
0
| throw new ApplicationRuntimeException(exception);
|
169 |
| } |
170 |
9
| return objectname;
|
171 |
| } |
172 |
| |
173 |
| |
174 |
| |
175 |
| |
176 |
2
| protected void addConfiguredLoggerMBeans()
|
177 |
| { |
178 |
2
| for (Iterator iterContributions = _loggerContributions.iterator(); iterContributions
|
179 |
| .hasNext();) |
180 |
| { |
181 |
4
| LoggerContribution contribution = (LoggerContribution) iterContributions.next();
|
182 |
4
| String loggerPattern = contribution.getLoggerPattern();
|
183 |
| |
184 |
4
| addLoggerMBeansForPattern(loggerPattern);
|
185 |
| } |
186 |
| } |
187 |
| |
188 |
| |
189 |
| |
190 |
| |
191 |
| |
192 |
| |
193 |
4
| protected void addLoggerMBeansForPattern(String loggerPattern)
|
194 |
| { |
195 |
| |
196 |
4
| Enumeration loggers = LogManager.getCurrentLoggers();
|
197 |
4
| while (loggers.hasMoreElements())
|
198 |
| { |
199 |
241
| Logger log = (Logger) loggers.nextElement();
|
200 |
241
| if (isMatch(log.getName(), loggerPattern))
|
201 |
9
| addLoggerMBean(log);
|
202 |
| } |
203 |
| } |
204 |
| |
205 |
| |
206 |
| |
207 |
| |
208 |
9
| public ObjectNameBuilder getObjectNameBuilder()
|
209 |
| { |
210 |
9
| return _objectNameBuilder;
|
211 |
| } |
212 |
| |
213 |
| |
214 |
| |
215 |
| |
216 |
| |
217 |
241
| protected boolean isMatch(String loggerName, String loggerPattern)
|
218 |
| { |
219 |
| |
220 |
241
| String realLoggerPattern = StringUtils
|
221 |
| .replace(loggerPattern, "" + WILDCARD, "." + WILDCARD); |
222 |
| |
223 |
241
| Perl5Compiler compiler = new Perl5Compiler();
|
224 |
241
| Perl5Matcher matcher = new Perl5Matcher();
|
225 |
241
| Pattern compiled;
|
226 |
241
| try
|
227 |
| { |
228 |
241
| compiled = compiler.compile(realLoggerPattern);
|
229 |
| } |
230 |
| catch (MalformedPatternException e) |
231 |
| { |
232 |
0
| throw new ApplicationRuntimeException("Malformed Logger Pattern:" + realLoggerPattern);
|
233 |
| } |
234 |
241
| return matcher.matches(loggerName, compiled);
|
235 |
| |
236 |
| } |
237 |
| |
238 |
| } |