Chapter 26 Common Object Request Broker ArchitectureCORBA Part
Chapter 26: Common Object Request Broker Architecture(CORBA): Part 1 Outline 26. 1 Introduction 26. 2 Step-by-Step 26. 3 First Example: System. Clock 26. 3. 1 System. Clock. idl 26. 3. 2 System. Clock. Impl. java 26. 3. 3 System. Clock. Client. java 26. 3. 4 Running the Example 26. 4 Technical/Architectural Overview 26. 5 CORBA Basics 26. 6 Example: Alarm. Clock 26. 6. 1 Alarm. Clock. idl 26. 6. 2 Alarm. Clock. Impl. java 26. 6. 3 Alarm. Clock. Client. java 26. 7 Distributed Exceptions 2002 Prentice Hall. All rights reserved.
Chapter 26: Common Object Request Broker Architecture(CORBA): Part 1 26. 8 26. 9 26. 10 Case Study: Chat 26. 8. 1 chat. idl 26. 8. 2 Chat. Server. Impl. java 26. 8. 3 Deitel. Messenger. java 26. 8. 4 Running Chat 26. 8. 5 Issues Comments and Comparisons Internet and World Wide Web Resources 2002 Prentice Hall. All rights reserved.
26. 1 Introduction • CORBA – Common Object Request Broker Architecture • Allows programs written in various languages to communicate with each other as would two processes in the same address space. • Features leveraged by clients: – Invocation transparency – Implementation transparency – Location transparency 2002 Prentice Hall. All rights reserved.
26. 1 Introduction (cont. ) • IDL – Interface Definition Language • a pure description language. – enables developers to describe an interface they wish to use remotely in a language-independent fashion. • language specific compiler required for each participating language. – creates files required by each language for CORBA related tasks. 2002 Prentice Hall. All rights reserved.
26. 1 Introduction (cont. ) • Components of Interoperability – Object reference pointing to a remote object. • • comes from an object adapter information contained: – location (network address) – reference to the adapter that created object reference – object ID – Parsing object reference to transmit • GIOP – Defines messages needed by ORBs to communicate with each other. – IIOP (Internet Inter-ORB Protocol)-GIOP implementation. 2002 Prentice Hall. All rights reserved.
26. 2 Step-by-Step • Distributed system implementation steps using Java and CORBA: 1. Analysis and Design – break problem into smaller fundamental services or subsystems – decide which subsystems to configure as distributed services 2. Defining the IDL – define functionality each service will expose – define data structures received and returned by each service 3. 4. 5. 6. 7. Implementing the servant Implementing a client Distribution of servant’s object reference Running the servant implementation Running the client 2002 Prentice Hall. All rights reserved.
26. 3 First Example: System. Clock • Provides a basic service by enabling client to query for current time. • Client requirements: – retrieve the current system time – display the retrieved time in GUI window 2002 Prentice Hall. All rights reserved.
26. 3. 1 System. Clock. idl • System. Clock server IDL definition • Declares a single method current. Time. Millis 2002 Prentice Hall. All rights reserved.
1 // Fig. 26. 1: systemclock. idl 2 // IDL definition of System. Clock. 3 4 module clock { 5 6 // The definition of the CORBA-enabled service 7 interface System. Clock { 8 long current. Time. Millis(); 9 }; 10 }; denote single line comments module keyword maps given name to a Java package curly braces denote scope boundaries of block semicolon identifies end of block server type from client’s perspective and server implemented interface return type - maps to Java long ( single long maps to Java int) Outline Fig. 26. 1 IDL definition for server System. Clock Lines 1 -2 and 6 Line 4 Lines 4, 10 Lines 9 -10 Line 7 2002 Prentice Hall. All rights reserved.
26. 3. 1 System. Clock. idl (cont. ) • Everything declared in IDL file is public. • Java IDL compiler, idlj, compiles systemclock. idl with command: idlj –td c: src –pkg. Prefix clock com. deitel. advjhtp 1. idl –fall systemclock. idl - Generates following files: - System. Clock. java - System. Clock. Operations. java - _System. Clock. Impl. Base. java 2002 Prentice Hall. All rights reserved.
1 package com. deitel. advjhtp 1. idl. clock; 2 3 4 /** 5 * com/deitel/jhtp 4/idl/clock/System. Clock. java 6 * Generated by the IDL-to-Java compiler (portable), version "3. 0" 7 * from systemclock. idl 8 * Wednesday, February 28, 2001 8: 24: 01 PM PST 9 */ 10 11 public interface System. Clock extends System. Clock. Operations, 12 org. omg. CORBA. Object, org. omg. CORBA. portable. IDLEntity 13 { 14 } // interface System. Clock CORBA-defined types from which all CORBA-enabled objects inherit Outline Fig. 26. 2 A Java interface generated by declares public operations of idlj. server (generated from IDL) Line 12 Line 11 2002 Prentice Hall. All rights reserved.
1 package com. deitel. advjhtp 1. idl. clock; 2 3 4 /** 5 * com/deitel/advjhtp 1/idl/clock/System. Clock. Operations. java 6 * Generated by the IDL-to-Java compiler (portable), version "3. 0" 7 * from systemclock. idl 8 * Sunday, July 1, 2001 10: 36: 53 PM PDT 9 */ 10 11 12 // The definition of the CORBA-enabled service 13 public interface System. Clock. Operations 14 { 15 long current. Time. Millis (); 16 } // interface System. Clock. Operations IDL file long mapped to Java long method defined in IDL file Outline Fig. 26. 3 System. Clock. Opera tions interface generated by idlj Line 15 2002 Prentice Hall. All rights reserved.
1 // Fig. 26. 4: System. Clock. Impl. java 2 // System. Clock service implementation. 3 4 package com. deitel. advjhtp 1. idl. clock; 5 6 // OMG CORBA packages 7 import org. omg. CORBA. ORB; 8 import org. omg. Cos. Naming. *; 9 import org. omg. Cos. Naming. Context. Package. *; 10 11 public class System. Clock. Impl extends _System. Clock. Impl. Base { 12 13 // return computer's current time in milliseconds 14 public long current. Time. Millis() 15 { 16 return System. current. Time. Millis(); 17 } 18 19 // initialize System. Clock. Impl object by calling method register 20 public System. Clock. Impl( String params[] ) throws Exception 21 { 22 register( "Time. Server", params ); 23 } 24 25 // register System. Clock. Impl object with Naming Service 26 private void register( String corba. Name, String params[] ) 27 throws org. omg. CORBA. ORBPackage. Invalid. Name, 28 org. omg. Cos. Naming. Context. Package. Invalid. Name, 29 Cannot. Proceed, Not. Found 30 { Outline Fig. 26. 4 Implementation of the System. Clock server 2002 Prentice Hall. All rights reserved.
Outline 31 // Check name of service. If name is null or blank 32 // do not attempt to bind to Naming Service. 33 if ( ( corba. Name == null ) || 34 ( corba. Name. trim(). length() == 0 ) ) 35 throw new Illegal. Argument. Exception ( 36 "Registration name cannot be null or blank. " ); 37 38 // create and initialize ORB Fig. ORB 26. 4 object 39 ORB orb = ORB. init( params, null ); creation of a new 40 Implementation of 41 // register this object with ORB the System. Clock 42 orb. connect( this ); pass implementation object to ORB server. 43 44 // find Naming Service 45 org. omg. CORBA. Object corba. Object = Line. Naming 39 retrieves Service 46 orb. resolve_initial_references( "Name. Service" ); 47 Naming. Context naming = CORBA mechanism to cast object reference 48 Naming. Context. Helper. narrow( corba. Object ); Line 42 one Object reference to 49 another (in this case, 50 // create Name. Component array with path information to Lines 45 -46 51 // find this object Naming. Context) 52 Name. Component naming. Component = naming context (identifies 53 new Name. Component( corba. Name, "" ); 47 -48 object Lines reference in naming 54 Name. Component path[] = { naming. Component }; service) 55 56 // bind System. Clock. Impl object with ORB Lines 52 -53 57 naming. rebind( path, this ); store reference to current 58 System. out. println( "Rebind complete" ); object using naming Line provided 57 59 } context 60 61 // main method to execute server 62 public static void main( String[] args ) throws Exception 63 { 64 // Create the System. Clock CORBA object. 2002 Prentice Hall. 65 System. Clock time. Server = new System. Clock. Impl( args ); All rights reserved.
66 67 // Wait for requests from the outside. 68 java. lang. Object object = new java. lang. Object(); 69 70 // keep server alive 71 synchronized( object ) { 72 object. wait(); 73 } 74 } 75 } // end class System. Clock. Impl Outline Fig. 26. 4 Implementation of the System. Clock server. 2002 Prentice Hall. All rights reserved.
26. 3. 3 System. Clock. Client. java • Represents the client that connects to System. Clock. • Connects to System. Clock service, requests current time and displays result. 2002 Prentice Hall. All rights reserved.
1 // Fig. 26. 5: System. Clock. Client. java 2 // Client application for the System. Clock example. 3 4 package com. deitel. advjhtp 1. idl. clock; 5 6 // Java core packages 7 import java. text. Date. Format; 8 import java. util. *; 9 10 // Java extension packages 11 import javax. swing. JOption. Pane; 12 13 // OMG CORBA packages 14 import org. omg. CORBA. ORB; 15 import org. omg. Cos. Naming. *; 16 import org. omg. Cos. Naming. Context. Package. *; 17 18 public class System. Clock. Client implements Runnable { 19 private System. Clock time. Server; 20 21 // initialize client 22 public System. Time. Client( String params[] ) throws Exception 23 { 24 connect. To. Time. Server ( params ); 25 start. Timer(); 26 } 27 28 // use Name. Service to connect to time server 29 private void connect. To. Time. Server( String params[] ) 30 throws org. omg. CORBA. ORBPackage. Invalid. Name, 31 org. omg. Cos. Naming. Context. Package. Invalid. Name, 32 Not. Found, Cannot. Proceed 33 { 34 // connect to System. Clock server 35 ORB orb = ORB. init( params, null ); Outline Fig. 26. 5 Client that connects to System. Clock. Line 35 create ORB 2002 Prentice Hall. All rights reserved.
Outline 36 37 org. omg. CORBA. Object corba. Object = 38 orb. resolve_initial_references( "Name. Service" ); 39 Naming. Context naming = 40 Naming. Context. Helper. narrow( corba. Object ); 41 42 // resolve object reference in naming 43 Name. Component name. Component = obtain reference of object 44 new Name. Component( "Time. Server", "" ); 45 Name. Component path[] = { name. Component }; obtain to Naming Service boundreference to name Time. Server 46 corba. Object = naming. resolve( path ); 47 time. Server = System. Clock. Helper. narrow( corba. Object ); cast to type Fig. System. Clock 26. 5 Client that 48 } connects to 49 System. Clock. 50 // start timer thread 51 private void start. Timer() 52 { Line 37 53 Thread thread = new Thread( this ); 54 thread. start(); 55 } Line 44 56 57 // talk to server on regular basis and display results Line 47 58 public void run() 59 { 60 long time = 0; Line 68 61 Date date = null; 62 Date. Format format = 63 Date. Format. get. Time. Instance( Date. Format. LONG ); 64 String time. String = null; 65 int response = 0; 66 invoke method on remote object 67 while( true ) { 68 time = time. Server. current. Time. Millis(); 69 date = new Date( time ); 2002 Prentice Hall. 70 time. String = format( date ); All rights reserved.
71 72 response = JOption. Pane. show. Confirm. Dialog( null, time. String, 73 "System. Clock Example", JOption. Pane. OK_CANCEL_OPTION ); 74 75 if ( response == JOption. Pane. CANCEL_OPTION ) 76 break; // Get us out of here 77 } 78 79 System. exit( 0 ); 80 } 81 82 // main method to execute client application 83 public static void main( String args[] ) throws Exception 84 { 85 // create client 86 try { 87 new System. Clock. Client( args ); 88 } 89 90 // process exceptions that occur while client executes 91 catch ( Exception exception ) { 92 System. out. println( 93 "Exception thrown by System. Clock. Client: " ); 94 exception. print. Stack. Trace(); 95 } 96 } 97 } // end of class System. Clock. Client Outline Fig. 26. 5 Client that connects to System. Clock. 2002 Prentice Hall. All rights reserved.
26. 3. 4 Running the Example • • Ensure JDK 1. 3 is installed on workstation and PATH environment variable includes JDK’s bin directory. Steps to execute System. Clock example: 1. compile IDL file using idlj – example (source code in c: src): idlj –pkg. Prefix clock com. deitel. advjhtp 1. idl –td c: src -fall System. Clock. idl 2. implement and compile server code 3. implement and compile client code 4. run Naming Service – – tnameserv provided included in JDK 1. 3 as a CORBA Object Service Naming Service test tool implementation example: tnameserv -ORBInitial. Port 1050 5. run server – java com. deitel. advjhtp 1. idl. clock. System. Clock. Impl –ORBInitial. Port 1050 2002 Prentice Hall. All rights reserved.
26. 3. 4 Running the Example (cont. ) 6. run client java com. deitel. advjhtp 1. idl. clock. System. Clock. Client -ORBInitial. Port 1050 2002 Prentice Hall. All rights reserved.
26. 4 Technical/Architectural Overview • • ORB is the central mechanism of CORBA. One ORB must exist for every object in a CORBA-enabled distributed system. An ORB’s relationship to services in a distributed system is comparable to the relationship between a computer’s communication bus and devices connected to it. Clients communicate with an ORB by using: 1. IDL compiler generated stub, 2. dynamic interface (using CORBA’s dynamic invocation API), or 3. ORB’s API 2002 Prentice Hall. All rights reserved.
26. 4 Technical/Architectural Overview (cont. ) Fig. 26. 6 Call path from a client to a distributed object. 2002 Prentice Hall. All rights reserved.
26. 4 Technical/Architectural Overview (cont. ) • An ORB communicates with servants through – a static skeleton, – a dynamic interface, or – a servant’s object adapter. 2002 Prentice Hall. All rights reserved.
26. 4 Technical/Architectural Overview (cont. ) Fig. 26. 8 ORB request-interface structure. Courtesy of Object Management Group, Inc. 2002 Prentice Hall. All rights reserved.
26. 4 Technical/Architectural Overview (cont. ) • CORBAservices are the baseline services available to all objects sitting on the ORB communication bus. – – – – Naming Service Event Management Service Life Cycle Service Persistent State Service Transaction Service Concurrency Service Relationship Service Externalization Service 2002 Prentice Hall. All rights reserved.
26. 4 Technical/Architectural Overview (cont. ) – – – – Query Service Licensing Service Property Service Time Service Security Service Notification Service Trader Service Collections Service 2002 Prentice Hall. All rights reserved.
26. 4 Technical/Architectural Overview (cont. ) • CORBAfacilities come in two groups: – horizontal facilities – target client-side functionality – vertical facilities – target domain-specific functionality 2002 Prentice Hall. All rights reserved.
26. 4 Technical/Architectural Overview (cont. ) • Horizontal facilities specifications: – Mobile Agents Facility – Printing Facility – Internationalization Facility • Vertical facilities domain-specific services are being developed for various business areas: 1. 2. 3. 4. 5. 6. Common Enterprise Models Finance/Insurance Electronic Commerce Manufacturing Healthcare Telecommunications 2002 Prentice Hall. All rights reserved.
26. 4 Technical/Architectural Overview (cont. ) – – Transportation Life Science Research Utilities C 4 I (Command, Control, Communications, Computers, and Intelligence) – Space 2002 Prentice Hall. All rights reserved.
26. 4 Technical/Architectural Overview (cont. ) Fig. 26. 7 Object Management Architecture reference model. Courtesy of Object Management Group, Inc. 2002 Prentice Hall. All rights reserved.
26. 4 Technical/Architectural Overview (cont. ) • Applications Objects are the top layer of the OMA. • Application Objecs have the functionality not found at the domain layer, facilities layer or the services layer. • Basic concepts of distributed systems are the same regardless of the designed system’s size. • An understanding of the OMA means a head start in developing an architecture, because most pieces needed are defined in the OMA. 2002 Prentice Hall. All rights reserved.
26. 5 CORBA Basics 2002 Prentice Hall. All rights reserved.
Outline 1 /* 2 * Any comments located outside of the module declaration are 3 * ignored by the IDL compiler. This multi-line comment does 4 * not appear in any of the files generated by idlj. 5 */ 6 7 // This single-line comment is also ignored by the IDL compiler 8 9 module maptest { maps directly to Java package maptest 10 Fig. 26. 10 IDL file 11 // This comment appears in the generated files for Struct. Map testing many of the 12 struct Struct. Map { maps directly to a Java class named Struct. Map (Struct. Map IDL keywords and 13 may contain object references but is declared final, which 14 // This comment appears at start of the type declarations types. prevents it from being subclassed) 15 boolean bool. Value; 16 char. Value; Line 9 17 wchar w. Char. Value; 18 octet. Value; 19 string. Value; Line 12 20 wstring w. String. Value; primitive types map to 21 short. Value; corresponding Java primitive types 22 unsigned short u. Short. Value; Lines 15 -28 23 long. Value; 24 unsigned long u. Long. Value; Line 33 25 long. Long. Value; 26 unsigned long u. Long. Value; 27 float. Value; Line 34 28 double. Value; 29 30 // fixed. Value; not supported by Java. IDL 31 }; 32 33 typedef sequence <Struct. Map> Struct. Map. Seq; unbounded sequence maps to a 34 typedef sequence <Struct. Map, 5> Bound. Struct. Map. Seq; Java array of type Struct. Map bounded sequence maps. Hall. to 2002 Prentice 35 Allarray rights reserved. Struct. Map of size 5
Outline 36 typedef long Int. Array[ 5 ]; 37 38 // This comment appears above 39 // the interface declaration for interface. Name Fig. 26. 10 IDL file 40 interface Interface. Name { 41 testing many of the 42 // comment above the readwrite attribute maps to array of type int of size 5 and IDL keywords 43 attribute long an. Attribute; types. 44 readonly attribute long ro. Attribute; 45 const long constant. Value = 42; maps to methods with signatures: 46 Line 36 47 // comment above the methods public void an. Attribute(int x) 48 void seq. Method( in Struct. Map. Seq seq ); 49 void bound. Seq. Method( in Bound. Struct. Map. Seq seq ); Line 40 and 50 void array. Method( in Int. Array array ); 51 void int. Out. Method( inout long int. Value ); mapspublic int an. Attribute() to interface Interface. Name maps to method with signature: Line 43(exposes 52 }; public functionality available to remote clients) 53 }; // end module maptest public int ro. Attribute() Line 44 maps to immutable int field with value 42 Line 45 methods will pass a copy of argument, i. e. , call-by-value (methods defined with keyword out pass references as arguments, i. e. , call-by-reference) Line 48 treats arguments as references, regardless of primitives or Line 49 object types Line 50 Line 51 2002 Prentice Hall. All rights reserved.
Outline 1 package maptest; 2 3 4 /** 5 * maptest/Struct. Map. java 6 * Generated by the IDL-to-Java compiler (portable), version "3. 0" 7 * from maptest. idl 8 * Monday, May 14, 2001 4: 18: 09 PM PDT cannot inherit from this class 9 */ 10 11 12 // This comment appears in the generated files for Struct. Map 13 public final class Struct. Map implements Fig. 26. 11 IDL 14 org. omg. CORBA. portable. IDLEntity generated file comments appearing in IDL file 15 { Struct. Map. java 16 17 // This comment appears at start of the type declarations 18 public boolean bool. Value = false; Line 33 19 public char. Value = ( char ) 0; 20 public char w. Char. Value = ( char ) 0; 21 public byte octet. Value = ( byte ) 0; Lines 12, 17 22 public String string. Value = null; 23 public String w. String. Value = null; 24 public short. Value = ( short ) 0; Line 13 25 public short u. Short. Value = ( short ) 0; 26 public int long. Value = ( int ) 0; 27 public int u. Long. Value = ( int ) 0; 28 public long. Long. Value = ( long ) 0; 29 public long u. Long. Value = ( long ) 0; 30 public float. Value = ( float ) 0; 31 public double. Value = ( double ) 0; 32 33 public Struct. Map () default constructor-all fields 34 { initialized to 0, false, or null 2002 Prentice Hall. 35 } // ctor All rights reserved.
36 37 public Struct. Map( boolean _bool. Value, char _char. Value, 38 char _w. Char. Value, byte _octet. Value, String _string. Value, 39 String w. String. Value, short _short. Value, 40 short _u. Short. Value, int _long. Value, int _u. Long. Value, 41 long _long. Long. Value, long _u. Long. Value, 42 float _float. Value, double _double. Value ) 43 { 44 bool. Value = _bool. Value; 45 char. Value = _char. Value; 46 w. Char. Value = _w. Char. Value; 47 octet. Value = _octet. Value; 48 string. Value = _string. Value; 49 w. String. Value = _w. String. Value; 50 short. Value = _short. Value; 51 u. Short. Value = _u. Short. Value; 52 long. Value = _long. Value; 53 u. Long. Value = _u. Long. Value; 54 long. Long. Value = _long. Long. Value; 55 u. Long. Value = _u. Long. Value; 56 float. Value = _float. Value; 57 double. Value = _double. Value; 58 } // ctor 59 60 } // class Struct. Map Outline Fig. 26. 11 IDLgenerated file Struct. Map. java 2002 Prentice Hall. All rights reserved.
Outline 1 package maptest; 2 3 4 /** 5 * maptest/Interface. Name. Operations. java 6 * Generated by the IDL-to-Java compiler (portable), version "3. 0" 7 * from maptest. idl 8 * Monday, May 14, 2001 4: 18: 09 PM PDT 9 */ 10 11 Fig. 26. 12 IDL 12 // the interface declaration for Interface. Name generated file 13 public interface Interface. Name. Operations generated from IDL file Interface. Name. Ope 14 { interface declaration 15 // comment above the readwrite attribute rations. java 16 int an. Attribute(); 17 generated from 18 // comment above the readwrite attribute Lineattribute 13 an. Attribute 19 void an. Attribute(int new. An. Attribute); generated from read-only attribute 20 int ro. Attribute(); Lines 16, 19 ro. Attribute 21 22 // comment above methods 23 void seq. Method( maptest. Struct. Map[] seq ); Line 20 24 void bound. Seq. Method( maptest. Struct. Map[] seq ); 25 void array. Method( int[] array ); generated method definitions 26 void int. Out. Method( org. omg. CORBA. Int. Holder int. Value ); Lines 23 -26 27 } // interface Interface. Name. Operations 2002 Prentice Hall. All rights reserved.
1 package maptest; 2 3 /** 4 * maptest/Interface. Name. java 5 * Generated by the IDL-to-Java compiler (portable), version "3. 0" 6 * from maptest. idl 7 * Monday, May 14, 2001 4: 18: 09 PM PDT 8 */ 9 10 // the interface declaration for Interface. Name 11 public interface Interface. Name extends Interface. Name. Operations , 12 org. omg. CORBA. Object, org. omg. CORBA. portable. IDLEntity 13 { 14 public static final int constant. Value = ( int ) ( 42 ); 15 } // interface Interface. Name Outline Fig. 26. 13 IDLgenerated file Interface. Name. ja constants willva always appear in file named after the interface declaration Line 14 2002 Prentice Hall. All rights reserved.
26. 5 CORBA Basics (cont. ) • Method call types: – synchronous • client makes call on remote blocking method – standard method call on remote service • client makes call on remote non-blocking method – remote method qualified with oneway modifier in IDL file – remote method receives only in arguments – asynchronous • client makes synchronous call on remote method, remote service responds at a later time by calling method on client • must be qualified with oneway to prevent deadlock 2002 Prentice Hall. All rights reserved.
26. 5 CORBA Basics (cont. ) Fig. 26. 14 Deadlock caused by client calling a server that calls the client. 2002 Prentice Hall. All rights reserved.
26. 6 Example: Alarm. Clock • Example of a typical push-model application – client sets alarm on server – server notifies client each time internal alarm event ocurrs 2002 Prentice Hall. All rights reserved.
1 // Fig. 26. 15: alarmclock 1. idl 2 // The IDL for the Alarm. Clock example 3 4 module alarm { 5 interface Alarm. Listener { 6 void update. Time( in long new. Time ); 7 }; 8 9 interface Alarm. Clock { 10 const string NAME = "Alarm. Clock"; 11 12 oneway void add. Alarm. Listener( in string listener. Name, 13 in Alarm. Listener listener ); 14 15 void set. Alarm( in string listener. Name, 16 in long seconds ); 17 }; 18 }; Outline Fig. 26. 15 alarmclock 1. idl Line 12 Line 13 receive copy of listener stub declare non-blocking method 2002 Prentice Hall. All rights reserved.
1 // Fig. 26. 16: Alarm. Clock. Impl. java 2 // Implementation of Alarm. Clock server. 3 4 package com. deitel. advjhtp 1. idl. alarm; 5 6 // Java core packages 7 import java. util. *; 8 9 // Java extension packages 10 import org. omg. CORBA. ORB; 11 import org. omg. Cos. Naming. *; 12 import org. omg. Cos. Naming. Context. Package. *; 13 14 public class Alarm. Clock. Impl extends _Alarm. Clock. Impl. Base { 15 16 // list contains name/alarm pairs of register 17 // registered objects waiting for an alarm 18 private Hashtable alarm. List = new Hashtable(); 19 20 // register Alarm. Clock. Impl object with Naming Service 21 public void register( String corba. Name, String params[] ) 22 throws org. omg. CORBA. ORBPackage. Invalid. Name, 23 org. omg. Cos. Naming. Context. Package. Invalid. Name, 24 Cannot. Proceed, Not. Found 25 { 26 if ( ( corba. Name == null ) || 27 ( corba. Name. trim(). length() == 0 ) ) 28 throw new Illegal. Argument. Exception ( 29 "Registration name cannot be null or blank" ); 30 31 // create and initialize ORB 32 ORB orb = ORB. init( params, null ); 33 34 // register this object with ORB 35 orb. connect( this ); Outline Fig. 26. 16 Alarm. Clock. Impl. j service with Naming Service ava is the Alarm. Clock server implementation. Line 21 2002 Prentice Hall. All rights reserved.
Outline 36 37 // retrieve reference to Naming Service 38 org. omg. CORBA. Object corba. Object = 39 orb. resolve_initial_references( "Name. Service" ); 40 Naming. Context naming = 41 Naming. Context. Helper. narrow( corba. Object ); 42 43 // create Name. Component array with path information to 44 // find this object 45 Name. Component naming. Component = 46 new Name. Component( corba. Name, "" ); 47 Name. Component path[] = { naming. Component }; 48 49 // bind Alarm. Clock. Impl object with ORB Fig. to 26. 16 50 naming. rebind( path, this ); add listener internal list Alarm. Clock. Impl. j 51 System. out. println( "Rebind complete" ); 52 } ava is the 53 Alarm. Clock server 54 // method used by clients wanting to register 55 // as callback/listener objects implementation. 56 public void add. Alarm. Listener( String listener. Name, 57 Alarm. Listener listener ) Line 56 58 throws Duplicate. Name. Exception 59 { 60 if ( listener. Name == null || 61 listener. Name. trim(). length() == 0 ) 62 throw new Illegal. Argument. Exception ( 63 "Name cannot be null or blank" ); 64 else 65 66 if ( alarm. List. get( listener. Name ) != null ) 67 throw new Illegal. Argument. Exception ( 68 "Name is already registered, please choose another" ); 2002 Prentice Hall. All rights reserved.
Outline 69 else 70 71 if ( listener == null ) 72 throw new Illegal. Argument. Exception ( 73 "Listener cannot be null" ); 74 75 // create new Timer and save it under listener name 76 alarm. List. put( listener. Name, new Alarm. Timer( listener ) ); 77 } 78 79 // Set an alarm for a client. If client not registered 80 // throw a runtime exception. 81 public void set. Alarm( String name, long seconds ) 82 { Fig. 26. 16 83 // get alarm for particular client Alarm. Clock. Impl. j 84 Alarm. Timer timer = ( Alarm. Timer ) alarm. List. get( name ); 85 ava is the 86 if ( timer == null ) schedule an alarm for time Alarm. Clock server 87 throw new Illegal. Argument. Exception ( specified by remote client 88 "No clock found for the incoming name" ); implementation. 89 else 90 timer. schedule( new Task. Wrapper(timer. get. Listener(), Line 90 91 seconds), seconds * 1000 ); 92 } 93 94 // main method to execute Alarm. Clock server 95 public static void main( String args[] ) throws Exception 96 { 97 Alarm. Clock. Impl alarm = new Alarm. Clock. Impl(); 98 alarm. register( Alarm. Clock. NAME, args ); 99 100 java. lang. Object object = new java. lang. Object(); 101 2002 Prentice Hall. All rights reserved.
102 // keep server alive 103 synchronized( object ) { 104 object. wait(); 105 } 106 } 107 108 // Every listener get an Alarm. Timer assigned to them. 109 private class Alarm. Timer extends Timer { 110 111 // The listener this Timer is assigned to. 112 private Alarm. Listener listener; 113 114 public Alarm. Timer( Alarm. Listener l ) 115 { 116 listener = l; 117 } 118 119 // Accessor method so we can get to the listener 120 // object reference. 121 public Alarm. Listener get. Listener () 122 { 123 return listener; 124 } 125 } // end of private inner class Task. Wrapper 126 127 // Task. Wrapper takes care of calling our clients 128 // when their alarm expires. 129 private class Task. Wrapper extends Timer. Task { 130 131 // The reference to our listener 132 private Alarm. Listener listener; 133 private long seconds; 134 Outline Fig. 26. 16 Alarm. Clock. Impl. j ava is the Alarm. Clock server implementation. Line 129 calls client when alarm expires 2002 Prentice Hall. All rights reserved.
135 // Task. Wrapper needs to know who to call and 136 // how long was the alarm set (in seconds). 137 public Task. Wrapper( Alarm. Listener l, long s ) 138 { 139 listener = l; 140 seconds = s; 141 } 142 143 public void run() 144 { 145 // Go wake them up! 146 listener. update. Time(seconds); 147 148 // Discard this Task. Wrapper. When the client 149 // wants a new alarm we create a new Task. Wrapper. 150 this. cancel(); 151 } 152 } // end private inner class Task. Wrapper 153 } // end class Alarm. Clock. Impl Outline call-back Fig. 26. 16 Alarm. Clock. Impl. j ava is the Alarm. Clock server implementation. Line 146 2002 Prentice Hall. All rights reserved.
1 // Fig. 26. 17: Clock. Client. GUI. java 2 // GUI used by the Alarm. Clock. Client. 3 4 package com. deitel. advjhtp 1. idl. alarm; 5 6 // Java core packages 7 import java. awt. *; 8 import java. awt. event. *; 9 10 // Java extension packages 11 import javax. swing. *; 12 13 public class Clock. Client. GUI extends JFrame { 14 private JLabel output. Label; 15 16 // set up GUI 17 public Clock. Client. GUI() 18 { 19 super( "Clock GUI" ); 20 21 output. Label = 22 new JLabel( "The alarm has not gone off. . . " ); 23 get. Content. Pane(). add( output. Label, Border. Layout. NORTH ); 24 25 set. Default. Close. Operation ( JFrame. EXIT_ON_CLOSE ); 26 set. Resizable( false ); 27 Dimension screen. Size = 28 Toolkit. get. Default. Toolkit(). get. Screen. Size(); 29 set. Size( new Dimension( 450, 100 ) ); 30 set. Location( ( screen. Size. width - 450 ) / 2, 31 ( screen. Size. height - 100 ) / 2 ); 32 } 33 Outline Fig. 26. 17 Clock. Client. GUI informs the user when the alarm has sounded. 2002 Prentice Hall. All rights reserved.
34 // set label's text 35 public void set. Text( String message ) 36 { 37 output. Label. set. Text( message ); 38 } 39 40 } // end of class Clock. Client. GUI Outline Fig. 26. 17 Clock. Client. GUI informs the user when the alarm has sounded. 2002 Prentice Hall. All rights reserved.
Outline 1 // Fig. 26. 18: Alarm. Clock. Client. java 2 // Client of the Alarm. Clock service 3 4 package com. deitel. advjhtp 1. idl. alarm; 5 6 // OMG CORBA packages 7 import org. omg. CORBA. ORB; 8 import org. omg. Cos. Naming. *; 9 import org. omg. Cos. Naming. Context. Package. *; 10 11 public class Alarm. Clock. Client extends _Alarm. Listener. Impl. Base { 12 13 // Reference to the GUI to be displayed 14 private Clock. Client. GUI gui; 15 Fig. 26. 18 16 // Reference to the alarm clock server we connect to Alarm. Clock. Client 17 private Alarm. Clock alarm. Clock; is the Alarm. Clock 18 19 // Name of this client used by server to make proper callback client. 20 private String name; 21 22 public Alarm. Clock. Client( String params[] ) throws Exception Line 34 23 { 24 // create displayable name that is unique among VMs 25 // running on same computer (or at least randomly unique) 26 name = new Long( 27 System. current. Time. Millis() % 10000 ). to. String(); 28 29 // create GUI to display name and 30 // number of seconds before alarm expires connect to service 31 gui = new Clock. Client. GUI(); through helper method 32 33 // connect to Time. Service 34 connect. To. Alarm. Server ( name, params ); 2002 Prentice Hall. 35 All rights reserved.
36 // display GUI and wait for user to terminate app. 37 gui. show(); 38 } 39 40 // perform connection to Alarm. Server 41 private void connect. To. Alarm. Server( 42 String name, String params[] ) 43 throws org. omg. CORBA. ORBPackage. Invalid. Name, 44 org. omg. Cos. Naming. Context. Package. Invalid. Name, 45 Not. Found, Cannot. Proceed 46 { 47 // connect Alarm. Clock. Client to an ORB 48 ORB orb = ORB. init( params, null ); 49 50 // connect to Naming Service and find object 51 // reference for Alarm. Clock service 52 org. omg. CORBA. Object corba. Object = 53 orb. resolve_initial_references( "Name. Service" ); 54 Naming. Context naming = 55 Naming. Context. Helper. narrow( corba. Object ); 56 57 // resolve object reference in naming 58 Name. Component name. Component = 59 new Name. Component( Alarm. Clock. NAME, "" ); 60 Name. Component path[] = { name. Component }; 61 alarm. Clock = 62 Alarm. Clock. Helper. narrow( naming. resolve( path ) ); 63 64 // register this object with Alarm. Clock service 65 alarm. Clock. add. Alarm. Listener( name, this ); 66 gui. show(); 67 update. Time( 0 ); 68 } 69 Outline Fig. 26. 18 obtain reference to service Alarm. Clock. Client is the Alarm. Clock client. Lines 41 -68 Line 65 register listener with service 2002 Prentice Hall. All rights reserved.
70 // The callback method defined in Alarm. Listener. 71 public void update. Time( long seconds ) 72 { 73 // Make up a length of time to use for the alarm setting. 74 int new. Time = ( int )( Math. random() * 10. 0 ) + 1; 75 gui. set. Text( "Alarm " + name + " came in at " + seconds + 76 " seconds. Resetting to " + new. Time + " seconds" ); 77 alarm. Clock. set. Alarm( name, new. Time ); 78 } 79 80 // main method to execute client 81 public static void main( String args[] ) throws Exception 82 { 83 // create client 84 try { 85 Alarm. Clock. Client client = new Alarm. Clock. Client( args ); 86 } 87 88 // process exceptions that occur while client executes 89 catch ( Exception exception ) { 90 System. out. println( 91 "Exception thrown by Alarm. Clock. Client: " ); 92 exception. print. Stack. Trace(); 93 } 94 } 95 96 } // end of class Alarm. Clock. Client Outline handle call-back Fig. 26. 18 Alarm. Clock. Client is the Alarm. Clock client. Lines 71 -78 2002 Prentice Hall. All rights reserved.
Outline Fig. 26. 18 Alarm. Clock. Client is the Alarm. Clock client. 2002 Prentice Hall. All rights reserved.
26. 7 Distributed Exceptions • CORBA specifies two exception types: – System Exceptions – defined for use by CORBA infrastructure – User Exceptions – defined using IDL by developers • Standard CORBA exceptions map to Java exceptions as final classes. 2002 Prentice Hall. All rights reserved.
Outline 1 module domain { 2 exception Database. Exception { 3 string msg; 4 }; 5 6 interface Customer. Home { 7 void find( in long key ) raises ( Database. Exception ); 8 }; 9 }; maps to Java’s throws keyword declaration of exception Fig. 26. 19 A userdefined CORBA exception (Datase. Exception ) and an operation capable of throwing the exception. Line 2 Line 7 2002 Prentice Hall. All rights reserved.
1 public final class Database. Exception 2 extends org. omg. CORBA. User. Exception 3 implements org. omg. CORBA. portable. IDLEntity 4 { 5 public String msg = null; 6 7 public Duplicate. Name. Exception () 8 { 9 } // ctor 10 11 public Duplicate. Name. Exception( String _msg ) 12 { 13 msg = _msg; 14 } // ctor 15 } // class Database. Exception defined in IDL file all exceptions inherit from User. Exception (which indirectly inherits from java. lang. Exception) Outline Fig. 26. 20 The generated Database. Exceptio n. java file (reformatted for clarity) Line 2 Line 5 2002 Prentice Hall. All rights reserved.
1 // Fig. 26. 21: alarmclock 2. idl 2 // The IDL for the Alarm. Clock example 3 4 module alarm { 5 exception Duplicate. Name. Exception { 6 string msg; 7 }; 8 9 interface Alarm. Listener { 10 void update. Time( in long new. Time ); 11 }; 12 changes 13 interface Alarm. Clock { 14 const string NAME = "Alarm. Clock"; 15 16 void add. Alarm. Listener( in string listener. Name, 17 in Alarm. Listener listener ) 18 raises( Duplicate. Name. Exception ); 19 20 void set. Alarm( in string listener. Name, 21 in long seconds ); 22 }; 23 }; Outline to previous alarm clock IDL definition Fig. 26. 21 Alarmclock 2. idl is the IDL for the Alarm. Clock example. Lines 5 -7, 18 2002 Prentice Hall. All rights reserved.
1 public void add. Alarm. Listener( String listener. Name, 2 Alarm. Listener listener ) throws Duplicate. Name. Exception 3 { 4 if ( listener. Name == null || 5 listener. Name. trim(). length() == 0 ) 6 throw new Illegal. Argument. Exception ( 7 "Name cannot be null or blank" ); 8 else 9 method can 10 if ( list. get( listener. Name ) != null ) 11 throw new Duplicate. Name. Exception( 12 "Name is already registered, please choose another" ); 13 else 14 15 if ( listener == null ) 16 throw new Illegal. Argument. Exception ( 17 "Listener cannot be null" ); 18 19 // Create a new Timer and save it under the listener name 20 alarm. List. put( listener. Name, new Alarm. Timer( listener ) ); 21 } Outline now throw exceptions Fig. 26. 22 Excerpt from Alarm. Clock. Imp. ja va Line 2 2002 Prentice Hall. All rights reserved.
26. 8 Case Study: Chat • Chat is a basic network application using a central broadcasting point where a collection of clients pushes in messages and the middleware pushes out messages. • Following use cases: 1. Connect: A client finds and connects to a chat server. 2. Disconnect: A client completes a chat session by disconnecting from the chat server. 3. Send message: A connected client creates a chat message and gives the message to the chat server. 4. Receive message: A connected client receives messages delivered by the chat server. 2002 Prentice Hall. All rights reserved.
Outline 1 // Chat. idl 2 // This file contains the IDL defining the API to the Chat. Server 3 // as well as the Chat. Client and Chat. Message. 4 5 module corba { 6 struct Chat. Message { abstracting message to struct 7 enables each client to receive its own 8 // Chat. Message properties 9 string from; copy of message object. Prevents 10 string message; bottleneck problem associated with 11 }; Fig. 26. 23 sending reference to one message 12 13 module client { object. Chat. Client and Chat. Message 14 interface Chat. Client { 15 interface definitions. 16 // receive new message 17 void deliver. Message( in Chat. Message message ); 18 Line 6 19 // method called when server shuting down 20 void server. Stopping(); Lines 13 -22, 24 -40 21 }; 22 }; 23 two different Linescope 33 levels 24 module server { 25 interface Stoppable. Chat. Server { 26 void stop. Server(); Line 33 scope level, resolves to client 27 }; resolves IDL namespace in same way “. ” Chat. Client interface 28 resolves Java package/class namespace 29 interface Chat. Server { 30 const string NAME = "Chat. Server"; 31 32 // register new Chat. Client with Chat. Server 33 oneway void register. Client( in client: : Chat. Client client ); 34 2002 Prentice Hall. All rights reserved.
35 // unregister Chat. Client with Chat. Server 36 void unregister. Client( in client: : Chat. Client client ); 37 38 // post new message to Chat. Server 39 void post. Message( in Chat. Message message ); 40 }; 41 42 // Create a combination interface 43 interface Chat. Service : Chat. Server, Stoppable. Chat. Server { 44 }; 45 }; 46 47 }; // end module corba Outline Fig. 26. 23 Chat. Client and Chat. Message interface definitions. 2002 Prentice Hall. All rights reserved.
26. 8. 2 Chat. Server. Impl. java • IDL compiler generates following server-side use files: – – – – messengerclient_Chat. Client. Impl. Base. java messengerserver_Chat. Server. Impl. Base. java messengerclientChat. Client. Operations. java messengerChat. Message. java messengerserverChat. Server. Operations. java 2002 Prentice Hall. All rights reserved.
26. 8. 2 Chat. Server. Impl. java • IDL compiler also generates following support files: – messengerChat. Message. Helper. java – messengerChat. Message. Holder. java 2002 Prentice Hall. All rights reserved.
1 // Chat. Server. Impl. java 2 // Chat. Server. Impl implements the CORBA Chat. Server. 3 package com. deitel. messenger. corba. server; abstract class that requires Chat. Server. Impl to implement 4 5 // Java core packages operations defined in interface Chat. Server. Operations 6 import java. io. *; 7 import java. util. *; 8 import java. net. Malformed. URLException; 9 10 // Java extension packages 11 import org. omg. Cos. Naming. *; 12 import org. omg. Cos. Naming. Context. Package. *; 13 import org. omg. CORBA. *; 14 15 // Deitel packages 16 import com. deitel. messenger. corba. Chat. Message; 17 import com. deitel. messenger. corba. client. Chat. Client; 18 19 public class Chat. Server. Impl extends _Chat. Server. Impl. Base { 20 21 // The ORB that connects us to the network 22 private ORB orb; 23 24 // Map of Chat. Client references 25 private Map clients = new Hash. Map(); register 26 27 // construct new Chat. Server. Impl 28 public Chat. Server. Impl( String[] args ) 29 throws Exception 30 { 31 super(); 32 register( Chat. Server. NAME, args ); 33 } 34 Outline Fig. 26. 24 Chat. Server. Impl implementation of the CORBA Chat. Server. Line 19 Line 32 service 2002 Prentice Hall. All rights reserved.
35 // register new Chat. Client with Chat. Server 36 public void register. Client( Chat. Client client ) 37 { 38 // add client to Map of registered clients 39 String key = orb. object_to_string( client ); 40 synchronized( clients ) { 41 clients. put( key, client ); 42 } 43 44 System. out. println( "Registered Client: " + key ); 45 46 } // end method register. Client 47 48 // unregister client with Chat. Server 49 public void unregister. Client( Chat. Client client ) 50 { 51 // remove client from Map of registered clients 52 String key = orb. object_to_string( client ); 53 synchronized( clients ) { 54 clients. remove( key ); 55 } 56 57 System. out. println( "Unregistered Client: " + key ); 58 59 } // end method unregister. Client 60 61 // post new message to Chat. Server 62 public void post. Message( Chat. Message message ) 63 { 64 Iterator iterator = null; 65 66 // get Iterator for Set of registered clients 67 synchronized( clients ) { 68 iterator = new Hash. Set( clients. entry. Set() ). iterator(); 69 } Outline Fig. 26. 24 Chat. Server. Impl implementation of the CORBA Chat. Server. 2002 Prentice Hall. All rights reserved.
Outline 70 71 // send message to every Chat. Client 72 while ( iterator. has. Next() ) { 73 Chat. Client client = 74 ( Chat. Client ) ( ( Map. Entry ) iterator. next() ). get. Value(); 75 client. deliver. Message( message ); 76 } 77 78 } // end method post. Message remote method invocation sending a struct 79 80 // Register Chat. Server. Impl object with Naming Service 81 public void register( String server. Name, String[] parameters ) 82 throws Not. Found, Cannot. Proceed, 83 org. omg. Cos. Naming. Context. Package. Invalid. Name, Fig. 26. 24 84 org. omg. CORBA. ORBPackage. Invalid. Name Chat. Server. Impl 85 { 86 if ( server. Name == null ) implementation of 87 throw new Illegal. Argument. Exception ( the CORBA 88 "Registration name can not be null" ); Chat. Server. 89 90 // Bind Chat. Server. Impl object to Naming Service. 91 // Create and initialize ORB. Line 75 92 orb = ORB. init( parameters, null ); 93 94 // create servant and register it with ORB 95 orb. connect( this ); 96 97 org. omg. CORBA. Object corba. Object = 98 orb. resolve_initial_references( "Name. Service" ); 99 Naming. Context naming = 100 Naming. Context. Helper. narrow( corba. Object ); 101 Name. Component naming. Component = 102 new Name. Component( server. Name, "" ); 103 Name. Component path[] = { naming. Component }; 2002 Prentice Hall. 104 naming. rebind( path, this ); All rights reserved.
105 System. out. println( "Server bound to naming" ); 106 } 107 108 // notify each client that server is shutting down and 109 // terminate server application 110 public void stop. Server() 111 { 112 System. out. println( "Terminating server. . . " ); 113 114 Iterator iterator = null; 115 116 // get Iterator for Set of registered clients 117 synchronized( clients ) { 118 iterator = new Hash. Set( clients. entry. Set() ). iterator(); 119 } 120 121 // send server. Stopping message to every Chat. Client 122 while ( iterator. has. Next() ) { 123 Chat. Client client = ( Chat. Client ) iterator. next(); 124 client. server. Stopping(); 125 System. err. println( "Disconnected: " + client ); 126 } 127 128 // create Thread to terminate application after 129 // stop. Server method returns to caller 130 Thread terminator = new Thread( 131 new Runnable() { 132 133 // sleep for 5 seconds, print message and terminate 134 public void run() 135 { 136 // sleep 137 try { 138 Thread. sleep( 5000 ); 139 } Outline Fig. 26. 24 Chat. Server. Impl implementation of the CORBA Chat. Server. 2002 Prentice Hall. All rights reserved.
140 141 // ignore Interrupted. Exceptions 142 catch ( Interrupted. Exception exception ) {} 143 144 System. err. println( "Server terminated" ); 145 System. exit( 0 ); 146 } 147 } 148 ); 149 150 terminator. start(); // start termination thread 151 152 } // end method stop. Server 153 154 // main method to execute Chat. Server. Impl 155 public static void main( String[] args ) 156 { 157 // set up Chat. Server. Impl object and bind to Naming Service 158 try { 159 160 // create Chat. Server. Impl object 161 Chat. Server. Impl chat. Server. Impl = 162 new Chat. Server. Impl( args ); 163 164 java. lang. Object object = new java. lang. Object(); 165 166 // keep server alive 167 synchronized( object ) { 168 object. wait(); ensure server object 169 } 170 } 171 Outline Fig. 26. 24 Chat. Server. Impl implementation of the CORBA Chat. Server. Line 168 not garbage collected 2002 Prentice Hall. All rights reserved.
172 // process problems setting up Chat. Server. Impl object 173 catch ( Exception exception ) { 174 exception. print. Stack. Trace(); 175 System. exit( 1 ); 176 } 177 178 } // end method main 179 } Outline Fig. 26. 24 Chat. Server. Impl implementation of the CORBA Chat. Server. 2002 Prentice Hall. All rights reserved.
26. 8. 3 Deitel. Messenger. java • IDL compiler generates following client-use files: – – – – messengerclient_Chat. Client. Stub. java messengerserver_Chat. Server. Stub. java messengerclientChat. Client. Operations. java messengerChat. Message. java messengerserverChat. Server. Helper. java messengerserverChat. Server. Operations. java 2002 Prentice Hall. All rights reserved.
26. 8. 3 Deitel. Messenger. java (cont. ) • Following generated server-side files are available for client to accept callback invocations from server: – messengerclient_Chat. Client. Impl. Base. java – messengerclientChat. Client. Operations. java • Following support files generated: – – – messengerclientChat. Client. Helper. java messengerclientChat. Client. Holder. java messengerChat. Message. Helper. java messengerChat. Message. Holder. java messengerserverChat. Server. Holder. java 2002 Prentice Hall. All rights reserved.
1 // CORBAMessage. Manager. java 2 // CORBAMessage. Manager implements the Chat. Client remote 3 // interface and manages incoming and outgoing chat messages 4 // using CORBA. abstract class requires CORBAMessage. Manager implement operation 5 package com. deitel. messenger. corba. client; 6 defined in Chat. Client. Operations (method deliver. Message) 7 // Java core packages 8 import java. awt. *; 9 import java. awt. event. *; 10 import java. util. *; 11 12 // Java extension packages 13 import org. omg. Cos. Naming. *; 14 import org. omg. Cos. Naming. Context. Package. *; 15 import org. omg. CORBA. *; 16 17 // Deitel packages 18 import com. deitel. messenger. *; 19 import com. deitel. messenger. corba. client. Chat. Client; 20 import com. deitel. messenger. corba. Chat. Message; 21 import com. deitel. messenger. corba. server. *; 22 23 public class CORBAMessage. Manager extends _Chat. Client. Impl. Base 24 implements Message. Manager { 25 26 // incoming ORB configuration parameters 27 private String[] configuration. Parameters ; 28 29 // listeners for incoming messages and disconnect notifications 30 private Message. Listener message. Listener ; 31 private Disconnect. Listener disconnect. Listener ; 32 33 // Chat. Server for sending and receiving messages 34 private Chat. Server chat. Server; 35 Outline Fig. 26. 25 CORBAMessage. Mana ger implementation of interface Message. Manager using CORBA. Line 23 2002 Prentice Hall. All rights reserved.
36 // CORBAMessage. Manager constructor 37 public CORBAMessage. Manager( String[] parameters ) 38 { 39 configuration. Parameters = parameters; 40 } 41 42 // connect to Chat. Server 43 public void connect( Message. Listener listener ) 44 throws Exception 45 { 46 initializes ORB and 47 // find Chat. Server remote object 48 ORB orb = ORB. init( configuration. Parameters , null ); 49 org. omg. CORBA. Object corba. Object = 50 orb. resolve_initial_references( "Name. Service" ); 51 Naming. Context naming = 52 Naming. Context. Helper. narrow( corba. Object ); 53 54 // Resolve the object reference in naming 55 Name. Component name. Component = 56 new Name. Component( Chat. Server. NAME, "" ); 57 Name. Component path[] = { name. Component }; 58 chat. Server = 59 Chat. Server. Helper. narrow( naming. resolve( path ) ); 60 61 // register client with Chat. Server to receive messages 62 chat. Server. register. Client( this ); 63 64 // set listener for incoming messages 65 message. Listener = listener; 66 67 } // end method connect 68 Outline gets Naming Service object Fig. 26. 25 CORBAMessage. Mana ger implementation of interface Message. Manager using CORBA. Lines 43 -67 2002 Prentice Hall. All rights reserved.
69 // disconnect from Chat. Server 70 public void disconnect( Message. Listener listener ) 71 throws Exception 72 { 73 if ( chat. Server == null ) 74 return; 75 76 chat. Server. unregister. Client( this ); 77 message. Listener = null; 78 79 // notify listener of disconnect 80 fire. Server. Disconnected ( "" ); 81 82 } // end method disconnect 83 84 // send Chat. Message to Chat. Server 85 public void send. Message( String from. User, String message ) 86 throws Exception 87 { 88 if ( chat. Server == null ) 89 return; 90 91 // create Chat. Message with message text and user name 92 Chat. Message chat. Message = create 93 new Chat. Message( from. User, message ); 94 95 // post message to Chat. Server 96 chat. Server. post. Message( chat. Message ); 97 98 } // end method send. Message 99 Outline Fig. 26. 25 CORBAMessage. Mana ger implementation of interface Message. Manager using CORBA. Lines struct, copy 92 -93 sent to server 2002 Prentice Hall. All rights reserved.
100 // process delivery of Chat. Message from Chat. Server 101 public void deliver. Message( Chat. Message message ) 102 { 103 if ( message. Listener != null ) 104 message. Listener. message. Received( message. from, 105 message ); 106 } 107 108 // process server shutting down notification 109 public void server. Stopping() 110 { 111 chat. Server = null; 112 fire. Server. Disconnected ( "Server shut down. " ); 113 } 114 115 // register listener for disconnect notifications 116 public void set. Disconnect. Listener( 117 Disconnect. Listener listener ) 118 { 119 disconnect. Listener = listener; 120 } 121 122 // send disconnect notification 123 private void fire. Server. Disconnected( String message ) 124 { 125 if ( disconnect. Listener != null ) 126 disconnect. Listener. server. Disconnected( message ); 127 } 128 } Outline receive broadcasts from server Fig. 26. 25 CORBAMessage. Mana ger implementation of interface Message. Manager using CORBA. Line 104 -105 2002 Prentice Hall. All rights reserved.
1 // Deitel. Messenger. java 2 // Deitel. Messenger uses Client. GUI and CORBAMessage. Manager to 3 // implement an CORBA chat client. 4 package com. deitel. messenger. corba. client; 5 6 // Deitel packages 7 import com. deitel. messenger. *; 8 9 public class Deitel. Messenger { 10 11 // launch Deitel. Messenger application 12 public static void main( String args[] ) throws Exception 13 { 14 // create CORBAMessage. Manager for communicating with server 15 Message. Manager message. Manager = 16 new CORBAMessage. Manager( args ); 17 18 // configure and display chat window 19 Client. GUI client. GUI = new Client. GUI( message. Manager ); 20 client. GUI. set. Size( 300, 400 ); 21 client. GUI. set. Resizable( false ); 22 client. GUI. set. Visible( true ); 23 } 24 } Outline Fig. 26 Deitel. Messenger application for launching the CORBA chat client. 2002 Prentice Hall. All rights reserved.
26. 8. 4 Running Chat • Steps for running example follows: – Compile IDL file using idlj -pkg. Prefix chat com. deitel. advjhtp 1. idl -td c: src -fall chat. idl – – – Implement and compile server class. Implement and compile client class. Open window, run tnameserv Naming Service: tnameserv –ORBInitial. Port 1050 – Open window, run server: java com. deitel. messenger. corba. server. Chat. Server. Impl -ORBInitial. Port 1050 – Open window, run client java com. deitel. messenger. corba. client. Deitel. Messenger -ORBInitial. Port 1050 2002 Prentice Hall. All rights reserved.
26. 8. 5 Issues • Why use local objects for the messages received by the clients? • How would making Chat. Message a distributed object alter the application’s complexity? 2002 Prentice Hall. All rights reserved.
26. 8. 5 Issues (cont. ) • Changing Chat. Message from struct to interface. – Advantages: • clearly defined API and proper encapsulation of Chat. Message • would be remote object (all clients could inspect message object and see any changes immediately) – Disadvantages: • clients sharing single remote object could create network bottleneck • must deal with data locking issues due to synchronous data access by multiple clients 2002 Prentice Hall. All rights reserved.
26. 8. 5 Issues (cont. ) • Objects-by-Value (OBV) specification defines new kind of interface type-valuetypes. – a struct with behavior semantics (ability to support declaration of operations) • Advantages: – ability to pass copies of “objects” around a distributed system that contain behavior in addition to data. 2002 Prentice Hall. All rights reserved.
Outline 1 // Fig. 26. 27: Chat. idl 2 // This file contains the IDL defining the 3 // API to the Chat. Server as well as the 4 // Chat. Client and Chat. Message. 5 6 module obvcorba { 7 valuetype Chat. Message { 8 9 // Chat. Message properties 10 private string from; field declarations 11 private string message; valuetype definition Fig. 26. 27 chat. idl constructor declaration 12 with Chat. Message 13 string get. Sender. Name(); method declarationschanged to be a 14 string get. Message(); 15 valuetype. 16 factory create( in string from, in string message ); 17 }; 18 Lines 7 -17 19 module client { 20 interface Chat. Client { Lines 10 -11 21 22 // receive new message 23 void deliver. Message( in Chat. Message message ); Lines 13 -14 24 }; 25 }; 26 Line 16 27 module server { 28 interface Chat. Server { 29 const string NAME = "Chat. Server"; 30 31 // register new Chat. Client with Chat. Server 32 void register. Client( in string chat. Name, 33 in client: : Chat. Client client ); 34 2002 Prentice Hall. All rights reserved.
35 // unregister Chat. Client with Chat. Server 36 void unregister. Client( in string chat. Name ); 37 38 // post new message to Chat. Server 39 void post. Message( in Chat. Message message ); 40 }; 41 }; 42 }; // end of module obvmessenger Outline Fig. 26. 27 chat. idl with Chat. Message changed to be a valuetype. 2002 Prentice Hall. All rights reserved.
26. 8. 5 Issues (cont. ) 2002 Prentice Hall. All rights reserved.
26. 8. 5 Issues (cont. ) • To create a Java valuetype object – define valuetype in an IDL – compile definition using idlj, and – derive new class from base valuetype class definition. 2002 Prentice Hall. All rights reserved.
1 // Fig. 26. 29: Chat. Message. Impl. java 2 package com. deitel. messenger. obvcorba; 3 4 public class Chat. Message. Impl extends Chat. Message { 5 6 // default constructor for empty Chat. Message. Impl object 7 public Chat. Message. Impl() 8 { 9 this( "", "" ); 10 } derive 11 12 // constructor to initialize from and message properties 13 public Chat. Message. Impl( String sender, String text ) 14 { 15 from = sender; 16 message = text; 17 } 18 19 // return name of sender 20 public String get. Sender. Name() 21 { 22 return from; 23 } 24 25 // get message 26 public String get. Message() 27 { 28 return message; 29 } 30 } Outline generated valuetype class Fig. 26. 29 Chat. Message. Impl is the Chat. Message implementation. Line 4 2002 Prentice Hall. All rights reserved.
26. 9 Comments and Comparisons • RMI mechanism in JDK 1. 2 implements stub-only communication model. • CORBA uses skeleton-stub model • CORBA is a powerful means of designing robust distributed systems. – Legacy applications have become valuable long-term assets, and new applications have become reusable services that can exchange data in a platform-neutral fashion. 2002 Prentice Hall. All rights reserved.
- Slides: 87