📄 ch13.htm
字号:
<TT>174: public void requestUpdateService(Account account) throws</TT><TT>175: InvalidAccountException {</TT><TT>176: </TT><TT>177: // First check to see if the Account is with this Bank.</TT><TT>178: if (!CORBAAlgorithms.contains(myAccounts, account)) {</TT><TT>179: </TT><TT>180: // Invalid Account; throw an exception.</TT><TT>181: System.out.println("BankImpl: Attempted to delete " +</TT><TT>182: "invalid Account.");</TT><TT>183: throw new InvalidAccountException();</TT><TT>184: }</TT><TT>185: </TT><TT>186: // If we got this far, the Account must exist with this</TT><TT>187: // Bank, so we can proceed.</TT><TT>188: mySubscribedAccounts.addElement(account._duplicate());</TT><TT>189: }</TT><TT>190: </TT><TT>191: // Return the next available account number.</TT><TT>192: protected String getNextAccountNumber() {</TT><TT>193: </TT><TT>194: return "Account" + myNextAccountNumber++;</TT><TT>195: }</TT><TT>196: </TT><TT>197: // Return the current date in the form "Mmm DD YYYY".</TT><TT>198: protected String getCurrentDate() {</TT><TT>199: </TT><TT>200: SimpleDateFormat format = new SimpleDateFormat("MMM dd " +</TT><TT>201: "yyyy");</TT><TT>202: return format.format(new Date());</TT><TT>203: }</TT></FONT></PRE><P><TT>204: }</TT><H4><FONT COLOR="#000077">Listing 13.9. UpdateAccountThread.java.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT> 1: // UpdateAccountThread.java</TT><TT> 2: </TT><TT> 3: import java.util.Enumeration;</TT><TT> 4: import java.util.Vector;</TT><TT> 5: </TT><TT> 6: import idlGlobal.Account;</TT><TT> 7: import idlGlobal.Customer;</TT><TT> 8: </TT><TT> 9: public class UpdateAccountThread extends Thread {</TT><TT>10: </TT><TT>11: // The Accounts to be updated by this thread.</TT><TT>12: Vector myAccounts;</TT><TT>13: </TT><TT>14: public UpdateAccountThread(Vector accounts) {</TT><TT>15: </TT><TT>16: myAccounts = accounts;</TT><TT>17: }</TT><TT>18: </TT><TT>19: protected UpdateAccountThread() {</TT><TT>20: </TT><TT>21: }</TT><TT>22: </TT><TT>23: public void run() {</TT><TT>24: </TT><TT>25: // Do this forever (or until the thread is killed).</TT><TT>26: while (true) {</TT><TT>27: </TT><TT>28: // Sleep for one minute between updates.</TT><TT>29: try {</TT><TT>30: sleep(60000);</TT><TT>31: } catch (InterruptedException ex) {</TT><TT>32: }</TT><TT>33: </TT><TT>34: // Iterate through each Account...</TT><TT>35: Enumeration e = myAccounts.elements();</TT><TT>36: while (e.hasMoreElements()) {</TT><TT>37: Account account = (Account)e.nextElement();</TT><TT>38: </TT><TT>39: // Iterate through each Customer associated with</TT><TT>40: // the Account...</TT><TT>41: Customer[] customers = account.getCustomers();</TT><TT>42: for (int i = 0; i < customers.length; i++) {</TT><TT>43: </TT><TT>44: try {</TT><TT>45: // Send an update message to each Customer.</TT><TT>46: customers[i].updateAccountBalance(account,</TT><TT>47: account.balance());</TT><TT>48: } catch (Exception ex) {</TT><TT>49: </TT><TT>50: // Ignore any exceptions that occur.</TT><TT>51: }</TT><TT>52: }</TT><TT>53: }</TT><TT>54: }</TT><TT>55: }56: }</TT></FONT></PRE><H4><FONT COLOR="#000077">Listing 13.10. BankMain.java.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT> 1: // BankMain.java</TT><TT> 2: </TT><TT> 3: import org.omg.CORBA.BOA;</TT><TT> 4: import org.omg.CORBA.ORB;</TT><TT> 5: </TT><TT> 6: import idlGlobal.BankServer;</TT><TT> 7: import idlGlobal.BankServerHelper;</TT><TT> 8: </TT><TT> 9: public class BankMain {</TT><TT>10: </TT><TT>11: public static ORB myORB;</TT><TT>12: public static BOA myBOA;</TT><TT>13: </TT><TT>14: public static void main(String[] args) {</TT><TT>15: </TT><TT>16: // Check the number of arguments; there should be exactly</TT><TT>17: // one.</TT><TT>18: if (args.length != 1) {</TT><TT>19: System.out.println("Usage: Bank <bankname>");</TT><TT>20: return;</TT><TT>21: }</TT><TT>22: </TT><TT>23: // Assign the Bank name to the first argument.</TT><TT>24: String bankName = args[0];</TT><TT>25: </TT><TT>26: BankServer bankServer;</TT><TT>27: BankImpl bank;</TT><TT>28: </TT><TT>29: try {</TT><TT>30: </TT><TT>31: // Initialize the ORB and BOA.</TT><TT>32: myORB = ORB.init(args, null);</TT><TT>33: myBOA = myORB.BOA_init();</TT><TT>34: </TT><TT>35: // Create an BankImpl object and register it with the</TT><TT>36: // ORB.</TT><TT>37: bank = new BankImpl(bankName);</TT><TT>38: myBOA.obj_is_ready(bank);</TT><TT>39: </TT><TT>40: // Bind to a BankServer.</TT><TT>41: bankServer = BankServerHelper.bind(myORB);</TT><TT>42: System.out.println("BankImpl: Successfully bound to " +</TT><TT>43: "BankServer.");</TT><TT>44: } catch (Exception ex) {</TT><TT>45: </TT><TT>46: // The bind attempt failed...</TT><TT>47: System.out.println("BankImpl: Unable to bind to " +</TT><TT>48: "BankServer.");</TT><TT>49: return;</TT><TT>50: }</TT><TT>51: </TT><TT>52: try {</TT><TT>53: </TT><TT>54: // Register with the BankServer.</TT><TT>55: bankServer.registerBank(bank);</TT><TT>56: </TT><TT>57: // Wait for CORBA events.</TT><TT>58: myBOA.impl_is_ready();</TT><TT>59: } catch (Exception ex) {</TT><TT>60: </TT><TT>61: // The registerBank() attempt failed...</TT><TT>62: System.out.println("BankImpl: Unable to register " +</TT><TT>63: "Bank:");</TT><TT>64: System.out.println(ex);</TT><TT>65: return;</TT><TT>66: }</TT><TT>67: </TT><TT>68: // When this point is reached, the application is finished.</TT><TT>69: return;</TT><TT>70: }71: }</TT> </FONT></PRE><P>Here are highlights from the various classes that implement the <TT>Bank</TT>server application. First, note the implementation for <TT>getCustomers()</TT> in<TT>AccountImpl.java</TT> (refer to lines 60-71 of Listing 13.4).</P><P>Notice the relative simplicity with which objects are copied from <TT>Vectors</TT>into arrays. First, an array of the appropriate size is created (in line 62), andthe contents of the <TT>Vector</TT> are copied into it with a single method call:<TT>copyInto()</TT> (line 63). Because these are outbound CORBA objects, they mustbe <TT>_duplicate()</TT>d, which is the reason for the subsequent iteration acrossthe <TT>Vector</TT>, as in lines 65-68.</P><P>Next, note the constructor of <TT>BankImpl</TT> (lines 34-46 of Listing 13.8).</P><P>In the constructor, an <TT>UpdateAccountThread</TT> object is created (which you'llget a closer look at in a moment) and started. For simple threads, this is all thatJava requires: Create a <TT>Thread</TT> object, and call <TT>start()</TT> on thatobject. Now, look at the <TT>UpdateAccountThread</TT> itself, beginning in line 9of Listing 13.9.</P><P>One way to implement threads in Java is to create a class that derives from <TT>java.lang.Thread</TT>(or simply <TT>Thread</TT>), as <TT>UpdateAccountThread</TT> does. The next stepis to implement the <TT>run()</TT> method. The actual workings of the <TT>UpdateAccountThread.run()</TT>method are self-explanatory.</P><P>Finally, notice in <TT>BankMain.java</TT> how an application binds to a CORBAobject (refer to Listing 13.10). Again, the ORB is initialized (lines 31-33).</P><P>Sometime after the ORB and BOA are initialized, <TT>BankMain</TT> uses the <TT>bind()</TT>method of <TT>BankServerHelper</TT> to bind to a <TT>BankServer</TT> object, as inline 41. (Recall again that <TT>bind()</TT> is nonstandard and is used here for simplicity.)<H4><FONT COLOR="#000077">ATM</FONT></H4><P>The implementation of the <TT>ATM</TT> server, consisting of <TT>ATMImpl.java</TT>and <TT>ATMMain.java</TT>, appears in Listings 13.11 and 13.12. This is clear-cutand contains no surprises.<H4><FONT COLOR="#000077">Listing 13.11. ATMImpl.java.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT> 1: // ATMImpl.java</TT><TT> 2: </TT><TT> 3: import idlGlobal.Account;</TT><TT> 4: import idlGlobal.ATMCard;</TT><TT> 5: </TT><TT> 6: import idlGlobal.AuthorizationException;</TT><TT> 7: import idlGlobal.InsufficientFundsException;</TT><TT> 8: import idlGlobal.InvalidAmountException;</TT><TT> 9: </TT><TT>10: public class ATMImpl extends idlGlobal._ATMImplBase {</TT><TT>11: </TT><TT>12: private String myName;</TT><TT>13: </TT><TT>14: public ATMImpl(String name) {</TT><TT>15: </TT><TT>16: myName = new String(name);</TT><TT>17: }</TT><TT>18: </TT><TT>19: private ATMImpl() {</TT><TT>20: </TT><TT>21: myName = new String();</TT><TT>22: }</TT><TT>23: </TT><TT>24: public String name() {</TT><TT>25: </TT><TT>26: return myName;</TT><TT>27: }</TT><TT>28: </TT><TT>29: public void name(String name) {</TT><TT>30: </TT><TT>31: myName = new String(name);</TT><TT>32: }</TT><TT>33: </TT><TT>34: public float withdraw(ATMCard card, Account account, short pin,</TT><TT>35: float amount) throws AuthorizationException,</TT><TT>36: InvalidAmountException, InsufficientFundsException {</TT><TT>37: </TT><TT>38: System.out.println("ATM: Authorizing Account for " +</TT><TT>39: "withdrawal.");</TT><TT>40: if (pin != card.pin() || !card.isAuthorized(account)) {</TT><TT>41: </TT><TT>42: // Incorrect PIN or card not authorized; throw an</TT><TT>43: // exception.</TT><TT>44: System.out.println(" Authorization failed.");</TT><TT>45: throw new AuthorizationException();</TT><TT>46: }</TT><TT>47: </TT><TT>48: System.out.println(" Authorization succeeded; forwarding "</TT><TT>49: + "withdrawal request to Account.");</TT><TT>50: return account.withdraw(amount);</TT><TT>51: }</TT><TT>52: </TT><TT>53: public float deposit(ATMCard card, Account account, short pin,</TT><TT>54: float amount) throws AuthorizationException,</TT><TT>55: InvalidAmountException {</TT><TT>56: </TT><TT>57: System.out.println("ATM: Authorizing Account for " +</TT><TT>58: "deposit.");</TT><TT>59: if (pin != card.pin() || !card.isAuthorized(account)) {</TT><TT>60: </TT><TT>61: // Incorrect PIN or card not authorized; throw an</TT><TT>62: // exception.</TT><TT>63: System.out.println(" Authorization failed.");</TT><TT>64: throw new AuthorizationException();</TT><TT>65: }</TT><TT>66: </TT><TT>67: System.out.println(" Authorization succeeded; forwarding "</TT><TT>68: + "deposit request to Account.");</TT><TT>69: return account.deposit(amount);</TT><TT>70: }</TT><TT>71: </TT><TT>72: public float getBalance(ATMCard card, Account account, short</TT><TT>73: pin) throws AuthorizationException {</TT><TT>74: </TT><TT>75: System.out.println("ATM: Authorizing Account for " +</TT><TT>76: "balance.");</TT><TT>77: if (pin != card.pin() || !card.isAuthorized(account)) {</TT><TT>78: </TT><TT>79: // Incorrect PIN or card not authorized; throw an</TT><TT>80: // exception.</TT><TT>81: System.out.println(" Authorization failed.");</TT><TT>82: throw new AuthorizationException();</TT><TT>83: }</TT><TT>84: </TT><TT>85: System.out.println(" Authorization succeeded; forwarding "</TT><TT>86: + "balance request to Account.");</TT><TT>87: return account.balance();</TT><TT>88: }89: }</TT></FONT></PRE><H4><FONT COLOR="#000077">Listing 13.12. ATMMain.java.</FONT>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -