001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.varia.stats.report;
023:
024: import org.jboss.system.ServiceMBeanSupport;
025: import org.jboss.varia.stats.TxStatistics;
026:
027: import javax.management.ObjectName;
028: import java.util.Iterator;
029:
030: /**
031: * @jmx:mbean extends="org.jboss.system.ServiceMBean"
032: *
033: * @author <a href="mailto:alex@jboss.org">Alexey Loubyansky</a>
034: * @version <tt>$Revision: 57210 $</tt>
035: */
036: public abstract class ReportGenerator extends ServiceMBeanSupport
037: implements ReportGeneratorMBean {
038: protected ObjectName statsCollector;
039:
040: protected String name;
041: protected String description;
042:
043: /**
044: * @jmx.managed-attribute
045: */
046: public void setName(String name) {
047: this .name = name;
048: }
049:
050: /**
051: * @jmx.managed-attribute
052: */
053: public String getName() {
054: return name;
055: }
056:
057: /**
058: * @jmx.managed-attribute
059: */
060: public void setDescription(String description) {
061: this .description = description;
062: }
063:
064: /**
065: * @jmx.managed-attribute
066: */
067: public String getDescription() {
068: return description;
069: }
070:
071: /**
072: * @jmx.managed-attribute
073: */
074: public void setStatsCollector(ObjectName statsCollector) {
075: this .statsCollector = statsCollector;
076: }
077:
078: /**
079: * @jmx.managed-attribute
080: */
081: public ObjectName getStatsCollector() {
082: return statsCollector;
083: }
084:
085: public void startService() throws Exception {
086: try {
087: server.invoke(statsCollector, "registerReportGenerator",
088: new Object[] { this },
089: new String[] { ReportGenerator.class.getName() });
090: } catch (Exception e) {
091: log.error("Failed to register report generator.");
092: throw e;
093: }
094: }
095:
096: public void stopService() throws Exception {
097: try {
098: server.invoke(statsCollector, "unregisterReportGenerator",
099: new Object[] { this },
100: new String[] { ReportGenerator.class.getName() });
101: } catch (Exception e) {
102: log.error("Failed to unregister report generator.");
103: throw e;
104: }
105: }
106:
107: /**
108: * @jmx.managed-operation
109: */
110: public String generate(String reportName) throws Exception {
111: StringBuffer content = new StringBuffer();
112: content.append("<a href='HtmlAdaptor?").append(
113: "action=invokeOpByName&name=").append(statsCollector)
114: .append("&methodName=reports").append(
115: "'>Back to report list</a>");
116:
117: content(reportName, content);
118:
119: return content.toString();
120: }
121:
122: // Protected
123:
124: protected abstract void content(String reportName, StringBuffer buf)
125: throws Exception;
126:
127: protected Iterator getReportsIterator() throws Exception {
128: try {
129: return (Iterator) server
130: .invoke(statsCollector, "reportsIterator",
131: new Object[] {}, new String[] {});
132: } catch (Exception e) {
133: log
134: .error("Failed to invoke getReportsIterator() operation.");
135: throw e;
136: }
137: }
138:
139: protected TxStatistics getTxStatistics() throws Exception {
140: try {
141: return (TxStatistics) server.invoke(statsCollector,
142: "txStatistics", new Object[] {}, new String[] {});
143: } catch (Exception e) {
144: log.error("Failed to invoke getTxStatistics() operation.");
145: throw e;
146: }
147: }
148: }
|