Programming with CORBA Hongtao Shi 04/23/01. §CORBA Overview §Advantages of CORBA §Interface...

46
Programming with CORBA Hongtao Shi 04/23/01

Transcript of Programming with CORBA Hongtao Shi 04/23/01. §CORBA Overview §Advantages of CORBA §Interface...

Page 1: Programming with CORBA Hongtao Shi 04/23/01. §CORBA Overview §Advantages of CORBA §Interface Definition Language §Application: Address Book Outline.

Programming with CORBA

Hongtao Shi

04/23/01

Page 2: Programming with CORBA Hongtao Shi 04/23/01. §CORBA Overview §Advantages of CORBA §Interface Definition Language §Application: Address Book Outline.

CORBA Overview

Advantages of CORBA

Interface Definition Language

Application: Address Book

Outline

Page 3: Programming with CORBA Hongtao Shi 04/23/01. §CORBA Overview §Advantages of CORBA §Interface Definition Language §Application: Address Book Outline.

CORBA Overview

• Acronym for Common Object Request

Broker Architecture

• Not a language, definition of an

application framework

What is CORBA?

Page 4: Programming with CORBA Hongtao Shi 04/23/01. §CORBA Overview §Advantages of CORBA §Interface Definition Language §Application: Address Book Outline.

• Platform-independent, language-independent

architecture for writing distributed, object-

oriented applications

• Object Management Group: www.omg.org

• More than 800 leading member companies

Note: (1)Not a competitor of RMI

(2)More powerful framework if

combined with RMI

Page 5: Programming with CORBA Hongtao Shi 04/23/01. §CORBA Overview §Advantages of CORBA §Interface Definition Language §Application: Address Book Outline.

CORBA Framework (Major Parts)Interface Definition Language (IDL):

Programming language independent

Object Request Broker (ORB): Creation and transmission of message, reference to remote object

Internet Inter-ORB Protocol (IIOP): TCP/IP version of generic GIOP for ORB to send message back and forth

Page 6: Programming with CORBA Hongtao Shi 04/23/01. §CORBA Overview §Advantages of CORBA §Interface Definition Language §Application: Address Book Outline.

Why Distributed Applications?

• The data used by the application are distributed.

• The computation is distributed.

• The users of the application are distributed.

Page 7: Programming with CORBA Hongtao Shi 04/23/01. §CORBA Overview §Advantages of CORBA §Interface Definition Language §Application: Address Book Outline.

Fundamental Realities of Distributed Systems

Co-locatedDistributedCommunication Fast Slow

Failures Objects Objects fail fail together separately

Network canpartition

Concurrent access Only with multiple threads Yes

Security Yes No

Page 8: Programming with CORBA Hongtao Shi 04/23/01. §CORBA Overview §Advantages of CORBA §Interface Definition Language §Application: Address Book Outline.

2-tier Applications

Client Object

Most Distributed Object Framework:

Client Stub Skeleton Object

Request

Reply

N-tier Applications

Page 9: Programming with CORBA Hongtao Shi 04/23/01. §CORBA Overview §Advantages of CORBA §Interface Definition Language §Application: Address Book Outline.

N-tier Using CORBA as Framework

Client

IDL ORB Stub Interface

ORB

Object

(w/in an application)

ORB IDL Interface Skeleton

ORB

Request and Reply

IIOP

IIOP: Internet Inter-ORB Protocol

Page 10: Programming with CORBA Hongtao Shi 04/23/01. §CORBA Overview §Advantages of CORBA §Interface Definition Language §Application: Address Book Outline.

Location of Remote CORBA Object

Using an object location service, such as OMG Naming Service

Recreating an object reference from its ‘stringified’ form (ORB’s interface: object_to_string() and string_to_object())

Receiving an object reference from another CORBA object, referred to as a factory object

Page 11: Programming with CORBA Hongtao Shi 04/23/01. §CORBA Overview §Advantages of CORBA §Interface Definition Language §Application: Address Book Outline.

Root Context

Bill Jones Mary Smith

Checking Saving Checking Saving

Naming Service:

Page 12: Programming with CORBA Hongtao Shi 04/23/01. §CORBA Overview §Advantages of CORBA §Interface Definition Language §Application: Address Book Outline.

CORBA Products

The CORBA products that support the Java programming language include:

The Java 2 ORB: The Java 2 ORB comes with Sun's Java 2 SDK. VisiBroker for Java: A popular Java ORB from Inprise Corporation. VisiBroker is also embedded in other products, e.g., the Netscape Communicator browser.Netscape Communicator: Netscape browsers have a version of VisiBroker embedded in them. Applets can issue request on CORBA objects without downloading ORB classes into the browser. They are already there.OrbixWeb: A popular Java ORB from Iona Technologies.WebSphere: A popular application server with an ORB from IBM.

Page 13: Programming with CORBA Hongtao Shi 04/23/01. §CORBA Overview §Advantages of CORBA §Interface Definition Language §Application: Address Book Outline.

Advantages of CORBALanguage Independence: OMG has defined

mappings for Java, C, C++, SmallTalk, Ada, Lisp, COBOL, Python and IDLscript

Location Transparency: Relocation no impact on the other

Support for Heterogeneous Network: Small, handheld devices to mainframes

Interoperability: Objects within different vendor’s ORBs can communicate.

Page 14: Programming with CORBA Hongtao Shi 04/23/01. §CORBA Overview §Advantages of CORBA §Interface Definition Language §Application: Address Book Outline.

Interoperability results from two key parts of the specification:

(1)OMG Interface Definition Language

(OMG IDL), ISO International Standard

for several years

(2)Standardized protocols GIOP and IIOP

Page 15: Programming with CORBA Hongtao Shi 04/23/01. §CORBA Overview §Advantages of CORBA §Interface Definition Language §Application: Address Book Outline.

Tools You Can UseJava 2 SDK: CORBA/IIOP 2.0 compliant

product, JavaIDL

SDK 1.3: Bundled with an IDL to Java compiler, idlj

Previous Version: Download idltojava compiler or RMI-IIOP 1.0.1 package which includes the idlj compiler

Page 16: Programming with CORBA Hongtao Shi 04/23/01. §CORBA Overview §Advantages of CORBA §Interface Definition Language §Application: Address Book Outline.

Related Classes:(1)Test.idl //starting point

(2)idlj -fall Test.idl //Two more options: //idlj -fclient Test.idl and //idlj -fserver Test.idl

(3)Test.java //empty interface in java //extending TestOperations.java //and org.omg.CORBA.Object

(4)TestOperations.java //This interface defines the IDL //to Java mapped operations of //the IDL interface

Page 17: Programming with CORBA Hongtao Shi 04/23/01. §CORBA Overview §Advantages of CORBA §Interface Definition Language §Application: Address Book Outline.

Related Classes (cntd’)

(5)_TestStub.java //the client stub which //implements interface Test

(6)_TestImplBase.java //the server skeleton which //implements interface Test

(7)TestHelper.java //downcasting object references //to type Test

(8)TestHolder.java //a container for streaming //objects of type Test to and //from input and output streams

Page 18: Programming with CORBA Hongtao Shi 04/23/01. §CORBA Overview §Advantages of CORBA §Interface Definition Language §Application: Address Book Outline.

Interface Definition Language (IDL) IDL Data Types

Modules

Interfaces

Attributes

Operations

Exceptions

Inheritance

Page 19: Programming with CORBA Hongtao Shi 04/23/01. §CORBA Overview §Advantages of CORBA §Interface Definition Language §Application: Address Book Outline.

IDL Data Types

integer(unsigned short, long, etc.) float char/string boolean struct union enum array sequence

Page 20: Programming with CORBA Hongtao Shi 04/23/01. §CORBA Overview §Advantages of CORBA §Interface Definition Language §Application: Address Book Outline.

//Example of structinterface Log

{struct date

{unsigned short month;

unsigned short day;

unsigned short year;

};

struct logMessage

{string msg;

date msgDate;

};

};

Page 21: Programming with CORBA Hongtao Shi 04/23/01. §CORBA Overview §Advantages of CORBA §Interface Definition Language §Application: Address Book Outline.

//Example of enum

enum Color(red, yellow, green, blue, purple);

//Example of array

interface Log

{//…interface same as before

typedef logMessage logMessageArray[10];

void submitLogMsg(in logMessage msg);

void submitLogMsgs(in out logMessageArray msgArray);

};

Page 22: Programming with CORBA Hongtao Shi 04/23/01. §CORBA Overview §Advantages of CORBA §Interface Definition Language §Application: Address Book Outline.

Note:

• An array could be multi-dimensional.

• Index starts with zero.

• A typedef must be created to identify array

type and size.

• Sequence can be bounded or unbounded,

which is different from arrays.

Page 23: Programming with CORBA Hongtao Shi 04/23/01. §CORBA Overview §Advantages of CORBA §Interface Definition Language §Application: Address Book Outline.

Modules

IDL interfaces may be defined within modules, providing naming space that contains and isolates any names within it.

e.g. module OnlineBroker

{interface Account

{

//...

};

};

Note: need scope resolution operator to access

the interface from outside the interface

Page 24: Programming with CORBA Hongtao Shi 04/23/01. §CORBA Overview §Advantages of CORBA §Interface Definition Language §Application: Address Book Outline.

Interfaces

• Define the functionality of an object in a

distributed system• Everything defined in an interface must be public.

Attributesmodule OnlineBroker

{interface Account

{

readonly attribute string accountNumber;

attribute string accountHolderName;

}; //get and set methods connected to the attributes

};

Page 25: Programming with CORBA Hongtao Shi 04/23/01. §CORBA Overview §Advantages of CORBA §Interface Definition Language §Application: Address Book Outline.

Operationsmodule OnlineBroker

{interface Account

{

readonly attribute string accountNumber;

attribute string accountHolderName;

boolean buy(in string symbol, inout long quantity);

boolean sell(in string symbol, out string cusip);

};

};

Note: No overloading is permitted!

Page 26: Programming with CORBA Hongtao Shi 04/23/01. §CORBA Overview §Advantages of CORBA §Interface Definition Language §Application: Address Book Outline.

Exceptionsmodule OnlineBroker

{interface Account

{readonly attribute string accountNumber;

attribute string accountHolderName;

exception InvalidSymbol()

{string symbol;

};

string sell(in string symbol)

raises (InvalidSymbol);

};

};

Page 27: Programming with CORBA Hongtao Shi 04/23/01. §CORBA Overview §Advantages of CORBA §Interface Definition Language §Application: Address Book Outline.

Inheritance

• IDL interface can inherit from other interfaces.

• Implicitly inherit from the Object interface

• Multiple inheritance possible, commas delimited module OnlineBroker

{// interface Account same as before

interface MarginAccount : Account

{readonly attribute double margin;

void RequestAdditionalMargin(in double additionalAmt);

};

};

Page 28: Programming with CORBA Hongtao Shi 04/23/01. §CORBA Overview §Advantages of CORBA §Interface Definition Language §Application: Address Book Outline.

Application: Address Book

Define the Interface (name=>email, email=>name, register a new user)

Create the CORBA Object and ServerCreate the CORBA ClientRun the Server and the Client

name email

David Reilly [email protected]

Page 29: Programming with CORBA Hongtao Shi 04/23/01. §CORBA Overview §Advantages of CORBA §Interface Definition Language §Application: Address Book Outline.

1. Interface Definition Language

// Address book system module module address_book_system { // Specify interface to our address book interface address_book { // Unknown user exception exception unknown_user {}; // User already exists exception exception user_exists {}; // Lookup name from email address string name_from_email(in string email) raises (unknown_user); // Lookup email from full name string email_from_name(in string name) raises (unknown_user); // Record a new name and email void record_user(in string name, in string email) raises (user_exists); };

};

Page 30: Programming with CORBA Hongtao Shi 04/23/01. §CORBA Overview §Advantages of CORBA §Interface Definition Language §Application: Address Book Outline.

2. From IDL to Java: idlj -fall address_book.idl

A package contains skeleton source code for your CORBA client and server. A second package covers two exceptions.

\address_book_system\ _address_bookStub.java address_book.java address_bookHolder.java address_bookHelper.java address_bookPackage _address_bookImplBase.java

address_bookPackage\ unknown_user.java unknown_userHelper.java unknown_userHolder.java user_exists.java user_existsHelper.java user_existsHolder.java

Page 31: Programming with CORBA Hongtao Shi 04/23/01. §CORBA Overview §Advantages of CORBA §Interface Definition Language §Application: Address Book Outline.

3. Implementing a CORBA Servant

package address_book_system; import address_book_system.address_bookPackage.*; import java.util.Hashtable; import java.util.Enumeration;

//// AddressBookServant // This servant class is responsible for implementing // three methods //// * String name_from_email ( String email ); // * String email_from_name ( String name ); // * void record_user ( String name, String email );

Page 32: Programming with CORBA Hongtao Shi 04/23/01. §CORBA Overview §Advantages of CORBA §Interface Definition Language §Application: Address Book Outline.

Implementing a CORBA Servant(cntd’)

class AddressBookServant extends _address_bookImplBase { private Hashtable name2email; public AddressBookServant() // Create a new hashtable to store name & email {name2email = new Hashtable(); } // Get the name of this email user public String name_from_email ( String email ) throws unknown_user { if (name2email.contains(email)) { // Begin search for that name for (Enumeration e = name2email.keys(); e.hasMoreElements();) { String name = (String) e.nextElement(); String e_mail = (String) name2email.get(name); // Match on email ? if (email.compareTo(e_mail) == 0) { return name; } } } } // User not found - throw unknown user exception throw new unknown_user(); }

Page 33: Programming with CORBA Hongtao Shi 04/23/01. §CORBA Overview §Advantages of CORBA §Interface Definition Language §Application: Address Book Outline.

Implementing a CORBA Servant(cntd’)

// Get the email of this person public String email_from_name ( String name ) throws unknown_user { // If user exists if (name2email.containsKey(name)) { // Return email address return (String) name2email.get(name); } // User doesn't exist throw new unknown_user(); } // Add a new user to the system public void record_user ( String name, String email ) throws user_exists { // Is the user already listed if (name2email.containsKey( name ) || name2email.contains( email ) ) { // If so, throw exception throw new user_exists(); } // Add to our hash table name2email.put (name, email); } }

Page 34: Programming with CORBA Hongtao Shi 04/23/01. §CORBA Overview §Advantages of CORBA §Interface Definition Language §Application: Address Book Outline.

4. Writing a CORBA Server

package address_book_system; import org.omg.CORBA.*; import org.omg.CosNaming.*; import org.omg.CosNaming.NamingContextPackage.*;// AddressBookServer// This server is responsible for creating an object // request broker (ORB), and registering an AddressBookServant// with a naming service public class AddressBookServer { public static void main(String args[]) { try { // Create a new object request broker ORB orb = ORB.init(args, null); // Create a new address book ... AddressBookServant servant = new AddressBookServant();

Page 35: Programming with CORBA Hongtao Shi 04/23/01. §CORBA Overview §Advantages of CORBA §Interface Definition Language §Application: Address Book Outline.

Writing a CORBA Server(cntd’)

// ... and connect it to our orb orb.connect(servant); // Object Request Broker Initialised System.out.println ("Address book ORB initialised");

// Obtain reference for our nameservice org.omg.CORBA.Object object = orb.resolve_initial_references("NameService");

// Since we have only an object reference, we must // cast it to a NamingContext. We use a helper // class for this purpose NamingContext namingContext = NamingContextHelper.narrow(object);

Page 36: Programming with CORBA Hongtao Shi 04/23/01. §CORBA Overview §Advantages of CORBA §Interface Definition Language §Application: Address Book Outline.

Writing a CORBA Server(cntd’)

// Add a new naming component for our interface NameComponent list[] = { new NameComponent("address_book", "") }; // Now notify naming service of our new interface namingContext.rebind(list, servant); // Wait for clients for (;;){} } catch (Exception e) { System.err.println ("ORB Error - " + e); e.printStackTrace(System.out); System.exit(0); } } }

Page 37: Programming with CORBA Hongtao Shi 04/23/01. §CORBA Overview §Advantages of CORBA §Interface Definition Language §Application: Address Book Outline.

5. Writing a CORBA client

package address_book_system; import address_book_system.address_bookPackage.*; import org.omg.CORBA.*; import org.omg.CosNaming.*; import java.io.*; // // AddressBookClient // // This client demonstrates the AddressBook server and servant. // A menu is presented to the user, allow he or she to add users,// and look up their names & email addresses.

Page 38: Programming with CORBA Hongtao Shi 04/23/01. §CORBA Overview §Advantages of CORBA §Interface Definition Language §Application: Address Book Outline.

Writing a CORBA client(cntd’)

public class AddressBookClient { public static void main(String args[]) throws IOException { try { // Create an object request broker ORB orb = ORB.init(args, null); // Obtain object reference for name service ... org.omg.CORBA.Object object = orb.resolve_initial_references("NameService"); // ... and narrow it to a NameContext NamingContext namingContext = NamingContextHelper.narrow(object); // Create a name component array NameComponent nc_array[] = { new NameComponent("address_book","") };

Page 39: Programming with CORBA Hongtao Shi 04/23/01. §CORBA Overview §Advantages of CORBA §Interface Definition Language §Application: Address Book Outline.

Writing a CORBA client(cntd’)

// Get an address book object reference ... org.omg.CORBA.Object objectReference = namingContext.resolve(nc_array);

// ... and narrow it to get an address book address_book AddressBook = address_bookHelper.narrow(objectReference);

// DataInputStream for system.in DataInputStream din = new DataInputStream (System.in);

Page 40: Programming with CORBA Hongtao Shi 04/23/01. §CORBA Overview §Advantages of CORBA §Interface Definition Language §Application: Address Book Outline.

Writing a CORBA client(cntd’)for (;;) { try

{ // Print menu System.out.println ("1- Add user"); System.out.println ("2- Look up email"); System.out.println ("3- Look up name"); System.out.println ("4- Exit"); System.out.print ("Command :"); // Read a line from user String command = din.readLine(); // Convert to number Integer num = new Integer (command); int choice = num.intValue(); // Variables we'll need for service calls

String name; String email;

Page 41: Programming with CORBA Hongtao Shi 04/23/01. §CORBA Overview §Advantages of CORBA §Interface Definition Language §Application: Address Book Outline.

Writing a CORBA client(cntd’)

switch (choice) {case 1: System.out.print ("Name:");

name = din.readLine(); System.out.print ("Email:"); email = din.readLine(); // Call AddressBook service

AddressBook.record_user(name,email); break;

case 2: System.out.println ("Name:"); name = din.readLine();

// Call AddressBook service email = ddressBook.email_from_name(name); System.out.println ("Email of " + name + " is ” + email); break;

Page 42: Programming with CORBA Hongtao Shi 04/23/01. §CORBA Overview §Advantages of CORBA §Interface Definition Language §Application: Address Book Outline.

Writing a CORBA client(cntd’)

case 3: System.out.println ("Email:"); email = din.readLine(); // Call AddressBook service name =

AddressBook.name_from_email(email); System.out.println ("Name of " + email + " is ”+ name); break;

case 4: System.exit(0); }

} catch (user_exists already_there) { System.out.println ("User already exists - cannot

be added to address book"); }

Page 43: Programming with CORBA Hongtao Shi 04/23/01. §CORBA Overview §Advantages of CORBA §Interface Definition Language §Application: Address Book Outline.

Writing a CORBA client(cntd’)

catch (unknown_user bad_user) { System.out.println ("User doesn't exist");

} }

} catch (Exception e) { System.err.println ("CORBA error - " + e);

} } }

Page 44: Programming with CORBA Hongtao Shi 04/23/01. §CORBA Overview §Advantages of CORBA §Interface Definition Language §Application: Address Book Outline.

Start the server:

java address_book_system.AddressBookServer

-ORBInitialPort 2000 -ORBInitialHost myhost

Start the client:

java address_book_system.AddressBookClient -ORBInitialPort 2000 -ORBInitialHost myhost

Page 45: Programming with CORBA Hongtao Shi 04/23/01. §CORBA Overview §Advantages of CORBA §Interface Definition Language §Application: Address Book Outline.

Running the client:

1- Add user2- Look up email3- Look up name4- ExitCommand :1Name: David ReillyEmail: [email protected] Add user2- Look up email3- Look up name4- ExitCommand :2Name:David ReillyEmail of David Reilly is [email protected]

Page 46: Programming with CORBA Hongtao Shi 04/23/01. §CORBA Overview §Advantages of CORBA §Interface Definition Language §Application: Address Book Outline.

References:

Professional Java Server Programming by Wrox

http://www.omg.org/gettingstartedhttp://www.corba.orghttp://java.sun.com