1 package server;
2 
3 import java.rmi.Remote;
4 import java.rmi.RemoteException;
5 import java.rmi.server.UnicastRemoteObject;
6 import java.math.BigDecimal;
7 import org.jstk.example.bank.Exceptions;
8 import org.jstk.example.bank.AccountIntf;
9 import javax.security.auth.Subject;
10import java.security.PrivilegedExceptionAction;
11import java.security.PrivilegedAction;
12import java.security.PrivilegedActionException;
13
14public class RemoteAccountImpl extends UnicastRemoteObject implements common.RemoteAccount {
15    private AccountIntf acct = null;
16    private Subject sub;
17
18    public RemoteAccountImpl(AccountIntf acct, Subject sub) throws RemoteException {
19        this.acct = acct;
20        this.sub = sub;
21    }
22    public RemoteAccountImpl(AccountIntf acct) throws RemoteException {
23        this.acct = acct;
24        this.sub = null;
25    }
26
27    public void deposit(BigDecimal amt) throws Exceptions.AccountClosed, RemoteException {
28        final AccountIntf acctf = acct;
29        final BigDecimal amtf = amt;
30        try {
31            Subject.doAs(sub, new PrivilegedExceptionAction() {
32                public Object run() throws Exception {
33                    acctf.deposit(amtf);
34                    return null;
35                }
36            });
37        } catch (PrivilegedActionException pae){
38            if (pae.getException() instanceof Exceptions.AccountClosed)
39                throw (Exceptions.AccountClosed)pae.getException();
40        }
41    }
42    public void withdraw(BigDecimal amt) throws Exceptions.AccountClosed,
43            Exceptions.InsufficientAmount, RemoteException {
44        final AccountIntf acctf = acct;
45        final BigDecimal amtf = amt;
46        try {
47            Subject.doAs(sub, new PrivilegedExceptionAction() {
48                public Object run() throws Exception {
49                    acctf.withdraw(amtf);
50                    return null;
51                }
52            });
53        } catch (PrivilegedActionException pae){
54            if (pae.getException() instanceof Exceptions.AccountClosed)
55                throw (Exceptions.AccountClosed)pae.getException();
56            else if (pae.getException() instanceof Exceptions.InsufficientAmount)
57                throw (Exceptions.InsufficientAmount)pae.getException();
58        }
59    }
60    public void close() throws Exceptions.AccountClosed, RemoteException {
61        acct.close();
62    }
63    public BigDecimal getBalance() throws Exceptions.AccountClosed, RemoteException {
64        final AccountIntf acctf = acct;
65        BigDecimal balance = null;
66        try {
67            balance = (BigDecimal)Subject.doAs(sub, new PrivilegedExceptionAction() {
68                public Object run() throws Exception {
69                    return acctf.getBalance();
70                }
71            });
72        } catch (PrivilegedActionException pae){
73            if (pae.getException() instanceof Exceptions.AccountClosed)
74                throw (Exceptions.AccountClosed)pae.getException();
75        }
76        return balance;
77    }
78    public String getAcctNo() throws RemoteException {
79        final AccountIntf acctf = acct;
80        return (String)Subject.doAs(sub, new PrivilegedAction() {
81                public Object run(){
82                    return acctf.getAcctNo();
83                }
84            });
85    }
86    public String getStatement() throws RemoteException {
87        return acct.getStatement();
88    }
89}
90