Java compiler scripts  Creating an applet client

Chapter 2: Creating CORBA Java Components and Clients

Creating the application

To create and run the sample application:

  1. Start EAServer and EAServer Manager.

  2. Define a package, component, and method.

  3. Generate stubs and skeletons.

  4. Write the server-side code.

  5. Create the client program.

  6. Run the client program.


Start EAServer and EAServer Manager

StepsStarting EAServer

  1. If EAServer is not already running, follow the instructions under “Starting the server” to start the server.

StepsStarting EAServer Manager

  1. If EAServer Manager is not already running, start it as described in “Using EAServer Manager”.


Define a package, component, and method

This section shows you how to use EAServer Manager to create the package, component, and method for the sample application.

For complete information on creating packages, components, and methods, see Chapter 5, “Defining Component Interfaces,” in the EAServer Programmer’s Guide.


Define a new package

In EAServer, a package is a unit of deployment for a group of components that perform related tasks. Before a component can be instantiated by clients, it must be installed in a package, and that package must be installed in the server. The steps below create the package and component within the predefined “Jaguar” server to satisfy these requirements.

StepsCreating the Tutorial package if it does not exist

  1. In EAServer Manager, expand the servers folder, then expand the Jaguar server icon.

  2. Expand the Installed Packages folder. If the Tutorial package is displayed, skip to “Define and install a new component”.

  3. Highlight the Installed Packages folder, and select File | Install Package.

    In the Package wizard, select Create and Install a New Package.

    For the package name, enter Tutorial.

  4. Click Create New Package.

    You see the Package Properties window.

  5. Click OK.


Define and install a new component

StepsDefining the new component

  1. Click the Tutorial package.

  2. Select File | New Component.

  3. In the Define New Component wizard, select Define New Component and click Next.

  4. For the component name, enter JavaArithmetic.

  5. Click Finish. You see the Component Properties window.

  6. Select the General tab. Fill in the fields as follows:

    Field

    Value

    Description

    Tutorial Java component

    Component Type

    Java - CORBA

    Java Class

    Sample.Intro.JavaArithmetic.JavaArithmeticImpl

  7. Leave the remaining fields at their default settings.

  8. Click OK.


Define the multiply method

The component interface will have one method, multiply.

StepsDefining the component interface

  1. Expand the Tutorial package. Double-click the JavaArithmetic component to show the Roles and Interfaces folders beneath it.

  2. Double-click the Interfaces folder, and highlight the Tutorial::JavaArithmetic interface. If you do not see this interface, install it as follows:

    1. Highlight the Interfaces folder and select File | Add Interfaces ...

    2. In the Install Interface dialog box, highlight Tutorial::JavaArithmetic in the Selected to Install table, then click Install.

    3. Highlight the Tutorial::JavaArithmetic interface that is now displayed under the Interfaces folder.

  3. Select File | New Method.

  4. Assign the name multiply to the method.

  5. Click Create New Method.

    You see the Method Properties window.

  6. In the Return field, select double as the method’s return type.

  7. Beneath the empty parameter list, click Add to add a parameter. In the New Parameter dialog:

  8. Click OK to close the New Parameter dialog box.

  9. Repeat steps 7 and 8 to add a second parameter named m2 with a Type of double.

  10. Click OK to close the Method Properties dialog box.


Generate stubs and skeletons

Once you have created the package, component, and methods, you generate the stub and skeleton files for the component. The client-side application uses the stubs to invoke the server-side component methods. The skeleton acts as an interface between EAServer and your component methods.

StepsGenerating the stub and skeleton files for the component

  1. Click the Tutorial package and select the JavaArithmetic component.

  2. Select File | Generate Stub/Skeleton.

  3. Select Generate Stubs, then select Generate Java Stubs. Fill in the Java Stubs fields as follows:

    1. Select CORBA from the drop-down list.

    2. Select Generate Java Files.

    3. Select Compile Java Stubs.

    4. Leave the Java Code Base field at the default:

      %JAGUAR%\html\classes

      For Windows

      $JAGUAR/html/classes

      For UNIX

    5. Deselect Generate C++ Stubs.

  4. Click Next to display the skeleton generation options and configure the settings as follows:

    1. Select Generate Skeletons on Client.

    2. Leave the Java Code Base field at the default value:

      %JAGUAR%\java\classes

      For Windows

      $JAGUAR/java/classes

      For UNIX

    3. Deselect “Compile Java Skeletons” (you cannot compile now because the component implementation source file is not ready).

  5. Click Finish.


Write the server-side code

At this point, EAServer Manager has created server-side implementation files in the following directory under your EAServer installation:

java/classes/Sample/Intro/JavaArithmetic

The implementation template file is JavaArithemeticImpl.Java.new and the skeleton is called _sk_Tutorial_JavaArithmetic.java.

StepsCompleting the component implementation

  1. Rename JavaArithemeticImpl.Java.new to JavaArithmeticImpl.Java (that is, delete the .new extension). Open the renamed file in a text editor, then find the definition of the multiply method. Change the definition so that it matches the one below:

        double multiply
            (double m1, 
            double m2)
         {
              return m1 * m2;
         }
    
  2. Save your changes.

  3. Compile the component skeleton and implementation files using a JDK 1.3 or later compiler—for example, if you are using Windows:

    cd %JAGUAR%\java\classes\Sample\Intro\JavaArithmetic
    %JAGUAR%\bin\jc.bat *.java
    

    Or, if you are using UNIX:

    cd $JAGUAR/java/classes/Sample/Intro/JavaArithmetic
    $JAGUAR/bin/jc *.java
    

Create the client program

In the html\classes subdirectory of your EAServer installation, create a new directory called TutorialApps if it does not exist. In this directory, create the Java file below as JAConsole.java.

This is a simple command-line application that:

You can find a copy of JAConsole.java in the html/docs/tutorial/java-corba subdirectory of your EAServer installation. Here is the source for JAConsole.java:

//
// This is a sample command-line Java application that 
// invokes the JavaArithmetic component created in the EAServer
// Java component tutorial.
//
// Usage:
//    arith iiop://<host>:<port>
//
//    Where:
//
//       <host> is the host name or IP address of the server machine. 
//
//       <iiop-port> is the server's IIOP port (9000 in the
//         default configuration).
//
//
package TutorialApps;

import org.omg.CORBA.*;
import SessionManager.*;
import Tutorial.*;  // Package for EAServer stub classes

public class JAConsole {

    static public void main(String options[]) {
 
        String _usage = "Usage: JAConsole iiop://<host>:<port>\n";
        String _ior = null;
       
       try {

           if (options.length >= 1)
           {
               _ior = options[0];
           }
           else
           {
                System.out.println(_usage);
                return;
           }

           //
           // Initialize the CORBA client-side ORB and 
           // obtain a stub for the EAServer component instance.
           //
           System.out.println("... Creating session.");

           //
           // Initialize the ORB.
           //
           java.util.Properties props = new java.util.Properties();
           props.put("org.omg.CORBA.ORBClass", "com.sybase.CORBA.ORB");

           ORB orb = ORB.init(options, props);

           //
           // Create an instance of the EAServer SessionManager::Manager 
           // CORBA IDL object.
           //

           Manager manager = ManagerHelper.narrow(orb.string_to_object(_ior));

           //
           // Create an authenticated session with user "Guest" and password
           // "GuestPassword".
           // 
           Session session = manager.createSession("Guest", "GuestPassword");

           System.out.println("... Creating component instance.");

           //
           // Create a stub object instance for the 
           // Tutorial/JavaArithmetic EAServer component.
           //
           Tutorial.JavaArithmetic comp = 
           Tutorial.JavaArithmeticHelper.narrow(
           session.create("Tutorial/JavaArithmetic"));

           if (comp == null)
           {
             System.out.print("ERROR: Null component instance. ");
             System.out.print(
       "Check Jaguar Manager and verify that the component ");
             System.out.print(
       "Tutorial/JavaArithmetic exists and that it implements the ");
             System.out.println(
       "Tutorial::JavaArithmetic IDL interface.");
             return;
           }

           System.out.println("... Created component instance.");
    
            // 
            // Invoke the multiply method.
            //
           System.out.println("... Multiplying:\n");
           double m1 = 3.1;
           double m2 = 2.5;
           double result = comp.multiply(m1, m2);
           System.out.println("    " + m1 + "*" + m2 + "=" + result);

        // Explicitly catch exceptions that can occur due to user error,
        // and print a generic error message for any other CORBA system 
        // exception.

       } catch ( org.omg.CORBA.COMM_FAILURE cfe)
       {
           // The server is not running, or the specified URL is
           // wrong. 
             System.out.println(
              "Error: could not connect to server at " + _ior + "\n"
             + "Make sure the specified address is correct and the "
             + "server is running.\n\n" + _usage );
       } catch ( org.omg.CORBA.OBJECT_NOT_EXIST cone )
        {
            // Requested object (component) does not exist.
            System.out.println(
               "Error: CORBA OBJECT_NOT_EXIST exception. Check the "
             + "server log file for more information. Also verify "
             + "that the Tutorial/JavaArithmetic "
             + "component has been created properly in "
            + "Jaguar Manager. \n"); 

        } catch (org.omg.CORBA.NO_PERMISSION npe) {
           // Login failed, or the component requires an authorization role 
           // that this user is not a member of.
            System.out.println(
               "Error: CORBA NO_PERMISSION exception. Check whether "
             + "login authentication is enabled for your server and "
             + "whether the component has restricted access.\n");
             npe.printStackTrace();

       } catch (org.omg.CORBA.SystemException se)
       {
            // Generic CORBA exception
           System.out.println(
              "Received CORBA system exception: " 
              + se.toString() );
           se.printStackTrace();
       }

        return;
    } // main()

}

Compile the application source using a JDK 1.3 or later compiler, for example:

%JAGUAR%\bin\jc JAConsole.java

Run the client program

If you have not refreshed or restarted the server since creating the JavaArithmetic component, refresh the server before running the client program.

Create a batch file or UNIX shell script to run the client application, then run it. The batch file or shell script configures the CLASSPATH environment variable, then runs the application using the JDK 1.3 java program included with your EAServer installation.

If necessary, you can run the client on a different machine than the server host, as long as your server uses a real host address and not localhost or 127.0.0.1.

StepsCreating the Windows batch file

  1. Create a file named runja.bat containing the commands below:

    call %JAGUAR%\bin\setenv.bat
    set CLASSPATH=%JAGUAR%\java\lib\easj2ee.jar;
    set CLASSPATH=%CLASSPATH%;%JAGUAR%\java\lib\easclient.jar
    set CLASSPATH=%CLASSPATH%;%JAGUAR%\html\classes
    set JAVA_HOME=%JAGUAR_JDK13%
    %JAVA_HOME%\jre\bin\java TutorialApps.JAConsole %* 
    
    

StepsCreating the UNIX shell script

  1. Create a file named runja containing the commands below:

    #!/bin/sh 
    . $JAGUAR/bin/setenv.sh
    CLASSPATH=$JAGUAR/java/lib/easj2ee.jar 
    CLASSPATH=$CLASSPATH:$JAGUAR/java/lib/easclient.jar
    CLASSPATH=$CLASSPATH:$JAGUAR/html/classes export CLASSPATH
    JAVA_HOME=$JAGUAR_JDK13
    $JAVA_HOME/jre/bin/java TutorialApps.JAConsole $*
    
    
  2. Change the file permissions to allow the script to be executed. For example:

    chmod 777 runja
    

StepsRunning the client application

  1. Run the batch or script file, specifying the server host name and IIOP port number on the command line as follows:

    runja iiop://host:iiop-port
    

    For example:

    runja iiop://myhost:9000
    

    If everything is working, the application prints the results from the invocation of the multiply method. If not, check the error text printed on the console where you ran the client, and check for error messages in the server log file.





Copyright © 2005. Sybase Inc. All rights reserved. Creating an applet client