Running the Hello World ApplicationThis topic walks you through running the server and client program that together make up the "Hello World" application or applet. Running the Hello World Application Despite its simple design, the Hello World program lets you learn and experiment with all the tasks required to develop almost any CORBA program that uses static invocation. To run this client-server application on your development machine: 1. Start the Java IDL Name Server. To do this from a UNIX command shell, enter:
tnameserv -ORBInitialPort 1050&
From an MS-DOS system prompt (Windows), enter:
start tnameserv -ORBInitialPort 1050
In this example, port 1050 has been chosen for the naming service. You can change this to a different value if port 1050 is occupied on your system. If the nameserverport is not specified, port 900 will be chosen by default. When using Solaris software, you must become root to start a process on a port under 1024. For this reason, we recommend that you use a port number greater than or equal to 1024. 2. From a second prompt or shell, start the Hello server: On Unix:
java HelloServer -ORBInitialPort 1050&
On Windows:
start java HelloServer -ORBInitialPort 1050
You can leave out -ORBInitialPort nameserverport if the name server is running on the default port. If the Hello server is running on a different host (machine) than the naming service, you would need to specify where the naming service is running when you start the server. To do this, you would use the -ORBInitialHost nameserverhost argument. 3. From a third prompt or shell, run the Hello application client:
java HelloClient -ORBInitialPort 1050
You can leave out -ORBInitialPort nameserverport if the name server is running on the default port. If the Hello client is running on a different host (machine) than the naming service, you would need to specify where the naming service is running when you start the client. To do this, you would use the -ORBInitialHost nameserverhost argument. 4. The client prints the string from the server to the command line:
Hello world!!
The name server and the Hello World server, like many CORBA servers, run until you explicitly stop them. To avoid having many servers running, kill the server processes after the client application returns successfully. Running the Hello World Applet You can run the applet from either a Java-enabled Web browser (with JRE 1.3) or from the Applet Viewer. In either case, you must run the Name Server and the HelloServer prior to invoking the applet. To run the applet, 1. From an MS-DOS system prompt (Windows) or command shell (UNIX), start the Java IDL name server: On Unix:
tnameserv -ORBInitialPort 1050&
On Windows: start tnameserv -ORBInitialPort 1050 In this example, the nameserverport, which is the port on which you want the name server to run, is set to 1050. If you do not specify this, port 900 will be chosen by default. Also note that using Solaris software, you must become root to start a process on a port under 1024. For this reason, we recommend that you use a port number greater than or equal to 1024. 2. Start the Hello server: On Unix:
java HelloServer -ORBInitialPort 1050 &
On Windows:
start java HelloServer -ORBInitialPort 1050
In this example, the ORBInitialPort, which is the port on which you want the name server to run, is set to 1050. If you do not specify this, port 900 will be chosen by default. Also note that using Solaris software, you must become root to start a process on a port under 1024. For this reason, we recommend that you use a port number greater than or equal to 1024. 3. Run the applet. To run the applet from the appletviewer, 1. Change to the applet directory, Hello. 2. Start the appletviewer and browse Tutorial.html by typing:
<path_to_appletviewer_executable>appletviewer Tutorial.html
Hello world!! prints to the appletviewer frame. To run the applet from a Web browser, make sure you have the Java 2 Runtime Environment, Standard Edition, version 1.3.0, which includes the Java Plugin 1.3, and the latest version of your browser, preferably Netscape 6, then: 3. Open the Web browser. 4. Open the file Tutorial.html.
Hello world!! prints in the browser frame.
If the applet does not run in the Web browser, make sure you have the Java 2 Runtime Environment, Standard Edition, version 1.3.0, which includes the Java Plugin 1.3. This plugin is needed to run the applet and may not be present in older Web browsers. The name server and the Hello World server, like many CORBA servers, run until you explicitly stop them. To avoid having many servers running, kill the server processes after the client application returns successfully. Troubleshooting Security violation: method verification error Make sure you have the Java 2 Runtime Environment, Standard Edition, version 1.3.0, which includes the Java Plugin 1.3. This plugin is needed to run the applet and may not be present in older Web browsers. It can be downloaded from http://java.sun.com/products/plugin/index.html. Specifying ORB Initial Port The default ORB Initial Port is port 900. If you prefer, you can omit the port specifications if you start the name server on port 900. Using Solaris software, you must become root to start a process on a port under 1024. Remember to exit from root access before continuing with the tutorial if you choose this port for your name server. 8. Using Stringified Object References To invoke an operation on a CORBA object, a client application needs a reference to the object. You can get such references in a number of ways, such as calling ORB.resolve_initial_references() or using another CORBA object (like the name service). In previous sections of this tutorial, you used both of these methods to get an initial object reference. Often, however, there is no naming service available in the distributed environment. In that situation, CORBA clients use a stringified object reference to find their first object. In this lesson, you will learn how to create a stringified object reference as a part of the server startup, and how the client gets that reference and destringifies it for use as a real object reference. The steps in this lesson are: 1. Making a Stringified Object Reference 2. Getting a Stringified Object Reference 3. Destringifying the Object Reference 4. Compiling and Running a Stringified Hello World To see completed versions of the source code, follow the links to HelloServer.java and HelloClient.java. Making a Stringified Object Reference For a stringified object reference to be available to the client, the server must create it and store it somewhere that the client can access. Your reference will be written to disk in the form of a text file. 1. Create a new directory at the same level as the Hello directory named HelloString. Copy Hello.idl to this directory. 2. Copy HelloServer.java from the Hello directory to the HelloString directory. Name it HelloServerString.java, and make the following changes in this file. 3. Because the new server will write a file to disk, you need to add an import statement. Add the following:
import java.io.*; // needed for output to the file system.
4. The new server won't use the naming service, so you don't need the CosNaming packages. Delete these lines from the code:
import org.omg.CosNaming.*; // not needed for stringified version
import org.omg.CosNaming.NamingContextPackage.*; // remove from code 5. Delete the code that gets the initial naming context and resolves the reference to a Hello object:
// 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); 6. Call the ORB's object_to_string() method and pass it the reference to the servant object. This returns the object reference in a string form that can be saved in a file on disk.
String ior = orb.object_to_string(helloRef);
7. Build the path to the file that will be stored, using system properties to determine the path structure and syntax.
String filename = System.getProperty("user.home")+
System.getProperty("file.separator")+"HelloIOR"; 8. Use standard Java operations to write the stringified ior to disk:
FileOutputStream fos = new FileOutputStream(filename);
PrintStream ps = new PrintStream(fos); ps.print(ior); ps.close(); 9. Save and close HelloServerString.java. When HelloServerString runs, instead of calling the ORB and registering the servant object with naming, it creates the text file HelloIOR containing a stringified reference to the servant. The file is stored in your home directory. Getting a Stringified Object Reference Note to Windows users: You should substitute backslashes (\) for the slashes (/) in all paths in this document. 1. Copy HelloClient.java from the Hello directory to the HelloString directory. Name it HelloClientString.java, and make the following changes in this file. 2. Because the new client will read a file from the disk, you need to change the import statements. Add the following:
import java.io.*; // needed for input from the file system.
3. The new client won't use the naming service, so you don't need the CosNaming package. Delete this line from the code:
import org.omg.CosNaming;* // not needed for stringified version
4. Delete the code that gets the initial naming context and registers the servant with the naming service:
// 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)); 5. Use standard Java operations to read the file that has the object reference. Note that client and server programs must know the name of the file and where it is stored.
String filename = System.getProperty("user.home")+
System.getProperty("file.separator")+"HelloIOR"; FileInputStream fis = new FileInputStream(filename); DataInputStream dis = new DataInputStream(fis); String ior = dis.readLine(); The HelloClientString application now has a String object containing the stringified object reference. Destringifying the Object Reference To destringify the object reference in ior, call the standard ORB method:
org.omg.CORBA.Object obj = orb.string_to_object(ior);
Finally, narrow the CORBA object to its proper type, so that the client can invoke on it:
Hello helloRef = HelloHelper.narrow(obj);
The rest of the client code stays the same. Save and close HelloClientString.java. Compiling and Running a Stringified Hello World Compiling and running the new version of Hello World requires most of the same steps as for the naming service version. Compiling Hello World 1. Change to the HelloString directory. 2. Compile the IDL file. Enter the compiler command:
idlj -fall Hello.idl
3. Run the Java compiler on your source code:
javac *.java
4. Correct any errors in your files and recompile if necessary. (You can copy the files from HelloServer.java and HelloClient.java if you have trouble finding any typographical errors.) HelloServerString.class, HelloServantString.class, and HelloClientString.class are generated to the HelloString directory. Running Hello World To be certain that you are running your own server, check that all Hello server and name server processes have been stopped. Stop them if they are running. 1. From an MS-DOS system prompt (Windows) or command shell (UNIX), start the Hello server with the stringified object reference:
java HelloServerString -ORBInitialPort 1050 &
2. From another prompt or shell, run the Hello application client with the stringified object reference:
java HelloServerString -ORBInitialPort 1050 &
3. The client prints the string from the server to the command line:
Hello world!!
For More Information Initialization: Obtaining Initial Object References Explains the various ways of getting an initial object reference 9. The "Hello World" Example on 2 machines To enable the Hello World Tutorial to run on two machines, follow the steps as directed in the tutorial, with the following changes. This tutorial was written for the Java (tm) 2 Platform, Standard Edition (J2Se(tm)), version 1.3. In this example, the client, stubs, and skeletons are located on the client machine, and the server and name server are located on the server machine. This scenario can be changed to meet your needs and is provided simply as an introduction to one way this can be accomplished. 1. Create and compile the Hello.idl file on the client machine as indicated in the tutorial:
idlj -fall Hello.idl
2. Create HelloClient.java on the client machine. Compile the .java files, including the stubs and skeletons (which are in the directory HelloApp):
javac *.java HelloApp/*.java
3. Create HelloServer.java on the server machine. Compile the .java files:
javac *.java
4. Start the Java IDL name server on the server machine. To do this on Unix:
tnameserv -ORBInitialPort 1050&
To do this on Windows:
start tnameserv -ORBInitialPort 1050
Note that the name server will run on port 1050 if you enter the command as listed. If you want a different nameserverport, replace 1050 with the correct port number. 5. On the server machine, start the Hello server, as follows:
java HelloServer -ORBInitialPort 1050
If you used a different nameserverport, replace 1050 with the correct port number. 6. On the client machine, run the Hello application client. From a DOS prompt or shell, type:
java HelloClient -ORBInitialHost namerserverhost -ORBInitialPort 1050
Note that nameserverhost is the host on which the IDL name server is running. In this case, it is the server machine. If you used a different nameserverport, replace 1050 with the correct port number. 7. Kill or stop the server process when finished. |