Building a CORBA Application – Part 3
Explanation (Listing 1.2):
package StockMarket;
Because the StockServer interface is part of the StockMarket module, the IDL compiler places the Java class and interface definitions into the StockMarket package. For convenience, StockServerImpl is placed into this package as well.
import java.util.Vector;
StockServerImpl will make use of the Vector class. This import should look familiar to Java developers already. The import statement behaves much like the #include preprocessor directive in C++; the java.util.Vector class is a container class that behaves as a growable array of elements.
import org.omg.CORBA.ORB;
import org.omg.CosNaming.NameComponent;
import org.omg.CosNaming.NamingContext;
import org.omg.CosNaming.NamingContextHelper;
The classes being imported here are commonly used in CORBA applications. The first, of course, is the class that provides the ORB functionality; the other classes are related to the CORBA Naming Service.
// StockServerImpl implements the StockServer IDL interface.
public class StockServerImpl extends _StockServerImplBase implements StockServer {
Given the StockServer IDL interface, the IDL compiler generates a class called StockServerImplBase and an interface called StockServer. To implement the StockServer IDL interface, the StockServerImpl class must extend _StockServerImplBase and implement StockServer, which is exactly what is declared here:
// Stock symbols and their respective values.
private Vector myStockSymbols;
private Vector myStockValues;
StockServerImpl uses Vectors to store the stock symbols and their values.
// Characters from which StockSymbol names are built.
private static char ourCharacters[] = { `A’, `B’, `C’, `D’, `E’, `F’, `G’, `H’, `I’, `J’, `K’, `L’, `M’, `N’, `O’, `P’, `Q’, `R’, `S’, `T’, `U’, `V’, `W’, `X’, `Y’, `Z’ };
The ourCharacters array contains the set of characters from which stock symbols are built.
// Path name for StockServer objects.
private static String ourPathName = “StockServer”;
The ourPathName variable stores the pathname by which this StockServer object can be located in the Naming Service. This can be any name.
// Create a new StockServerImpl.
public StockServerImpl() {
myStockSymbols = new Vector();
myStockValues = new Vector();
Although constructors aren’t a part of an IDL interface, the class implementing that interface will still have constructors so that the server can create the implementation objects. StockServerImpl has only a default constructor, but like any other class, a class that implements an IDL interface can have any number of constructors. This part of the constructor creates Vectors to hold the stock symbols and their respective values.
// Initialize the symbols and values with some random values.
for (int i = 0; i < 10; i++) {
Rather arbitrarily, the StockServerImpl creates ten stock symbols.
// Generate a string of four random characters.
StringBuffer stockSymbol = new StringBuffer(” “);
for (int j = 0; j < 4; j++) {
stockSymbol.setCharAt(j, ourCharacters[(int)(Math.random()* 26f)]);
}
myStockSymbols.addElement(stockSymbol.toString());
For each stock symbol, the StockServerImpl creates a string of four random characters (chosen from the preceding ourCharacters array). The four-character length, like the number of symbols, was chosen arbitrarily. For the sake of simplicity, no checks are made for duplicate strings.
// Give the stock a value between 0 and 100. In this example,
// the stock will retain this value for the duration of the application.
myStockValues.addElement(new Float(Math.random() * 100f));
}
Here, a random value between 0 and 100 is given to each stock symbol. In this example, the stock will retain the assigned value for as long as the StockServerImpl runs.
// Print out the stock symbols generated above.
System.out.println(”Generated stock symbols:”);
for (int i = 0; i < 10; i++) {
System.out.println(” ” + myStockSymbols.elementAt(i) + ” ” +
myStockValues.elementAt(i));
}
System.out.println();
}
Finally, the constructor prints out the stock symbols and their values.
// Return the current value for the given StockSymbol.
public float getStockValue(String symbol) {
// Try to find the given symbol.
int stockIndex = myStockSymbols.indexOf(symbol);
if (stockIndex != -1) {
// Symbol found; return its value.
return ((Float)myStockValues.elementAt(stockIndex)).
floatValue();
} else {
// Symbol was not found.
return 0f;
}
}
The getStockValue() method takes a String, attempts to find a match in the myStockSymbols data member, and returns the value for the stock symbol (if found). If the stock symbol is not found, a zero value is returned.
// Return a sequence of all StockSymbols known by this StockServer.
public String[] getStockSymbols() {
String[] symbols = new String[myStockSymbols.size()];
myStockSymbols.copyInto(symbols);
return symbols;
}
The getStockSymbols() method simply creates an array of Strings, copies the stock symbols (contained in myStockSymbols) into the array, and returns the array.
// Create and initialize a StockServer object.
public static void main(String args[]) {
The main() method in StockServerImpl creates a StockServerImpl object, binds that object to a naming context, and then waits for clients to call methods on that object.
try {
Because the methods that main() will later call might throw exceptions, those calls are wrapped in a try … catch block.
// Initialize the ORB.
ORB orb = ORB.init(args, null);
Before doing anything with the ORB, the server application must first initialize the ORB.
// Create a StockServerImpl object and register it with the ORB.
StockServerImpl stockServer = new StockServerImpl();
orb.connect(stockServer);
Here a new StockServerImpl object is created and registered with the ORB.
// Get the root naming context.
org.omg.CORBA.Object obj = orb.
resolve_initial_references(”NameService”);
NamingContext namingContext = NamingContextHelper.narrow(obj);
Now for a little black magic. The CORBA Naming Service is a service that allows CORBA objects to register by name and subsequently be located, using that name, by other CORBA objects. In order for clients to connect to the StockServerImpl, they must have some way of locating the service on the network. One way to accomplish this is through the CORBA Naming Service. Here, a NamingContext object is located by resolving a reference to an object named NameService.
// Bind the StockServer object reference in the naming
// context.
NameComponent nameComponent = new NameComponent(ourPathName, “”);
NameComponent path[] = { nameComponent };
namingContext.rebind(path, stockServer);
Now the NamingContext object is asked to bind the StockServerImpl object to the pathname defined earlier (StockServer). Clients can now query the Naming Service for an object by this name; the Naming Service will return a reference to this StockServerImpl object.
// Wait for invocations from clients.
java.lang.Object waitOnMe = new java.lang.Object();
synchronized (waitOnMe) {
waitOnMe.wait();
}
}
Because the StockServerImpl object is now registered with the Naming Service, the only thing left to do is to wait for clients to invoke methods on the object. Because the actual handling of these method invocations occurs in a separate thread, the main() thread simply needs to wait indefinitely.
catch (Exception ex) {
System.err.println(”Couldn’t bind StockServer: ” + ex.
getMessage());
}
}
}
If any exceptions are thrown by any of the methods called, they are caught and handled here.
Compiling and Running the Server
Compiling the server application is simple. Issue the command
javac StockMarket\StockServerImpl.java
Tip: Before compiling the server, make sure that your CLASSPATH contains the appropriate directory or file for the CORBA classes. For Sun’s JavaIDL package, the file (directory where JavaIDL is installed) /lib/classes.zip will appear in the CLASSPATH. Consult your CORBA product’s documentation to determine your CLASSPATH setting.
The exact method for running the Name Server varies from product to product, but the end result is the same. For Sun’s JavaIDL, simply running nameserv will bring up the Name Server.
When the Name Server is running, you’re ready to run the server. You can invoke the server with the command
java StockMarket.StockServerImpl
If everything works correctly, see output similar to Listing 1.4 will be produced.
Listing 1.4. Sample StockServer output.
Generated stock symbols:
PTLF 72.00064
SWPK 37.671585
CHHL 78.37782
JTUX 75.715645
HUPB 41.85024
OHQR 14.932466
YOEX 64.3376
UIBP 75.80115
SIPR 91.13683
XSTD 16.010124