CSIS 0402 System Architecture Distributed Computing Middleware Technology

CSIS 0402 System Architecture Distributed Computing - Middleware Technology (2) K. P. Chow University of Hong Kong

Object Middleware l l l Calling an operation of an object that resides in another system Object in another system is accessed through the object reference Processing steps: 1. Get a reference to an object 2. Call an operation of the object l E. g. Account. Ref = Account. Set. Get. Account(012345678); Account. Ref. Get. Name. And. Balance(…); // print account’s detail RPC: … Get. Name. And. Balance(012345678, // debit the account Name, Balance); Accrount. Ref. Debit(100); // print account’s detail Debit(012345678, 100);

How to obtain an object reference? Possible approaches: l A special object reference is returned to the client when it first connected to the middleware l The client calls a special “naming” service (directory) with a name, and the directory returns the object reference l The client requests that a new object of a certain class is created (through the object factory) and the new object reference is returned l An operation on one object returns a reference to another object

Object Middleware Compilation and Interpretation Client Interface Definition Language (IDL) Unlike RPC: l Operations can be called through an interpretative interface Interface Repository Skeleton Stub Interpreted Runtime Environment Skeleton Similar to RPC: l Operation is declared in IDL l IDL generates stubs and skeletons Stub Compiled Runtime Environment Server

Why use object middleware instead of RPC? l l Object middleware fits naturally with object-oriented languages: data and logic are in the object Object middleware is more flexible: – Interface is de-linked from the program, e. g. an interface can be implemented by many servers – Updates to interface can be incremented, i. e. a server object can keep both the old and the new interfaces simultaneously l Force the programmer writing the client to think about state in the server

Object Middleware Technologies l CORBA – Object can last for ages – One can convert a CORBA object reference to a string and send the string to somebody else, the recipient can write a program that convert the string back to an object reference and calls the object l COM/DCOM – COM object is pinned to a memory location and belongs to an active object – Object is automatically deleted when reference count to the object is 0 l Java RMI (Remote Method Invocation)

CORBA l l l A standard, not a product; developed by OMG (Object Management Group, a consortium of major software DI: Dynamic Invocation vendors and user organizations) OI: ORB Interface Common Object Request Broker Architecture SIS: Static IDL Skeleton DS: Dynamic IDL Skeleton CORBA model: Object Implementation Client IDL Stub D I O I ORB Core SIS Object Adapter ORB Core IIOP DS

CORBA Structure l l CORBA defines GIOP (General Inter-ORB Protocol), IIOP is GIOP over TCP/IP CORBA client programming: – Initialization – Obtain object reference – Call operations on object reference l l l ORB is also an object Many services, naming, event notification, transaction, … 2 kinds of interfaces: – Compiled interface: IDL is used to create the IDL stub for client and Static IDL skeleton for the server, stub and skeleton are used to marshal the parameters – Interpreted interface: client use Interface Repository to find out about the interface, server uses Dynamic Skeleton for interpreted interface

Compiled Interface // Interface definition interface This. Or. That. Server { string do. This(in string what); string do. That(in string what); }; idltojava This. Or. That. Server public interface This. Or. That. Server extends org. omg. CORBA. Object { String do. This(String what); String do. That(String what); } public class. This. Or. That. Server. Impl extends _This. Or. That. Server. Impl. Base { public This. Or. That. Server. Impl() { } public String do. This(String what) { return do. Something(“this”, what); } public String do. That(String what) { return do. Something(“that”, what); } private String do. Something(String todo, String what) { … } } // client implementation obj. Ref = object referemce of the target object; obj. Ref. do. This(“what”);

Java RMI l l RMI is a java-specific version of a CORBA framework An object on one system can call a method in an object somewhere else on the network Client Skeleton l Stub Client method calls into local stub Server Skeleton calls server method RMI characteristics: l Objects are sent between client and server across the network l Type of the object arguments are sent with the data l If class doesn’t exist on the receiving side, the class object is also loaded over the network (mobile code)

COM – Component Object Model l l From Microsoft, a standard for components Designed to support OLE: Object Linking and Embedding Technology A COM component: a separate code file that has one or more object interfaces (not source, and can be written in any language) To start a COM component, the client needs to supply: – Class ID (CLSID): 128 -bit integer, used to search the registry for the component’s code file and initiates it if needed – Interface ID (IID): 128 integer, use to identify the required interface – Both can be generated by the system – When an object is created, an interface pointer is returned of type IID l Details to be discussed

What is COM? l l A distributed object architecture Objects can be written in any language, adhere to a few rules, the objects will be able to communicate with client applications that want to use them and with one another across process and network boundaries Object exports its functionalities by interfaces, which are groups of logically related functions Example in C++ class ICalc { public: virtual Add()=0; virutal Subtract()=0; }; class IFinancial { public: virtual Get. Mortgage. Payment()=0; virtual Get. Prime. Rate()=0; }; class Calc. Object: public ICalc, public IFinancial { };

Interface l l l Act as a contract between client and object: all methods declared in the interface must be defined Clients can think about objects as black boxes that support one or many interfaces Server object can implement the functions any way it chooses Add() IUnknown Subtract() ICalc Com. Calc Get. Mortgage. Payment() Get. Prime. Rate() IFinancial

Origin of COM Interfaces Client proxy l RPC – Proxy: an entity that stands in for something else and appears to a client to be an actual instance of that something else – Stub: proxy’s packaged information is unpackaged by an entity called stub on the server l RPC IDL: – IDL compiler (MIDL. EXE) generates C code from an. IDL file, generates code defines a proxy and stub – E. g. [ uuid (C 2557720 -CA 46 -1067 -B 31 C-00 DD 010662 DA), ] interface Calc { int Add([in] int x, [in] int y); int Subtract([in] int x, [in] int y); } COM object stub

COM and RPC l l Today’s COM is built on RPC Additional features: – Support for languages other than C – Support for objects, not just flat, ANSI-C style functions – Capability to automatically find and run server when client requests it l MS implementation: type library (TLB file) – – a universal, binary header file a compiled, tokenized form of an IDL file contains all the function prototypes, interface definitions, coclasses can be parsed and read by an application or development environment using Win 32 functions – used for marshalling, i. e. construct the proxy and stubs necessary for communication – does not need to compiled into the client and server: COM read the type library at runtime

COM object definition in C++ (1) import “oaidl. idl”; import “ocidl. idl”; [ object, uuid(638094 E 5 -758 F-11 d 1 -8366 -0000 E 83 B 6 EF 3), dual, … ] interface ICalc : IUnknown { [id(1), helpstring(“method Add”)] HRESULT Add([in] int x, [in] int y, [out, retval] int *r); [id(2), helpstring(“method Divide”)] HRESULT Divide([in] int x, [in] int y, [out, retval] int *r); };

COM Object definition in C++ (2) [ uuid(638094 E 0 -758 F-11 d 1 -8366 -0000 E 83 B 6 EF 3), version(1. 0), helpstring(“calcsdk 1. 0 Type Library”) ] library COMCALCLib { importlib(“stdole 32. tlb”); importlib(“stdole 2. tlb”); [ uuid(638094 E 0 -758 F-11 d 1 -8366 -0000 E 83 B 6 EF 3), helpstring(“Calc Class”) ] C++ class implements coclass Calc. SDK ICalc interface { [default] interface ICalc; }; };

C++ Implementation #include “comcalc. h” class Calc. SDK: public ICalc { public: STDMETHODIMP Query. Interface(REFIID riid, void **ppv); STDMETHODIMP _(ULONG) Add. Ref(void); STDMETHODIMP _(ULONG) Release(void); STDMETHOD(Add)(int x, int y, int *r) { *r = x + y; return S_OK; } STDMETHOD(Subtract)(int x, int y, int *r) { *r = x - y; return S_OK; } Com. Calc() { }; };

What is IUnknown? l l All interfaces must inherit from IUnknown contains 3 methods: – Query. Interface(): to enable clients to get one or many interfaces from objects – Add. Ref(): to control lifetime of the objects – Release() l Example: const GUID CLSID_Calc = {0 x 638094 e 0, 0 x 758 f, 0 x 11 d 1, {0 x 83, 0 x 66, 0 x 00, 0 xe 8, 0 x 3 b, 0 x 6 e, 0 xf 3} }; const GUID IID_ICalc = {0 x 638094 e 5, 0 x 758 f, 0 x 11 d 1, {0 x 83, 0 x 66, 0 x 00, 0 xe 8, 0 x 3 b, 0 x 6 e, 0 xf 3} }; const GUID IID_IFinancial = { … }; ICalc *p. ICalc; IFInancial *PIFin;

What is IUnknown? (cont) // create an instance of the object whose GUID is CLSID_Calc and get me // the interface of the object whose GUID is IID_ICalc and put that // interface in variable p. ICalc Co. Create. Instance(CLSID_Calc, NULL, CLSCTX_ALL, IID_ICalc, (void **)&p. ICalc); int result = p. ICalc->Add(1, 2); p. ICalc->Query. Interface(IID_IFinancial, (void **)&p. IFin); float rate = p. IFin->Get. Prime. Rate(); p. ICalc->Release(); p. IFin->Release(); Add() IUnknown Subtract() ICalc Com. Calc Get. Mortgage. Payment() Get. Prime. Rate() IFinancial

Dynamic Link Libraries (DLL) l l l In early Windows, DLLs were groupings of functions that could be loaded into an application when the application wanted them, and discarded when no longer needed, allowed EXEs to be smaller A binary standard: sources can be written in any language and compiled into DLLs, which can be interoperable: clean up the call stack, list exported functions, … COM DLL: – Other names for COM DLL: OCX, Active. X Component, Active. X Control, Active. X server – Repositories for COM objects (COM object is the actual implementation of a specific coclass in a type library) – A DLL can contain many objects – A DLL also has a. TLB file (type library): describes the objects and interfaces

COM DLL l Example: 2 coclasses and its type library IUnknown Resource Section ISome. Interface coclass 1 IUnknown IOther. Interface coclass 2 Type Library

Registry and Self-Registration l l Type library and component are 2 separate things, how can COM associate the two? Self-registration: COM object needs to know how to put the entries in the Registry, which can then be used later How COM DLL put the entry in the registry? (DLLs are loaded and acted upon by a host process (EXE)) 2 approaches: manually or use utility regsvr 32. exe, e. g. – regsvr 32. exe comcalc. dll

COM and Virtual Table l Virtual Table: – Also called vtable, used in C++ for dynamic binding – An array of pointers to virtual function – Used in COM to implement interface COMCalc: ICalc, IFinancial Address of ICalc vtable Query. Interface() Add. Ref() COM Interface Address IFinancial vtable Query. Interface() Add. Ref() Release() Mortgage. Payment () IFinancial: IUnknown Get. Prime. Rate () Release() Add () Subtract () ICalc: IUnknown Virtual Table

Putting it all together l RPC, DLL, Type Libraries, vtables COM DLL RPC Type Library Client IUnknown request COM service coclass 1 Service Control Manager ISome. Interface vtable Registry regsvr 32. exe xyz. dll

COM Object Creation l Call Co. Create. Instance with arguments: CLSID and IID, and others: 1. Map the given CLSID to an actual component that contains the requested class, and the mapping is stored in the registry 2. Create a COM object that is instantiated from the COM class 3. Return an interface of the request type IID l The registry stores which servers are available and which classes they support: 3 types of servers – In-process server: objects that live in the client’s process, started by loading and linking a DLL – Local server: objects on the same machine, but in a separate process, started by a separate executable (EXE) – Remote server: objects on a different machine, started by loading and starting the required server on the remote machine through the service control manager (SCM)

Compound Document and OLE l l OLE is Microsoft’s compound document standard OLE is a single document-centric paradigm – Some objects are created and existed in OLE setting only, e. g. Active. X object – Some standard-alone applications can be integrated with OLE l l OLE can be viewed as a collection of predefined COM interfaces OLE compound document can be divided into following parts: – Document server: provides some content model and the capabilities to display and manipulate that content – Document container: has no native content, but can accept parts provided by arbitrary document servers – Some document containers are also document servers, e. g. MS Word, Excel, …

OLE – Object Linking and Embedding l Object embedding: – In-place editing in MS Office applications – In-place activation: the container has to hand off part of the container’s screen state to the server of an embedded part, need to change all menus, toolbars, and others l Object linking: – A container stores a reference to the linked object – The linked object advises the container of changes

OLE Containers and Servers l l l In-process server: container and server are in the same process Local out-of-process server: need a “representative” of the server object, executing in the container process, called in-process handler Remote server: an out-of-process server on a remote machine In-process handler Container Local server IOle. Client. Site IAdvise. Sink Client site object IOle. Object IData. Object IPersistent. Storage Others

Controls l Visual Basic controls (VBX): – Simple and fixed model: controls are embedded into forms – A form binds the embedded controls together and allows the attachment of scripts that enable the controls to interact – Entire applications can be assembled by composing controls into forms l OLE controls (OCX): – Migrate the VBX concept to the OLE containers – OCXs are COM objects – To qualify as an OLE control, a COM object has to implement a large number of interfaces, e. g. IOle. In. Place. Active. Object, IOle. In. PLace. Object, … l Active. X controls: – Revised specification for OCX: large number of features and interactions, and all of them are optional – Active. X controls are COM objects supported by a special server – Has to be implemented by a self-registering server

Building Application out of OCX Controls Elementary word processor made from OCX controls Spell checker Compound document Word processing data Page layout Print services Screen layout Controls can be reused Spreadsheet data Arith. evaluation Elementary spreadsheet from OCX controls Server Screen layout Numeric I/O Table Container

Transactional Component Middleware l l Also called COMWare and Object Transaction Managers (OTM) Covers following technologies – Microsoft Transaction Server (MTS), part of COM+ – Enterprise Java Beans EJB – CORBA-based standard for transactional component middleware from OMG l Components: – Runtime code with an object interface that can be taken out of one context and run in another – E. g. COM code files and EJB

COM Component vs. Enterprise Java Beans l COM component – Run in any Windows operating systems – DCOM to call remote COM objects – Can call database systems and perform transactions l Enterprise Java Beans: – Run in any Java Virtual Machine – RMI to call remote EJB – Can call database systems and perform transactions

Transactional Component Middleware Structure l l Makes transaction processing systems easier to implement and more scalable based on object oriented framework Container – Likes the transaction monitor for traditional systems – Provides many useful features, e. g. transaction support and resource pooling – Provides standard facilities instead of component implementer to write the “ugly” system calls l Components can be deployed with different settings to behave in different ways, e. g. change the security environment

Transactional Environment l Typical transactional environments (supported by COM+ and EJB): – Requires a transaction: either the client is in transaction state (within the scope of a transaction) or COM+/EJB will start a new transaction when the component’s object is created – Requires a new transaction: COM+/EJB will start a new transaction when the component’s object is created, even if the caller is in transaction state – Supports transactions: the client may or may not be in transaction state, the component’s object does not care – Does not support transactions: the object will not run in transaction state, even if the client is in transaction state l Develop components to support transaction handling: – Component developer defines the transactional environment supported by the component – Container defines the transaction start and end points (based on the transactional environment) – Coding: program code needs to do only commit or abort – Attribute setting: deployer sets the attributes for transactional environment

COM+ Model COM+ Explorer: • For administrator to provide information to the container, e. g. transactional requirement IMy. Interface Object Wrapper Client DCOM User-Written COM Object Life Cycle Control IClass. Factory IObject. Context Client reference: • Does not point to user-written component, only interacts with the object wrapper Context Object COM+ Container

Object Wrapper l l l A barrier between the client and the component Every operation call is intercepted: can provide additional security checking Performance improvement by saving resource: – Object wrapper can deactivate component objects without the client knowing about the deactivation – When client next tries to use the object, the wrapper reactivates the object again – E. g. the application support thousands of end users, there will be thousands of clients, which will needs many thousands of objects, object deactivation will make the memory utilization more efficient l Connection pooling (managed by container): – Pool of database connection shared by objects – When an object is deactivated, the connection will be returned to the pool – When an object is activated, it reuses an inactive connection from the pool

EJB Model • Administrator uses XML to provide information to the container, e. g. transactional requirement My. Interface Client RMI or IIOP My. Interface Object Wrapper User-Written Enterprise Java Bean EJB Home My. Home. Interface Java. ejb. Session. Context Client reference: • Does not point to user-written component, only interacts with the object wrapper Context Object EJB Container

COM+ l l Extension of the COM model by merging MTS into core COM implementation Underlying protocol: DCOM SOAP (Simple Object Access Protocol): an alternative network protocol which uses XML, may allow a client running on a non. Microsoft platform to call a COM+ server Object deactivation: – Same as elimination, i. e. object is recreated every time – Can be deactivated after every operation: same as traditional transaction monitor, data object attributes are reset to their initial state at the beginning of an operation – Can be at the end of a transaction: allows the client to make several calls to the same object, e. g. search for a record in the DB in a call, update the DB in another call l Does not support temporary data on a session basis: in traditional transaction monitor, data area is provided to store temporary data which can be used by multiple transactions from the same terminal

EJB l l l A standard, not a product EJB products by BEA, IBM, Oracle, … Network connection to EJB is RMI and IIOP 3 kinds of components: Entity beans, session beans and message beans Entity beans – An entity bean is designed to represent a row in the database: a proxy object – Persistence: can be container managed or bean managed l Session beans – A session bean implements a business process that is performed on the behalf of the client in a single session: an agent object – Can be stateless or stateful l Message-driven beans: use the Java Message Services to support asynchronous interactions between clients and beans l More details in the EJB tutorial
- Slides: 40