001: /*
002: * Copyright (C) The MX4J Contributors.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the MX4J License version 1.0.
006: * See the terms of the MX4J License in the documentation provided with this software.
007: */
008:
009: package mx4j.tools.stats;
010:
011: import javax.management.MBeanAttributeInfo;
012: import javax.management.MBeanInfo;
013: import javax.management.ObjectName;
014:
015: /**
016: * @version $Revision: 1.4 $
017: */
018: public abstract class ObserverStatisticsRecorder extends
019: AbstractStatisticsRecorder implements
020: ObserverStatisticsRecorderMBean {
021: protected ObjectName observedName = null;
022:
023: protected String observedAttribute = null;
024:
025: public void setObservedObject(ObjectName object) {
026: this .observedName = object;
027: }
028:
029: public ObjectName getObservedObject() {
030: return observedName;
031: }
032:
033: public String getObservedAttribute() {
034: return observedAttribute;
035: }
036:
037: public void setObservedAttribute(String attribute) {
038: this .observedAttribute = attribute;
039: }
040:
041: protected void doStart() throws Exception {
042: if (observedName == null || observedAttribute == null) {
043: getLogger().warn(
044: new StringBuffer(this .toString()).append(
045: " cannot start with objectName ").append(
046: observedName).append(" and attribute ")
047: .append(observedAttribute).toString());
048: stop();
049: return;
050: }
051: if (!server.isRegistered(observedName)) {
052: getLogger()
053: .warn(
054: new StringBuffer(this .toString())
055: .append(
056: " cannot start since objectName is not registered")
057: .toString());
058: stop();
059: return;
060: }
061:
062: MBeanInfo info = server.getMBeanInfo(observedName);
063: MBeanAttributeInfo[] attributes = info.getAttributes();
064: MBeanAttributeInfo theAttribute = null;
065: boolean found = false;
066: for (int i = 0; i < attributes.length; i++) {
067: if (attributes[i].getName().equals(observedAttribute)) {
068: theAttribute = attributes[i];
069: found = true;
070: break;
071: }
072: }
073: if (!found) {
074: getLogger()
075: .warn(
076: new StringBuffer(this .toString())
077: .append(
078: " cannot start with objectName ")
079: .append(observedName)
080: .append(" since attribute ")
081: .append(observedAttribute)
082: .append(
083: " does not belong to the MBean interface")
084: .toString());
085: stop();
086: return;
087: }
088: if (!theAttribute.isReadable()) {
089: getLogger().warn(
090: new StringBuffer(this .toString()).append(
091: " cannot start with objectName ").append(
092: observedName).append(" since attribute ")
093: .append(observedAttribute).append(
094: " is not readable").toString());
095: stop();
096: return;
097: }
098: Object value = server.getAttribute(observedName,
099: observedAttribute);
100: if (!(value instanceof Number)) {
101: getLogger().warn(
102: new StringBuffer(this .toString()).append(
103: " cannot start with objectName ").append(
104: observedName).append(" since attribute ")
105: .append(observedAttribute).append(
106: " is not a number").toString());
107: stop();
108: return;
109: }
110: startObserving();
111: }
112:
113: protected abstract void startObserving() throws Exception;
114:
115: protected abstract void stopObserving() throws Exception;
116:
117: protected void doStop() throws Exception {
118: stopObserving();
119: }
120:
121: }
|