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

📄 ch07.htm

📁 corba比较入门级的介绍详细间接了corba访问发布各种细节。
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<P>Because the <TT>CheckingAccount</TT> interface adds no new methods to the <TT>Account</TT>interface, no additional analysis is necessary to determine exception raising forthis interface. For your review, the listing for <TT>CheckingAccount.idl</TT> reappearsin Listing 7.4.<H4><FONT COLOR="#000077">Listing 7.4. Original CheckingAccount.idl.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT>// CheckingAccount.idl</TT><TT>#ifndef CheckingAccount_idl</TT><TT>#define CheckingAccount_idl</TT><TT>#include &quot;Account.idl&quot;</TT><TT>// A CheckingAccount is an Account which supports checking. It</TT><TT>// does not gain any interest, as its sibling, the SavingsAccount,</TT><TT>// does.</TT><TT>interface CheckingAccount : Account {</TT><TT>};#endif</TT></FONT></PRE><H3><A NAME="Heading6"></A><FONT COLOR="#000077">Exceptions in SavingsAccount</FONT></H3><P>Listing 7.5 reproduces the <TT>SavingsAccount.idl</TT> listing from Day 6.<H4><FONT COLOR="#000077">Listing 7.5. Original SavingsAccount.idl.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT> 1: // SavingsAccount.idl</TT><TT> 2: </TT><TT> 3: #ifndef SavingsAccount_idl</TT><TT> 4: #define SavingsAccount_idl</TT><TT> 5: </TT><TT> 6: #include &quot;Account.idl&quot;</TT><TT> 7: </TT><TT> 8: // A SavingsAccount is an Account which supports savings</TT><TT> 9: // account semantics, such as gaining interest.</TT><TT>10: interface SavingsAccount : Account {</TT><TT>11: </TT><TT>12:     // This Account's interest rate.</TT><TT>13:     readonly attribute float interestRate;</TT><TT>14: </TT><TT>15:     // Set this Account's interest rate to the given rate.</TT><TT>16:     // Returns the previous rate.</TT><TT>17:     float setInterestRate(in float rate);</TT><TT>18: };</TT><TT>19: 20: #endif</TT> </FONT></PRE><P>The <TT>SavingsAccount</TT> interface defines one additional method, <TT>setInterestRate()</TT>(lines 15-17), which can throw an exception if an invalid rate (for example, a negativeone) is specified. In the interest of not using too many different exception names,you can reuse the <TT>InvalidAmountException</TT> from the preceding <TT>Account</TT>interfaces:</P><PRE><FONT COLOR="#0066FF"><TT>float setInterestRate(in float rate)</TT><TT>       raises (InvalidAmountException);</TT></FONT></PRE><H3><A NAME="Heading7"></A><FONT COLOR="#000077">Exceptions in Customer</FONT></H3><P>The <TT>Customer</TT> interface defines only one method, <TT>getAccounts()</TT>,which need not raise any exceptions. For your review, the original <TT>Customer.idl</TT>appears in Listing 7.6.<H4><FONT COLOR="#000077">Listing 7.6. Original Customer.idl.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT> 1: // Customer.idl</TT><TT> 2: </TT><TT> 3: // Forward declaration of Customer interface.</TT><TT> 4: interface Customer;</TT><TT> 5: </TT><TT> 6: #ifndef Customer_idl</TT><TT> 7: #define Customer_idl</TT><TT> 8: </TT><TT> 9: // sequence of Customers</TT><TT>10: typedef sequence&lt;Customer&gt; CustomerList;</TT><TT>11: </TT><TT>12: #include &quot;Account.idl&quot;</TT><TT>13: </TT><TT>14: // A Customer can hold one or more Accounts. Presumably, the</TT><TT>15: // Customer is what drives the rest of this application.</TT><TT>16: interface Customer {</TT><TT>17: </TT><TT>18:     // This Customer's name.</TT><TT>19:     attribute string name;</TT><TT>20: </TT><TT>21:     // This Customer's Social Security number.</TT><TT>22:     readonly attribute string socialSecurityNumber;</TT><TT>23: </TT><TT>24:     // This Customer's address.</TT><TT>25:     attribute string address;</TT><TT>26: </TT><TT>27:     // This Customer's mother's maiden name.</TT><TT>28:     readonly attribute string mothersMaidenName;</TT><TT>29: </TT><TT>30:     // Return a list of Accounts held (or co-held) by this</TT><TT>31:     // Customer.</TT><TT>32:     AccountList getAccounts();</TT><TT>33: };</TT><TT>34: 35: #endif</TT></FONT></PRE><H2><A NAME="Heading8"></A><FONT COLOR="#000077">Modifying Server IDL to Use Exceptions</FONT></H2><P>Much of the work of adding exceptions has already been done (adding the <TT>raises</TT>clauses to the methods that throw exceptions). However, you did not yet define theexceptions themselves. For the sake of simplicity, define all exceptions in a singlefile--<TT>Exceptions.idl</TT>, which can be <TT>#include</TT>d from other IDL files.<TT>Exceptions.idl</TT> appears in Listing 7.7.<H4><FONT COLOR="#000077">Listing 7.7. Exceptions.idl.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT> 1: // Exceptions.idl</TT><TT> 2: </TT><TT> 3: #ifndef Exceptions_idl</TT><TT> 4: #define Exceptions_idl</TT><TT> 5: </TT><TT> 6: // This exception is thrown when an invalid amount is passed to a</TT><TT> 7: // method; for instance, if an account is asked to deposit a</TT><TT> 8: // negative amount of funds.</TT><TT> 9: exception InvalidAmountException {</TT><TT>10: </TT><TT>11: };</TT><TT>12: </TT><TT>13: // This exception is thrown when an invalid Account is passed to a</TT><TT>14: // method expecting an Account object.</TT><TT>15: exception InvalidAccountException {</TT><TT>16: </TT><TT>17: };</TT><TT>18: </TT><TT>19: // This exception is thrown when an invalid Bank is passed to a</TT><TT>20: // method expecting a Bank object.</TT><TT>21: exception InvalidBankException {</TT><TT>22: </TT><TT>23: };</TT><TT>24: </TT><TT>25: // This exception is thrown when there are insufficient funds to</TT><TT>26: // cover a transaction; for instance, if a withdrawal attempts to</TT><TT>27: // remove more funds than are available in an account.</TT><TT>28: exception InsufficientFundsException {</TT><TT>29: </TT><TT>30: };</TT><TT>31: 32: #endif</TT> </FONT></PRE><P>Modifying each IDL file to reflect the changes is a simple matter. Listings 7.8-7.13show the modified IDL files for each interface, with the changes from the originalversions highlighted in<B> bold.</B><H4><FONT COLOR="#000077">Listing 7.8. Modified BankServer.idl.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT> 1: // BankServer.idl</TT><TT> 2: </TT><TT> 3: #ifndef BankServer_idl</TT><TT> 4: #define BankServer_idl</TT><TT> 5: </TT><TT> 6: #include &quot;Bank.idl&quot;</TT><B><TT> 7: #include &quot;Exceptions.idl&quot;</TT></B><TT> 8: </TT><TT> 9: // A BankServer provides clients with visibility to Bank objects.</TT><TT>10: interface BankServer {</TT><TT>11: </TT><TT>12:     // Register the given Bank with this BankServer. The Bank will</TT><TT>13:     // be listed by getBanks() until unregisterBank() is called with</TT><TT>14:     // that Bank.</TT><TT>15:     void registerBank(in Bank bank)</TT><B><TT>16:</TT></B><TT>             <B>raises (InvalidBankException);</B></TT><TT>17: </TT><TT>18:     // Unregister the given Bank from this BankServer. If the Bank</TT><TT>19:     // was not previously registered, this operation does nothing.</TT><TT>20:     void unregisterBank(in Bank bank)</TT><B><TT>21:</TT></B><TT>             <B>raises (InvalidBankException);</B></TT><TT>22: </TT><TT>23:     // Return a list of all Banks currently registered with this</TT><TT>24:     // BankServer.</TT><TT>25:     BankList getBanks();</TT><TT>26: };</TT><TT>27: 28: #endif</TT></FONT></PRE><H4><FONT COLOR="#000077">Listing 7.9. Modified Bank.idl.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT> 1: // Bank.idl</TT><TT> 2: </TT><TT> 3: // Forward declaration of Bank interface.</TT><TT> 4: interface Bank;</TT><TT> 5: </TT><TT> 6: #ifndef Bank_idl</TT><TT> 7: #define Bank_idl</TT><TT> 8: </TT><TT> 9: // sequence of Banks</TT><TT>10: typedef sequence&lt;Bank&gt; BankList;</TT><TT>11: </TT><TT>12: #include &quot;Customer.idl&quot;</TT><TT>13: #include &quot;Account.idl&quot;</TT><B><TT>14: #include &quot;Exceptions.idl&quot;</TT></B><TT>15: </TT><TT>16: // A Bank provides access to Accounts. It can create an Account</TT><TT>17: // on behalf of a Customer, delete an Account, or list the current</TT><TT>18: // Accounts with the Bank.</TT><TT>19: interface Bank {</TT><TT>20: </TT><TT>21:     // This Bank's name.</TT><TT>22:     attribute string name;</TT><TT>23: </TT><TT>24:     // This Bank's address.</TT><TT>25:     attribute string address;</TT><TT>26: </TT><TT>27:     // Create an Account on behalf of the given Customer, with the</TT><TT>28:     // given account type (&quot;savings&quot; or &quot;checking&quot;, where case is</TT><TT>29:     // significant), and the given opening balance.</TT><TT>30:     Account createAccount(in Customer customer, in string</TT><TT>31:             accountType, in float openingBalance);</TT><TT>32: </TT><TT>33:     // Delete the given Account. If the Account is not with this</TT><TT>34:     // Bank, this operation does nothing.</TT><TT>35:     void deleteAccount(in Account account)</TT><B><TT>36:</TT></B><TT>             <B>raises (InvalidAccountException);</B></TT><TT>37: </TT><TT>38:     // List all Accounts with this Bank.</TT><TT>39:     AccountList getAccounts();</TT><TT>40: };</TT><TT>41: 42: #endif</TT></FONT></PRE><H4><FONT COLOR="#000077">Listing 7.10. Modified Account.idl.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT> 1: // Account.idl</TT><TT> 2: </TT><TT> 3: // Forward declaration of Account interface.</TT><TT> 4: interface Account;</TT><TT> 5: </TT><TT> 6: #ifndef Account_idl</TT><TT> 7: #define Account_idl</TT><TT> 8: </TT><TT> 9: // sequence of Accounts</TT><TT>10: typedef sequence&lt;Account&gt; AccountList;</TT><TT>11: </TT><TT>12: #include &quot;Customer.idl&quot;</TT><B><TT>13: #include &quot;Exceptions.idl&quot;</TT></B><TT>14: </TT><TT>15: // An Account is an entity owned by a Bank and held by a Customer</TT><TT>16: // (or multiple Customers). An Account has a balance which can be</TT><TT>17: // affected by deposits and withdrawals.</TT><TT>18: interface Account {</TT><TT>19: </TT><TT>20:     // This Account's account number.</TT><TT>21:     readonly attribute string accountNumber;</TT><TT>22: </TT><TT>23:     // This Account's creation date.</TT><TT>24:     readonly attribute string creationDate;</TT><TT>25: </TT><TT>26:     // This Account's current balance.</TT><TT>27:     readonly attribute float balance;</TT><TT>28: </TT><TT>29:     // Return a list of Customers who hold this Account.</TT><TT>30:     CustomerList getCustomers();</TT><TT>31: </TT><TT>32:     // Withdraw the given amount from this Account. Returns the new</TT><TT>33:     // account balance.</TT><TT>34:     float withdraw(in float amount)</TT><B><TT>35:</TT></B><TT>             <B>raises (InvalidAmountException,</B></TT><B><TT>36:</TT></B><TT>             <B>InsufficientFundsException);</B></TT><TT>37: </TT><TT>38:     // Deposit the given amount into this Account. Returns the new</TT><TT>39:     // account balance.</TT><TT>40:     float deposit(in float amount)</TT><B><TT>41:</TT></B><TT>             <B>raises (InvalidAmountException);</B></TT><TT>42: };</TT><TT>43: 44: #endif</TT></FONT></PRE><H4><FONT COLOR="#000077">Listing 7.11. Modified CheckingAccount.idl.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT> 1: // CheckingAccount.idl</TT><TT> 2: </TT><TT> 3: #ifndef CheckingAccount_idl</TT><TT> 4: #define CheckingAccount_idl</TT><TT> 5: </TT><TT> 6: #include &quot;Account.idl&quot;</TT><TT> 7: </TT><TT> 8: // A CheckingAccount is an Account which supports checking. It</TT><TT> 9: // does not gain any interest, as its sibling, the SavingsAccount,</TT><TT>10: // does.</TT><TT>11: interface CheckingAccount : Account {</TT><TT>12: </TT><TT>13: };</TT><TT>14: 15: #endif</TT></FONT></PRE><H4><FONT COLOR="#000077">Listing 7.12. Modified SavingsAccount.idl.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT> 1: // SavingsAccount.idl</TT><TT> 2: </TT><TT> 3: #ifndef SavingsAccount_idl</TT><TT> 4: #define SavingsAccount_idl</TT><TT> 5: </TT><TT> 6: #include &quot;Account.idl&quot;</TT><B><TT> 7: #include &quot;Exceptions.idl&quot;</TT></B><TT> 8: </TT><TT> 9: // A SavingsAccount is an Account which supports savings</TT><TT>10: // account semantics, such as gaining interest.</TT><TT>11: interface SavingsAccount : Account {</TT><TT>12: </TT><TT>13:     // This Account's interest rate.</TT><TT>14:     readonly attribute float interestRate;</TT><TT>15: </TT><TT>16:     // Set this Account's interest rate to the given rate.</TT><TT>17:     // Returns the previous rate.</TT><TT>18:     float setInterestRate(in float rate)</TT><B><TT>19:</TT></B><TT>             <B>raises (InvalidAmountException);</B></TT><TT>20: };</TT><TT>21: 22: #endif</TT></FONT></PRE><H4><FONT COLOR="#000077">Listing 7.13. Modified Customer.idl.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT> 1: // Customer.idl</TT><TT> 2: </TT><TT> 3: // Forward declaration of Customer interface.</TT><TT> 4: interface Customer;</TT><TT> 5: </TT><TT> 6: #ifndef Customer_idl</TT><TT> 7: #define Customer_idl</TT>

⌨️ 快捷键说明

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