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

📄 ch13.htm

📁 corba比较入门级的介绍详细间接了corba访问发布各种细节。
💻 HTM
📖 第 1 页 / 共 5 页
字号:
</BLOCKQUOTE><P>To prepare for development of the Java <TT>Bank</TT> application, copy the IDLsource files from Day 9 into a new directory, and then compile them with the JavaIDL compiler. It's convenient to create a batch file, shell script file, or <TT>Makefile</TT>to perform this step automatically. When you complete this step, you will have adirectory named <TT>idlGlobal</TT> containing a number of files with the <TT>.java</TT>extension. You're now ready to proceed with development, starting with the serverfunctionality.<H3><A NAME="Heading8"></A><FONT COLOR="#000077">Implementing the Server Functionality</FONT></H3><P>Because you have made no changes to the <TT>Bank</TT> application, other thanusing a different development language, the architecture has not altered from previouschapters. Consequently, all the application components will look very familiar toyou by now.<H4><FONT COLOR="#000077">BankServer</FONT></H4><P>The <TT>BankServer</TT> is now implemented in two files, <TT>BankServerImpl.java</TT>and <TT>BankServerMain. java</TT>, appearing in Listings 13.1 and 13.2, respectively.Also, there is the file <TT>CORBAAlgorithms. java</TT>, which contains some utilitymethods, appearing in Listing 13.3.<H4><FONT COLOR="#000077">Listing 13.1. BankServerImpl.java.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT>  1: // BankServerImpl.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.ATM;</TT><TT>  7: import idlGlobal.Bank;</TT><TT>  8: </TT><TT>  9: import idlGlobal.InvalidATMException;</TT><TT> 10: import idlGlobal.InvalidBankException;</TT><TT> 11: </TT><TT> 12: import util.CORBAAlgorithms;</TT><TT> 13: </TT><TT> 14: public class BankServerImpl extends idlGlobal._BankServerImplBase {</TT><TT> 15: </TT><TT> 16:     // This BankServer's list of Banks.</TT><TT> 17:     private Vector myBanks;</TT><TT> 18: </TT><TT> 19:     // This BankServer's list of ATMs.</TT><TT> 20:     private Vector myATMs;</TT><TT> 21: </TT><TT> 22:     public BankServerImpl(String name) {</TT><TT> 23: </TT><TT> 24:         super(name);</TT><TT> 25: </TT><TT> 26:         myBanks = new Vector();</TT><TT> 27:         myATMs = new Vector();</TT><TT> 28:     }</TT><TT> 29: </TT><TT> 30:     public void registerBank(Bank bank) throws InvalidBankException</TT><TT> 31:             {</TT><TT> 32: </TT><TT> 33:         // First, ensure that the given Bank doesn't exist already.</TT><TT> 34:         if (!CORBAAlgorithms.contains(myBanks, bank)) {</TT><TT> 35: </TT><TT> 36:             // Bank was not found, so add the given Bank to the end</TT><TT> 37:             // of the list.</TT><TT> 38:             System.out.println(&quot;BankServerImpl: Registering Bank &quot;</TT><TT> 39:                     + &quot;\&quot;&quot; + bank.name() + &quot;\&quot;.&quot;);</TT><TT> 40:             myBanks.addElement(bank._duplicate());</TT><TT> 41:             return;</TT><TT> 42:         }</TT><TT> 43: </TT><TT> 44:         // The Bank was already registered, so throw an exception.</TT><TT> 45:         System.out.println(&quot;BankServerImpl: Attempted to &quot; +</TT><TT> 46:                 &quot;register duplicate Bank.&quot;);</TT><TT> 47:         throw new InvalidBankException();</TT><TT> 48:     }</TT><TT> 49: </TT><TT> 50:     public void unregisterBank(Bank bank) throws</TT><TT> 51:             InvalidBankException {</TT><TT> 52: </TT><TT> 53:         if (!CORBAAlgorithms.contains(myBanks, bank)) {</TT><TT> 54: </TT><TT> 55:             // Invalid Bank; throw an exception.</TT><TT> 56:             System.out.println(&quot;BankServerImpl: Attempted to &quot; +</TT><TT> 57:                     &quot;unregister invalid Bank.&quot;);</TT><TT> 58:             throw new InvalidBankException();</TT><TT> 59:         }</TT><TT> 60: </TT><TT> 61:         System.out.println(&quot;BankServerImpl: Unregistering Bank \&quot;&quot;</TT><TT> 62:                 + bank.name() + &quot;\&quot;.&quot;);</TT><TT> 63: </TT><TT> 64:         // Delete the given Bank.</TT><TT> 65:         CORBAAlgorithms.removeElement(myBanks, bank);</TT><TT> 66:         bank._release();</TT><TT> 67:     }</TT><TT> 68: </TT><TT> 69:     public Bank[] getBanks() {</TT><TT> 70: </TT><TT> 71:         Bank[] list = new Bank[myBanks.size()];</TT><TT> 72:         myBanks.copyInto(list);</TT><TT> 73: </TT><TT> 74:         Enumeration e = myBanks.elements();</TT><TT> 75:         while (e.hasMoreElements()) {</TT><TT> 76:             ((Bank)e.nextElement())._duplicate();</TT><TT> 77:         }</TT><TT> 78: </TT><TT> 79:         return list;</TT><TT> 80:     }</TT><TT> 81: </TT><TT> 82:     public void registerATM(ATM atm) throws InvalidATMException {</TT><TT> 83: </TT><TT> 84:         // First, ensure that the given ATM doesn't exist already.</TT><TT> 85:         if (!CORBAAlgorithms.contains(myATMs, atm)) {</TT><TT> 86: </TT><TT> 87:             // ATM was not found, so add the given ATM to the end of</TT><TT> 88:             // the list.</TT><TT> 89:             System.out.println(&quot;BankServerImpl: Registering ATM &quot;</TT><TT> 90:                     + &quot;\&quot;&quot; + atm.name() + &quot;\&quot;.&quot;);</TT><TT> 91:             myATMs.addElement(atm._duplicate());</TT><TT> 92:             return;</TT><TT> 93:         }</TT><TT> 94: </TT><TT> 95:         // The ATM was already registered, so throw an exception.</TT><TT> 96:         System.out.println(&quot;BankServerImpl: Attempted to &quot; +</TT><TT> 97:                 &quot;register duplicate ATM.&quot;);</TT><TT> 98:         throw new InvalidATMException();</TT><TT> 99:     }</TT><TT>100: </TT><TT>101:     public void unregisterATM(ATM atm) throws InvalidATMException {</TT><TT>102: </TT><TT>103: </TT><TT>104:         if (!CORBAAlgorithms.contains(myATMs, atm)) {</TT><TT>105: </TT><TT>106:             // Invalid ATM; throw an exception.</TT><TT>107:             System.out.println(&quot;BankServerImpl: Attempted to &quot; +</TT><TT>108:                     &quot;unregister invalid ATM.&quot;);</TT><TT>109:             throw new InvalidATMException();</TT><TT>110:         }</TT><TT>111: </TT><TT>112:         System.out.println(&quot;BankServerImpl: Unregistering ATM \&quot;&quot;</TT><TT>113:                 + atm.name() + &quot;\&quot;.&quot;);</TT><TT>114: </TT><TT>115:         // Delete the given ATM.</TT><TT>116:         CORBAAlgorithms.removeElement(myATMs, atm);</TT><TT>117:         atm._release();</TT><TT>118:     }</TT><TT>119: </TT><TT>120:     public ATM[] getATMs() {</TT><TT>121: </TT><TT>122:         ATM[] list = new ATM[myATMs.size()];</TT><TT>123:         myATMs.copyInto(list);</TT><TT>124: </TT><TT>125:         Enumeration e = myATMs.elements();</TT><TT>126:         while (e.hasMoreElements()) {</TT><TT>127:             ((ATM)e.nextElement())._duplicate();</TT><TT>128:         }</TT><TT>129: </TT><TT>130:         return list;</TT><TT>131:     }132: }</TT></FONT></PRE><H4><FONT COLOR="#000077">Listing 13.2. BankServerMain.java.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT> 1: // BankServerMain.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: public class BankServerMain {</TT><TT> 7: </TT><TT> 8:     public static ORB myORB;</TT><TT> 9:     public static BOA myBOA;</TT><TT>10: </TT><TT>11:     public static void main(String[] args) {</TT><TT>12: </TT><TT>13:         try {</TT><TT>14: </TT><TT>15:             // Initialize the ORB and BOA.</TT><TT>16:             myORB = ORB.init(args, null);</TT><TT>17:             myBOA = myORB.BOA_init();</TT><TT>18: </TT><TT>19:             // Create a BankServerImpl object and register it with</TT><TT>20:             // the ORB.</TT><TT>21:             BankServerImpl bankServer = new</TT><TT>22:                     BankServerImpl(&quot;BankServer&quot;);</TT><TT>23:             myBOA.obj_is_ready(bankServer);</TT><TT>24: </TT><TT>25:             // Wait for CORBA events.</TT><TT>26:             System.out.println(&quot;BankServer ready.&quot;);</TT><TT>27:             myBOA.impl_is_ready();</TT><TT>28:         } catch (Exception ex) {</TT><TT>29: </TT><TT>30:             // Something failed...</TT><TT>31:             System.out.println(&quot;BankServerImpl: Unable to bind &quot; +</TT><TT>32:                     &quot;BankServer:&quot;);</TT><TT>33:             System.out.println(ex);</TT><TT>34:             return;</TT><TT>35:         }</TT><TT>36: </TT><TT>37:         // When this point is reached, the application is finished.</TT><TT>38:         return;</TT><TT>39:     }40: }</TT></FONT></PRE><H4><FONT COLOR="#000077">Listing 13.3. CORBAAlgorithms.java.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT> 1: // CORBAAlgorithms.java</TT><TT> 2: </TT><TT> 3: package util;</TT><TT> 4: </TT><TT> 5: import java.util.Enumeration;</TT><TT> 6: import java.util.Vector;</TT><TT> 7: </TT><TT> 8: public class CORBAAlgorithms {</TT><TT> 9: </TT><TT>10:     // Return true if the given Vector contains the given CORBA</TT><TT>11:     // object, that is, if there is an element in the Vector for</TT><TT>12:     // which obj._is_equivalent() returns true.</TT><TT>13:     public static boolean contains(Vector vector, org.omg.CORBA.</TT><TT>14:             Object obj) {</TT><TT>15: </TT><TT>16:         Enumeration e = vector.elements();</TT><TT>17: </TT><TT>18:         while (e.hasMoreElements()) {</TT><TT>19:             if (obj._is_equivalent((org.omg.CORBA.Object)e.</TT><TT>20:                     nextElement())) {</TT><TT>21: </TT><TT>22:                 // A match was found.</TT><TT>23:                 return true;</TT><TT>24:             }</TT><TT>25:         }</TT><TT>26: </TT><TT>27:         // A match was not found.</TT><TT>28:         return false;</TT><TT>29:     }</TT><TT>30: </TT><TT>31:     // Remove the first element from the given Vector for which the</TT><TT>32:     // obj._is_equivalent() returns true for that element. If no</TT><TT>33:     // such element is found, this method does nothing.</TT><TT>34:     public static void removeElement(Vector vector, org.omg.CORBA.</TT><TT>35:             Object obj) {</TT><TT>36: </TT><TT>37:         Enumeration e = vector.elements();</TT><TT>38: </TT><TT>39:         while (e.hasMoreElements()) {</TT><TT>40:             org.omg.CORBA.Object cobj = (org.omg.CORBA.Object)e.</TT><TT>41:                     nextElement();</TT><TT>42:             if (obj._is_equivalent(cobj)) {</TT><TT>43: </TT><TT>44:                 // A match was found; remove the element.</TT><TT>45:                 vector.removeElement(cobj);</TT><TT>46:                 return;</TT><TT>47:             }</TT><TT>48:         }</TT><TT>49:     }50: }</TT> </FONT></PRE><P>The logic of the <TT>BankServer</TT> implementation is identical to its C++ counterpart.Here are some highlights of the Java implementation.</P><P>The <TT>import</TT> statement in Java, as seen in lines 3-12 of Listing 13.1,is a distant cousin to C++'s <TT>#include</TT> preprocessor directive. The <TT>#include</TT>directive imports information about other classes into a source file; Java's <TT>import</TT>statement serves the same purpose. Note that the <TT>import</TT> statement uses the<I>fully qualified class name</I> of the class--that is, the class's package nameplus the class name itself. For instance, the <TT>Enumeration</TT> class from thepreceding listings is in the <TT>java.util</TT> package.</P><P>Note the <TT>import</TT>s of classes in the <TT>idlGlobal</TT> package (in lines6 and 7 of Listing 13.1). These are the classes generated by the IDL compiler inthe previous step.</P><P>Now notice the <TT>class</TT> declaration in line 14. In the Java language mappingfor IDL, implementation classes for CORBA interfaces extend a certain base class.For the <TT>BankServerImpl</TT> class, this base class is <TT>idlGlobal._BankServerImplBase</TT>.</P><P><TT>Vector</TT> (or more specifically, <TT>java.util.Vector</TT>) is a built-inJava class that provides the semantics of a resizable array. The <TT>BankServerImpl</TT>uses <TT>Vector</TT>s to store the <TT>Bank</TT>s and <TT>ATM</TT>s that registerwith the <TT>BankServer</TT>, as seen in lines 16-20.</P><P>Note that in the constructor for <TT>BankServerImpl</TT> (appearing in lines 22-28),a call is made to <TT>super()</TT>. This is a special method name denoting a constructorin the superclass (in this case, <TT>_BankServerImplBase</TT>) that accepts the givenarguments (or argument, in this case). Also, note that the <TT>BankServerImpl</TT>constructor creates the two <TT>Vector</TT>s declared earlier.</P><P>The remainder of the <TT>BankServerImpl</TT> implementation is self-explanatory.You'll now move on to <TT>BankServerMain.java</TT>, which creates a <TT>BankServerImpl</TT>and registers the object in the CORBA environment.</P><P>Now turn your attention to Listing 13.2. Note first the <TT>import</TT> statementsappearing in line 3 and 4. These <TT>import</TT> statements import two importantclasses in a CORBA application: the Basic Object Adapter (BOA) and the Object RequestBroker (ORB). In a moment, you'll see what they are used for.</P><P>You might have already guessed this, but static class members in Java (such asthose that appear in lines 8 and 9) behave like <TT>static</TT> members in C++: Theyare available independently of a particular instance of that class.</P><P>Every Java application must have a <TT>public static void main()</TT> in at leastone class (actually, every class can have a <TT>main()</TT> method, but only one

⌨️ 快捷键说明

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