⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch13.htm

📁 corba比较入门级的介绍详细间接了corba访问发布各种细节。
💻 HTM
📖 第 1 页 / 共 5 页
字号:
can invoke the application at any given time). This method always takes <TT>String[]args</TT> as a parameter (or the semantically equivalent <TT>String args[]</TT>).The <TT>main()</TT> method of <TT>BankServerMain</TT> begins in line 11.</P><P>Before any CORBA objects are created, the ORB and BOA must be initialized, asshown in lines 13-17.</P><P>Then the <TT>BankServerImpl</TT> object is created and registered with the BOA,through the <TT>obj_is_ready()</TT> method, as seen in lines 19-23.</P><P>The <TT>impl_is_ready()</TT> method, called in line 27, enters the CORBA eventloop, passing incoming CORBA events to the appropriate objects.</P><P>Finally, in lines 28-35, a <TT>catch</TT> handler is added in case exceptionsare thrown by any of the methods called. The <TT>try...catch</TT> mechanism worksthe same in Java as in C++.</P><P><TT>CORBAAlgorithms.java</TT>, appearing in Listing 13.3, contains a couple ofuseful methods.</P><P>Note the use of the <TT>package</TT> statement in line 3. This indicates thatthe <TT>CORBAAlgorithms</TT> class belongs to the <TT>util</TT> package.</P><P>Note also that <TT>contains()</TT>, which begins in line 10, is declared as a<TT>static</TT> method. Such methods behave the same as in C++--that is, they existindependently of any object instance and are invoked as freestanding functions ratherthan as methods on objects.</P><P>As seen in lines 16-29, this method iterates through the provided <TT>Vector</TT>,searching for an object for which <TT>_is_equivalent()</TT> (a standard CORBA method)returns <TT>true</TT>. The next method, <TT>removeElement()</TT>, behaves similarly.<BLOCKQUOTE>	<P><HR><B>Warning: </B>Remember that the <TT>_is_equivalent()</TT> method returns <TT>true</TT>	if it is known that two object references refer to the same object. It is possible	for <TT>_is_equivalent()</TT> to return <TT>false</TT> even if two references are	in fact equivalent; the only guarantee is that the method will never return <TT>true</TT>	if the references are not equivalent. Use this method with caution. <HR></BLOCKQUOTE><H4><FONT COLOR="#000077">Bank</FONT></H4><P>Listings 13.4-10 contain the definitions of the Java classes that implement the<TT>Bank</TT> functionality. You'll see that the functionality provided by theseclasses mirrors their counterparts from previous chapters.<H4><FONT COLOR="#000077">Listing 13.4. AccountImpl.java.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT>  1: // AccountImpl.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.Customer;</TT><TT>  7: </TT><TT>  8: import idlGlobal.InsufficientFundsException;</TT><TT>  9: import idlGlobal.InvalidAmountException;</TT><TT> 10: </TT><TT> 11: public class AccountImpl extends idlGlobal._AccountImplBase {</TT><TT> 12: </TT><TT> 13:     // This Account's account number.</TT><TT> 14:     private String myAccountNumber;</TT><TT> 15: </TT><TT> 16:     // This Account's creation date.</TT><TT> 17:     private String myCreationDate;</TT><TT> 18: </TT><TT> 19:     // This Account's balance.</TT><TT> 20:     private float myBalance;</TT><TT> 21: </TT><TT> 22:     // This Account's list of owners (which are Customers).</TT><TT> 23:     private Vector myOwners;</TT><TT> 24: </TT><TT> 25:     public AccountImpl(String accountNumber, String creationDate,</TT><TT> 26:             float initialBalance, Customer customer) {</TT><TT> 27: </TT><TT> 28:         super(accountNumber);</TT><TT> 29: </TT><TT> 30:         myAccountNumber = new String(accountNumber);</TT><TT> 31:         myCreationDate = new String(creationDate);</TT><TT> 32:         myBalance = initialBalance;</TT><TT> 33:         myOwners = new Vector();</TT><TT> 34:         myOwners.addElement(customer._duplicate());</TT><TT> 35:     }</TT><TT> 36: </TT><TT> 37:     protected AccountImpl() {</TT><TT> 38: </TT><TT> 39:         myAccountNumber = new String();</TT><TT> 40:         myCreationDate = new String();</TT><TT> 41:         myBalance = 0.0f;</TT><TT> 42:         myOwners = new Vector();</TT><TT> 43:     }</TT><TT> 44: </TT><TT> 45:     public String accountNumber() {</TT><TT> 46: </TT><TT> 47:         return myAccountNumber;</TT><TT> 48:     }</TT><TT> 49: </TT><TT> 50:     public String creationDate() {</TT><TT> 51: </TT><TT> 52:         return myCreationDate;</TT><TT> 53:     }</TT><TT> 54: </TT><TT> 55:     public float balance() {</TT><TT> 56: </TT><TT> 57:         return myBalance;</TT><TT> 58:     }</TT><TT> 59: </TT><TT> 60:     public Customer[] getCustomers() {</TT><TT> 61: </TT><TT> 62:         Customer[] list = new Customer[myOwners.size()];</TT><TT> 63:         myOwners.copyInto(list);</TT><TT> 64: </TT><TT> 65:         Enumeration e = myOwners.elements();</TT><TT> 66:         while (e.hasMoreElements()) {</TT><TT> 67:             ((Customer)e.nextElement())._duplicate();</TT><TT> 68:         }</TT><TT> 69: </TT><TT> 70:         return list;</TT><TT> 71:     }</TT><TT> 72: </TT><TT> 73:     public float withdraw(float amount) throws</TT><TT> 74:             InvalidAmountException, InsufficientFundsException {</TT><TT> 75: </TT><TT> 76:         // Disallow the withdrawal of negative amounts and throw an</TT><TT> 77:         // exception if this is attempted.</TT><TT> 78:         if (amount &lt; 0.0) {</TT><TT> 79:             System.out.println(&quot;AccountImpl: Attempted to &quot; +</TT><TT> 80:                     &quot;withdraw invalid amount.&quot;);</TT><TT> 81:             throw new InvalidAmountException();</TT><TT> 82:         }</TT><TT> 83: </TT><TT> 84:         // Disallow withdrawal of an amount greater than the</TT><TT> 85:         // current balance and throw an exception if this is</TT><TT> 86:         // attempted.</TT><TT> 87:         if (amount &gt; myBalance) {</TT><TT> 88:             System.out.println(&quot;AccountImpl: Insufficient funds to&quot;</TT><TT> 89:                     + &quot; withdraw specified amount.&quot;);</TT><TT> 90:             throw new InsufficientFundsException();</TT><TT> 91:         }</TT><TT> 92: </TT><TT> 93:         myBalance -= amount;</TT><TT> 94: </TT><TT> 95:         return myBalance;</TT><TT> 96:     }</TT><TT> 97: </TT><TT> 98:     public float deposit(float amount) throws</TT><TT> 99:             InvalidAmountException {</TT><TT>100: </TT><TT>101:         // Disallow the deposit of negative amounts and throw an</TT><TT>102:         // exception if this is attempted.</TT><TT>103:         if (amount &lt; 0.0) {</TT><TT>104:             System.out.println(&quot;AccountImpl: Attempted to deposit &quot;</TT><TT>105:                     + &quot;invalid amount.&quot;);</TT><TT>106:             throw new InvalidAmountException();</TT><TT>107:         }</TT><TT>108: </TT><TT>109:         myBalance += amount;</TT><TT>110: </TT><TT>111:         return myBalance;</TT><TT>112:     }113: }</TT></FONT></PRE><H4><FONT COLOR="#000077">Listing 13.5. ATMCardImpl.java.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT> 1: // ATMCardImpl.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: </TT><TT> 8: import idlGlobal.InvalidAccountException;</TT><TT> 9: </TT><TT>10: import util.CORBAAlgorithms;</TT><TT>11: </TT><TT>12: public class ATMCardImpl extends idlGlobal._ATMCardImplBase {</TT><TT>13: </TT><TT>14:     // This ATMCard's Personal Identification Number (PIN).</TT><TT>15:     private short myPIN;</TT><TT>16: </TT><TT>17:     // The Accounts which are authorized for use with this ATMCard.</TT><TT>18:     private Vector myAccounts;</TT><TT>19: </TT><TT>20:     public ATMCardImpl(short pin, Account initialAccount) {</TT><TT>21: </TT><TT>22:         myPIN = pin;</TT><TT>23:         myAccounts = new Vector();</TT><TT>24:         myAccounts.addElement(initialAccount._duplicate());</TT><TT>25:     }</TT><TT>26: </TT><TT>27:     private ATMCardImpl() {</TT><TT>28: </TT><TT>29:         myPIN = 0;</TT><TT>30:         myAccounts = new Vector();</TT><TT>31:     }</TT><TT>32: </TT><TT>33:     public void pin(short pin) {</TT><TT>34: </TT><TT>35:         myPIN = pin;</TT><TT>36:     }</TT><TT>37: </TT><TT>38:     public short pin() {</TT><TT>39: </TT><TT>40:         return myPIN;</TT><TT>41:     }</TT><TT>42: </TT><TT>43:     public Account[] getAccounts() {</TT><TT>44: </TT><TT>45:         Account[] list = new Account[myAccounts.size()];</TT><TT>46:         myAccounts.copyInto(list);</TT><TT>47: </TT><TT>48:         Enumeration e = myAccounts.elements();</TT><TT>49:         while (e.hasMoreElements()) {</TT><TT>50:             ((Account)e.nextElement())._duplicate();</TT><TT>51:         }</TT><TT>52: </TT><TT>53:         return list;</TT><TT>54:     }</TT><TT>55: </TT><TT>56:     public void addAccount(Account account) throws</TT><TT>57:             InvalidAccountException {</TT><TT>58: </TT><TT>59:         if (isAuthorized(account)) {</TT><TT>60: </TT><TT>61:             // Account has already been added, so throw an</TT><TT>62:             // exception.</TT><TT>63:             throw new InvalidAccountException();</TT><TT>64:         }</TT><TT>65: </TT><TT>66:         // Add the created Account at the end of the list.</TT><TT>67:         myAccounts.addElement(account._duplicate());</TT><TT>68:     }</TT><TT>69: </TT><TT>70:     public void removeAccount(Account account) throws</TT><TT>71:             InvalidAccountException {</TT><TT>72: </TT><TT>73:         if (!isAuthorized(account)) {</TT><TT>74: </TT><TT>75:             // Invalid Account; throw an exception.</TT><TT>76:             throw new InvalidAccountException();</TT><TT>77:         }</TT><TT>78: </TT><TT>79:         // Delete the given Account.</TT><TT>80:         CORBAAlgorithms.removeElement(myAccounts, account);</TT><TT>81:         account._release();</TT><TT>82:     }</TT><TT>83: </TT><TT>84:     public boolean isAuthorized(Account account) {</TT><TT>85: </TT><TT>86:         return CORBAAlgorithms.contains(myAccounts, account);</TT><TT>87:     }88: }</TT></FONT></PRE><H4><FONT COLOR="#000077">Listing 13.6. CheckingAccountImpl.java.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT> 1: // CheckingAccountImpl.java</TT><TT> 2: </TT><TT> 3: import idlGlobal.Customer;</TT><TT> 4: </TT><TT> 5: import idlGlobal.InsufficientFundsException;</TT><TT> 6: import idlGlobal.InvalidAmountException;</TT><TT> 7: </TT><TT> 8: public class CheckingAccountImpl extends idlGlobal.</TT><TT> 9:         _CheckingAccountImplBase {</TT><TT>10: </TT><TT>11:     // The AccountImpl to which most of this CheckingAccountImpl's</TT><TT>12:     // functionality is delegated.</TT><TT>13:     private AccountImpl myAccount;</TT><TT>14: </TT><TT>15:     public CheckingAccountImpl(String accountNumber, String</TT><TT>16:             creationDate, float initialBalance, Customer customer)</TT><TT>17:             {</TT><TT>18: </TT><TT>19:         myAccount = new AccountImpl(accountNumber, creationDate,</TT><TT>20:                 initialBalance, customer);</TT><TT>21:     }</TT><TT>22: </TT><TT>23:     protected CheckingAccountImpl() {</TT><TT>24: </TT><TT>25:     }</TT><TT>26: </TT><TT>27:     public String accountNumber() {</TT><TT>28: </TT><TT>29:         return myAccount.accountNumber();</TT><TT>30:     }</TT><TT>31: </TT><TT>32:     public String creationDate() {</TT><TT>33: </TT><TT>34:         return myAccount.creationDate();</TT><TT>35:     }</TT><TT>36: </TT>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -