KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mx4j > tools > stats > ObserverStatisticsRecorder


1 /*
2  * Copyright (C) The MX4J Contributors.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8
9 package mx4j.tools.stats;
10
11 import javax.management.MBeanAttributeInfo JavaDoc;
12 import javax.management.MBeanInfo JavaDoc;
13 import javax.management.ObjectName JavaDoc;
14
15 /**
16  * @version $Revision: 1.4 $
17  */

18 public abstract class ObserverStatisticsRecorder extends AbstractStatisticsRecorder implements ObserverStatisticsRecorderMBean
19 {
20    protected ObjectName JavaDoc observedName = null;
21
22    protected String JavaDoc observedAttribute = null;
23
24    public void setObservedObject(ObjectName JavaDoc object)
25    {
26       this.observedName = object;
27    }
28
29    public ObjectName JavaDoc getObservedObject()
30    {
31       return observedName;
32    }
33
34    public String JavaDoc getObservedAttribute()
35    {
36       return observedAttribute;
37    }
38
39    public void setObservedAttribute(String JavaDoc attribute)
40    {
41       this.observedAttribute = attribute;
42    }
43
44    protected void doStart() throws Exception JavaDoc
45    {
46       if (observedName == null || observedAttribute == null)
47       {
48          getLogger().warn(new StringBuffer JavaDoc(this.toString()).append(" cannot start with objectName ").append(observedName).append(" and attribute ").append(observedAttribute).toString());
49          stop();
50          return;
51       }
52       if (!server.isRegistered(observedName))
53       {
54          getLogger().warn(new StringBuffer JavaDoc(this.toString()).append(" cannot start since objectName is not registered").toString());
55          stop();
56          return;
57       }
58
59       MBeanInfo JavaDoc info = server.getMBeanInfo(observedName);
60       MBeanAttributeInfo JavaDoc[] attributes = info.getAttributes();
61       MBeanAttributeInfo JavaDoc theAttribute = null;
62       boolean found = false;
63       for (int i = 0; i < attributes.length; i++)
64       {
65          if (attributes[i].getName().equals(observedAttribute))
66          {
67             theAttribute = attributes[i];
68             found = true;
69             break;
70          }
71       }
72       if (!found)
73       {
74          getLogger().warn(new StringBuffer JavaDoc(this.toString()).append(" cannot start with objectName ").append(observedName).append(" since attribute ").append(observedAttribute).append(" does not belong to the MBean interface").toString());
75          stop();
76          return;
77       }
78       if (!theAttribute.isReadable())
79       {
80          getLogger().warn(new StringBuffer JavaDoc(this.toString()).append(" cannot start with objectName ").append(observedName).append(" since attribute ").append(observedAttribute).append(" is not readable").toString());
81          stop();
82          return;
83       }
84       Object JavaDoc value = server.getAttribute(observedName, observedAttribute);
85       if (!(value instanceof Number JavaDoc))
86       {
87          getLogger().warn(new StringBuffer JavaDoc(this.toString()).append(" cannot start with objectName ").append(observedName).append(" since attribute ").append(observedAttribute).append(" is not a number").toString());
88          stop();
89          return;
90       }
91       startObserving();
92    }
93
94    protected abstract void startObserving() throws Exception JavaDoc;
95
96    protected abstract void stopObserving() throws Exception JavaDoc;
97
98    protected void doStop() throws Exception JavaDoc
99    {
100       stopObserving();
101    }
102
103 }
104
Popular Tags