KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > jms > JmsTransaction


1 /*
2  * $Id: JmsTransaction.java 3798 2006-11-04 04:07:14Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.providers.jms;
12
13 import javax.jms.Connection JavaDoc;
14 import javax.jms.JMSException JavaDoc;
15 import javax.jms.Session JavaDoc;
16
17 import org.mule.config.i18n.Message;
18 import org.mule.config.i18n.Messages;
19 import org.mule.transaction.AbstractSingleResourceTransaction;
20 import org.mule.transaction.IllegalTransactionStateException;
21 import org.mule.umo.TransactionException;
22
23 /**
24  * <code>JmsTransaction</code> is a wrapper for a JMS local transaction. This
25  * object holds the JMS session and controls when the transaction is committed or
26  * rolled back.
27  */

28 public class JmsTransaction extends AbstractSingleResourceTransaction
29 {
30
31     /*
32      * (non-Javadoc)
33      *
34      * @see org.mule.umo.UMOTransaction#bindResource(java.lang.Object,
35      * java.lang.Object)
36      */

37     public void bindResource(Object JavaDoc key, Object JavaDoc resource) throws TransactionException
38     {
39         if (!(key instanceof Connection JavaDoc) || !(resource instanceof Session JavaDoc))
40         {
41             throw new IllegalTransactionStateException(new Message(
42                 Messages.TX_CAN_ONLY_BIND_TO_X_TYPE_RESOURCES, "javax.jms.Connection/javax.jms.Session"));
43         }
44
45         Session JavaDoc session = (Session JavaDoc)resource;
46         try
47         {
48             if (!session.getTransacted())
49             {
50                 throw new IllegalTransactionStateException(new Message("jms", 4));
51             }
52         }
53         catch (JMSException JavaDoc e)
54         {
55             throw new IllegalTransactionStateException(new Message(Messages.TX_CANT_READ_STATE), e);
56         }
57
58         super.bindResource(key, resource);
59     }
60
61     /*
62      * (non-Javadoc)
63      *
64      * @see org.mule.transaction.AbstractSingleResourceTransaction#doBegin()
65      */

66     protected void doBegin() throws TransactionException
67     {
68         // do nothing
69
}
70
71     /*
72      * (non-Javadoc)
73      *
74      * @see org.mule.transaction.AbstractSingleResourceTransaction#doCommit()
75      */

76     protected void doCommit() throws TransactionException
77     {
78         try
79         {
80             ((Session JavaDoc)resource).commit();
81         }
82         catch (JMSException JavaDoc e)
83         {
84             throw new TransactionException(new Message(Messages.TX_COMMIT_FAILED), e);
85         }
86     }
87
88     /*
89      * (non-Javadoc)
90      *
91      * @see org.mule.transaction.AbstractSingleResourceTransaction#doRollback()
92      */

93     protected void doRollback() throws TransactionException
94     {
95         try
96         {
97             ((Session JavaDoc)resource).rollback();
98         }
99         catch (JMSException JavaDoc e)
100         {
101             throw new TransactionException(new Message(Messages.TX_ROLLBACK_FAILED), e);
102         }
103     }
104
105 }
106
Popular Tags