001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.services.loggingmonitor;
023:
024: import javax.management.JMException;
025: import javax.management.MBeanServer;
026: import javax.management.MalformedObjectNameException;
027: import javax.management.ObjectName;
028:
029: import org.apache.log4j.Logger;
030: import org.jboss.mx.util.JMXExceptionDecoder;
031: import org.jboss.mx.util.MBeanServerLocator;
032:
033: /**
034: * This class encapsulates the information necessary for monitoring an MBean.
035: *
036: * @author <a href="mailto:jimmy.wilson@acxiom.com">James Wilson</a>
037: * @author <a href="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
038: * @version $Revision: 57210 $
039: */
040: class MonitoredMBean {
041: private MBeanServer mbeanServer;
042: private ObjectName objectName;
043: private String[] attributes;
044: private Logger logger; //apache logger
045:
046: /**
047: * Constructor.
048: *
049: * @param objectName this monitored MBean's object name.
050: * @param attributes the attributes of this MBean to monitor.
051: * @param logger the logger for this monitored MBean.
052: * @param throws MalformedObjectNameException if the specified object name
053: * is invalid.
054: */
055: public MonitoredMBean(String objectName, String[] attributes,
056: Logger logger) throws MalformedObjectNameException {
057: this .objectName = new ObjectName(objectName);
058: this .attributes = attributes;
059: this .logger = logger;
060: this .mbeanServer = MBeanServerLocator.locateJBoss();
061: }
062:
063: /**
064: * Retrieves the object name of this monitored MBean.
065: *
066: * @return this monitored MBean's object name.
067: */
068: public ObjectName getObjectName() {
069: return objectName;
070: }
071:
072: /**
073: * Retrieves the attributes of this monitored MBean.
074: *
075: * @return this monitored MBean's attributes.
076: */
077: public String[] getAttributes() {
078: return attributes;
079: }
080:
081: /**
082: * Retrieves the logger for this monitored MBean.
083: *
084: * @return this monitored MBean's logger.
085: */
086: public Logger getLogger() {
087: return logger;
088: }
089:
090: /**
091: * Logs the format message for this monitored MBean.
092: */
093: public void logFormat() {
094: logger.info(buildFormatMessage());
095: }
096:
097: /**
098: * Logs the attributes of this MBean that are being monitored.
099: *
100: * @throws JMException if the retrieval of a MBean attribute causes such an
101: * exception.
102: */
103: public void logAttributes() throws Exception {
104: try {
105: StringBuffer message = new StringBuffer();
106:
107: for (int j = 0; j < attributes.length; ++j) {
108: Object attributeValue = mbeanServer.getAttribute(
109: objectName, attributes[j]);
110:
111: message.append(attributeValue);
112:
113: if (j < (attributes.length - 1)) {
114: message.append(",");
115: }
116: }
117:
118: logger.info(message);
119: } catch (Exception e) {
120: JMXExceptionDecoder.rethrow(e);
121: }
122: }
123:
124: /**
125: * Builds the format message for this monitored MBean.
126: *
127: * @return this monitored MBean's format message.
128: */
129: private String buildFormatMessage() {
130: StringBuffer message = new StringBuffer(objectName.toString());
131: message.append(" monitor format: (");
132:
133: for (int i = 0; i < attributes.length; ++i) {
134: message.append(attributes[i]);
135:
136: if (i < (attributes.length - 1)) {
137: message.append(',');
138: }
139: }
140: message.append(")");
141:
142: return message.toString();
143: }
144: }
|