ws-kandula-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dasar...@apache.org
Subject svn commit: r406100 - in /webservices/kandula/branches/Kandula_1/src/samples/banking/src: ./ bankone/ bankone/BankOneDBMS.java bankone/BankOneSoapBindingImpl.java
Date Sat, 13 May 2006 13:51:17 GMT
Author: dasarath
Date: Sat May 13 06:51:16 2006
New Revision: 406100

URL: http://svn.apache.org/viewcvs?rev=406100&view=rev
Log: (empty)

Added:
    webservices/kandula/branches/Kandula_1/src/samples/banking/src/
    webservices/kandula/branches/Kandula_1/src/samples/banking/src/bankone/
    webservices/kandula/branches/Kandula_1/src/samples/banking/src/bankone/BankOneDBMS.java
    webservices/kandula/branches/Kandula_1/src/samples/banking/src/bankone/BankOneSoapBindingImpl.java

Added: webservices/kandula/branches/Kandula_1/src/samples/banking/src/bankone/BankOneDBMS.java
URL: http://svn.apache.org/viewcvs/webservices/kandula/branches/Kandula_1/src/samples/banking/src/bankone/BankOneDBMS.java?rev=406100&view=auto
==============================================================================
--- webservices/kandula/branches/Kandula_1/src/samples/banking/src/bankone/BankOneDBMS.java
(added)
+++ webservices/kandula/branches/Kandula_1/src/samples/banking/src/bankone/BankOneDBMS.java
Sat May 13 06:51:16 2006
@@ -0,0 +1,146 @@
+/*
+ * Created on May 10, 2006
+ *
+ */
+package bankone;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.transaction.xa.XAException;
+import javax.transaction.xa.XAResource;
+import javax.transaction.xa.Xid;
+
+import org.apache.geronimo.transaction.manager.NamedXAResource;
+
+/**
+ * @author Dasarath Weeratunge
+ *  
+ */
+public class BankOneDBMS implements NamedXAResource {
+	private static BankOneDBMS instance;
+
+	private boolean locked;
+
+	private Xid tx;
+
+	private double account[];
+
+	private double cache[];
+
+	private Map store;
+
+	protected BankOneDBMS() {
+		this.tx = null;
+		this.cache = null;
+		this.account = new double[2];
+		this.account[0] = 0.0;
+		this.account[1] = 0.0;
+		locked = false;
+		store = Collections.synchronizedMap(new HashMap());
+	}
+
+	public static BankOneDBMS getInstance() {
+		if (instance == null)
+			instance = new BankOneDBMS();
+		return instance;
+	}
+
+	public double getBalance(int accountNo) {
+		if (cache == null)
+			return this.account[accountNo];
+		return cache[accountNo];
+	}
+
+	public double debit(int accountNo, double amount) {
+		if (cache == null)
+			throw new IllegalStateException("transaction required.");
+		return cache[accountNo] -= amount;
+	}
+
+	public double credit(int accountNo, double amount) {
+		if (cache == null)
+			throw new IllegalStateException("transaction required.");
+		return cache[accountNo] += amount;
+	}
+
+	public void commit(Xid arg0, boolean arg1) throws XAException {
+		System.out.println("[BankOneDBMS] commit");
+		if (!locked)
+			throw new XAException();
+		double[] _cache = (double[]) store.get(arg0);
+		this.account[0] = _cache[0];
+		this.account[1] = _cache[1];
+		locked = false;
+		store.remove(arg0);
+	}
+
+	public void end(Xid arg0, int arg1) throws XAException {
+		System.out.println("[BankOneDBMS] end");
+		cache = null;
+	}
+
+	public void forget(Xid arg0) throws XAException {
+		System.out.println("[BankOneDBMS] forget");
+		store.remove(arg0);
+	}
+
+	public int getTransactionTimeout() throws XAException {
+		return 0;
+	}
+
+	public boolean isSameRM(XAResource arg0) throws XAException {
+		System.out.println("[BankOneDBMS] isSameRM");
+		return arg0 == this;
+	}
+
+	public int prepare(Xid arg0) throws XAException {
+		System.out.println("[BankOneDBMS] prepare");
+		locked = true;
+		if (tx.equals(arg0))
+			return XAResource.XA_OK;
+		locked = false;
+		throw new XAException();
+	}
+
+	public Xid[] recover(int arg0) throws XAException {
+		return null;
+	}
+
+	public void rollback(Xid arg0) throws XAException {
+		System.out.println("[BankOneDBMS] rollback");
+		if (tx.equals(arg0))
+			locked = false;
+		store.remove(arg0);
+	}
+
+	public boolean setTransactionTimeout(int arg0) throws XAException {
+		return false;
+	}
+
+	public void start(Xid arg0, int arg1) throws XAException {
+		System.out.println("[BankOneDBMS] start");
+		if (locked)
+			throw new XAException();
+		this.tx = arg0;
+		cache = (double[]) store.get(arg0);
+		if (cache == null) {
+			cache = new double[2];
+			this.cache[0] = this.account[0];
+			this.cache[1] = this.account[1];
+			store.put(arg0, cache);
+		}
+	}
+
+	/*
+	 * (non-Javadoc)
+	 * 
+	 * @see org.apache.geronimo.transaction.manager.NamedXAResource#getName()
+	 */
+	public String getName() {
+		// TODO Auto-generated method stub
+		return "BankOneDBMS";
+	}
+
+}
\ No newline at end of file

Added: webservices/kandula/branches/Kandula_1/src/samples/banking/src/bankone/BankOneSoapBindingImpl.java
URL: http://svn.apache.org/viewcvs/webservices/kandula/branches/Kandula_1/src/samples/banking/src/bankone/BankOneSoapBindingImpl.java?rev=406100&view=auto
==============================================================================
--- webservices/kandula/branches/Kandula_1/src/samples/banking/src/bankone/BankOneSoapBindingImpl.java
(added)
+++ webservices/kandula/branches/Kandula_1/src/samples/banking/src/bankone/BankOneSoapBindingImpl.java
Sat May 13 06:51:16 2006
@@ -0,0 +1,71 @@
+/**
+ * BankOneSoapBindingImpl.java
+ */
+
+package bankone;
+
+import java.rmi.RemoteException;
+
+import javax.transaction.RollbackException;
+import javax.transaction.Status;
+import javax.transaction.SystemException;
+import javax.transaction.TransactionManager;
+import javax.transaction.xa.XAResource;
+
+import org.apache.kandula.geronimo.Bridge;
+
+public class BankOneSoapBindingImpl implements BankOne {
+
+	TransactionManager tm = Bridge.getInstance().getTM();
+
+	public double getBalance(int accountNo) throws RemoteException {
+		System.out.println("[BankOne] getBalance(" + accountNo + ")");
+		try {
+			if (tm.getStatus() == Status.STATUS_NO_TRANSACTION)
+				return BankOneDBMS.getInstance().getBalance(accountNo);
+			BankOneDBMS conn = getConnection();
+			double balance = conn.getBalance(accountNo);
+			closeConnection(conn);
+			return balance;
+		} catch (Exception e) {
+			throw new RemoteException(e.getMessage());
+		}
+	}
+
+	public double debit(int accountNo, double amount) throws RemoteException {
+		System.out.println("[BankOne] debit(" + accountNo + ", " + amount + ")");
+		try {
+			BankOneDBMS conn = getConnection();
+			double balance = conn.debit(accountNo, amount);
+			closeConnection(conn);
+			return balance;
+		} catch (Exception e) {
+			throw new RemoteException(e.getMessage());
+		}
+	}
+
+	public double credit(int accountNo, double amount) throws RemoteException {
+		System.out.println("[BankOne] credit(" + accountNo + ", " + amount
+				+ ")");
+		try {
+			BankOneDBMS conn = getConnection();
+			double balance = conn.credit(accountNo, amount);
+			closeConnection(conn);
+			return balance;
+		} catch (Exception e) {
+			throw new RemoteException(e.getMessage());
+		}
+	}
+
+	private BankOneDBMS getConnection() throws IllegalStateException,
+			RollbackException, SystemException {
+		BankOneDBMS conn = BankOneDBMS.getInstance();
+		tm.getTransaction().enlistResource(conn);
+		return conn;
+	}
+
+	private void closeConnection(BankOneDBMS conn)
+			throws IllegalStateException, SystemException {
+		tm.getTransaction().delistResource(conn, XAResource.TMSUSPEND);
+	}
+}
\ No newline at end of file



---------------------------------------------------------------------
To unsubscribe, e-mail: kandula-dev-unsubscribe@ws.apache.org
For additional commands, e-mail: kandula-dev-help@ws.apache.org


Mime
View raw message