Spec-Zone .ru
спецификации, руководства, описания, API
|
This page contains:
Instructions for compiling and running
the example are provided, and source code is also included.
Interface Definition (
Hello.idl
)
The following example OMG IDL describes a CORBA object whose single sayHello() operation returns a string.
module HelloApp { interface Hello { string sayHello(); }; };
Compile the IDL interface with the command:
idltojava Hello.idlThis generates five files in a
HelloApp
subdirectory:
HelloServer.java
and HelloClient.java
.
HelloServer.java
)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:
// 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); } } }
HelloClient.java
)The example application client that follows:
// 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); } } }
HelloApplet.java
)The example applet client that follows:
// 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 Hello World example is located in the examples/hello directory. The following instructions assume you can use port 1050 for the Java IDL name server. Substitute a different port if necessary. Note that for ports below 1024, you need root access on UNIX machines, and administrator privileges on Windows95 and NT. Note also that the instructions use a slash (/) for path names. Windows95 and NT users should subtitute a backslash (\).
NOTE: You must have downloaded the idltojava
compiler before you can build and run Hello World. It is available
free from the JDC (Java Developer Connection) web page. If you are not
already a member, you must register with the JDC to access this page,
but there is no cost. The download page is
http://developer.java.sun.com/developer/earlyAccess/jdk12/idltojava.htmlYou will get a file with the extension (".bin"). Simply run this file (type the complete filename at the command line) to unbundle its contents . You cannot use the
idltojava
compiler until after you have
unbundled it.
cd docs/guide/idl/examples/hello
idltojava Hello.idl
javac *.java HelloApp/*.java
tnameserv -ORBInitialPort 1050&
java HelloServer -ORBInitialPort 1050
java HelloClient -ORBInitialPort 1050
Home |
Copyright © 1995-98