KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > tm > usertx > client > ServerVMClientUserTransaction


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.tm.usertx.client;
23
24
25 import java.util.ArrayList JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.EventListener JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import javax.naming.InitialContext JavaDoc;
30 import javax.naming.NamingException JavaDoc;
31 import javax.transaction.HeuristicMixedException JavaDoc;
32 import javax.transaction.HeuristicRollbackException JavaDoc;
33 import javax.transaction.NotSupportedException JavaDoc;
34 import javax.transaction.RollbackException JavaDoc;
35 import javax.transaction.SystemException JavaDoc;
36 import javax.transaction.TransactionManager JavaDoc;
37 import javax.transaction.UserTransaction JavaDoc;
38
39
40 /**
41  * The client-side UserTransaction implementation for clients
42  * operating in the same VM as the server.
43  * This will delegate all UserTransaction calls to the
44  * <code>TransactionManager</code> of the server.
45  *
46  * @author <a HREF="mailto:osh@sparre.dk">Ole Husgaard</a>
47  * @version $Revision: 37459 $
48  */

49 public class ServerVMClientUserTransaction
50    implements UserTransaction JavaDoc
51 {
52    // Static --------------------------------------------------------
53

54    /**
55     * Our singleton instance.
56     */

57    private final static ServerVMClientUserTransaction singleton = new ServerVMClientUserTransaction();
58
59
60    /**
61     * The <code>TransactionManagerz</code> we delegate to.
62     */

63    private final TransactionManager JavaDoc tm;
64
65
66    private final Collection JavaDoc listeners = new ArrayList JavaDoc();
67
68    /**
69     * Return a reference to the singleton instance.
70     */

71    public static ServerVMClientUserTransaction getSingleton()
72    {
73       return singleton;
74    }
75
76
77    // Constructors --------------------------------------------------
78

79    /**
80     * Create a new instance.
81     */

82    private ServerVMClientUserTransaction()
83    {
84       // Lookup the local TM
85
TransactionManager JavaDoc local = null;
86       try {
87          local = (TransactionManager JavaDoc)new InitialContext JavaDoc().lookup("java:/TransactionManager");
88
89       } catch (NamingException JavaDoc ex)
90       {
91          //throw new RuntimeException("TransactionManager not found: " + ex);
92
}
93       tm = local;
94    }
95    //public constructor for TESTING ONLY
96
public ServerVMClientUserTransaction(final TransactionManager JavaDoc tm)
97    {
98       this.tm = tm;
99    }
100
101    // Public --------------------------------------------------------
102

103    //Registration for TransactionStartedListeners.
104

105    public void registerTxStartedListener(UserTransactionStartedListener txStartedListener)
106    {
107       listeners.add(txStartedListener);
108    }
109
110    public void unregisterTxStartedListener(UserTransactionStartedListener txStartedListener)
111    {
112       listeners.remove(txStartedListener);
113    }
114
115    //
116
// implements interface UserTransaction
117
//
118

119    public void begin()
120       throws NotSupportedException JavaDoc, SystemException JavaDoc
121    {
122       tm.begin();
123       for (Iterator JavaDoc i = listeners.iterator(); i.hasNext(); )
124       {
125          ((UserTransactionStartedListener)i.next()).userTransactionStarted();
126       } // end of for ()
127

128    }
129
130    public void commit()
131       throws RollbackException JavaDoc,
132              HeuristicMixedException JavaDoc,
133              HeuristicRollbackException JavaDoc,
134              SecurityException JavaDoc,
135              IllegalStateException JavaDoc,
136              SystemException JavaDoc
137    {
138       tm.commit();
139    }
140
141    public void rollback()
142       throws SecurityException JavaDoc,
143              IllegalStateException JavaDoc,
144              SystemException JavaDoc
145    {
146       tm.rollback();
147    }
148
149    public void setRollbackOnly()
150       throws IllegalStateException JavaDoc,
151              SystemException JavaDoc
152    {
153       tm.setRollbackOnly();
154    }
155
156    public int getStatus()
157       throws SystemException JavaDoc
158    {
159       return tm.getStatus();
160    }
161
162    public void setTransactionTimeout(int seconds)
163       throws SystemException JavaDoc
164    {
165       tm.setTransactionTimeout(seconds);
166    }
167
168    public interface UserTransactionStartedListener extends EventListener JavaDoc
169    {
170       void userTransactionStarted() throws SystemException JavaDoc;
171    }
172                                                        
173
174 }
175
Popular Tags