📄 ch04.htm
字号:
either. Finally, you can see that the <TT>StockServer</TT> interface contains twomethods that correspond to the IDL methods in <TT>StockServer.idl</TT>. Note, however,that the IDL types have been mapped to their Java counterparts: <TT>StockSymbol</TT>,which was <TT>typedef</TT>'ed as an IDL <TT>string</TT>, maps to a Java <TT>String</TT>;<TT>StockSymbolList</TT>, which was a <TT>sequence</TT> of <TT>StockSymbol</TT>s,is mapped to a Java array of <TT>String</TT>s. The IDL <TT>float</TT>, not surprisingly,is mapped to a Java <TT>float</TT>.<H4><FONT COLOR="#000077">Writing the Implementation</FONT></H4><P>The implementation for the <TT>StockServer</TT> is straightforward. This sectionwalks you through the implementation (the example uses the class name <TT>StockServerImpl</TT>,but you can name the implementation class anything you want) line by line and explainswhat is happening at every step of the way.<H4><FONT COLOR="#000077">Listing 4.3. StockServerImpl.java.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT> 1: // StockServerImpl.java</TT><TT> 2: </TT><TT> 3: package StockMarket;</TT><TT> 4: </TT><TT> 5: import java.util.Vector;</TT><TT> 6: </TT><TT> 7: import org.omg.CORBA.ORB;</TT><TT> 8: import org.omg.CosNaming.NameComponent;</TT><TT> 9: import org.omg.CosNaming.NamingContext;</TT><TT> 10: import org.omg.CosNaming.NamingContextHelper;</TT><TT> 11: </TT><TT> 12: // StockServerImpl implements the StockServer IDL interface.</TT><TT> 13: public class StockServerImpl extends _StockServerImplBase implements</TT><TT> 14: StockServer {</TT><TT> 15: </TT><TT> 16: // Stock symbols and their respective values.</TT><TT> 17: private Vector myStockSymbols;</TT><TT> 18: private Vector myStockValues;</TT><TT> 19: </TT><TT> 20: // Characters from which StockSymbol names are built.</TT><TT> 21: private static char ourCharacters[] = { `A', `B', `C', `D', `E', `F',</TT><TT> 22: `G', `H', `I', `J', `K', `L', `M', `N', `O', `P', `Q', `R',</TT><TT> 23: `S', `T', `U', `V', `W', `X', `Y', `Z' };</TT><TT> 24: </TT><TT> 25: // Path name for StockServer objects.</TT><TT> 26: private static String ourPathName = "StockServer";</TT><TT> 27: </TT><TT> 28: // Create a new StockServerImpl.</TT><TT> 29: public StockServerImpl() {</TT><TT> 30: </TT><TT> 31: myStockSymbols = new Vector();</TT><TT> 32: myStockValues = new Vector();</TT><TT> 33: </TT><TT> 34: // Initialize the symbols and values with some random values.</TT><TT> 35: for (int i = 0; i < 10; i++) {</TT><TT> 36: </TT><TT> 37: // Generate a string of four random characters.</TT><TT> 38: StringBuffer stockSymbol = new StringBuffer(" ");</TT><TT> 39: for (int j = 0; j < 4; j++) {</TT><TT> 40: </TT><TT> 41: stockSymbol.setCharAt(j, ourCharacters[(int)(Math.random()</TT><TT> 42: * 26f)]);</TT><TT> 43: }</TT><TT> 44: </TT><TT> 45: myStockSymbols.addElement(stockSymbol.toString());</TT><TT> 46: </TT><TT> 47: // Give the stock a value between 0 and 100. In this example,</TT><TT> 48: // the stock will retain this value for the duration of the</TT><TT> 49: // application.</TT><TT> 50: myStockValues.addElement(new Float(Math.random() * 100f));</TT><TT> 51: }</TT><TT> 52: </TT><TT> 53: // Print out the stock symbols generated above.</TT><TT> 54: System.out.println("Generated stock symbols:");</TT><TT> 55: for (int i = 0; i < 10; i++) {</TT><TT> 56: System.out.println(" " + myStockSymbols.elementAt(i) + " " +</TT><TT> 57: myStockValues.elementAt(i));</TT><TT> 58: }</TT><TT> 59: System.out.println();</TT><TT> 60: }</TT><TT> 61: </TT><TT> 62: // Return the current value for the given StockSymbol.</TT><TT> 63: public float getStockValue(String symbol) {</TT><TT> 64: </TT><TT> 65: // Try to find the given symbol.</TT><TT> 66: int stockIndex = myStockSymbols.indexOf(symbol);</TT><TT> 67: if (stockIndex != -1) {</TT><TT> 68: </TT><TT> 69: // Symbol found; return its value.</TT><TT> 70: return ((Float)myStockValues.elementAt(stockIndex)).</TT><TT> 71: floatValue();</TT><TT> 72: } else {</TT><TT> 73: </TT><TT> 74: // Symbol was not found.</TT><TT> 75: return 0f;</TT><TT> 76: }</TT><TT> 77: }</TT><TT> 78: </TT><TT> 79: // Return a sequence of all StockSymbols known by this StockServer.</TT><TT> 80: public String[] getStockSymbols() {</TT><TT> 81: </TT><TT> 82: String[] symbols = new String[myStockSymbols.size()];</TT><TT> 83: myStockSymbols.copyInto(symbols);</TT><TT> 84: </TT><TT> 85: return symbols;</TT><TT> 86: }</TT><TT> 87: </TT><TT> 88: // Create and initialize a StockServer object.</TT><TT> 89: public static void main(String args[]) {</TT><TT> 90: </TT><TT> 91: try {</TT><TT> 92: </TT><TT> 93: // Initialize the ORB.</TT><TT> 94: ORB orb = ORB.init(args, null);</TT><TT> 95: </TT><TT> 96: // Create a StockServerImpl object and register it with the</TT><TT> 97: // ORB.</TT><TT> 98: StockServerImpl stockServer = new StockServerImpl();</TT><TT> 99: orb.connect(stockServer);</TT><TT>100: </TT><TT>101: // Get the root naming context.</TT><TT>102: org.omg.CORBA.Object obj = orb.</TT><TT>103: resolve_initial_references("NameService");</TT><TT>104: NamingContext namingContext = NamingContextHelper.narrow(obj);</TT><TT>105: </TT><TT>106: // Bind the StockServer object reference in the naming</TT><TT>107: // context.</TT><TT>108: NameComponent nameComponent = new NameComponent(ourPathName,</TT><TT>109: "");</TT><TT>110: NameComponent path[] = { nameComponent };</TT><TT>111: namingContext.rebind(path, stockServer);</TT><TT>112: </TT><TT>113: // Wait for invocations from clients.</TT><TT>114: java.lang.Object waitOnMe = new java.lang.Object();</TT><TT>115: synchronized (waitOnMe) {</TT><TT>116: waitOnMe.wait();</TT><TT>117: }</TT><TT>118: } catch (Exception ex) {</TT><TT>119: System.err.println("Couldn't bind StockServer: " + ex.</TT><TT>120: getMessage());</TT><TT>121: }</TT><TT>122: }</TT>123: } </FONT></PRE><P><BR>The entire listing for <TT>StockServerImpl.java</TT> appears in Listing 4.3; theremainder of this section will walk you through the file step by step, so that youcan see the details of what's going on.</P><PRE><FONT COLOR="#0066FF"><TT>package StockMarket;</TT></FONT></PRE><P>Because the <TT>StockServer</TT> interface is part of the <TT>StockMarket</TT>module, the IDL compiler places the Java class and interface definitions into the<TT>StockMarket</TT> package. For convenience, <TT>StockServerImpl</TT> is placedinto this package as well. (If you're not familiar with Java or with packages, youcan safely ignore this bit of code for now.)</P><PRE><FONT COLOR="#0066FF"><TT>import java.util.Vector;</TT></FONT></PRE><P><TT>StockServerImpl</TT> will make use of the <TT>Vector</TT> class. This <TT>import</TT>should look familiar to Java developers already. If you're not familiar with Java,the <TT>import</TT> statement behaves much like the <TT>#include</TT> preprocessordirective in C++; the <TT>java.util.Vector</TT> class is a container class that behavesas a growable array of elements.</P><PRE><FONT COLOR="#0066FF"><TT>import org.omg.CORBA.ORB;</TT><TT>import org.omg.CosNaming.NameComponent;</TT><TT>import org.omg.CosNaming.NamingContext;</TT><TT>import org.omg.CosNaming.NamingContextHelper;</TT></FONT></PRE><P>The classes being <TT>import</TT>ed here are commonly used in CORBA applications.The first, of course, is the class that provides the ORB functionality; the otherclasses are related to the CORBA Naming Service, which you'll explore further onDay 12.</P><PRE><FONT COLOR="#0066FF"><TT>// StockServerImpl implements the StockServer IDL interface.</TT><TT>public class StockServerImpl extends _StockServerImplBase implements</TT><TT> StockServer {</TT></FONT></PRE><P>Given the <TT>StockServer</TT> IDL interface, the IDL compiler generates a classcalled <TT>StockServerImplBase</TT> and an interface called <TT>StockServer</TT>.To implement the <TT>StockServer</TT> IDL interface, your <TT>StockServerImpl</TT>class must extend <TT>_StockServerImplBase</TT> and implement <TT>StockServer</TT>,which is exactly what is declared here:</P><PRE><FONT COLOR="#0066FF"><TT>// Stock symbols and their respective values.</TT><TT>private Vector myStockSymbols;</TT><TT>private Vector myStockValues;</TT></FONT></PRE><P><TT>StockServerImpl</TT> uses <TT>Vector</TT>s to store the stock symbols andtheir values.</P><PRE><FONT COLOR="#0066FF"><TT>// Characters from which StockSymbol names are built.</TT><TT>private static char ourCharacters[] = { `A', `B', `C', `D', `E', `F',</TT><TT>`G', `H', `I', `J', `K', `L', `M', `N', `O', `P', `Q', `R',</TT><TT>`S', `T', `U', `V', `W', `X', `Y', `Z' };</TT></FONT></PRE><P>The <TT>ourCharacters</TT> array contains the set of characters from which stocksymbols are built.</P><PRE><FONT COLOR="#0066FF"><TT>// Path name for StockServer objects.</TT><TT>private static String ourPathName = "StockServer";</TT></FONT></PRE><P>The <TT>ourPathName</TT> variable stores the pathname by which this <TT>StockServer</TT>object can be located in the Naming Service. This can be any name, but for the purposesof this example, <TT>StockServer</TT> works well.</P><PRE><FONT COLOR="#0066FF"><TT>// Create a new StockServerImpl.</TT><TT>public StockServerImpl() {</TT><TT>myStockSymbols = new Vector();</TT><TT>myStockValues = new Vector();</TT></FONT></PRE><P>Although constructors aren't a part of an IDL interface, the class implementingthat interface will still have constructors so that the server can create the implementationobjects. <TT>StockServerImpl</TT> has only a default constructor, but like any otherclass, a class that implements an IDL interface can have any number of constructors.</P><P>This part of the constructor creates <TT>Vector</TT>s to hold the stock symbolsand their respective values.</P><PRE><FONT COLOR="#0066FF"><TT>// Initialize the symbols and values with some random values.</TT><TT>for (int i = 0; i < 10; i++) {</TT></FONT></PRE><P>Rather arbitrarily, the <TT>StockServerImpl</TT> creates ten stock symbols.</P><PRE><FONT COLOR="#0066FF"><TT>// Generate a string of four random characters.</TT><TT>StringBuffer stockSymbol = new StringBuffer(" ");</TT><TT>for (int j = 0; j < 4; j++) {</TT><TT> stockSymbol.setCharAt(j, ourCharacters[(int)(Math.random()</TT><TT> * 26f)]);</TT><TT>}</TT><TT>myStockSymbols.addElement(stockSymbol.toString());</TT></FONT></PRE><P>For each stock symbol, the <TT>StockServerImpl</TT> creates a string of four randomcharacters (chosen from the preceding <TT>ourCharacters</TT> array). The four-characterlength, like the number of symbols, was chosen arbitrarily. For the sake of simplicity,no checks are made for duplicate strings.</P><PRE><FONT COLOR="#0066FF"><TT>// Give the stock a value between 0 and 100. In this example,</TT><TT>// the stock will retain this value for the duration of the</TT><TT>/ application.</TT><TT>myStockValues.addElement(new Float(Math.random() * 100f));</TT><TT>}</TT></FONT></PRE><P>Here, a random value between 0 and 100 is given to each stock symbol. In thisexample, the stock will retain the assigned value for as long as the <TT>StockServerImpl</TT>runs.</P><PRE><FONT COLOR="#0066FF"><TT>// Print out the stock symbols generated above.</TT><TT>System.out.println("Generated stock symbols:");</TT><TT>for (int i = 0; i < 10; i++) {</TT><TT> System.out.println(" " + myStockSymbols.elementAt(i) + " " +</TT><TT> myStockValues.elementAt(i));</TT><TT>}</TT><TT> System.out.println();</TT><TT>}</TT></FONT></PRE><P>Finally, the constructor prints out the stock symbols and their values.</P><PRE><FONT COLOR="#0066FF"><TT>// Return the current value for the given StockSymbol.</TT><TT>public float getStockValue(String symbol) {</TT><TT> // Try to find the given symbol.</TT><TT> int stockIndex = myStockSymbols.indexOf(symbol);</TT><TT> if (stockIndex != -1) {</TT><TT> // Symbol found; return its value.</TT><TT> return ((Float)myStockValues.elementAt(stockIndex)).</TT><TT> floatValue();</TT><TT> } else {</TT><TT> // Symbol was not found.</TT><TT> return 0f;</TT><TT> }</TT>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -