KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.management.JMException JavaDoc;
25 import javax.management.MBeanServer JavaDoc;
26 import javax.management.MalformedObjectNameException JavaDoc;
27 import javax.management.ObjectName JavaDoc;
28
29 import org.apache.log4j.Logger;
30 import org.jboss.mx.util.JMXExceptionDecoder;
31 import org.jboss.mx.util.MBeanServerLocator;
32
33 /**
34  * This class encapsulates the information necessary for monitoring an MBean.
35  *
36  * @author <a HREF="mailto:jimmy.wilson@acxiom.com">James Wilson</a>
37  * @author <a HREF="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
38  * @version $Revision: 37459 $
39  */

40 class MonitoredMBean
41 {
42    private MBeanServer JavaDoc mbeanServer;
43    private ObjectName JavaDoc objectName;
44    private String JavaDoc[] attributes;
45    private Logger logger; //apache logger
46

47    /**
48     * Constructor.
49     *
50     * @param objectName this monitored MBean's object name.
51     * @param attributes the attributes of this MBean to monitor.
52     * @param logger the logger for this monitored MBean.
53     * @param throws MalformedObjectNameException if the specified object name
54     * is invalid.
55     */

56    public MonitoredMBean(String JavaDoc objectName, String JavaDoc[] attributes, Logger logger) throws MalformedObjectNameException JavaDoc
57    {
58       this.objectName = new ObjectName JavaDoc(objectName);
59       this.attributes = attributes;
60       this.logger = logger;
61       this.mbeanServer = MBeanServerLocator.locateJBoss();
62    }
63
64    /**
65     * Retrieves the object name of this monitored MBean.
66     *
67     * @return this monitored MBean's object name.
68     */

69    public ObjectName JavaDoc getObjectName()
70    {
71       return objectName;
72    }
73
74    /**
75     * Retrieves the attributes of this monitored MBean.
76     *
77     * @return this monitored MBean's attributes.
78     */

79    public String JavaDoc[] getAttributes()
80    {
81       return attributes;
82    }
83
84    /**
85     * Retrieves the logger for this monitored MBean.
86     *
87     * @return this monitored MBean's logger.
88     */

89    public Logger getLogger()
90    {
91       return logger;
92    }
93
94    /**
95     * Logs the format message for this monitored MBean.
96     */

97    public void logFormat()
98    {
99       logger.info(buildFormatMessage());
100    }
101
102    /**
103     * Logs the attributes of this MBean that are being monitored.
104     *
105     * @throws JMException if the retrieval of a MBean attribute causes such an
106     * exception.
107     */

108    public void logAttributes() throws Exception JavaDoc
109    {
110       try
111       {
112          StringBuffer JavaDoc message = new StringBuffer JavaDoc();
113
114          for (int j = 0; j < attributes.length; ++j)
115          {
116             Object JavaDoc attributeValue = mbeanServer.getAttribute(objectName, attributes[j]);
117
118             message.append(attributeValue);
119
120             if (j < (attributes.length - 1))
121             {
122                message.append(",");
123             }
124          }
125
126          logger.info(message);
127       }
128       catch (Exception JavaDoc e)
129       {
130          JMXExceptionDecoder.rethrow(e);
131       }
132    }
133
134    /**
135     * Builds the format message for this monitored MBean.
136     *
137     * @return this monitored MBean's format message.
138     */

139    private String JavaDoc buildFormatMessage()
140    {
141       StringBuffer JavaDoc message = new StringBuffer JavaDoc(objectName.toString());
142       message.append(" monitor format: (");
143
144       for (int i = 0; i < attributes.length; ++i)
145       {
146          message.append(attributes[i]);
147
148          if (i < (attributes.length - 1))
149          {
150             message.append(',');
151          }
152       }
153       message.append(")");
154
155       return message.toString();
156    }
157 }
158
Popular Tags