JMU
Common Object Request Broker Architecture (CORBA)
An Introduction with Examples in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu


Getting Started
Stubs and Skeletons
Stubs and Skeletons (cont.)

An Overview

images/stubs_skeletons.gif
IDL Data Types
char A single ASCII character
wchar A single "wide" character (which may or may not use Unicode)
octet Eight binary "bits"
boolean A binary type that can have a value of either TRUE or FALSE
string An ordered set of characters
wstring An ordered set of wide characters
short A short integer
long A long integer
float A short floating point number
double A long floating point number
any Any entity in CORBA
IDL Modules

Allow for the Grouping of Entities

  module name {

  };
  

Can be Nested to Create Submodules

  module name1 {

    module name2 {

    };

  };
  
IDL Interfaces

A Collection of Operations

  interface Prioritized {

  };
  

Can be Specialized

  interface TextMessage {

  };


  interface TextAlert : TextMessage {

  };
  
Operations
Attributes
Collections of Attributes

An Example of a struct

  struct AccidentReport {

    attribute int     priority;
    attribute string  message;
  };
  
Enumerated Data Types

An Example

  enum Color {

    red, orange, yellow, green, blue, indigo, violet
  };
  
Arrays and Sequences
Exceptions

An Example

  exception ConnectionException {

    string   reason;
  };
  
Java Mappings
Java Mappings (cont.)
Java Mappings (cont.)
Java Mappings (cont.)

An IDL enum is mapped to a Java class. For example, the following IDL specification:

  enum EnumType {first, second, third, fourth, fifth};
  

results in the following Java code:

  public class EnumType implements org.omg.CORBA.portable.IDLEntity 
  {
    public static final int _first = 0;
    public static final EnumType first = new EnumType(_first);

    public static final int _second = 1;
    public static final EnumType second = new EnumType(_second);

    public static final int _third = 2;
    public static final EnumType third = new EnumType(_third);

    public static final int _fourth = 3;
    public static final EnumType fourth = new EnumType(_fourth);

    public static final int _fifth = 4;
    public static final EnumType fifth = new EnumType(_fifth);

    public int value() {...}

    public static EnumType from_int(int value) {...};

    // constructor
   protected EnumType(int) {...}
  };
  
Java Mappings (cont.)
An Example

From the book:


McCarty B.
James Madison University


Cassady-Dorion L.
James Madison University

SAMS Press
Indianapolis, IN
1999 0-672-31537-8

javaexamples/corba-bookexample/musicServer.txt
        
module musicServer {
    exception NoSuchUserException { string reason; };
    exception UserIDExistsException { string reason; };
    enum MediaType { CD, TAPE, RECORD, NOT_SPECIFIED };

    interface AlbumI {
        attribute string      sArtistName;
        attribute string      sAlbumName;
        attribute string      sListeningNotes;
        attribute float       fPrice;

        attribute MediaType   type;
    };
    typedef sequence<AlbumI>AlbumSequence;

    struct AlbumQueryS {
        string      sArtistName;
        string      sAlbumName;
        float       fPrice;
        MediaType   type;
    };

    interface MusicCollectionI {
        attribute string sUserName;
        attribute string sPassword;

        AlbumSequence getAllAlbums();
        AlbumSequence getAllAlbumsByArtistName();
        AlbumSequence getAllAlbumsByAlbumName();
        void addAlbum(in AlbumI album);
        void deleteAlbum(in AlbumI album);
        AlbumI obtainEmptyAlbum();
    };

    interface RequestorI {
        void albumFound(in AlbumSequence album);
    };

    interface MusicServerI {
        MusicCollectionI obtainCollection(in string sUserName, 
                                          in string sPassword)
                                          raises(NoSuchUserException);

        MusicCollectionI createCollection(in string sUserName, 
                                          in string sPassword)
                                          raises(UserIDExistsException);

        void logOut(in MusicCollectionI collection);

        AlbumQueryS obtainEmptyQuery();

        void searchCatalog(in AlbumQueryS query, in RequestorI requestor);

        void saveCollection();
    };
};

        
javaexamples/corba-bookexample/Album.java
        import java.io.*;
import musicServer.*;

/**
 * Models a unique album, with all of its properties
 */
public class Album extends _AlbumIImplBase implements Serializable {
    private String      _sArtistName;
    private String      _sAlbumName;
    private String      _sListeningNotes;
    private float       _fPrice;

    private MediaType   _type;

    public Album() {
        this("", "", "", 0f, MediaType.NOT_SPECIFIED);
    }

    public Album(String sArtistName,
                 String sAlbumName,
                 String sListeningNotes,
                 float fPrice,
                 MediaType type) {
        _sArtistName = sArtistName;
        _sAlbumName = sAlbumName;
        _sListeningNotes = sListeningNotes;
        _fPrice = fPrice;
        _type = type;
    }

    public String sArtistName() { return _sArtistName; }
    public void sArtistName(String sArtistName) { _sArtistName = sArtistName; }

    public String sAlbumName() { return _sAlbumName; }
    public void sAlbumName(String sAlbumName) { _sAlbumName = sAlbumName; }

    public String sListeningNotes() { return _sListeningNotes; }
    public void sListeningNotes(String sListeningNotes) { _sListeningNotes = sListeningNotes; }

    public float fPrice() { return _fPrice; }
    public void fPrice(float fPrice) { _fPrice = fPrice; }

    public MediaType type() { return _type; }
    public void type(MediaType type) { _type = type; }
}        
javaexamples/corba-bookexample/MusicCollection.java
        import musicServer.*;
import java.util.*;
import java.io.*;
import org.omg.CORBA.*;

/**
 * Models a collection of albums.
 */
public class MusicCollection extends _MusicCollectionIImplBase implements Serializable {
    private Vector          _vecAlbums;
    private String          _sUserName;
    private String          _sPassword;

    private transient BOA   _boa;
    private Vector          _vecActivatedObjects;

    private boolean         _bObjectsDeactivated = false;

    public MusicCollection(String sUserName, String sPassword, BOA boa) {
        super();
        _sUserName = sUserName;
        _sPassword = sPassword;
        _vecAlbums = new Vector();
        _boa = boa;
        _vecActivatedObjects = new Vector();
    }

    /**
     * Invoked after being de-serialized with a new reference to the BOA
     */
    public void updateTransientData(BOA boa) {
        _boa = boa;
    }

    /**
     * Obtains all AlbumI objects ordered by artist name
     */
    public AlbumI[] getAllAlbumsByArtistName() {
        AlbumI[] albums = getAllAlbums();
        AlbumSorter.sortByArtistName(albums);
        return albums;
    }

    /**
     * Obtains all AlbumI objects ordered by album name
     */
    public AlbumI[] getAllAlbumsByAlbumName() {
        AlbumI[] albums = getAllAlbums();
        AlbumSorter.sortByAlbumName(albums);
        return albums;
    }

    /**
     * Obtains all AlbumI objects in default order
     */
    public AlbumI[] getAllAlbums() {
        if(_bObjectsDeactivated) {
            _bObjectsDeactivated = false;
            Enumeration e = _vecAlbums.elements();
            while(e.hasMoreElements()) {
                _boa.obj_is_ready((org.omg.CORBA.Object)e.nextElement());
            }
        }

        AlbumI[] returnValue = new AlbumI[_vecAlbums.size()];
        _vecAlbums.copyInto(returnValue);
        return returnValue;
    }

    /**
     * Adds an AlbumI object to the collection
     */
    public void addAlbum(AlbumI album) {
        _vecAlbums.addElement(album);
    }

    /**
     * Removes an AlbumI object from the collection
     */
    public void deleteAlbum(AlbumI album) {
        _vecAlbums.removeElement(album);
    }

    /**
     * Obtains an empty AlbumI object
     */
    public AlbumI obtainEmptyAlbum() {
        AlbumI returnValue = new Album();
        _boa.obj_is_ready(returnValue);
        _vecActivatedObjects.addElement(returnValue);
        return returnValue;
    }

    public void sUserName(String sUserName) { _sUserName = sUserName; }
    public String sUserName() { return _sUserName; }

    public void sPassword(String sPassword) { _sPassword = sPassword; }
    public String sPassword() { return _sPassword; }

    /**
     * Deactivates all activated objects
     */
    public void deactivateObjects() {
        _bObjectsDeactivated = true;
        Enumeration e = _vecAlbums.elements();
        while(e.hasMoreElements()) {
            _boa.deactivate_obj((org.omg.CORBA.Object)e.nextElement());
        }
    }

}
        
javaexamples/corba-bookexample/MusicServer.java
        import musicServer.*;
import org.omg.CORBA.*;


/**
 * Main server class
 */
public final class MusicServer extends _MusicServerIImplBase {
    private static BOA              _boa;
    private MusicCollectionHolder   _musicCollectionHolder;

    public MusicServer() {
        super("MusicServer");
        _musicCollectionHolder = new MusicCollectionHolder(_boa);
    }

    /**
     * Invoked by the client when he wants to attempt a login
     */
    public MusicCollectionI obtainCollection(String sUserName, String sPassword) throws NoSuchUserException {
        MusicCollectionI collection = _musicCollectionHolder.obtainCollection(sUserName, sPassword);
        if(collection == null) throw new NoSuchUserException("Invalid Login Information");

        _boa.obj_is_ready(collection);
        return collection;
    }

    /**
     * Invoked by the client when he wants to create a new
     * MusicCollectionI object.
     */
    public MusicCollectionI createCollection(String sUserName, String sPassword) throws UserIDExistsException {
        if(_musicCollectionHolder.doesUserNameExist(sUserName)) {
            throw new UserIDExistsException(sUserName+" is already in use");
        }

        MusicCollectionI collection = new MusicCollection(sUserName, sPassword, _boa);
        _boa.obj_is_ready(collection);
        _musicCollectionHolder.addMusicCollection(collection);

        return collection;
    }

    /**
     * Helper method that obtains an AlbumQueryS
     * object populated with dummy data.
     */
    public AlbumQueryS obtainEmptyQuery() {
        return new AlbumQueryS("", "", 0f, MediaType.NOT_SPECIFIED);
    }

    /**
     * Performs an exhaustive search of all available
     * catalogs. Demonstrates the callback design pattern.
     */
    public void searchCatalog(AlbumQueryS query, RequestorI requestor) {
        AlbumSearcher searcher = new AlbumSearcher(query, requestor, _boa);
        searcher.start();
    }

    /**
     * Invoked by the client when he wants to logout, deactivates
     * all activated objects.
     */
    public void logOut(MusicCollectionI collection) {
        Deactivator deactivator = new Deactivator(collection, _boa);
        deactivator.start();
    }

    public void saveCollection() {
        _musicCollectionHolder.saveCollection();
    }

    public static void main(String[] args) {
        ORB orb = ORB.init();
        _boa = orb.BOA_init();

        MusicServer server = new MusicServer();

        _boa.obj_is_ready(server);
        _boa.impl_is_ready();
    }
}