1 /*
2 * JBoss, the OpenSource J2EE webOS
3 *
4 * Distributable under LGPL license.
5 * See terms of license at gnu.org.
6 */
7
8 package org.jboss.tm.usertx.client;
9
10
11 import java.util.ArrayList;
12 import java.util.Collection;
13 import java.util.EventListener;
14 import java.util.Iterator;
15 import javax.naming.InitialContext;
16 import javax.naming.NamingException;
17 import javax.transaction.HeuristicMixedException;
18 import javax.transaction.HeuristicRollbackException;
19 import javax.transaction.NotSupportedException;
20 import javax.transaction.RollbackException;
21 import javax.transaction.Status;
22 import javax.transaction.SystemException;
23 import javax.transaction.Transaction;
24 import javax.transaction.TransactionManager;
25 import javax.transaction.UserTransaction;
26
27
28 /**
29 * The client-side UserTransaction implementation for clients
30 * operating in the same VM as the server.
31 * This will delegate all UserTransaction calls to the
32 * <code>TransactionManager</code> of the server.
33 *
34 * @author <a href="mailto:osh@sparre.dk">Ole Husgaard</a>
35 * @version $Revision: 1.2 $
36 */
37 public class ServerVMClientUserTransaction
38 implements UserTransaction
39 {
40 // Static --------------------------------------------------------
41
42 /**
43 * Our singleton instance.
44 */
45 private final static ServerVMClientUserTransaction singleton = new ServerVMClientUserTransaction();
46
47
48 /**
49 * The <code>TransactionManagerz</code> we delegate to.
50 */
51 private final TransactionManager tm;
52
53
54 private final Collection listeners = new ArrayList();
55
56 /**
57 * Return a reference to the singleton instance.
58 */
59 public static ServerVMClientUserTransaction getSingleton()
60 {
61 return singleton;
62 }
63
64
65 // Constructors --------------------------------------------------
66
67 /**
68 * Create a new instance.
69 */
70 private ServerVMClientUserTransaction()
71 {
72 // Lookup the local TM
73 TransactionManager local = null;
74 try {
75 local = (TransactionManager)new InitialContext().lookup("java:/TransactionManager");
76
77 } catch (NamingException ex)
78 {
79 //throw new RuntimeException("TransactionManager not found: " + ex);
80 }
81 tm = local;
82 }
83 //public constructor for TESTING ONLY
84 public ServerVMClientUserTransaction(final TransactionManager tm)
85 {
86 this.tm = tm;
87 }
88
89 // Public --------------------------------------------------------
90
91 //Registration for TransactionStartedListeners.
92
93 public void registerTxStartedListener(UserTransactionStartedListener txStartedListener)
94 {
95 listeners.add(txStartedListener);
96 }
97
98 public void unregisterTxStartedListener(UserTransactionStartedListener txStartedListener)
99 {
100 listeners.remove(txStartedListener);
101 }
102
103 //
104 // implements interface UserTransaction
105 //
106
107 public void begin()
108 throws NotSupportedException, SystemException
109 {
110 tm.begin();
111 for (Iterator i = listeners.iterator(); i.hasNext(); )
112 {
113 ((UserTransactionStartedListener)i.next()).userTransactionStarted();
114 } // end of for ()
115
116 }
117
118 public void commit()
119 throws RollbackException,
120 HeuristicMixedException,
121 HeuristicRollbackException,
122 SecurityException,
123 IllegalStateException,
124 SystemException
125 {
126 tm.commit();
127 }
128
129 public void rollback()
130 throws SecurityException,
131 IllegalStateException,
132 SystemException
133 {
134 tm.rollback();
135 }
136
137 public void setRollbackOnly()
138 throws IllegalStateException,
139 SystemException
140 {
141 tm.setRollbackOnly();
142 }
143
144 public int getStatus()
145 throws SystemException
146 {
147 return tm.getStatus();
148 }
149
150 public void setTransactionTimeout(int seconds)
151 throws SystemException
152 {
153 tm.setTransactionTimeout(seconds);
154 }
155
156 public interface UserTransactionStartedListener extends EventListener
157 {
158 void userTransactionStarted() throws SystemException;
159 }
160
161
162 }