1 5 package persistentaccount; 6 7 import java.sql.Connection ; 8 import java.sql.DriverManager ; 9 import java.sql.PreparedStatement ; 10 import java.sql.ResultSet ; 11 import java.sql.SQLException ; 12 import java.util.Collections ; 13 import java.util.Map ; 14 import java.util.WeakHashMap ; 15 16 import org.apache.commons.logging.Log; 17 import org.apache.commons.logging.LogFactory; 18 19 import ve.luz.ica.jackass.component.ApplicationContext; 20 import ve.luz.ica.jackass.component.StatelessContext; 21 import ve.luz.ica.jackass.component.StatelessHook; 22 23 import account.AccountData; 24 import account.AccountManagerPOA; 25 import account.OperationFailed; 26 27 31 public class AccountManagerImpl extends AccountManagerPOA implements StatelessHook 32 { 33 private static final Log LOG = LogFactory.getLog(AccountManagerImpl.class); 34 35 private static final String BALANCE_FIELD = "balance"; 36 private static final String NAME_FIELD = "name"; 37 private static final String ID_FIELD = "id"; 38 39 private static final String ACCOUNT_NOT_FOUND = "Account not found"; 40 private static Map accounts = Collections.synchronizedMap(new WeakHashMap ()); 41 42 private StatelessContext compContext = null; 43 private ApplicationContext appContext = null; 44 private Connection connection = null; 45 46 private AccountData getAccount(int id) throws Exception  47 { 48 AccountData account = (AccountData) accounts.get(new Integer (id)); 49 if (account == null) 50 { 51 try { 52 account = new AccountData(); 53 String s = "select id, name, balance from account where id = ?"; 54 PreparedStatement ps = connection.prepareStatement(s); 55 ps.setInt(1, id); 56 ResultSet result = ps.executeQuery(); 57 58 if (result.next()) 59 { 60 account.id = result.getInt(ID_FIELD); 61 account.name = result.getString(NAME_FIELD); 62 account.balance = result.getDouble(BALANCE_FIELD); 63 accounts.put(new Integer (id), account); 64 } 65 else 66 { 67 LOG.error("Account " + id + " not found"); 68 throw new Exception (ACCOUNT_NOT_FOUND); 69 } 70 } 71 catch (Exception e) 72 { 73 LOG.error("Unexpected error " + e.getMessage()); 74 if (LOG.isDebugEnabled()) LOG.debug("Stack trace follows ", e); 75 throw new Exception (e); 76 } 77 } 78 return account; 79 } 80 81 private void updateAccount(AccountData account) throws Exception  82 { 83 String s = "update account set id = ?, name = ?, balance = ? where id = ?"; 84 PreparedStatement ps = connection.prepareStatement(s); 85 ps.setInt(1, account.id); 86 ps.setString(2, account.name); 87 ps.setDouble(3, account.balance); 88 ps.setInt(4, account.id); 89 if (ps.executeUpdate() != 1) 90 { 91 LOG.error("Unexpected error when updating account " + account.id); 92 throw new Exception ("Error when updating account"); 93 } 94 } 95 96 99 public void createAccount(int id, String name, double initialBalance) throws OperationFailed 100 { 101 try 102 { 103 AccountData account = new AccountData(id, name, initialBalance); 104 String s = "insert into account (id, name, balance) values (?, ?, ?)"; 105 PreparedStatement ps = connection.prepareStatement(s); 106 ps.setInt(1, account.id); 107 ps.setString(2, account.name); 108 ps.setDouble(3, account.balance); 109 if (ps.executeUpdate() != 1) 110 { 111 LOG.error("Unexpected error when updating account " + id); 112 throw new Exception ("Error when updating account"); 113 } 114 accounts.put(new Integer (id), account); 115 } 116 catch (Exception e) 117 { 118 throw new OperationFailed(e.getMessage()); 119 } 120 } 121 122 125 public void deleteAccount(int id) throws OperationFailed 126 { 127 try 128 { 129 accounts.remove(new Integer (id)); 130 String s = "delete account where id = ?"; 131 PreparedStatement ps = connection.prepareStatement(s); 132 ps.setInt(1, id); 133 if (ps.executeUpdate() != 1) 134 { 135 LOG.error("Unexpected error when deleting account " + id); 136 throw new Exception ("Error when deleting account"); 137 } 138 } 139 catch (Exception e) 140 { 141 throw new OperationFailed(e.getMessage()); 142 } 143 } 144 145 148 public void deposit(int id, double amount) throws OperationFailed 149 { 150 try 151 { 152 AccountData account = this.getAccount(id); 153 account.balance += amount; 154 updateAccount(account); 155 } 156 catch (Exception e) 157 { 158 throw new OperationFailed(e.getMessage()); 159 } 160 } 161 162 165 public void withdraw(int id, double amount) throws OperationFailed 166 { 167 try 168 { 169 AccountData account = this.getAccount(id); 170 account.balance -= amount; 171 updateAccount(account); 172 } 173 catch (Exception e) 174 { 175 throw new OperationFailed(e.getMessage()); 176 } 177 } 178 179 182 public double getBalance(int id) throws OperationFailed 183 { 184 try 185 { 186 AccountData account = this.getAccount(id); 187 return account.balance; 188 } 189 catch (Exception e) 190 { 191 throw new OperationFailed(e.getMessage()); 192 } 193 } 194 195 198 public AccountData getAccountData(int id) throws OperationFailed 199 { 200 try 201 { 202 AccountData account = this.getAccount(id); 203 return account; 204 } 205 catch (Exception e) 206 { 207 throw new OperationFailed(e.getMessage()); 208 } 209 } 210 211 214 public void jackassSetContexts(ApplicationContext appCtx, StatelessContext compCtx) 215 { 216 this.appContext = appCtx; 217 this.compContext = compCtx; 218 } 219 220 223 public void jackassCreate() 224 { 225 try 226 { 227 String dbDriverClassName = compContext.getProperty("driver_class"); 228 Class.forName(dbDriverClassName, true, this.getClass().getClassLoader()); 229 230 String connectionString = compContext.getProperty("connection_string"); 231 String username = compContext.getProperty("username"); 232 String password = compContext.getProperty("password"); 233 234 connection = DriverManager.getConnection (connectionString, username, password); 235 } 236 catch (Exception e) 237 { 238 if (LOG.isInfoEnabled()) LOG.info("Error during create " + e.getMessage()); 239 if (LOG.isDebugEnabled()) LOG.debug("Stack trace follows ", e); 240 } 241 } 242 243 246 public void jackassRemove() 247 { 248 try 249 { 250 connection.close(); 251 } 252 catch (SQLException e) 253 { 254 if (LOG.isInfoEnabled()) LOG.info("Error during remove " + e.getMessage()); 255 if (LOG.isDebugEnabled()) LOG.debug("Stack trace follows ", e); 256 } 257 } 258 } 259
| Popular Tags
|