KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > transaction > monitor > JTAStatsImpl


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.transaction.monitor;
25
26 import com.sun.enterprise.admin.monitor.registry.*;
27 import com.sun.enterprise.admin.monitor.stats.*;
28 import com.sun.enterprise.admin.monitor.stats.JTAStats;
29 import javax.management.j2ee.statistics.*;
30
31 import java.util.logging.Logger JavaDoc;
32 import java.util.logging.Level JavaDoc;
33 import java.util.Map JavaDoc;
34 import java.util.List JavaDoc;
35 import com.sun.logging.LogDomains;
36
37
38 public class JTAStatsImpl implements JTAStats, MonitoringLevelListener {
39
40     private JTSMonitorMBean mBean = null;
41     private GenericStatsImpl gStatsDelegate = null;
42     
43     static Logger JavaDoc _logger = LogDomains.getLogger(LogDomains.JTA_LOGGER);
44     private static JTAStatsImpl instance = null;
45
46     // Will be instantiated by JTSMonitorMBean
47
private JTAStatsImpl(JTSMonitorMBean mBean) {
48         this.mBean = mBean;
49         try {
50             gStatsDelegate = new GenericStatsImpl("com.sun.enterprise.admin.monitor.stats.JTAStats",this);
51         } catch (ClassNotFoundException JavaDoc clex) {
52             _logger.log(Level.WARNING,"transaction.monitor.error_creating_jtastatsimpl",clex);
53             // log and forget. Should not happen
54
}
55     }
56
57     public static void createInstance(JTSMonitorMBean mBean) {
58         if (instance == null)
59             instance = new JTAStatsImpl(mBean);
60     }
61     public static JTAStatsImpl getInstance() {
62         if (instance == null)
63             throw new UnsupportedOperationException JavaDoc();
64         return instance;
65     }
66
67     // Remove once it is deprecated
68
public void setLevel(MonitoringLevel level) {
69         if (level == MonitoringLevel.OFF) {
70             mBean.stopMonitoring();
71         }
72         else if (level == MonitoringLevel.LOW || level == MonitoringLevel.HIGH) {
73             mBean.startMonitoring();
74         }
75     }
76
77     // MonitoringLevelListener method
78
public void changeLevel(MonitoringLevel from, MonitoringLevel to,
79                             javax.management.j2ee.statistics.Stats JavaDoc handback) {
80         if (from != to) {
81             _logger.log(Level.FINE,"JTAStats Monitoring level changed from " + from + " to " + to);
82             if (to == MonitoringLevel.OFF) {
83                 mBean.stopMonitoring();
84             }
85             else if (to == MonitoringLevel.LOW || to == MonitoringLevel.HIGH) {
86                 mBean.startMonitoring();
87             }
88         }
89     }
90
91     public StringStatistic getActiveIds() {
92         String JavaDoc activeStr = null;
93         try {
94             activeStr = (String JavaDoc)mBean.getAttribute(JTSMonitorMBean.INFLIGHT_TRANSACTIONS);
95         }catch (javax.management.AttributeNotFoundException JavaDoc jmxex) {
96            _logger.log(Level.WARNING,"transaction.monitor.attribute_not_found",jmxex);
97         }
98         return new StringStatisticImpl(activeStr,
99                                       "ActiveIds",
100                                        //"getActiveIds",
101
"List",
102                                        "List of inflight transactions",
103                                        mBean.getStartTime(),
104                                        System.currentTimeMillis());
105     }
106
107     public StringStatistic getState() {
108         String JavaDoc str = null;
109         try {
110             str = (String JavaDoc)mBean.getAttribute(JTSMonitorMBean.IS_FROZEN);
111         }catch (javax.management.AttributeNotFoundException JavaDoc jmxex) {
112            _logger.log(Level.WARNING,"transaction.monitor.attribute_not_found",jmxex);
113           // log and forget. Should not happen
114
}
115         
116         return new StringStatisticImpl(str,
117                                        "State",
118                                        //"getState",
119
"String",
120                                        "Transaction system state: frozen?",
121                                        mBean.getStartTime(),
122                                        System.currentTimeMillis());
123     }
124
125     public CountStatistic getActiveCount() {
126         Integer JavaDoc count = null;
127         try {
128             count = (Integer JavaDoc)mBean.getAttribute(JTSMonitorMBean.NUM_TRANSACTIONS_INFLIGHT);
129         }catch (javax.management.AttributeNotFoundException JavaDoc jmxex) {
130            _logger.log(Level.WARNING,"transaction.monitor.attribute_not_found",jmxex);
131           // log and forget. Should not happen
132
}
133         return new CountStatisticImpl(count.longValue(),
134                                       "ActiveCount",
135                                       // "getActiveCount",
136
CountStatisticImpl.DEFAULT_UNIT,
137                                       "number of active transactions",
138                                        System.currentTimeMillis(),
139                                        mBean.getStartTime());
140     }
141
142     public CountStatistic getCommittedCount() {
143         Integer JavaDoc count = null;
144         try {
145             count = (Integer JavaDoc)mBean.getAttribute(JTSMonitorMBean.NUM_TRANSACTIONS_COMPLETED);
146         }catch (javax.management.AttributeNotFoundException JavaDoc jmxex) {
147            _logger.log(Level.WARNING,"transaction.monitor.attribute_not_found",jmxex);
148           // log and forget. Should not happen
149
}
150         return new CountStatisticImpl(count.longValue(),
151                                       "CommittedCount",
152                                       //"getCommittedCount",
153
CountStatisticImpl.DEFAULT_UNIT,
154                                       "number of committed transactions",
155                                        System.currentTimeMillis(),
156                                        mBean.getStartTime());
157     }
158
159     public CountStatistic getRolledbackCount() {
160         Integer JavaDoc count = null;
161         try {
162             count = (Integer JavaDoc)mBean.getAttribute(JTSMonitorMBean.NUM_TRANSACTIONS_ROLLEDBACK);
163         }catch (javax.management.AttributeNotFoundException JavaDoc jmxex) {
164            _logger.log(Level.WARNING,"transaction.monitor.attribute_not_found",jmxex);
165           // log and forget. Should not happen
166
}
167         return new CountStatisticImpl(count.longValue(),
168                                       "RolledbackCount",
169                                       //"getRolledbackCount",
170
CountStatisticImpl.DEFAULT_UNIT,
171                                       "number of rolled-back transactions",
172                                        System.currentTimeMillis(),
173                                        mBean.getStartTime());
174     }
175
176     public void freeze() {
177         mBean.freeze();
178     }
179
180     public void unfreeze() {
181         mBean.unfreeze();
182     }
183
184     /**
185      * method for rolling back a single transaction
186      * @param txnId String representing the Id of the transaction to be
187      * roled back
188      */

189     public String JavaDoc rollback(String JavaDoc txnId) {
190         String JavaDoc result = (String JavaDoc) mBean.setRollback(txnId);
191         if (_logger.isLoggable(Level.FINE))
192             _logger.log(Level.FINE, result);
193         return result;
194     }
195         
196     
197     /**
198      public String[] rollback(String[] txnIds) {
199         return mBean.rollback(txnIds);
200     }
201      */

202
203
204     public Statistic getStatistic(String JavaDoc statisticName) {
205         return gStatsDelegate.getStatistic(statisticName);
206     }
207
208     public String JavaDoc[] getStatisticNames() {
209         return gStatsDelegate.getStatisticNames();
210     }
211
212     public Statistic[] getStatistics() {
213         return gStatsDelegate.getStatistics();
214     }
215
216     public void changeLevel(MonitoringLevel from, MonitoringLevel to, MonitoredObjectType type) {
217         if (from != to) {
218             _logger.log(Level.FINE,"JTAStats Monitoring level changed from " + from + " to " + to);
219             if (to == MonitoringLevel.OFF) {
220                 mBean.stopMonitoring();
221             }
222             else if (to == MonitoringLevel.LOW || to == MonitoringLevel.HIGH) {
223                 mBean.startMonitoring();
224             }
225         }
226     }
227
228     public List JavaDoc<Map JavaDoc<String JavaDoc, String JavaDoc>> listActiveTransactions() {
229         return mBean.listActiveTransactions();
230     }
231 }
232
Popular Tags