001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.tm.usertx.client;
023:
024: import java.util.ArrayList;
025: import java.util.Collection;
026: import java.util.EventListener;
027: import java.util.Iterator;
028: import javax.naming.InitialContext;
029: import javax.naming.NamingException;
030: import javax.transaction.HeuristicMixedException;
031: import javax.transaction.HeuristicRollbackException;
032: import javax.transaction.NotSupportedException;
033: import javax.transaction.RollbackException;
034: import javax.transaction.Status;
035: import javax.transaction.SystemException;
036: import javax.transaction.Transaction;
037: import javax.transaction.TransactionManager;
038: import javax.transaction.UserTransaction;
039:
040: /**
041: * The client-side UserTransaction implementation for clients
042: * operating in the same VM as the server.
043: * This will delegate all UserTransaction calls to the
044: * <code>TransactionManager</code> of the server.
045: *
046: * @author <a href="mailto:osh@sparre.dk">Ole Husgaard</a>
047: * @version $Revision: 57209 $
048: */
049: public class ServerVMClientUserTransaction implements UserTransaction {
050: // Static --------------------------------------------------------
051:
052: /**
053: * Our singleton instance.
054: */
055: private final static ServerVMClientUserTransaction singleton = new ServerVMClientUserTransaction();
056:
057: /**
058: * The <code>TransactionManagerz</code> we delegate to.
059: */
060: private final TransactionManager tm;
061:
062: private final Collection listeners = new ArrayList();
063:
064: /**
065: * Return a reference to the singleton instance.
066: */
067: public static ServerVMClientUserTransaction getSingleton() {
068: return singleton;
069: }
070:
071: // Constructors --------------------------------------------------
072:
073: /**
074: * Create a new instance.
075: */
076: private ServerVMClientUserTransaction() {
077: // Lookup the local TM
078: TransactionManager local = null;
079: try {
080: local = (TransactionManager) new InitialContext()
081: .lookup("java:/TransactionManager");
082:
083: } catch (NamingException ex) {
084: //throw new RuntimeException("TransactionManager not found: " + ex);
085: }
086: tm = local;
087: }
088:
089: //public constructor for TESTING ONLY
090: public ServerVMClientUserTransaction(final TransactionManager tm) {
091: this .tm = tm;
092: }
093:
094: // Public --------------------------------------------------------
095:
096: //Registration for TransactionStartedListeners.
097:
098: public void registerTxStartedListener(
099: UserTransactionStartedListener txStartedListener) {
100: listeners.add(txStartedListener);
101: }
102:
103: public void unregisterTxStartedListener(
104: UserTransactionStartedListener txStartedListener) {
105: listeners.remove(txStartedListener);
106: }
107:
108: //
109: // implements interface UserTransaction
110: //
111:
112: public void begin() throws NotSupportedException, SystemException {
113: tm.begin();
114: for (Iterator i = listeners.iterator(); i.hasNext();) {
115: ((UserTransactionStartedListener) i.next())
116: .userTransactionStarted();
117: } // end of for ()
118:
119: }
120:
121: public void commit() throws RollbackException,
122: HeuristicMixedException, HeuristicRollbackException,
123: SecurityException, IllegalStateException, SystemException {
124: tm.commit();
125: }
126:
127: public void rollback() throws SecurityException,
128: IllegalStateException, SystemException {
129: tm.rollback();
130: }
131:
132: public void setRollbackOnly() throws IllegalStateException,
133: SystemException {
134: tm.setRollbackOnly();
135: }
136:
137: public int getStatus() throws SystemException {
138: return tm.getStatus();
139: }
140:
141: public void setTransactionTimeout(int seconds)
142: throws SystemException {
143: tm.setTransactionTimeout(seconds);
144: }
145:
146: public interface UserTransactionStartedListener extends
147: EventListener {
148: void userTransactionStarted() throws SystemException;
149: }
150:
151: }
|