Building Distributed Applications With Java and CORBA

by Bryan Morgan





Listing One 

Module TheModule

{

     interface TheInterface

     {

          long TheAttribute;

     };

};





Listing Two

package TheModule;

public interface TheInterface

{

     public int TheAttribute;

}



Listing Three

// EMPLOYEE represents a single employee's personal information

module EmployeeServer

{

    struct Employee

    {

        string Name;

        string Address;

        string City;

        string State;

        long long SSN;

        float Salary;

       string Title;

    };

    typedef sequence<Employee> EmployeeList;

    interface EmployeeInfo

    {

        attribute EmployeeList theList;

        readonly attribute long Count;



        Employee getEmployee(in long index);

        void deleteEmployee(in long index);

        void newEmployee(in Employee newPerson);

        void updateEmployee(in long index, in Employee newData);

    };

};



Listing Four

// Employee.java - Java version of IDL struct Employee

final public class Employee {

  public java.lang.String Name;

  public java.lang.String Address;

  public java.lang.String City;

  public java.lang.String State;

  public long SSN;

  public float Salary;

  public java.lang.String Title;



  public Employee() 

  {

  }

  public Employee(java.lang.String Name, java.lang.String Address,

    java.lang.String City, java.lang.String State, long SSN, 

    float Salary, java.lang.String Title)

 {

    this.Name = Name;

    this.Address = Address;

    this.City = City;

    this.State = State;

    this.SSN = SSN;

    this.Salary = Salary;

    this.Title = Title;

  }

  public java.lang.String toString() 

  {

    org.omg.CORBA.Any any = org.omg.CORBA.ORB.init().create_any();

    EmployeeServer.EmployeeHelper.insert(any, this);

    return any.toString();

  }

}

// EmployeeInfo.java - Java interface version of IDL interface EmployeeInfo

public interface EmployeeInfo extends org.omg.CORBA.Object {

  public void theList(EmployeeServer.Employee[] theList);

  public EmployeeServer.Employee[] theList();

  public int Count();

  public EmployeeServer.Employee getEmployee(int index);

  public void deleteEmployee(int index);

  public void newEmployee(EmployeeServer.Employee newPerson);

  public void updateEmployee(int index,  EmployeeServer.Employee newData);

}



Listing Five

package EmployeeServer;

public class EmployeeInfoImpl extends EmployeeServer._EmployeeInfoImplBase {

  private java.util.Vector Employees;

  /** Construct a persistently named object. */

  public EmployeeInfoImpl(java.lang.String name)

  {

    super(name);

    Employees = new java.util.Vector();

  }

  /** Construct a transient object. */

  public EmployeeInfoImpl()

  {

    super();

    Employees = new java.util.Vector();

  }

  public EmployeeServer.Employee getEmployee(int index)

  {

    if (index <= (Employees.size() -1))

        return (EmployeeServer.Employee)Employees.elementAt(index);

    else

        return null;

  }



  public void deleteEmployee(int index)

  {

    Employees.removeElementAt(index);

  }

  public void newEmployee(EmployeeServer.Employee newPerson)

  {

     Employees.addElement(newPerson);

  }

  public void updateEmployee(int index, EmployeeServer.Employee newData)

  {

    Employees.setElementAt(newData, index);

  }

  public void theList(EmployeeServer.Employee[] theList)

  {

    // implement attribute writer...

  }

  public EmployeeServer.Employee[] theList()

  {

    // implement attribute reader...

    return null;

  }

  public int Count()

  {

    return Employees.size();

  }

}



Listing Six

public class Server

{

       public static void main(String[] args)

       {

              //Initialize the ORB

              org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args, null);

              //Initialize the BOA

              org.omg.CORBA.BOA boa = orb.BOA_init();

              //Create the EmployeeInfo object

              EmployeeServer.EmployeeInfo info = 

                       new EmployeeServer.EmployeeInfoImpl("EmployeeInfo");

              //Export the object

              boa.obj_is_ready(info);

              System.out.println(info + " is ready.");

              //Wait for incoming requests

              boa.impl_is_ready();

       }

}



Listing Seven



// Initialize the ORB

org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init();

// Locate and retrieve EmployeeInfo object from server

EmployeeFrame.infoObject = 

          EmployeeServer.EmployeeInfoHelper.bind(orb, "EmployeeInfo");





4



