001: /* JFox, the OpenSource J2EE Application Server
002: *
003: * Copyright (C) 2002 huihoo.org
004: * Distributable under GNU LGPL license
005: * See the GNU Lesser General Public License for more details.
006: */
007:
008: package example.jmx.monitor;
009:
010: import javax.management.DynamicMBean;
011: import javax.management.MBeanRegistration;
012: import javax.management.ObjectName;
013: import javax.management.MBeanServer;
014: import javax.management.AttributeList;
015: import javax.management.ReflectionException;
016: import javax.management.MBeanException;
017: import javax.management.AttributeNotFoundException;
018: import javax.management.Attribute;
019: import javax.management.InvalidAttributeValueException;
020: import javax.management.MBeanInfo;
021: import javax.management.MBeanAttributeInfo;
022:
023: /**
024: *
025: * @author <a href="mailto:young_yy@hotmail.org">Young Yang</a>
026: */
027:
028: public class DynamicObservedObject implements DynamicMBean,
029: MBeanRegistration {
030: /*
031: * ------------------------------------------
032: * PRIVATE VARIABLES
033: * ------------------------------------------
034: */
035:
036: private MBeanServer server = null;
037:
038: /*
039: * ------------------------------------------
040: * PUBLIC METHODS
041: * ------------------------------------------
042: */
043:
044: public ObjectName preRegister(MBeanServer server, ObjectName name)
045: throws java.lang.Exception {
046:
047: if (name == null)
048: name = new ObjectName(server.getDefaultDomain() + ":name="
049: + this .getClass().getName());
050:
051: this .server = server;
052: return name;
053: }
054:
055: public void postRegister(Boolean registrationDone) {
056: }
057:
058: public void preDeregister() throws java.lang.Exception {
059: }
060:
061: public void postDeregister() {
062: }
063:
064: // DYNAMIC MBEAN INTERFACE IMPLEMENTATION
065: //---------------------------------------
066:
067: public AttributeList getAttributes(String[] attributes) {
068: return null;
069: }
070:
071: public AttributeList setAttributes(AttributeList attributes) {
072: return null;
073: }
074:
075: public Object getAttribute(String attribute)
076: throws AttributeNotFoundException, MBeanException,
077: ReflectionException {
078:
079: if (attribute.equals("NbObjects")) {
080: try {
081: return new Integer((server.queryMBeans(new ObjectName(
082: "*:*"), null)).size());
083: } catch (Exception e) {
084: return new Integer(-1);
085: }
086: }
087: return null;
088: }
089:
090: public void setAttribute(Attribute attribute)
091: throws AttributeNotFoundException,
092: InvalidAttributeValueException, MBeanException,
093: ReflectionException {
094: }
095:
096: public Object invoke(String actionName, Object params[],
097: String signature[]) throws MBeanException,
098: ReflectionException {
099: return null;
100: }
101:
102: public MBeanInfo getMBeanInfo() {
103:
104: MBeanAttributeInfo attributes[] = new MBeanAttributeInfo[1];
105: try {
106: attributes[0] = new MBeanAttributeInfo("NbObjects",
107: "java.lang.Integer",
108: "Returns the number of MBeans registered", true,
109: false, false);
110: } catch (Exception e) {
111: }
112:
113: return new MBeanInfo(getClass().getName(),
114: "The object to be observed by the CounterMonitor",
115: attributes, null, null, null);
116: }
117:
118: }
|