KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > management > log4j > LogManagementMBean


1 // Copyright 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.hivemind.management.log4j;
16
17 import java.util.Enumeration JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20
21 import javax.management.InstanceAlreadyExistsException JavaDoc;
22 import javax.management.JMException JavaDoc;
23 import javax.management.MBeanAttributeInfo JavaDoc;
24 import javax.management.MBeanOperationInfo JavaDoc;
25 import javax.management.MBeanParameterInfo JavaDoc;
26 import javax.management.ObjectName JavaDoc;
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  * MBean that manages MBeans for Log4j Loggers. New MBeans can be added by specifying the Logger
43  * name or a logger pattern. Each MBean allows managing level and appenders of a single logger. Uses
44  * the LoggerDynamicMBean from the log4j library. Similar to
45  * {@link org.apache.log4j.jmx.HierarchyDynamicMBean} but implements the hivemind ObjectName scheme
46  * by using ObjectNameBuilder service.
47  *
48  * @author Achim Huegen
49  * @since 1.1
50  */

51 public class LogManagementMBean extends AbstractDynamicMBean implements LogManagement
52 {
53     private static final String JavaDoc 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 JavaDoc _loggerContributions;
64
65     public LogManagementMBean(ObjectNameBuilder objectNameBuilder, List JavaDoc loggerContributions)
66     {
67         _objectNameBuilder = objectNameBuilder;
68         _loggerRepository = LogManager.getLoggerRepository();
69         _loggerContributions = loggerContributions;
70     }
71
72     protected MBeanAttributeInfo JavaDoc[] createMBeanAttributeInfo()
73     {
74         return new MBeanAttributeInfo JavaDoc[]
75         { new MBeanAttributeInfo JavaDoc("Threshold", String JavaDoc.class.getName(),
76                 "The \"threshold\" state of the logger hierarchy.", true, true, false) };
77     }
78
79     protected MBeanOperationInfo JavaDoc[] createMBeanOperationInfo()
80     {
81         MBeanParameterInfo JavaDoc parameterInfo[] = new MBeanParameterInfo JavaDoc[1];
82         parameterInfo[0] = new MBeanParameterInfo JavaDoc("loggerPattern", "java.lang.String",
83                 "Name of the Logger. Use * as wildcard");
84         return new MBeanOperationInfo JavaDoc[]
85         { new MBeanOperationInfo JavaDoc("addLoggerMBean", "Adds a MBean for a single Logger or "
86                 + "a group of Loggers", parameterInfo, "void", 1) };
87     }
88     
89     public void postRegister(Boolean JavaDoc registrationDone)
90     {
91         addConfiguredLoggerMBeans();
92     }
93
94     public String JavaDoc getThreshold()
95     {
96         return _loggerRepository.getThreshold().toString();
97     }
98
99     public void setThreshold(String JavaDoc threshold)
100     {
101         OptionConverter.toLevel(threshold, _loggerRepository.getThreshold());
102
103         _loggerRepository.setThreshold(threshold);
104     }
105
106     /**
107      * @see org.apache.hivemind.management.log4j.LogManagement#addLoggerMBean(java.lang.String)
108      */

109     public void addLoggerMBean(String JavaDoc loggerPattern)
110     {
111         boolean hasWildcard = loggerPattern.indexOf(WILDCARD) >= 0;
112         if (hasWildcard)
113         {
114             addLoggerMBeansForPattern(loggerPattern);
115         }
116         else
117         {
118             Logger log = LogManager.getLogger(loggerPattern);
119             addLoggerMBean(log);
120         }
121     }
122
123     /**
124      * Adds a MBean for a logger.
125      *
126      * @param log
127      * the logger
128      * @return ObjectName of created MBean
129      */

130     protected ObjectName JavaDoc addLoggerMBean(Logger log)
131     {
132         String JavaDoc name = log.getName();
133         ObjectName JavaDoc objectname = null;
134         try
135         {
136             LoggerMBean loggerMBean = new LoggerMBean(log);
137             objectname = getObjectNameBuilder().createObjectName(name, OBJECT_NAME_TYPE);
138             getMBeanServer().registerMBean(loggerMBean, objectname);
139         }
140         catch (InstanceAlreadyExistsException JavaDoc exception)
141         {
142             // just warn
143
logger.warn("MBean for Logger " + log.getName() + " already exists");
144         }
145         catch (JMException JavaDoc exception)
146         {
147             throw new ApplicationRuntimeException(exception);
148         }
149         return objectname;
150     }
151
152     /**
153      * Adds MBeans for all Loggers that are defined in the service configuration
154      */

155     protected void addConfiguredLoggerMBeans()
156     {
157         for (Iterator JavaDoc iterContributions = _loggerContributions.iterator(); iterContributions
158                 .hasNext();)
159         {
160             LoggerContribution contribution = (LoggerContribution) iterContributions.next();
161             String JavaDoc loggerPattern = contribution.getLoggerPattern();
162
163             addLoggerMBeansForPattern(loggerPattern);
164         }
165     }
166
167     /**
168      * Adds MBeans for all existing Loggers, that match the loggerPattern
169      *
170      * @param loggerPattern
171      */

172     protected void addLoggerMBeansForPattern(String JavaDoc loggerPattern)
173     {
174         // Add MBeans for all loggers that match the pattern
175
Enumeration JavaDoc loggers = LogManager.getCurrentLoggers();
176         while (loggers.hasMoreElements())
177         {
178             Logger log = (Logger) loggers.nextElement();
179             if (isMatch(log.getName(), loggerPattern))
180                 addLoggerMBean(log);
181         }
182     }
183
184     /**
185      * @return Returns the _objectNameBuilder.
186      */

187     public ObjectNameBuilder getObjectNameBuilder()
188     {
189         return _objectNameBuilder;
190     }
191
192     /**
193      * Returns true if loggerName matches a loggerPattern The pattern kann contain '*' as wildcard
194      * character. This gets translated to '.*' and is used for a regex match using jakarta oro
195      */

196     protected boolean isMatch(String JavaDoc loggerName, String JavaDoc loggerPattern)
197     {
198         // Adapt loggerPattern for oro
199
String JavaDoc realLoggerPattern = StringUtils
200                 .replace(loggerPattern, "" + WILDCARD, "." + WILDCARD);
201
202         Perl5Compiler compiler = new Perl5Compiler();
203         Perl5Matcher matcher = new Perl5Matcher();
204         Pattern compiled;
205         try
206         {
207             compiled = compiler.compile(realLoggerPattern);
208         }
209         catch (MalformedPatternException e)
210         {
211             throw new ApplicationRuntimeException("Malformed Logger Pattern:" + realLoggerPattern);
212         }
213         return matcher.matches(loggerName, compiled);
214
215     }
216
217 }
Popular Tags