import calculator.*; import org.omg.CORBA.*; import org.omg.CosNaming.*; import org.omg.PortableServer.*; import org.omg.CosNaming.NamingContextPackage.*; // This class implements a very simple CORBA server. // It provides add, sub, and mult methods that // clients may invoke. // // This Server requires that a CORBA Name Service be running. // // Written by: Stuart Hansen // Date: February 2, 2009 public class CalculatorServer extends CalculatorPOA{ private ORB orb; // the orb for this server private POA rootpoa; // the root POA for this server // The constructor sets up the ORB for communication public CalculatorServer (String [] args) { setUpServerORB(args); } // implement the add method public int add(int a, int b) { return a + b; } // implement the sub method public int sub(int a, int b) { return a - b; } // implement the mult method public int mult(int a, int b) { return a * b; } // This method setups the Server ORB public void setUpServerORB(String [] args) { try{ // create and initialize the ORB orb = ORB.init(args, null); // get reference to rootpoa & activate the POAManager rootpoa = POAHelper.narrow( orb.resolve_initial_references("RootPOA")); rootpoa.the_POAManager().activate(); // Convert our server to a CORBA object and IOR org.omg.CORBA.Object ref = rootpoa.servant_to_reference(this); Calculator calc = CalculatorHelper.narrow(ref); // Look up the Name Service org.omg.CORBA.Object nameServiceObj = orb.resolve_initial_references("NameService"); NamingContextExt nameService = NamingContextExtHelper.narrow(nameServiceObj); // Bind (register) the Calculator Server with the Name Service String name = "CalculatorServer"; NameComponent path[] = nameService.to_name( name ); nameService.rebind(path, calc); System.out.println("Calculator Server Ready"); // wait for client requests orb.run(); } catch (Exception e) { System.err.println("ERROR: " + e); e.printStackTrace(System.out); } } // The main program simply creates a new Calculator Server public static void main(String [] args) { new CalculatorServer(args); } }