KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > services > loggingmonitor > LoggingMonitor


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.services.loggingmonitor;
23
24 import java.util.Timer JavaDoc;
25 import java.util.TimerTask JavaDoc;
26
27 import javax.management.MalformedObjectNameException JavaDoc;
28
29 import org.apache.log4j.Appender;
30 import org.apache.log4j.Level;
31 import org.apache.log4j.Logger;
32 import org.apache.log4j.PatternLayout;
33 import org.jboss.logging.appender.DailyRollingFileAppender;
34 import org.jboss.system.ServiceMBeanSupport;
35 import org.w3c.dom.DOMException JavaDoc;
36 import org.w3c.dom.Element JavaDoc;
37 import org.w3c.dom.Node JavaDoc;
38 import org.w3c.dom.NodeList JavaDoc;
39 import org.w3c.dom.Text JavaDoc;
40
41 /**
42  * This class implements the LoggingMonitor service which provides the ability
43  * to create monitoring logs for various MBeans and their attributes.
44  *
45  * @author <a HREF="mailto:jimmy.wilson@acxiom.com">James Wilson</a>
46  * @author <a HREF="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
47  * @version $Revision: 37459 $
48  */

49 public class LoggingMonitor extends ServiceMBeanSupport
50    implements LoggingMonitorMBean
51 {
52    // Static --------------------------------------------------------
53

54    public final static String JavaDoc MONITORED_MBEAN_ELEMENT = "monitoredmbean";
55    public final static String JavaDoc MBEAN_NAME_ATTRIBUTE = "name";
56    public final static String JavaDoc MBEAN_LOGGER_ATTRIBUTE = "logger";
57    public final static String JavaDoc MBEAN_ATTRIBUTE_ELEMENT = "attribute";
58    
59    public final static String JavaDoc DEFAULT_PATTERN_LAYOUT = "%d %-5p [%c] %m%n";
60    
61    // Private data --------------------------------------------------
62

63    private String JavaDoc filename;
64    private boolean appendToFile;
65    private RolloverPeriod rolloverPeriod;
66    private MonitoredMBean[] monitoredObjects;
67    private long monitorPeriod;
68    private String JavaDoc patternLayout;
69    
70    private Appender appender;
71    private Timer JavaDoc timer;
72
73    // Constructors -------------------------------------------------
74

75    /**
76     * Default constructor.
77     */

78    public LoggingMonitor()
79    {
80       appendToFile = true;
81       rolloverPeriod = new RolloverPeriod("DAY");
82       patternLayout = DEFAULT_PATTERN_LAYOUT;
83    }
84
85    // Attributes ----------------------------------------------------
86

87    /**
88     * @jmx.managed-attribute
89     */

90    public void setFilename(String JavaDoc filename)
91    {
92       if (filename == null || filename.length() == 0)
93       {
94          throw new IllegalArgumentException JavaDoc("Logging monitor's filename can not be null or empty");
95       }
96       this.filename = filename;
97    }
98    
99    /**
100     * @jmx.managed-attribute
101     */

102    public String JavaDoc getFilename()
103    {
104       return filename;
105    }
106
107    /**
108     * @jmx.managed-attribute
109     */

110    public void setAppendToFile(boolean appendToFile)
111    {
112       this.appendToFile = appendToFile;
113    }
114
115    /**
116     * @jmx.managed-attribute
117     */

118    public boolean getAppendToFile()
119    {
120       return appendToFile;
121    }
122
123    /**
124     * @jmx.managed-attribute
125     */

126    public void setRolloverPeriod(String JavaDoc rolloverPeriod)
127    {
128       this.rolloverPeriod = new RolloverPeriod(rolloverPeriod);
129    }
130    
131    /**
132     * @jmx.managed-attribute
133     */

134    public String JavaDoc getRolloverPeriod()
135    {
136       return rolloverPeriod.toString();
137    }
138
139    /**
140     * @jmx.managed-attribute
141     */

142    public void setMonitorPeriod(long monitorPeriod)
143    {
144       if (monitorPeriod < 1)
145       {
146          throw new IllegalArgumentException JavaDoc("Logging monitor's monitor period must be a positive, non-zero value");
147       }
148       this.monitorPeriod = monitorPeriod;
149    }
150    
151    /**
152     * @jmx.managed-attribute
153     */

154    public long getMonitorPeriod()
155    {
156       return monitorPeriod;
157    }
158    
159    /**
160     * @jmx.managed-attribute
161     */

162    public void setPatternLayout(String JavaDoc patternLayout)
163    {
164       this.patternLayout = patternLayout;
165    }
166    
167    /**
168     * @jmx.managed-attribute
169     */

170    public String JavaDoc getPatternLayout()
171    {
172       return patternLayout;
173    }
174    
175    /**
176     * @jmx.managed-attribute
177     */

178    public String JavaDoc getRolloverFormat()
179    {
180       return rolloverPeriod.getRolloverFormat();
181    }
182    
183    /**
184     * @jmx.managed-attribute
185     */

186    public void setMonitoredObjects(Element JavaDoc monitoredObjects) throws MalformedObjectNameException JavaDoc
187    {
188       NodeList JavaDoc monitoredMBeans = monitoredObjects.getElementsByTagName(MONITORED_MBEAN_ELEMENT);
189
190       int mbeanCount = monitoredMBeans.getLength();
191       if (mbeanCount < 1)
192       {
193          throw createMissingElementException(MONITORED_MBEAN_ELEMENT);
194       }
195
196       this.monitoredObjects = new MonitoredMBean[mbeanCount];
197       for (int i = 0; i < mbeanCount; ++i)
198       {
199          Node JavaDoc monitoredMBean = monitoredMBeans.item(i);
200          this.monitoredObjects[i] = toMonitoredMBean((Element JavaDoc) monitoredMBean);
201       }
202    }
203    
204    // ServiceMBeanSupport overrides ---------------------------------
205

206    protected void startService()
207    {
208       if (monitoredObjects == null)
209       {
210          throw new IllegalStateException JavaDoc("'MonitoredObjects' attribute not configured");
211       }
212       DailyRollingFileAppender appender = new DailyRollingFileAppender();
213       appender.setFile(filename);
214       appender.setAppend(appendToFile);
215       appender.setDatePattern(rolloverPeriod.getRolloverFormat());
216       appender.setLayout(new PatternLayout(patternLayout));
217       appender.setThreshold(Level.INFO);
218       appender.activateOptions();
219       this.appender = appender;
220
221       for (int i = 0; i < monitoredObjects.length; ++i)
222       {
223          monitoredObjects[i].getLogger().addAppender(appender);
224       }
225
226       // use the ServiceMBeanSupport logger for reporting errors
227
TimerTask JavaDoc task = new LoggingMonitorTimerTask(monitoredObjects, log);
228
229       timer = new Timer JavaDoc();
230       timer.schedule(task, 0, monitorPeriod);
231
232       log.debug("Logging monitor started logging to " + filename);
233    }
234
235    protected void stopService()
236    {
237       timer.cancel();
238
239       for (int i = 0; i < monitoredObjects.length; ++i)
240       {
241          monitoredObjects[i].getLogger().removeAllAppenders();
242       }
243
244       appender.close();
245
246       log.debug("Logging monitor stopped logging to " + filename);
247    }
248
249    // Private -------------------------------------------------------
250

251    /**
252     * Converts the specified XML DOM element to a monitored MBean.
253     *
254     * @param element the XML DOM element to be converted.
255     * @return a monitored MBean represented by the specified XML DOM element.
256     * @throws MalformedObjectNameException if the specified XML DOM element
257     * does not contain a valid object
258     * name.
259     */

260    private MonitoredMBean toMonitoredMBean(Element JavaDoc element) throws MalformedObjectNameException JavaDoc
261    {
262       String JavaDoc objectName = element.getAttribute(MBEAN_NAME_ATTRIBUTE);
263
264       if ("".equals(objectName))
265       {
266          throw createAttributeNotFoundException(MBEAN_NAME_ATTRIBUTE);
267       }
268
269       String JavaDoc loggerName = element.getAttribute(MBEAN_LOGGER_ATTRIBUTE);
270       if ("".equals(loggerName))
271       {
272          throw createAttributeNotFoundException(MBEAN_LOGGER_ATTRIBUTE);
273       }
274
275       Logger logger = Logger.getLogger(loggerName.toLowerCase());
276       logger.setAdditivity(false);
277       logger.setLevel(Level.INFO);
278
279       String JavaDoc[] attributes = getMonitoredAttributes(element);
280
281       return new MonitoredMBean(objectName, attributes, logger);
282    }
283     
284    /**
285     * Retrieves the attributes of the MBean to monitor.
286     *
287     * @param monitoredMBean a MBean, represented as a XML DOM element, for
288     * which to retrieve the attributes to monitor.
289     * @return the attributes of the MBean to monitor.
290     */

291    private String JavaDoc[] getMonitoredAttributes(Element JavaDoc monitoredMBean)
292    {
293       NodeList JavaDoc monitoredAttributes = monitoredMBean.getElementsByTagName(MBEAN_ATTRIBUTE_ELEMENT);
294
295       int monitoredAttributesCount = monitoredAttributes.getLength();
296       if (monitoredAttributesCount < 1)
297       {
298          throw createMissingElementException(MBEAN_ATTRIBUTE_ELEMENT);
299       }
300
301       String JavaDoc[] attributes = new String JavaDoc[monitoredAttributesCount];
302       for (int i = 0; i < monitoredAttributesCount; ++i)
303       {
304          Node JavaDoc node = monitoredAttributes.item(i);
305          Node JavaDoc attribute = node.getFirstChild();
306
307          if (attribute.getNodeType() != Node.TEXT_NODE)
308          {
309             throw new DOMException JavaDoc(DOMException.HIERARCHY_REQUEST_ERR,
310                "Unexpected node type inside <attribute> for monitored MBean.");
311          }
312          attributes[i] = (((Text JavaDoc) attribute).getData()).trim();
313       }
314       return attributes;
315    }
316    
317    /**
318     * Creates a <code>DOMException</code> relating that at least one occurrence
319     * of the specified element was not found as expected.
320     *
321     * @param element the expected element.
322     * @return a <code>DOMException</code> relating that at least one occurrence
323     * of the specified element was not found as expected.
324     */

325    private DOMException JavaDoc createMissingElementException(String JavaDoc element)
326    {
327       return new DOMException JavaDoc(DOMException.NOT_FOUND_ERR,
328             "At least one <" + element + "> element is expected");
329    }
330    
331    /**
332     * Creates a <code>DOMException</code> relating that the specified attribute
333     * was not found as expected.
334     *
335     * @param attribute the expected attribute.
336     * @return a <code>DOMException</code> relating that the specified attribute
337     * was not found as expected.
338     */

339    private DOMException JavaDoc createAttributeNotFoundException(String JavaDoc attribute)
340    {
341        return new DOMException JavaDoc(DOMException.NOT_FOUND_ERR,
342              "Missing expected '" + attribute + "' attribute of a <monitoredmbean> element");
343    }
344    
345 }
346
Popular Tags