KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > persistentaccount > AccountManagerImpl


1 /*
2  * Copyright (c) 2003 by The Jackass Team
3  * Licensed under the Open Software License version 2.0
4  */

5 package persistentaccount;
6
7 import java.sql.Connection JavaDoc;
8 import java.sql.DriverManager JavaDoc;
9 import java.sql.PreparedStatement JavaDoc;
10 import java.sql.ResultSet JavaDoc;
11 import java.sql.SQLException JavaDoc;
12 import java.util.Collections JavaDoc;
13 import java.util.Map JavaDoc;
14 import java.util.WeakHashMap JavaDoc;
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 /**
28  *
29  * @author Carlos Arévalo
30  */

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 JavaDoc BALANCE_FIELD = "balance";
36     private static final String JavaDoc NAME_FIELD = "name";
37     private static final String JavaDoc ID_FIELD = "id";
38
39     private static final String JavaDoc ACCOUNT_NOT_FOUND = "Account not found";
40     private static Map JavaDoc accounts = Collections.synchronizedMap(new WeakHashMap JavaDoc());
41
42     private StatelessContext compContext = null;
43     private ApplicationContext appContext = null;
44     private Connection JavaDoc connection = null;
45
46     private AccountData getAccount(int id) throws Exception JavaDoc
47     {
48         AccountData account = (AccountData) accounts.get(new Integer JavaDoc(id));
49         if (account == null)
50         {
51             try {
52                 account = new AccountData();
53                 String JavaDoc s = "select id, name, balance from account where id = ?";
54                 PreparedStatement JavaDoc ps = connection.prepareStatement(s);
55                 ps.setInt(1, id);
56                 ResultSet JavaDoc 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 JavaDoc(id), account);
64                 }
65                 else
66                 {
67                     LOG.error("Account " + id + " not found");
68                     throw new Exception JavaDoc(ACCOUNT_NOT_FOUND);
69                 }
70             }
71             catch (Exception JavaDoc e)
72             {
73                 LOG.error("Unexpected error " + e.getMessage());
74                 if (LOG.isDebugEnabled()) LOG.debug("Stack trace follows ", e);
75                 throw new Exception JavaDoc(e);
76             }
77         }
78         return account;
79     }
80
81     private void updateAccount(AccountData account) throws Exception JavaDoc
82     {
83         String JavaDoc s = "update account set id = ?, name = ?, balance = ? where id = ?";
84         PreparedStatement JavaDoc 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 JavaDoc("Error when updating account");
93         }
94     }
95
96     /**
97      * @see account.AccountManagerOperations#createAccount(int, java.lang.String, int)
98      */

99     public void createAccount(int id, String JavaDoc name, double initialBalance) throws OperationFailed
100     {
101         try
102         {
103             AccountData account = new AccountData(id, name, initialBalance);
104             String JavaDoc s = "insert into account (id, name, balance) values (?, ?, ?)";
105             PreparedStatement JavaDoc 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 JavaDoc("Error when updating account");
113             }
114             accounts.put(new Integer JavaDoc(id), account);
115         }
116         catch (Exception JavaDoc e)
117         {
118             throw new OperationFailed(e.getMessage());
119         }
120     }
121
122     /**
123      * @see account.AccountManagerOperations#deleteAccount(int)
124      */

125     public void deleteAccount(int id) throws OperationFailed
126     {
127         try
128         {
129             accounts.remove(new Integer JavaDoc(id));
130             String JavaDoc s = "delete account where id = ?";
131             PreparedStatement JavaDoc 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 JavaDoc("Error when deleting account");
137             }
138         }
139         catch (Exception JavaDoc e)
140         {
141             throw new OperationFailed(e.getMessage());
142         }
143     }
144
145     /**
146      * @see account.AccountManagerOperations#deposit(int, double)
147      */

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 JavaDoc e)
157         {
158             throw new OperationFailed(e.getMessage());
159         }
160     }
161
162     /**
163      * @see account.AccountManagerOperations#withdraw(int, double)
164      */

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 JavaDoc e)
174         {
175             throw new OperationFailed(e.getMessage());
176         }
177     }
178
179     /**
180      * @see account.AccountManagerOperations#getBalance(int)
181      */

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 JavaDoc e)
190         {
191             throw new OperationFailed(e.getMessage());
192         }
193     }
194
195     /**
196      * @see account.AccountManagerOperations#getAccountData(int)
197      */

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 JavaDoc e)
206         {
207             throw new OperationFailed(e.getMessage());
208         }
209     }
210
211     /**
212      * @see ve.luz.ica.jackass.component.StatelessHook#jackassSetContext(ve.luz.ica.jackass.component.JackassContext)
213      */

214     public void jackassSetContexts(ApplicationContext appCtx, StatelessContext compCtx)
215     {
216         this.appContext = appCtx;
217         this.compContext = compCtx;
218     }
219
220     /**
221      * @see ve.luz.ica.jackass.component.StatelessHook#jackassCreate()
222      */

223     public void jackassCreate()
224     {
225         try
226         {
227             String JavaDoc dbDriverClassName = compContext.getProperty("driver_class");
228             Class.forName(dbDriverClassName, true, this.getClass().getClassLoader());
229
230             String JavaDoc connectionString = compContext.getProperty("connection_string");
231             String JavaDoc username = compContext.getProperty("username");
232             String JavaDoc password = compContext.getProperty("password");
233
234             connection = DriverManager.getConnection (connectionString, username, password);
235         }
236         catch (Exception JavaDoc 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     /**
244      * @see ve.luz.ica.jackass.component.StatelessHook#jackassRemove()
245      */

246     public void jackassRemove()
247     {
248         try
249         {
250             connection.close();
251         }
252         catch (SQLException JavaDoc 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