Corba Example

download Corba Example

of 21

Transcript of Corba Example

  • 8/10/2019 Corba Example

    1/21

    CORBA Example

    Description of the example

    This example illustrates the basic tasks in building a CORBA distributed application

    using Java D!" #ou $ill build the classic %ello &orld program as a distributed

    application" The %ello &orld program has a single operation that returns a string to be

    printed"

    '"The client invokes the sa(%ello method of the %ello)erver"

    *"The ORB transfers that invocation to the servant ob+ect registered for that D!

    interface","The servant-s sa(%ello method runs. returning a Java )tring"

    /"The ORB transfers that )tring back to the client"

    0"The client prints the value of the )tring"

    1etting started

    #ou $ill need t$o things2 version 1.2 of the JDKsoft$are and the idltojava compiler"The JD3 provides the A4 and ORB needed to enable CORBA5based distributed ob+ect

    interaction" The idlto+ava compiler uses the D!5to5Java mapping to convert D!

    interface definitions to corresponding Java interfaces. classes. and methods. $hich (oucan then use to implement (our client and server code"

    &riting the D! nterface

    n this section. (ou $ill $rite a simple D! interface for the %ello &orld program" The

    D! interface defines the contract bet$een the client and server parts of (our application.

    specif(ing $hat operations and attributes are available" O61 D! is programming5

    language independent" #ou must map it to Java before $riting an( of the implementationcode" 7Running idlto+ava on the D! file does this for (ou automaticall("8 %ere-s the

    complete %ello"idl file2

  • 8/10/2019 Corba Example

    2/21

    module HelloApp

    {

    interface Hello

    {

    string sayHello();

    };};

    O61 D! is a purel( declarative language designed for specif(ing programming5language5independent operational interfaces for distributed applications" O61 specifies

    a mapping from D! to several different programming languages. including C. C99.

    )malltalk. COBO!. Ada. and Java" &hen mapped. each statement in O61 D! is

    translated to a corresponding statement in the programming language of choice" #ou canuse the tool idltojavato map an D! interface to Java and implement the client class"

    &hen (ou map the same D! to C99 and implement the server in that language. the Java

    client and C99 server interoperate through the ORB as though the( $ere $ritten in the

    same language"

    )teps needed to $rite the D! for the %ello &orld application

    )tep '2 Declaring the CORBA D! 6odule

    A CORBA moduleis a namespace that acts as a container for related interfaces and

    declarations" tcorresponds closely to a Java pacage!Each module statement in an

    D! file is mapped to a Java package statement"

    module %elloApp :

    ;; Add subse ;; lines"

    =>

  • 8/10/2019 Corba Example

    3/21

    &hen (ou compile the D!. this statement $ill generate an interface statement in the

    Java code" #our client and server classes $ill implement the %ello interface in different

    $a(s"

    )tep ,2 Declaring the Operations

    CORBA operations are the behavior that servers promise to perform on behalf of clients

    that invoke them" "ac# operation statement in t#e $%& generates a correspondingmet#od statement in t#e generated Java interface!

    n (our %ello"idl file. enter the operation statement2

    module %elloApp : interface %ello

    :

    string sa(%ello78> ;; Add this line"

    => ;; notice the semicolon => ;;notice the semicolon

    Because our little %ello &orld application has onl( a single operation. %ello"idl is no$

    complete"

    6apping %ello"idl from D! to Java

    The tool idltojavareads O61 D! files and creates the re

    =

  • 8/10/2019 Corba Example

    4/21

    &ith an interface this simple. it is eas( to see ho$ the D! statements map to the

    generated Java statements"

    apping of $%& *tatements to Java *tatements

    D! )tatement Java )tatement

    module %elloApp package %elloApp>interface %ello public interface %ello

    string sa(%ello78> )tring sa(%ello78>

    The single surprising item is the extends statement" All CORBA ob+ects are derived from

    org"omg"CORBA"Ob+ect to ensure re

  • 8/10/2019 Corba Example

    5/21

    HelloHolder!ava

    This final class holds a public instance member of t(pe %ello" t provides

    operations foro!t and ino!t arguments,$hich CORBA has but $hich do not map easil(to Java-s semantics"

    &hen (ou $rite the D! interface. (ou do all the programming re

  • 8/10/2019 Corba Example

    6/21

    Developing a Client Application

    %ere-s the completed version of %elloClient"+ava"

    import HelloApp!0; 11 2#e pacage containing our stu,s!

    import org!omg!Cos3aming!0; 11 HelloClient 'ill use t#e naming service!import org!omg!CORBA!0; 11 All CORBA applications need t#ese classes!

    pu,lic class HelloClient

    {

    pu,lic static void main(*tring args45)

    {

    try{

    11 Create and initiali6e t#e ORB

    ORB or, 7 ORB!init(args8 null);

    11 9et t#e root naming conte:t

    org!omg!CORBA!O,ect o,Ref 7 or,!resolve+initial+references(.3ame*ervice.);

    3amingConte:t ncRef 7 3amingConte:tHelper!narro'(o,Ref);

    11 Resolve t#e o,ect reference in naming

    3ameComponent nc 7 ne' 3ameComponent(.Hello.8 ..);

    3ameComponent pat#45 7 {nc};

    Hello #elloRef 7 HelloHelper!narro'(ncRef!resolve(pat#));

    11 Call t#e Hello server o,ect and print results *tring #ello 7 #elloRef!sayHello();

    *ystem!out!println(#ello);

    } catc#(":ception e) {

    *ystem!out!println(."RROR - . e);

    e!print*tac2race(*ystem!out);

    }

    }

    }

  • 8/10/2019 Corba Example

    7/21

    mporting Re e"print)tackTrace7)(stem"out8>

    =

  • 8/10/2019 Corba Example

    8/21

    Creating an ORB Ob+ect

    A CORBA client needs a local ORB ob+ect to perform all of its marshaling and O4$ork" Ever( client instantiates an org"omg"CORBA"ORB ob+ect and initialiGes it b(

    passing to the ob+ect certain information about itself"

    nside%elloClient"+ava-s tr(5catch block. declare and initialiGe an ORB variable2

    ORB orb H ORB"init7args. null8>

    The call to the ORB-s init method passes in (our application-s command line

    arguments. allo$ing (ou to set certain properties at runtime"

    Iinding the %ello )erver

    Once the application has an ORB. it can ask the ORB to locate the actual service it

    needs. in this case the %ello server" There are a number of $a(s for a CORBA client toget an initial ob+ect reference> our client application $ill use the CO) aming )ervice

    specified b( O61 and provided $ith Java D!"7)ee @sing )tringified Ob+ect References for information on ho$ to get an initial ob+ect

    reference $hen no naming service is available8"

    Obtaining the nitial aming Context

    The first step in using the naming service is to get the initial naming context" n the

    tr(5catch block. belo$ (our ORB initialiGation. call orb"resolveinitialreferences to getan ob+ect reference to the name server2

    org"omg"CORBA"Ob+ect ob+Ref H orb"resolveinitialreferences7Fame)erviceF8>

    The string Fame)erviceF is defined for all CORBA ORBs" &hen (ou pass in thatstring. the ORB returns the initial naming context. an ob+ect reference to the name

    service"

    arro$ing the Ob+ect Reference

    As $ith all CORBA ob+ect references. ob+Ref is a generic CORBA ob+ect" To use it as

    a amingContext ob+ect. (ou must narro$ it to its proper t(pe" Add the call to narro$+ust belo$ the previous statement"

    amingContext ncRef H amingContext%elper"narro$7ob+Ref8>

    %ere $e see the use of an idlto+ava 5generated helper class. similar in function to

    %ello%elper" The ncRef ob+ect is no$ an org"omg"Cosaming"amingContext and (ou

    can use it to access the naming service and find other services"

  • 8/10/2019 Corba Example

    9/21

    Iinding a )ervice in aming

    ames can have different structures depending upon the implementation of the

    naming service" Conse

    %ere (ou see the %ello%elper helper class at $ork" The resolve method returns a

    generic CORBA ob+ect as (ou sa$ above $hen locating the name service itself"Therefore. (ou immediatel( narro$ it to a %ello ob+ect. $hich is the ob+ect reference (ou

    need to perform the rest of (our $ork"

    nvoking the sa(%ello Operation

    CORBA invocations look like a method call on a local ob+ect" The complications of

    marshaling parameters to the $ire. routing them to the server5side ORB. unmarshaling.and placing the upcall to the server method are completel( transparent to the client

    programmer" Because so much is done for (ou b( generated code. invocation is reall( the

    easiest part of CORBA programming"

    '"Continuing $ith the tr(5catch block in %elloClient"+ava. enter the follo$ing

    invocation belo$ the call to the name service-s resolve method2

    )tring hello H helloRef"sa(%ello78>

    *"Iinall(. add code to print the results of the invocation to standard output2

  • 8/10/2019 Corba Example

    10/21

    )(stem"out"println7hello8>

    ,")ave and close %elloClient"+ava"

  • 8/10/2019 Corba Example

    11/21

    Developing the %ello &orld )erver

    %ere-s the complete version of %ello)erver"+ava"

    11 2#e pacage containing our stu,s!

    import HelloApp!0;

    11 Hello*erver 'ill use t#e naming service!

    import org!omg!Cos3aming!0;

    11 2#e pacage containing special e:ceptions t#ro'n ,y t#e name service!

    import org!omg!Cos3aming!3amingConte:t

  • 8/10/2019 Corba Example

    12/21

    }

    }

    class Hello*ervant e:tends +Hello$mplBase

    {

    pu,lic *tring sayHello() {

    return .>nHello 'orld??>n.;

    }

    }

  • 8/10/2019 Corba Example

    13/21

    mporting Re ;; %ello)erver $ill use the naming service"

    import org"omg"Cosaming"?>

    ;; The package containing special exceptions thro$n b( the name service" import org"omg"Cosaming"amingContext4ackage"?>

    ;; All CORBA applications need these classes"

    import org"omg"CORBA"?>

    Declaring the )erver Class

    Declare the server class2

    public class %ello)erver :

    ;; Add the main method here in the next step" =

    Defining the main 6ethod

    Declare a standard main method2

    public static void main7)tring args8 : ;; Add the tr(5catch block here in the next step"

    =

    %andling CORBA )(stem Exceptions

    Because all CORBA programs can thro$ CORBA s(stem exceptions at runtime. (ou$ill place all of the main functionalit( $ithin a tr(5catch block" CORBA programs thro$

    runtime exceptions $henever trouble occurs during an( of the processes 7marshaling.

    unmarshaling. upcall8 involved in invocation" The exception handler simpl( prints the

    exception and its stack trace to standard output so (ou can see $hat kind of thing hasgone $rong"

    nside main. set up a tr(5catch block2

    tr( :

    ;; Add the rest of the %ello)erver code here" = catch7Exception e8 :

    )(stem"err"println7FERROR2 F 9 e8>

    e"print)tackTrace7)(stem"out8>

    =

  • 8/10/2019 Corba Example

    14/21

    Creating an ORB Ob+ect

    Just like a client. a CORBA server also needs a local ORB ob+ect" Ever( server

    instantiates an ORB and registers its servant ob+ects so that the ORB can find the server

    $hen it receives an invocation for it"

    nside %ello)erver"+ava-s tr(5catch block. declare and initialiGe an ORB variable2

    ORB orb H ORB"init7args. null8>

    The call to the ORB-s init method passes in the server-s command line arguments.

    allo$ing (ou to set certain properties at runtime"

    6anaging the )ervant Ob+ect

    A server is a process that instantiates one or more servant ob+ects" The servantimplements the interface generated b( idlto+ava and actuall( performs the $ork of the

    operations on that interface" Our %ello)erver needs a %ello)ervant"

    nstantiating the )ervant Ob+ect

    nside the tr(5catch block. +ust belo$ the call to init. instantiate the servant ob+ect2

    %ello)ervant helloRef H ne$ %ello)ervant78>

    This servant class isn-t defined (et> (ou $ill do that in a later step" ext. connect the

    servant to the ORB. so that the ORB can recogniGe invocations on it and pass them along

    to the correct servant2

    orb"connect7helloRef8>

    Defining the )ervant Class

    At the end of %ello)erver"+ava. outside the %ello)erver class. define the class for the

    servant ob+ect"

    '"Declare the servant class2

    class %ello)ervant extends %ellomplBase :

    ;; Add the sa(%ello method here in the next step"

    =

    *"The servant is a subclass of %ellomplBase so that it inherits the general CORBA

    functionalit( enerated for it b( the compiler"

    ,"Declare the re

  • 8/10/2019 Corba Example

    15/21

    public )tring sa(%ello78 :

    ;; Add the method implementation here in the next step" =

    /"&rite the sa(%ello implementation2

    return Fn%ello &orldKKnF>

    &orking $ith CO) aming

    The %ello)erver $orks $ith the naming service to make the servant ob+ect-s

    operations available to clients" The server needs an ob+ect reference to the name service.so that it can register itself and ensure that invocations on the %ello interface are routed to

    its servant ob+ect"

    Obtaining the nitial aming Context

    n the tr(5catch block. belo$ instantiation of the servant. callorb"resolveinitialreferences to get an ob+ect reference to the name server2

    org"omg"CORBA"Ob+ect ob+Ref H orb"resolveinitialreferences7Fame)erviceF8>

    The string ame)ervice is defined for all CORBA ORBs" &hen (ou pass in that

    string. the ORB returns a naming context ob+ect that is an ob+ect reference for the nameservice"

    arro$ing the Ob+ect Reference

    As $ith all CORBA ob+ect references. ob+Ref is a generic CORBA ob+ect" To use it as

    a amingContext ob+ect. (ou must narro$ it to its proper t(pe" Add the call to narro$+ust belo$ the previous statement2

    amingContext ncRef H amingContext%elper"narro$7ob+Ref8>

    %ere (ou see the use of an idlto+ava 5generated helper class. similar in function to

    %ello%elper" The ncRef ob+ect is no$ an org"omg"Cosaming"amingContext and (ou

    can use it to access the naming service and register the server" #ou $ill do that in the nextstep"

    Registering the )ervant $ith the ame )erver

    '"Just belo$ the call to narro$. create a ne$ ameComponent member2

    ameComponent nc H ne$ ameComponent7F%elloF. FF8>

  • 8/10/2019 Corba Example

    16/21

    *"This statement sets the id field of nc. the ne$ ameComponent. to F%elloF and the

    kind component to the empt( string" Because the path to the %ello has a single element.create the single5element arra( that amingContext"resolve re

    ,"Iinall(. pass path and the servant ob+ect to the naming service. binding the servant

    ob+ect to the F%elloF id2

    ncRef"rebind7path. helloRef8>

    o$. $hen the client calls resolve7F%elloF8 on the initial naming context. the namingservice returns an ob+ect reference to the %ello servant"

    &aiting for nvocation

    The server is read(> it simpl( needs to $ait around for a client to re

    =

    This form of Ob+ect"$ait re

  • 8/10/2019 Corba Example

    17/21

    Running the Client5)erver Application

    '"Irom an 6)5DO) s(stem prompt 7&indo$s8 or command shell 7@L8. start the

    Java D! name server2

    tnameserv 5ORBnitial4ort nameserverport

    ote that na(eserverportis the port on $hich (ou $ant the name server to run" f(ou do not specif( this. port MNN $ill be chosen b( default" Also note that using )olaris

    soft$are. (ou must become root to start a process on a port under 'N*/" Ior this reason.

    $e recommend that (ou use a port number greater than or e

  • 8/10/2019 Corba Example

    18/21

    Troubleshooting

    *pecifying ORB $nitial

  • 8/10/2019 Corba Example

    19/21

    Commands to run the CORBA example

    '" Command to invoke the idlto+ava compiler

    idltoava fno cpp Hello!idl

    *" Command to compile %elloClient"+ava and %ello)erver"+ava

    avac cp ! 0!ava HelloApp>0!ava

    ," Command to run the name server on port *NNN

    timeserv ORB$nitial

  • 8/10/2019 Corba Example

    20/21

    Applet 3ote- &hile this lesson focuses on $riting a CORBA client application. man( of

    the steps are identical to those re

    ;; All CORBA applications need these classes"

    import org"omg"CORBA"?>

    ;; eeded for the applet"import +ava"a$t"1raphics>

    public class %elloApplet extends +ava"applet"Applet

    : public void init78

    :

    tr(:

    ;; Create and initialiGe the ORB

    ORB orb H ORB"init7this. null8>

    ;; 1et the root naming context

    org"omg"CORBA"Ob+ect ob+Ref H orb"resolveinitialreferences7Fame)erviceF8>

    amingContext ncRef H amingContext%elper"narro$7ob+Ref8>

    ;; Resolve the ob+ect reference in naming

    ameComponent nc H ne$ ameComponent7F%elloF. FF8>

    ameComponent path H :nc=> %ello helloRef H %ello%elper"narro$7ncRef"resolve7path88>

    ;; Call the %ello server ob+ect and print the results message H helloRef"sa(%ello78>

    = catch7Exception e8 :

    )(stem"out"println7F%elloApplet exception2 F 9 e8>

    e"print)tackTrace7)(stem"out8>

    =

  • 8/10/2019 Corba Example

    21/21

    =

    )tring message H FF>

    public void paint71raphics g8

    :

    g"dra$)tring7message. *0. 0N8> =

    =

    )etting @p the %T6! Iile 7Applets Onl(8

    Tutorial"html is provided for displa(ing (our finished applet. but (ou need tocustomiGe a fe$ attributes and

    parameters"

    '"Open Tutorial"html in (our text editor"*"nside the A44!ET tag. enter the path to (our pro+ect director( as the value for the

    CODEBA)E attribute","n the first 4ARA6 tag. enter the name of the machine $here the CORBA name

    server runs 7most likel( (our local machine name8 as the value for ORBnitial%ost"

    /"6ake sure the second 4ARA6 tag is set to the value of ORBnitial4ort that (ouare using to run thename server 7it-s preset to 'N0N to $ork $ith the default used in the

    examples in this trail8" n an( case. it should be a value above 'N*P"