Week 5 Mapping IDL to C and Java - PowerPoint PPT Presentation

1 / 51
About This Presentation
Title:

Week 5 Mapping IDL to C and Java

Description:

... time and raises the exception CORBA::MARSHALL if exceeded ... to a Java final class that provides fields for the members of the struct and some constructors ... – PowerPoint PPT presentation

Number of Views:76
Avg rating:3.0/5.0
Slides: 52
Provided by: wolfgang95
Category:
Tags: idl | fields | java | mapping | marshall | week

less

Transcript and Presenter's Notes

Title: Week 5 Mapping IDL to C and Java


1
Week 5Mapping IDL to C and Java
2
IDL Language Mappings
  • How do we code the behavior for an IDL interface
    in a real programming language
  • Mapping to a language involves following a set of
    rules for the target programming language
  • Follow the rules, and all will be fine-

3
IDL-C Mapping
  • By an example

4
Stock-Online IDL
module Stock // // The exception DBError is
used to indicate some sort of failure in the //
back-end database. All operations that access
the database // can raise a DBError. It
contains a single string which represents // a
textual description of the error. // exception
DBError string reason // Consts/Typedefs
for zero terminated strings const short
subscriberName 30 1 typedef
stringltsubscriberNamegt TSubscriberName const
short subscriberAddr 60 1 typedef
stringltsubscriberAddrgt TSubscriberAddr const
short stockCode 4 1 typedef
stringltstockCodegt TStockCode const short
MAX_ENTRIES 20
5
Stock-Online IDL
// structures struct TSubscriber
TSubscriberName sName TSubscriberAddr sAdd
r unsigned long sCredit struct
TStockHolding unsigned long
stock_id unsigned long amount //
Sequence Typedefs to handle return of Stock
Holdings // (0-20 possible) typedef
sequenceltTStockHolding, MAX_ENTRIESgt
TStockHoldingSeq
6
Stock-Online IDL
interface SubscriberServices void
CreateAccount( in TSubscriber subscriberInfo,
out unsigned long subAccNo ) raises
(DBError) void UpdateAccount( in unsigned long
subAccNo, in unsigned long sCredit) raises
(DBError) void QueryStockValueByID(in unsigned
long stockID, out float currentVal, out
float highVal, out float lowVal ) raises
(DBError) void QueryStockValueByCode(in
TStockCode stockCode,out float currentVal,
out float highVal, out float lowVal )
raises (DBError) void BuyStock (in unsigned
long subAccNo, in unsigned long stockID, in
unsigned long amount ) raises (DBError) void
SellStock (in unsigned long subAccNo, in unsigned
long stockID, in unsigned long amount )
raises (DBError) void GetHoldingStatement
(in unsigned long subAccNo, in unsigned long
startStID, out TStockHoldingSeq stockList )
raises (DBError) //end SubscriberServices
7
Servant Class
include "stock.hh //generated by IDL
compiler namespace Stock class
SubscriberServices_i public virtual
SubscriberServicesBOAImpl public SubscriberSer
vices_i() // programmer defined SubscriberServi
ces_i() virtual void CreateAccount
(const TSubscriber subscriberInfo,
CORBAULong subAccNo, CORBAEnviron
ment IT_env) throw (DBError)
virtual void UpdateAccount (CORBAULong
subAccNo, CORBAULong sCredit, CORBA
Environment IT_env) throw (DBError)
virtual void QueryStockValueByID ( CORBAULong
stockID, CORBAFloat currentVal,
CORBAFloat highVal, CORBAFloat
lowVal, CORBAEnvironment IT_env)
throw (DBError)
8
Servant Class (cont)
virtual void QueryStockValueByCode (const char
stockCode, CORBAFloat currentVal,
CORBAFloat highVal, CORBAFloat
lowVal, CORBAEnvironment IT_env)
throw (DBError) virtual void BuyStock
( CORBAULong subAccNo, CORBAULong
stockID, CORBAULong amount,
CORBAEnvironment IT_env) throw
(DBError) virtual void SellStock ( CORBAULong
subAccNo, CORBAULong stockID,
CORBAULong amount, CORBAEnvironment
IT_env) throw (DBError) virtual void
GetHoldingStatement ( CORBAULong subAccNo,
CORBAULong startStID, TStockHoldingS
eq stockList, CORBAEnvironment IT_env)
throw (DBError) // end class and
namespace
9
Object Implementation
  • Lets look at the C to implement some of these
    functions
  • Focus on
  • handling of parameters
  • memory management for in/out parameters
  • exceptions

10
CreateAccount - Client Code
StockTSubscriber subInfo // Allocate strings
to pass to server subInfo.sName
CORBAstring_alloc(StocksubscriberName )
subInfo.sAddr CORBAstring_alloc(Stocksubsc
riberAddr ) // get name and address values
somehow - omitted try subObject-gtCreateAccoun
t (subInfo, subAccNo ) catch (const
StockDBError ex) cerr ltlt " (Database error
occured)" ltlt endl cerr ltlt " " ltlt ex.reason ltlt
endl catch (CORBASystemException
ex) cerr ltlt " (system exception -
transaction failed)" ltlt endl cerr ltlt " " ltlt ex
ltlt endl
11
CreateAccount - Simple Server Code
void StockSubscriberServices_i CreateAccount
(const StockTSubscriber subscriberInfo,
CORBAULong subAccNo, CORBAEnvironment
IT_env) // display the input
parameters TRACE ("Create " ltlt
subscriberInfo.sName ltlt " " ltlt subscriberInfo.
sAddr ltlt " " ltlt subscriberInfo.sCredit )
// set the output parameter subAccNo 100
// silly example, always return 100 to
client //exception throwing code
omitted TRACE ("Exit Create Account" )
12
QueryByCode - Client
char sCodeStockstockCode float currVal,
highVal, lowVal cout ltlt "Query Stock Value By
Code" ltlt endl cout ltlt "Enter Stock Code (max 4
chars) " cin.ignore(256, '\n') cin.getline(sCo
de, StockstockCode) try subObj-gtQueryStockV
alueByCode (sCode, currVal, highVal, lowVal )
cout ltlt "Query Transaction Successful" ltlt
endl cout ltlt "Stock Code " ltlt sCode ltlt "
Current " ltlt currVal ltlt " High " ltlt highVal
ltlt " Low " ltlt lowVal ltlt endl catch
(const StockDBError ex) // lots missing
13
QueryByCode - Server
void StockSubscriberServices_i
QueryStockValueByCode (const char stockCode,
CORBAFloat currentVal, CORBAFloat
highVal, CORBAFloat lowVal,
CORBAEnvironment IT_env) // display input
value TRACE ("Query by Code" ltlt stockCode
) // set output values to silly numbers -
example only currentVal highVal lowVal 99
TRACE ("Exit Query by Code" )
14
GetHoldingStatement - Client
unsigned long startStID 0 int i cout ltlt
"STATEMENT LISTING" ltlt endl subAccNo 99 //
just an example - needs to be set StockTStockHo
ldingSeq list NULL //initialize the
sequence to NULL try subObject-gtGetHoldingSta
tement (subAccNo, startStID, list ) cout ltlt
"Stock ID \t " ltlt "Amount" ltlt endl cout ltlt
"----------------------------" ltlt endl //
Print out each entry in the sequence returned
from the server for (i 0 i lt list-gtlength()
i ) cout ltlt (list)i.stock_id ltlt "\t\t"
ltlt (list) i.amount ltlt endl catch
// etc
15
GetHoldingStatement - Server
void StockSubscriberServices_i
GetHoldingStatement (CORBAULong subAccNo,
CORBAULong startStID, StockTStockHoldingSeq
stockList, CORBAEnvironment IT_env)
TRACE ("Statement" ltlt subAccNo ltlt "starting
at" ltlt startStID ) CStockHolding list20
// non-ORB version of TStockHoldingSeq short
len // query the database to get the results -
details not important int result
GetHoldingList ( subAccNo, startStID, list, len)
if (result ! SUCCESS ) throw ex //
setting exception values omitted stockList
new TStockHoldingSeq // ORB frees
this... stockList-gtlength(len) for (int i 0
i lt len i ) (stockList)i.stock_id
listi.stock_id (stockList)i.amount
listi.amount TRACE ("Exit Get Holdings "
)
16
IDL-Java Mapping
17
Mapping of Module
  • IDL module is mapped to a Java package with the
    samename.
  • All IDL type declarations within the module are
    mapped tothe corresponding Java class or
    interface declarationswithin the generated
    package.
  • //IDL
  • module finance     
  • interface cash     ...     
  • //Java
  • package finance     
  • public interface cash     ...

18
Mapping of Basic Types

19
Mapping to Basic Types
  • Holder Classes
  • Accommodate the passing of out and inout
    parameters in Java
  • Predefined and user-defined types
  • User-defined types generated by IDL compiler
  • Predefined types provided by CORBA class library
  • Predefined holder classes are defined in
    org.omg.CORBA
  • Client creates object framework and passes it on
    to the server. Server fills the items and returns
    the same

20
Mapping to Basic Types
Example Package org.omg.CORBA final public class
IntHolder public int value public
IntHolder() public IntHolder(int
initial) valueinitial
21
Mapping to Basic Types
  • Generic Pattern
  • Holder class for a user defined TYPE is defined
    according to the following pattern
  • final public class TypeHolder
  • public Type value
  • public TypeHolder()
  • public TypeHolder(Type initial)
  • public void
  • _read(org.omg.CORBA.portable.InputStream i)
  • public void _write(org.omg.CORBA.portable.O
    utputStream o)
  • public org.omg.CORBA.TypeCode _type()

22
Mapping to Basic Types
  • Generic Pattern
  • The _read() and _write() methods are used for
    marshalling code
  • The _type() method provides a easy way to access
    the TypeCode of a user-defined type

23
Mapping to Basic Types
  • String Types
  • IDL defines strings that are bounded or unbounded
  • All IDL strings are mapped to the Java object of
    class java.lang.String
  • The stub code generated from the IDL checks the
    correctness of the string bounded at run time and
    raises the exception CORBAMARSHALL if exceeded
  • Character range violations cause a
    CORBADATA_CONVERSION exception
  • Bounds violations cause a CORBABAD_PARAM
    exception to be raised

24
Mapping to Basic Types
  • String Types
  • The holder class for strings and wide strings is
    defined in the package org.omg.package as
    follows
  • final public class StringHolder
  • public java.lang.String value
  • public StringHolder()
  • public StringHolder(java.lang.String initial)
  • valueinitial

25
Mapping for Constants
  • IDL constants are generally mapped to a static
    final variable that has a value of the constant.
  • static ensures that instances of that class have
    the same value
  • final ensures that the value cannot be overridden

26
Mapping for Constants
  • //IDL Code
  • interface Tester
  • const short MaxSlots 8
  • //generated Java
  • public interface Tester extends
    org.omg.CORBA.Object
  • final public static short MaxSlots (short) 8
  •  

27
Mapping for Constants
  • Constants not within an interface
  • Constants not declared within an interface are
    mapped to public interface with the same name as
    the constant and containing a field, namely
    value, that holds the constants value
  • Example
  • // IDL Code
  • module Exampleconst long aLongerOne-123
  • // Generated Java
  • package Example
  • public interface aLongerOne
  • int value (int) (-123L)

28
Mapping for Complex Types
  • Helpers
  • All user defined (complex) IDL types have an
    additional abstract helper Java class with the
    suffix appended to the type name generated.
  • Generic pattern
  • abstract public class lttypenamegtHelper
  • public static void
  • insert(org.omg.CORBA.Any a, lttypenamegt t)
  • public static lttypenamegtextract (Any a)
  • public static org.omg.CORBA.TypeCode type()
  • public static String id()
  • // stuff missing
  • public static lttypenamegt //for interface
    only narrow(org.omg.CORBA.Object obj)

29
Mapping for Complex Types
  • Struct
  • IDLs struct is mapped to a Java final class that
    provides fields for the members of the struct and
    some constructors
  • The class is named after the struct
  • There is a constructor which has a parameter for
    each member of the struct and initializes the
    object properly
  • A second constructor, null, creates the object.
    The values of the structure members have to be
    filled in later

30
Mapping for Complex Types
  • Struct example
  • // IDL Code
  • Struct TestStruct
  • short a_short
  • long a_long
  • This is mapped to the following
  • // Java Code
  • final public class TestStruct
  • public short a_short
  • public int a_long
  • public TestStruct()
  • public TestStruct (short a_short, int a_long)
  • this.a_shorta_short
  • this.a_longa_long

31
Typedefs
  • Typedef
  • Java has no aliasing for types
  • Helper classes are generated for all typedefs
  • The base type has to be used where the typedef
    name is expected in the Java implementation
  • An exception to this rule is that new types are
    generated for IDL array and sequence typedefs

32
Typedefs
  • Arrays
  • IDL arrays are mapped to Java arrays
  • No Java type or class are generated
  • Example
  • //IDL array
  • typedef long long_array105
  • //Java- a programmer has to declare and allocate
    the array in the Java application code
  • int a_long_array
  • a_long_array new int105

33
Sequences
  • Sequence
  • Sequences are mapped similarly to arrays
  • The bound of bounded sequences is checked at run
    time and the exception
  • BAD_PARAM is raised if it is violated. Its the
    application programmers
  • responsibility to declare and create a Java
    array of the corresponding
  • member type.
  •  //IDL
  • typedef sequenceltlong, 10gtbounded_10_seq
  • typedef sequenceltlonggtunbounded_seq
  •      

34
Sequences
  • final public class unboundedHolder implements
                            org.omg.CORBA.portable.St
    reamable     
  • public int value     public
    UnboundedDataHolder()     
  • public UnboundedDataHolder(int initial) ...
  • public void _read(org.omg.CORBA.portable.InputStr
    eam i) ...     
  • public void _write(org.omg.CORBA.portable.OutputS
    tream o) ...     
  • public org.omg.CORBA.TypeCode _type() ...
  • final public class boundedHolder implements
               org.omg.CORBA.portable.Streamable
        
  • public int value     public
    BoundedDataHolder()     
  • public BoundedDataHolder(int initial) ...
        
  • public void _read(org.omg.CORBA.portable.InputStr
    eam i) ...     
  • public void _write(org.omg.CORBA.portable.OutputS
    tream o) ...     
  • public org.omg.CORBA.TypeCode _type() ...

35
Mapping for Complex Types
  • Operations to Methods
  • IDL operations are mapped to Java methods of the
    same name.
  • IDL void return type is mapped according to Java
    void.
  • IDL in parameters are mapped according to the
    mapping for IDL types
  • IDL inout and out parameters are mapped to the
    Holder classes which aregenerated for their IDL
    type.
  • IDL operations can explicitly raise one or more
    user-defined exceptions and can also implicitly
    raise system exceptions.
  • Raise clause of an IDL operation is mapped to a
    throw clause on the equivalent Java method.

36
Interfaces
  • Example
  • //IDL
  • Exception SomethingWrong
  • string reason
  • long id
  •  
  • interface Tester
  • boolean test(in string name, inout boolean flag,
    out long id)
  • raises(SomethingWrong)
  • //Java
  • Public interface Tester extends
    org.omg.CORBA.Object
  • Public boolean test( java.lang.String name,
  • org.omg.CORBA.BooleanHolder flag,
  • org.omg.CORBA.LongHolder id )
  • throws SomethingWrong

37
Interfaces
  • Interfaces
  • An interface type is mapped to a Java public
    interface of the same name
  • An IDL interface type is mapped to a Java public
    interface of the same name.
  • Attributes are mapped to a pair of Java accessor
    and modifier methods. These methods have the same
    name as the IDL attribute and are overloaded.
    There is no modifier method for IDL read only
    attributes.
  •  

38
Mapping for Complex Types
  • Example
  • IDL
  • module Example     
  • interface Face         
  • long method (in long arg) raises (e)
  •    attribute long assignable
  • readonly attribute long nonassignable     
  • Java
  • package Example  
  • public interface Face extends org.omg.CORBA.Objec
    t     
  • int method(int arg) throws Example.e
  • int assignable()
  • void assignable(int i)
  • int nonassignable()
  •  

39
Mapping for Complex Types
  • //Helper class
  • public class FaceHelper      // standard helper
    methods
  • public static void insert(org.omg.CORBA.Any a,
    Face t)
  • ...    
  • public static Face extract (Any a) ...    
  • public static org.omg.CORBA.TypeCode type()...
  • public static String id() ...    
  • public static Face read(org.omg.CORBA.port
    able.InputStream istream) ...
  • public static void write(org.omg.CORBA.por
    table.OutputStream ostream, Facevalue)
    ...     
  • // interface specific narrow method    
  • public static Face narrow(org.omg.CORBA.Object
    obj)
  • ...  
  •  

40
Mapping for Complex Types
  • // Holder class
  • final public class FaceHolder implements
    org.omg.CORBA.portable.Streamable     
  • public Face value     
  • public FaceHolder()     
  • public FaceHolder(Face initial) ...     
  • public void _read(org.omg.CORBA.portable.InputStr
    eam i) ...     
  • public void _write(org.omg.CORBA.portable.Output
    Stream o)
  • ...     
  • public org.omg.CORBA.TypeCode _type() ...

41
Mapping for Exceptions
  • IDL Exception
  • Mapped very similarly to structs.
  • Mapped to Java class that provides instance
    variables for the fields of exception and
    constructors.
  • User-defined exceptions system exceptions

42
Mapping for Exceptions
  • Example
  • // IDL code
  • Exception SomethingWrong
  • string reason
  • long id

43
Mapping for Exceptions
  • The generated Java code is
  • final public class SomethingWrong extends
  • org.omg.CORBA.UserException
  • public java.lang.String reason
  • public int id
  • public SomethingWrong ( )
  • public SomethingWrong (java.lang.String reason,
    int id) this.reasonreason
  • this.idid

44
Mapping for Exceptions
  • The generated holder class is
  • final public class SomethingWrongHolder
    implements
  • org.omg.CORBA.portable.Streamable
  • public SomethingWrong value
  • public SomethingWrongHolder ( )
  • public SomethingWrongHolder (SomethingWrong
    value)
  • this.value value
  • // stuff missing

45
Mapping for Exceptions
  • The table below lists all CORBA system exceptions
    and their mapping to Java counterparts

46
Memory Management Issues
  • Java provides automatic garbage collection
  • Greatly simplifies the memory management issues
    associated with the IDL to Java mapping
  • However, an CORBA client programmer should
    understand where memory needs to be allocated for
    IDL operation parameters.

47
Semantics of in, out and inout
  • in
  • passed-by-value
  • passed-by-reference
  • out inout
  • passed-by-reference

48
Parameters
  • In Parameters
  • All IDL basic types except type Any and String
    are passed-by-value
  • Struct, union, sequence, array and Any are
    passed-by-reference as in the form of holder
    class
  • Out/inout parameters
  • All IDL basic and user defined types are
    passed-by-reference in the form of holder class

49
Parameter Passing Example
  • IDL
  • module Example
  • interface Modes
  • long operation(in long inArg, out long outArg,
    inout long inoutArg)

50
Parameter Passing Example
  • Java
  • package Example
  • public interface Modes
  • long operation(int inArg, IntHolder outArg,
    IntHolder inoutArg)

51
Example Client
  • // set the in actual value
  • int inArg 99
  • // prepare to receive out
  • IntHolder outHolder new IntHolder()
  • // set up the in side of the inout
  • IntHolder inoutHolder new IntHolder(199)
  • // make the invocation
  • int result target.operation(inArg, outHolder,
    inoutHolder)
  • // use the value of the outHolder
  • outHolder.value...
  • // use the value of the inoutHolder
  • inoutHolder.value...
Write a Comment
User Comments (0)
About PowerShow.com