1 22 package org.jboss.services.loggingmonitor; 23 24 import javax.management.JMException ; 25 import javax.management.MBeanServer ; 26 import javax.management.MalformedObjectNameException ; 27 import javax.management.ObjectName ; 28 29 import org.apache.log4j.Logger; 30 import org.jboss.mx.util.JMXExceptionDecoder; 31 import org.jboss.mx.util.MBeanServerLocator; 32 33 40 class MonitoredMBean 41 { 42 private MBeanServer mbeanServer; 43 private ObjectName objectName; 44 private String [] attributes; 45 private Logger logger; 47 56 public MonitoredMBean(String objectName, String [] attributes, Logger logger) throws MalformedObjectNameException 57 { 58 this.objectName = new ObjectName (objectName); 59 this.attributes = attributes; 60 this.logger = logger; 61 this.mbeanServer = MBeanServerLocator.locateJBoss(); 62 } 63 64 69 public ObjectName getObjectName() 70 { 71 return objectName; 72 } 73 74 79 public String [] getAttributes() 80 { 81 return attributes; 82 } 83 84 89 public Logger getLogger() 90 { 91 return logger; 92 } 93 94 97 public void logFormat() 98 { 99 logger.info(buildFormatMessage()); 100 } 101 102 108 public void logAttributes() throws Exception 109 { 110 try 111 { 112 StringBuffer message = new StringBuffer (); 113 114 for (int j = 0; j < attributes.length; ++j) 115 { 116 Object 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 e) 129 { 130 JMXExceptionDecoder.rethrow(e); 131 } 132 } 133 134 139 private String buildFormatMessage() 140 { 141 StringBuffer message = new StringBuffer (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 |