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