001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)StatisticsBaseImpl.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.util.monitoring;
030:
031: import com.sun.jbi.monitoring.StatisticsBase;
032: import com.sun.jbi.util.jmx.MBeanUtils;
033:
034: import java.util.ArrayList;
035: import java.util.Hashtable;
036: import java.util.Iterator;
037: import java.util.List;
038:
039: import javax.management.ObjectName;
040:
041: /**
042: * This class provides the common utility methods used by all of the statistics
043: * gathering classes. The methods here provide the infrastucture for
044: * creating an object tree that represents the statistics for the entire JBI
045: * instance. Each instance of a statistics class has a parent and optionally
046: * one or more children.
047: *
048: * @author Sun Microsystems, Inc.
049: */
050: public class StatisticsBaseImpl implements StatisticsBase {
051: /**
052: * Flag to indicate whether statistics gathering is enabled.
053: */
054: private boolean mEnabled = true;
055:
056: /**
057: * Key for this statistics object.
058: */
059: private String mKey;
060:
061: /**
062: * Parent object in the tree.
063: */
064: private StatisticsBase mParent;
065:
066: /**
067: * Synchronized access to child objects.
068: */
069: private Hashtable mChildren;
070:
071: /**
072: * JMX Object Name for this MBean.
073: */
074: private ObjectName mMBeanName;
075:
076: /**
077: * Constructor.
078: * @param key The key string for this statistics object.
079: */
080: public StatisticsBaseImpl(String key) {
081: mKey = key;
082: }
083:
084: /**
085: * Get the key of this object.
086: * @return the string key for this instance.
087: */
088: public String getKey() {
089: return mKey;
090: }
091:
092: /**
093: * Set the parent of this object.
094: * @param parent the StatisticsBase instance that is the parent of this
095: * instance.
096: */
097: public void setParent(StatisticsBase parent) {
098: mParent = parent;
099: }
100:
101: /**
102: * Get the parent of this object.
103: * @return the StatisticsBase instance that is the parent of this instance.
104: */
105: public StatisticsBase getParent() {
106: return mParent;
107: }
108:
109: /**
110: * Add a child.
111: * @param child the StatisticsBase instance to be added to the list of
112: * children of this instance.
113: */
114: public void addChild(StatisticsBase child) {
115: if (null == mChildren) {
116: mChildren = new Hashtable();
117: }
118: if (null != child) {
119: child.setParent(this );
120: mChildren.put((Object) child.getKey(), (Object) child);
121: }
122: }
123:
124: /**
125: * Get a particular child of this object based on a key.
126: * @param key the string key of the requested child.
127: * @return the StatisticsBase instance referenced by the specified key,
128: * or null if none is found.
129: */
130: public StatisticsBase getChild(String key) {
131: StatisticsBase child = null;
132: if (null != mChildren) {
133: child = (StatisticsBase) mChildren.get(key);
134: }
135: return child;
136: }
137:
138: /**
139: * Get the list of children of this object.
140: * @return the list of instances that are children of this instance.
141: */
142: public List getChildren() {
143: if (null != mChildren) {
144: return new ArrayList(mChildren.values());
145: } else {
146: return new ArrayList();
147: }
148: }
149:
150: /**
151: * Test whether or not statistics are enabled.
152: * @return true if statistics are enabled, false if not.
153: */
154: public boolean isEnabled() {
155: return mEnabled;
156: }
157:
158: /**
159: * Disable statistics.
160: */
161: public void setDisabled() {
162: mEnabled = false;
163: if (null != mChildren) {
164: Iterator c = mChildren.values().iterator();
165: StatisticsBase child;
166: while (c.hasNext()) {
167: child = (StatisticsBase) c.next();
168: child.setDisabled();
169: }
170: }
171: }
172:
173: /**
174: * Enable statistics.
175: */
176: public void setEnabled() {
177: mEnabled = true;
178: if (null != mChildren) {
179: Iterator c = mChildren.values().iterator();
180: StatisticsBase child;
181: while (c.hasNext()) {
182: child = (StatisticsBase) c.next();
183: child.setEnabled();
184: }
185: }
186: }
187:
188: /**
189: * Create and register a StandardMBean for this instance, using the
190: * specified interface and MBean ObjectName.
191: * @param interfaceClass The interface implemented by the instance.
192: * @param mbeanName The MBean ObjectName to use for this MBean.
193: * @throws javax.jbi.JBIException If the MBean creation or registration
194: * fails.
195: */
196: public void registerMBean(Class interfaceClass, Object instance,
197: javax.management.ObjectName mbeanName)
198: throws javax.jbi.JBIException {
199: mMBeanName = mbeanName;
200: MBeanUtils.registerStandardMBean(interfaceClass, instance,
201: mbeanName, true);
202: }
203:
204: /**
205: * Unregister the MBean for this instance.
206: * @throws javax.jbi.JBIException If the MBean unregistration fails.
207: */
208: public void unregisterMBean() throws javax.jbi.JBIException {
209: MBeanUtils.unregisterMBean(mMBeanName);
210: }
211: }
|