KickJava   Java API By Example, From Geeks To Geeks.

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


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 /**
25  * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
26  *
27  * Copyright 2001-2002 by iPlanet/Sun Microsystems, Inc.,
28  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
29  * All rights reserved.
30  * $Id: JTSMonitorMBean.java,v 1.5 2006/03/14 11:11:44 sankara Exp $
31  */

32 package com.sun.enterprise.transaction.monitor;
33
34 import java.util.Map JavaDoc;
35 import java.util.Hashtable JavaDoc;
36 import java.util.ArrayList JavaDoc;
37 import java.util.List JavaDoc;
38 import java.util.HashMap JavaDoc;
39
40 import javax.management.Attribute JavaDoc;
41 import javax.management.AttributeList JavaDoc;
42 import javax.management.AttributeNotFoundException JavaDoc;
43 import javax.management.RuntimeOperationsException JavaDoc;
44 import javax.management.DynamicMBean JavaDoc;
45 import javax.management.InvalidAttributeValueException JavaDoc;
46 import javax.management.MBeanException JavaDoc;
47 import javax.management.MBeanInfo JavaDoc;
48 import javax.management.MBeanOperationInfo JavaDoc;
49 import javax.management.MBeanParameterInfo JavaDoc;
50 import javax.management.ReflectionException JavaDoc;
51
52 import javax.transaction.Transaction JavaDoc;
53 import javax.transaction.SystemException JavaDoc;
54
55 import com.sun.enterprise.admin.monitor.BaseMonitorMBean;
56 import com.sun.enterprise.admin.monitor.MonitoredObjectType;
57 import com.sun.enterprise.admin.monitor.types.Counter;
58 import com.sun.enterprise.admin.monitor.types.MonitoredAttributeType;
59 import com.sun.enterprise.admin.monitor.types.StringMonitoredAttributeType;
60
61 import com.sun.enterprise.J2EETransactionManager;
62 import com.sun.enterprise.Switch;
63 import com.sun.enterprise.transaction.TransactionAdminBean;
64 import com.sun.enterprise.resource.ResourceInstaller;
65 import java.util.logging.Logger JavaDoc;
66 import java.util.logging.Level JavaDoc;
67 import com.sun.logging.LogDomains;
68
69 import com.sun.enterprise.util.i18n.StringManager;
70
71 //jsr 77 support
72
import com.sun.enterprise.server.ApplicationServer;
73 import com.sun.enterprise.server.ServerContext;
74 import com.sun.enterprise.admin.monitor.registry.*;
75 import com.sun.enterprise.config.ConfigContext;
76 import com.sun.enterprise.config.ConfigBean;
77 import com.sun.enterprise.config.serverbeans.ServerBeansFactory;
78 import com.sun.enterprise.config.serverbeans.Config;
79 import com.sun.enterprise.config.ConfigException;
80 import com.sun.enterprise.config.serverbeans.TransactionService;
81 import com.sun.enterprise.config.serverbeans.ElementProperty;
82
83
84
85 /**
86  * MBean implementation to monitor Transaction Manager.
87  */

88 public class JTSMonitorMBean extends BaseMonitorMBean {
89
90     static final String JavaDoc NUM_TRANSACTIONS_COMPLETED = "total-tx-completed";
91     static final String JavaDoc NUM_TRANSACTIONS_ROLLEDBACK = "total-tx-rolled-back";
92     static final String JavaDoc NUM_TRANSACTIONS_INFLIGHT = "total-tx-inflight";
93     static final String JavaDoc IS_FROZEN = "isFrozen";
94     static final String JavaDoc INFLIGHT_TRANSACTIONS = "inflight-tx";
95     static final String JavaDoc ROLLBACK = "rollbackList";
96     static final String JavaDoc FREEZE = "freeze";
97     static final int COLUMN_LENGTH = 25;
98
99     public static final String JavaDoc TRANSACTION_ID = "TransactionId";
100     public static final String JavaDoc STATE = "TransactionState";
101     public static final String JavaDoc ELAPSED_TIME = "ElapsedTime";
102     public static final String JavaDoc COMPONENT_NAME = "ComponentName";
103     public static final String JavaDoc RESOURCE_NAMES = "ResourceNames";
104
105
106     // Sting Manager for Localization
107
private static StringManager sm = StringManager.getManager(JTSMonitorMBean.class);
108
109  /**
110     Logger to log transaction messages
111  */

112     static Logger JavaDoc _logger = LogDomains.getLogger(LogDomains.JTA_LOGGER);
113
114
115     /**
116     * A 2-d array initialized to attribute names and their types
117     */

118     private static Object JavaDoc[][] attrNameTypeArray = {
119         {NUM_TRANSACTIONS_COMPLETED, Counter.INTEGER},
120     {NUM_TRANSACTIONS_ROLLEDBACK, Counter.INTEGER},
121     {NUM_TRANSACTIONS_INFLIGHT, Counter.INTEGER},
122         {IS_FROZEN, StringMonitoredAttributeType.DEFAULT},
123         {INFLIGHT_TRANSACTIONS, StringMonitoredAttributeType.DEFAULT}
124        };
125
126     private static MBeanOperationInfo JavaDoc[] operationInfoArray =
127         new MBeanOperationInfo JavaDoc[2];
128
129     /**
130     * JTSAdminClient to get the monitor data from, and to invoke user actions
131     */

132     private J2EETransactionManager txnMgr;
133     private Hashtable JavaDoc txnTable = null;
134     private MonitoredObjectType type = MonitoredObjectType.TXNMGR;
135     private boolean monitorOn = false;
136     private long startTime = 0;
137
138     /**
139      * Map of attribute names and their types
140      */

141     private static Map JavaDoc attrNameTypeMap;
142
143     /**
144      * Info on this MBean
145      */

146     private static MBeanInfo JavaDoc mBeanInfo;
147
148     static {
149         attrNameTypeMap = createAttrNameTypeMap(attrNameTypeArray);
150         operationInfoArray[0] = new MBeanOperationInfo JavaDoc(ROLLBACK,
151                                     "rollback(String txnId): Marks the transaction for rollback",
152                                     null, "void", MBeanOperationInfo.ACTION);
153         operationInfoArray[1] = new MBeanOperationInfo JavaDoc(FREEZE,
154                                     "freeze(): Freezes the transactions",
155                                     null, "void", MBeanOperationInfo.ACTION);
156         mBeanInfo = createMBeanInfo(attrNameTypeMap, operationInfoArray);
157     }
158
159     /**
160      * Creates a new instance of JTSMonitorMBean
161      */

162     public JTSMonitorMBean() {
163            txnMgr = Switch.getSwitch().getTransactionManager();
164            ServerContext sCtx = ApplicationServer.getServerContext();
165            if (sCtx != null) {
166                try {
167                    ConfigContext ctx = sCtx.getConfigContext();
168                    Config cfg = ServerBeansFactory.getConfigBean(ctx);
169                    String JavaDoc lvl = cfg.getMonitoringService().getModuleMonitoringLevels().getTransactionService();
170                    MonitoringLevel l = MonitoringLevel.instance(lvl);
171                    if (l != MonitoringLevel.OFF) {
172                        startMonitoring();
173                    }
174                    MonitoringRegistry registry = sCtx.getMonitoringRegistry();
175                    JTAStatsImpl.createInstance(this);
176                    JTAStatsImpl statImpl = JTAStatsImpl.getInstance();
177                    registry.registerJTAStats(statImpl, statImpl);
178                    _logger.log(Level.FINE,"JTAStats monitoring registration completed");
179                    TransactionService txnService = ServerBeansFactory.getTransactionServiceBean(ctx);
180                    ElementProperty[] eprops = txnService.getElementProperty();
181                    for (int index = 0; index < eprops.length; index++) {
182                        if ("pending-txn-cleanup-interval".equals(eprops[index].getName())) {
183                            int interval = 60;
184                            if (eprops[index].getValue() != null)
185                                interval = Integer.parseInt(eprops[index].getValue());
186                            new RecoveryHelperThread(interval).start();
187                            if (_logger.isLoggable(Level.FINE))
188                                _logger.log(Level.FINE,"Asynchronous thread for incomplete tx is enabled with interval " + interval);
189                     }
190                 }
191
192                } catch (MonitoringRegistrationException mex) {
193                    _logger.log(Level.WARNING,"transaction.monitor.registration_failed", mex);
194                } catch (ConfigException e) {
195                    _logger.log(Level.WARNING,"transaction.monitor.registration_failed", e);
196                }
197            }
198            else {
199                _logger.log(Level.FINE,"JTSMonitorMBean: ServerContext is null: monitoring is not enabled");
200            }
201     }
202
203     public List JavaDoc<Map JavaDoc<String JavaDoc, String JavaDoc>> listActiveTransactions() {
204         ArrayList JavaDoc aList = txnMgr.getActiveTransactions();
205         if (aList.isEmpty())
206             return new ArrayList JavaDoc<Map JavaDoc<String JavaDoc, String JavaDoc>>(0);
207         txnTable = new Hashtable JavaDoc();
208         List JavaDoc<Map JavaDoc<String JavaDoc, String JavaDoc>> activeTxnList = new ArrayList JavaDoc<Map JavaDoc<String JavaDoc, String JavaDoc>>();
209         Map JavaDoc<String JavaDoc, String JavaDoc> txnListEntry = null;
210         for (int i=0; i < aList.size(); i++) {
211             TransactionAdminBean txnBean = (TransactionAdminBean)aList.get(i);
212             Transaction JavaDoc j2eeTxn = (Transaction JavaDoc) txnBean.getIdentifier();
213             String JavaDoc txnId = txnBean.getId();
214             txnTable.put(txnId, j2eeTxn);
215             txnListEntry = new HashMap JavaDoc<String JavaDoc, String JavaDoc>(5);
216             txnListEntry.put(TRANSACTION_ID, txnId);
217             txnListEntry.put(ELAPSED_TIME, String.valueOf(txnBean.getElapsedTime()));
218             txnListEntry.put(COMPONENT_NAME, txnBean.getComponentName());
219             ArrayList JavaDoc<String JavaDoc> resourceList = txnBean.getResourceNames();
220             StringBuffer JavaDoc strBuf = new StringBuffer JavaDoc(" ");
221             if (resourceList != null) {
222                 for (int k = 0; k < resourceList.size(); k++) {
223                     strBuf.append(resourceList.get(k));
224                     strBuf.append(",");
225                 }
226             }
227             txnListEntry.put(RESOURCE_NAMES, strBuf.toString());
228             txnListEntry.put(STATE, txnBean.getStatus());
229             activeTxnList.add(txnListEntry);
230         }
231         return activeTxnList;
232     }
233
234     /**
235      * Obtains the value of a specific monitored attribute.
236      * @param attribute The name of the attribute to be retrieved
237      * @return The value of the attribute retrieved.
238      * @throws AttributeNotFoundException if attribute name is not valid
239      */

240     public Object JavaDoc getAttribute(String JavaDoc attribute) throws AttributeNotFoundException JavaDoc{
241         if (attribute == null) {
242             /**
243          throw new RuntimeOperationsException(
244              new IllegalArgumentException("Attribute name cannot be null"));
245             **/

246          throw new RuntimeOperationsException JavaDoc(
247              new IllegalArgumentException JavaDoc(sm.getString("transaction.monitor.attribute_is_null")));
248         }
249         // Call the corresponding getter for a recognized attribute_name
250
if (attribute.equals(NUM_TRANSACTIONS_COMPLETED)) {
251             return new Integer JavaDoc(txnMgr.getNumberOfTransactionsCommitted());
252         }
253         if (attribute.equals(NUM_TRANSACTIONS_ROLLEDBACK)) {
254             return new Integer JavaDoc(txnMgr.getNumberOfTransactionsRolledBack());
255         }
256         if (attribute.equals(NUM_TRANSACTIONS_INFLIGHT)) {
257             return new Integer JavaDoc(txnMgr.getNumberOfActiveTransactions());
258         }
259         if (attribute.equals(IS_FROZEN)) {
260             if (txnMgr.isFrozen())
261                 return "True";
262             else
263                 return "False";
264         }
265         if (attribute.equals(INFLIGHT_TRANSACTIONS)) {
266             ArrayList JavaDoc aList = txnMgr.getActiveTransactions();
267             // if (aList.isEmpty()) return "No active transaction found.";
268
if (aList.isEmpty()) return "";
269             StringBuffer JavaDoc strBuf = new StringBuffer JavaDoc(1024);
270             txnTable = new Hashtable JavaDoc();
271         
272             //Set the headings for the tabular output
273
if (aList.size() > 0) {
274                 String JavaDoc colName = "Transaction Id";
275                 strBuf.append("\n\n");
276                 strBuf.append(colName);
277                 for (int i=colName.length(); i<COLUMN_LENGTH+15; i++){
278                     strBuf.append(" ");
279                 }
280                 colName = "Status";
281                 strBuf.append(colName);
282                 for (int i=colName.length(); i<COLUMN_LENGTH; i++){
283                     strBuf.append(" ");
284                 }
285                 colName = "ElapsedTime(ms)";
286                 strBuf.append(colName);
287                 for (int i=colName.length(); i<COLUMN_LENGTH; i++){
288                     strBuf.append(" ");
289                 }
290                 colName = "ComponentName";
291                 strBuf.append(colName);
292                 for (int i=colName.length(); i<COLUMN_LENGTH; i++){
293                     strBuf.append(" ");
294                 }
295                 strBuf.append("ResourceNames\n");
296             }
297
298             for (int i=0; i < aList.size(); i++) {
299                 TransactionAdminBean txnBean = (TransactionAdminBean)aList.get(i);
300                 Transaction JavaDoc j2eeTxn = (Transaction JavaDoc) txnBean.getIdentifier();
301                 String JavaDoc txnId = txnBean.getId();
302                 txnTable.put(txnId, j2eeTxn);
303
304                 strBuf.append("\n");
305                 strBuf.append(txnId);
306                 for (int j=txnId.length(); j<COLUMN_LENGTH+15; j++){
307                     strBuf.append(" ");
308                 }
309                 strBuf.append(txnBean.getStatus());
310                 for (int j=txnBean.getStatus().length(); j<COLUMN_LENGTH; j++){
311                     strBuf.append(" ");
312                 }
313                 strBuf.append(String.valueOf(txnBean.getElapsedTime()));
314                 for (int j=(String.valueOf(txnBean.getElapsedTime()).length()); j<COLUMN_LENGTH; j++){
315                     strBuf.append(" ");
316                 }
317
318                 strBuf.append(txnBean.getComponentName());
319                 for (int j=txnBean.getComponentName().length(); j<COLUMN_LENGTH; j++){
320                     strBuf.append(" ");
321                 }
322                 ArrayList JavaDoc<String JavaDoc> resourceList = txnBean.getResourceNames();
323                 if (resourceList != null) {
324                     for (int k = 0; k < resourceList.size(); k++) {
325                         strBuf.append(resourceList.get(k));
326                         strBuf.append(",");
327                     }
328                 }
329             }
330
331             return strBuf.toString();
332         }
333
334          // If attribute_name has not been recognized
335
// throw(new AttributeNotFoundException("Cannot find " + attribute + " attribute" ));
336
throw(new AttributeNotFoundException JavaDoc(sm.getString("transaction.monitor.attribute_not_found",attribute ) ));
337     }
338
339     /**
340      * Get the values of several attributes of the monitoring MBean.
341      * @param attributes A list of the attributes to be retrieved.
342      * @return The list of attributes retrieved.
343      */

344     public AttributeList JavaDoc getAttributes(String JavaDoc[] attributeNames) {
345     // Check attributeNames to avoid NullPointerException later on
346
if (attributeNames == null) {
347             /**
348             throw new RuntimeOperationsException(
349                 new IllegalArgumentException(
350                     "attributeNames[] cannot be null"));
351             **/

352             throw new RuntimeOperationsException JavaDoc(
353                 new IllegalArgumentException JavaDoc(
354                     sm.getString("transaction.monitor.attributes_not_null")));
355         }
356         AttributeList JavaDoc resultList = new AttributeList JavaDoc();
357         // if attributeNames is empty, return an empty result list
358
if (attributeNames.length == 0)
359              return resultList;
360              // build the result attribute list
361
for (int i=0 ; i<attributeNames.length ; i++){
362          try {
363              Object JavaDoc value = getAttribute((String JavaDoc) attributeNames[i]);
364              resultList.add(new Attribute JavaDoc(attributeNames[i],value));
365          } catch (Exception JavaDoc e) {
366              // print debug info but continue processing list
367
_logger.log(Level.WARNING,"transaction.monitor.error_while_getting_monitor_attr",e);
368          }
369         }
370         return(resultList);
371     }
372
373     public Object JavaDoc invoke(String JavaDoc operationName, Object JavaDoc[] params, String JavaDoc[] signature) throws MBeanException JavaDoc, ReflectionException JavaDoc {
374         if (operationName == null || operationName.equals("")) {
375             /**
376             throw new RuntimeOperationsException(
377                 new IllegalArgumentException("operationName cannot be null"));
378             **/

379             throw new RuntimeOperationsException JavaDoc(
380                 new IllegalArgumentException JavaDoc(sm.getString("transaction.monitor.operation_name_is_null")));
381         }
382
383         if (params == null)
384             return null;
385
386         AttributeList JavaDoc resultList = new AttributeList JavaDoc();
387         if (operationName.equals(ROLLBACK)) {
388             for (int i=0; i<params.length; i++) {
389                 String JavaDoc txnId = (String JavaDoc) params[i];
390                 Object JavaDoc value = setRollback(txnId);
391                 resultList.add(new Attribute JavaDoc(txnId, value));
392             }
393         } else if (operationName.equals(FREEZE)) {
394             if (params[0].equals("true")) {
395         txnMgr.freeze();
396             resultList.add(new Attribute JavaDoc("freeze", "Successful"));
397             } else {
398         txnMgr.unfreeze();
399             resultList.add(new Attribute JavaDoc("unfreeze", "Successful"));
400             }
401         } else {
402             throw new UnsupportedOperationException JavaDoc(UNSUPPORTED_ERRMSG);
403         }
404
405         return resultList;
406     }
407
408     public void freeze() {
409         txnMgr.freeze();
410     }
411     public void unfreeze() {
412     txnMgr.unfreeze();
413     }
414     
415     public String JavaDoc[] rollback(String JavaDoc[] txnIds) {
416         if (txnIds == null || txnIds.length == 0)
417             return new String JavaDoc[0];
418         String JavaDoc result[] = new String JavaDoc[txnIds.length];
419         for (int i = 0; i < txnIds.length; i++) {
420             result[i] = (String JavaDoc) setRollback(txnIds[i]);
421         }
422         return result;
423     }
424     
425     public Object JavaDoc setRollback(String JavaDoc txnId) throws IllegalStateException JavaDoc {
426         // Lookup the transaction Array for the txnid
427
String JavaDoc result = "";
428         if (txnTable == null || txnTable.get(txnId) == null) {
429             result = sm.getString("transaction.monitor.rollback_invalid_id");
430             throw new IllegalStateException JavaDoc(result);
431         }
432         else {
433             // Call the TransactionManager to rollback
434
try {
435                 txnMgr.forceRollback((Transaction JavaDoc)txnTable.get(txnId));
436                 result = sm.getString("transaction.monitor.rollback_sucessful");
437             } catch (IllegalStateException JavaDoc e) {
438                 // current thread is not associated with the transaction
439
result = sm.getString("transaction.monitor.rollback_unsuccessful_not_associated");
440                 IllegalStateException JavaDoc ex = new IllegalStateException JavaDoc(result);
441                 ex.initCause(e);
442                 throw ex;
443                 
444             } catch (SecurityException JavaDoc e) {
445                 // Thread is not allowed to rollback the transaction
446
result = sm.getString("transaction.monitor.rollback_unsuccessful_security_exception");
447                 SecurityException JavaDoc ex = new SecurityException JavaDoc(result);
448                 ex.initCause(e);
449                 throw ex;
450             } catch (SystemException JavaDoc e) {
451                 // Transaction Manager encountered unexpected error condition
452
result = sm.getString("transaction.monitor.rollback_unsuccessful_unexpected_exception");
453                 IllegalStateException JavaDoc ex = new IllegalStateException JavaDoc(result);
454                 ex.initCause(e);
455                 throw ex;
456             }
457         }
458
459     return result;
460     }
461
462
463     /**
464      * Start monitoring on this component. This will be called when monitoring
465      * is enabled on this component (or the group containing this component)
466      * through user interface.
467      * @see stopMonitoring
468      */

469     public void startMonitoring() {
470     txnMgr.setMonitoringEnabled(true);
471         monitorOn = true;
472         startTime = System.currentTimeMillis();
473     }
474
475     /**
476      * Stop monitoring on this component. Called when monitoring is disabled on
477      * user interface.
478      */

479     public void stopMonitoring() {
480     txnMgr.setMonitoringEnabled(false);
481         monitorOn = false;
482         startTime = 0;
483     }
484
485
486    public long getStartTime() {
487        return startTime;
488    }
489
490     /**
491      * Provides the exposed attributes and actions of the monitoring MBean using
492      * an MBeanInfo object.
493      * @return An instance of MBeanInfo with all attributes and actions exposed
494      * by this monitoring MBean.
495      */

496     public MBeanInfo JavaDoc getMBeanInfo() {
497         return mBeanInfo;
498     }
499
500     /**
501      * Get a map of monitored attribute names and their types. The keys in
502      * the map are names of the attribute and the values are their types. The
503      * type value are instances of class
504      * com.iplanet.ias.monitor.type.MonitoredAttributeType (or its sub-classes)
505      *
506      * @return map of names and types of all monitored attributes
507      */

508     public Map JavaDoc getMonitoringMetaData() {
509         return attrNameTypeMap;
510     }
511
512     /**
513      * Get type of the specified monitored attribute.
514      */

515     public MonitoredAttributeType getAttributeType(String JavaDoc attrName) {
516         MonitoredAttributeType type = null;
517         if (attrNameTypeMap != null && attrName != null) {
518             type = (MonitoredAttributeType)attrNameTypeMap.get(attrName);
519         }
520         return type;
521     }
522
523     public MonitoredObjectType getMonitoredObjectType() {
524         return type;
525     }
526
527     public static void recover(boolean delegated, String JavaDoc txLogDir) throws Exception JavaDoc {
528         ResourceInstaller resInstaller = Switch.getSwitch().getResourceInstaller();
529         boolean result = true;
530         if (resInstaller == null) {
531             throw new IllegalStateException JavaDoc();
532         }
533         if (!delegated) { // own recovery
534
result = resInstaller.recoverIncompleteTx(false, null);
535         }
536         else { // delegated recovery
537
result = resInstaller.recoverIncompleteTx(true, txLogDir);
538         }
539         if (!result)
540             throw new IllegalStateException JavaDoc();
541     }
542
543     class RecoveryHelperThread extends Thread JavaDoc {
544         private int interval;
545         RecoveryHelperThread(int interval) {
546             setName("Recovery Helper Thread");
547             setDaemon(true);
548             this.interval = interval;
549         }
550         public void run() {
551             try {
552                 while(true) {
553                     Thread.sleep(interval*1000);
554                     Switch.getSwitch().getResourceInstaller().recoverIncompleteTx(false, null);
555                 }
556             } catch (Exception JavaDoc ex) {
557                 if (JTSMonitorMBean._logger.isLoggable(Level.FINE))
558                     JTSMonitorMBean._logger.log(Level.FINE, " Exception occurred in recoverInCompleteTx ");
559             }
560         }
561     }
562 }
563
564
Popular Tags