This page contains a collection of minimal working examples:
![]() | A transient server that creates an object and registers it in
a naming context
![]() An application client that, knowing the object's name,
retrieves a reference for it from the naming context, and invokes
it.
| ![]() An applet that does the same.
| |
Instructions for compiling and running the examples, whose source code is included in the Java IDL release, are also provided.
The example IDL shown below describes a CORBA object whose single sayHello() operation returns a string.
module HelloApp { interface hello { string sayHello(); }; };
The example server consists of two classes, the servant and the server. The servant, helloServant, is the implementation of the hello IDL interface; each hello instance is implemented by a helloServant instance. The servant is a subclass of _helloImplBase, which is generated by the idltojava compiler from the example IDL. The servant contains one method for each IDL operation, in this example, just the sayHello() method. Servant methods are just like ordinary Java methods; the extra code to deal with the ORB, with marshaling arguments and results, and so on, is provided by the server and the stubs.
The server class has the server's main() method, which:
![]() | creates an ORB instance
![]() creates a servant instance (the implementation of one CORBA
hello object) and tells the ORB about it
| ![]() gets a CORBA object reference for a naming context in which to
register the new CORBA object
| ![]() registers the new object in the naming context under the name "Hello"
| ![]() waits for invocations of the new object
| |
// Copyright and License import HelloApp.*; import org.omg.CosNaming.*; import org.omg.CosNaming.NamingContextPackage.*; import org.omg.CORBA.*; class helloServant extends _helloImplBase { public String sayHello() { return "\nHello world !!\n"; } } public class helloServer { public static void main(String args[]) { try{ // create and initialize the ORB ORB orb = ORB.init(args, null); // create servant and register it with the ORB helloServant helloRef = new helloServant(); orb.connect(helloRef); // get the root naming context org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService"); NamingContext ncRef = NamingContextHelper.narrow(objRef); // bind the Object Reference in Naming NameComponent nc = new NameComponent("Hello", ""); NameComponent path[] = {nc}; ncRef.rebind(path, helloRef); // wait for invocations from clients java.lang.Object sync = new java.lang.Object(); synchronized (sync) { sync.wait(); } } catch (Exception e) { System.err.println("ERROR: " + e); e.printStackTrace(System.out); } } }
The example application client below:
![]() | creates an ORB
![]() obtains a reference to the naming context
| ![]() looks up "Hello" in the naming context and receives a reference to that CORBA object
| ![]() invokes the object's sayHello() operation and prints
the result
| |
// Copyright and License import HelloApp.*; import org.omg.CosNaming.*; import org.omg.CORBA.*; public class helloClient { public static void main(String args[]) { try{ // create and initialize the ORB ORB orb = ORB.init(args, null); // get the root naming context org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService"); NamingContext ncRef = NamingContextHelper.narrow(objRef); // resolve the Object Reference in Naming NameComponent nc = new NameComponent("Hello", ""); NameComponent path[] = {nc}; hello helloRef = helloHelper.narrow(ncRef.resolve(path)); // call the hello server object and print results String hello = helloRef.sayHello(); System.out.println(hello); } catch (Exception e) { System.out.println("ERROR : " + e) ; e.printStackTrace(System.out); } } }
The example applet client below:
![]() | creates an ORB
![]() obtains a reference to the naming context
| ![]() looks up "Hello" in the naming context and receives a reference to that CORBA object
| ![]() invokes the object's sayHello() operation and prints
the result
| |
// Copyright and License import HelloApp.*; import org.omg.CosNaming.*; import org.omg.CosNaming.NamingContextPackage.*; import org.omg.CORBA.*; import java.awt.Graphics; public class helloApplet extends java.applet.Applet { public void init() { try { // create and initialize the ORB ORB orb = ORB.init(this, null); // get the root naming context org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService"); NamingContext ncRef = NamingContextHelper.narrow(objRef); // resolve the Object Reference in Naming NameComponent nc = new NameComponent("Hello", ""); NameComponent path[] = {nc}; hello helloRef = helloHelper.narrow(ncRef.resolve(path)); // call the hello server object and print results message = helloRef.sayHello(); } catch (Exception e) { System.out.println("HelloApplet exception: " + e.getMessage()); e.printStackTrace(System.out); } } public void paint(Graphics g) { g.drawString(message, 25, 50); } String message = ""; }
The source code for the examples is located in ${JAVAIDL_HOME}/examples/hello, where JAVAIDL_HOME is the directory in which Java IDL is installed. The following instructions assume you can use port 1050 for the Java IDL name server. Substitute a different port if necessary.
![]() | cd to the example directory:
cd ${JAVAIDL_HOME}/examples/hello ![]() Compile the IDL file with the idltojava compiler to
create stubs and skeletons:
| idltojava -fclient -fserver hello.idl ![]() Compile the Java files, including the stubs and skeletons:
| javac -classpath \ ${JAVA_HOME}/lib/classes.zip:${JAVAIDL_HOME}/lib/classes.zip:. \ *.java HelloApp/*.java where JAVA_HOME and JAVAIDL_HOME are where the JDK and Java IDL are installed, respectively. ![]() Start the Java IDL name server:
| cd ${JAVAIDL_HOME}/bin nameserv -ORBInitialPort 1050 ![]() Start the Hello server:
| java -classpath \ ${JAVA_HOME}/lib/classes.zip:${JAVAIDL_HOME}/lib/classes.zip:. \ helloServer -ORBInitialPort 1050 ![]() Run the Hello application client:
| java -classpath \ ${JAVA_HOME}/lib/classes.zip:${JAVAIDL_HOME}/lib/classes.zip:. \ helloClient -ORBInitialPort 1050
Copyright © 1996,1997 Sun Microsystems, Inc., 2550 Garcia Ave., Mtn. View, CA. 94043-1100 USA., All rights reserved. |