Java RMI CORBA 2007 2009 Java RMI CORBA
Java RMI & CORBA 2007 -2009 Java RMI & CORBA
Java RMI/IIOP - проект Серверна частина import javax. rmi. Portable. Remote. Object; public class sm. Impl extends Portable. Remote. Object implements sm{ public sm. Impl() throws java. rmi. Remote. Exception { super(); // invoke rmi linking and remote object initialization } public float add(float arg 1, float arg 2) import javax. naming. Initial. Context; throws java. rmi. Remote. Exception { import javax. naming. Context; return (arg 1 + arg 2); public class Serv. RMI_IIOP { } public static void main(String[] args) { } try { sm. Impl. java sm. Impl srvnt = new sm. Impl(); Context initctx = new Initial. Context(); initctx. rebind("sm. Service_IIOP", srvnt ); System. out. println("Serv. RMI_IIOP: Ready. . . "); import java. rmi. Remote; } catch (Exception e) { public interface sm extends Remote { System. out. println("Trouble: " + e); float add(float arg 1, float arg 2) e. print. Stack. Trace(); throws java. rmi. Remote. Exception; } } } sm. java Serv. RMI_IIOP. java } Java RMI & CORBA 10
Крок: отримання IDL-файлів rmic -idl sm. Impl У підкаталозі з'являється файл sm. idl /** * sm. idl * Generated by rmic -idl. Do not edit * 3 Июнь 2007 г. 13: 45: 26 EEST */ #include "orb. idl" sm. idl #ifndef __sm__ #define __sm__ Можна було б спростити interface sm { float add( in float arg 0, in float arg 1 ); Sm 1. java }; #pragma ID sm "RMI: sm: 00000000" #endif Java RMI & CORBA 12
Крок: IDL-компіляція idlj -fclient sm. idl Java RMI & CORBA 13
import org. omg. Cos. Naming. *; import org. omg. Cos. Naming. Context. Package. *; import org. omg. CORBA. *; CORBA-клієнт public class sm. Client. IDL { static sm sm_Impl; public static void main(String args[]) sm. Client. IDL. java { try{ (основний клієнтський клас) ORB orb = ORB. init(args, null); org. omg. CORBA. Object obj. Ref = orb. resolve_initial_references("Name. Service"); Naming. Context. Ext nc. Ref = Naming. Context. Ext. Helper. narrow(obj. Ref); String name = "sm. Service_IIOP"; sm_Impl = sm. Helper. narrow(nc. Ref. resolve_str(name)); System. out. println("Obtained a handle on server object: " + sm_Impl); System. out. println(sm_Impl. add(10, 5)); } catch (Exception e) { System. out. println("ERROR : " + e) ; e. print. Stack. Trace(System. out); Serv. RMI_IIOP. java. . . } sm. Impl srvnt = new sm. Impl(); } Context initctx = new Initial. Context(); } initctx. rebind("sm. Service_IIOP", srvnt Java RMI & CORBA 14 );
Java RMI/IIOP -сервер та CORBA-клієнт java -Djava. naming. factory. initial= com. sun. jndi. cosnaming. CNCtx. Factory -Djava. naming. provider. url=iiop: //localhost: 1050 Serv. RMI_IIOP java sm. Client. IDL -ORBInitial. Port 1050 -ORBInitial. Host localhost Java RMI & CORBA 15
CORBA - проект interface sm { float add( in float arg 0, in float arg 1 ); }; idlj -fall sm. idl Java RMI & CORBA 18
CORBA-сервер. Файл sm. Server. IDL. java (поч. ) import org. omg. Cos. Naming. *; import org. omg. Cos. Naming. Context. Package. *; import org. omg. CORBA. *; import org. omg. Portable. Server. POA; class sm. Impl extends sm. POA { public float add(float arg 1, float arg 2) { return arg 1+arg 2; } } sm. Server. IDL. java (поч. ) Клас sm. Impl – клас-сервант public interface sm extends sm. Operations, org. omg. CORBA. Object, org. omg. CORBA. portable. IDLEntity { public interface sm. Operations } // interface sm { float add (float arg 0, float arg 1); Java RMI & CORBA } // interface sm. Operations 19
public class sm. Server. IDL { public static void main(String args[]) { CORBA-сервер. Файл try{ sm. Server. IDL. java (заверш. ) ORB orb = ORB. init(args, null); POA rootpoa = POAHelper. narrow(orb. resolve_initial_references("Root. POA")); rootpoa. the_POAManager(). activate(); sm. Impl s = new sm. Impl(); org. omg. CORBA. Object ref = rootpoa. servant_to_reference(s); sm href = sm. Helper. narrow(ref); org. omg. CORBA. Object obj. Ref = orb. resolve_initial_references("Name. Service"); Naming. Context. Ext nc. Ref = Naming. Context. Ext. Helper. narrow(obj. Ref); String name = "Sm. Service_CORBA"; Name. Component path[] = nc. Ref. to_name( name ); nc. Ref. rebind(path, href); System. out. println("Sm. Server. IDL ready and waiting. . . "); orb. run(); // wait for invocations from clients } sm. Server. IDL. java catch (Exception e) { System. err. println("ERROR: " + e); e. print. Stack. Trace(System. out); } } } Java RMI & CORBA 20
import java. rmi. Remote. Exception; import javax. rmi. *; import javax. naming. Naming. Exception; Java RMI/IIOP -клієнт import javax. naming. Initial. Context; import javax. naming. Context; Client. RMI_IIOPto. CORBA. java public class Client. RMI_IIOPto. CORBA { public static void main( String args[] ) { try { Context ic = new Initial. Context(); Object objref = ic. lookup("Sm. Service_CORBA"); System. out. println("Client: Obtained a ref to from name Sm. Service_CORBA "); sm s = (sm) Portable. Remote. Object. narrow(objref, sm. class); System. out. println("Obtained a handle on server object: " + s); System. out. println (s. add(2, 5)); } catch( Exception e ) { System. err. println( "Exception " + e); e. print. Stack. Trace( ); return; } } } Java RMI & CORBA 21
CORBA-сервер та Java RMI/IIOP -клієнт Java RMI & CORBA 22
CORBA-сервер, розроблений під Visi. Broker у Borland C++ Builder interface sm { float add(in float a 1, in float a 2); }; addit. idl Java RMI & CORBA 24
import org. omg. CORBA. *; import java. io. *; public class client 2 { public static void main(String[] argv) { try{ String s = null; String s 1 = null; Buffered. Reader in = new Buffered. Reader(new File. Reader("My. ORef. ior")); s=in. read. Line(); s 1=s. substring(0, s. length()-1); System. out. println(s 1); ORB my. ORB = ORB. init(argv, null); org. omg. CORBA. Object obj. Ref = my. ORB. string_to_object(s 1); sm server = sm. Helper. narrow(obj. Ref); System. out. println("ob to_string - " + server. to. String()); System. out. println("adding 45+54 "+server. add(45, 54)); Buffered. Reader stdin = new Buffered. Reader( new Input. Stream. Reader(System. in)); System. out. print("Type Enter key"); client 2. java System. out. println(stdin. read. Line()); } catch (Exception e) { System. out. println("Error occurred while initializing server object: "); e. print. Stack. Trace(); } Java RMI & CORBA } } CORBA-клієнт (java) idlj -fclient addit. idl 25
Портабельність Corba-рішень. Проблеми. . . String s = null; String s 1 = null; Buffered. Reader in = new Buffered. Reader(new File. Reader("My. ORef. ior")); s=in. read. Line(); Відрізається останній символ s 1=s. substring(0, s. length()-1); (символ нового рядка) ! System. out. println(s 1); ORB my. ORB = ORB. init(argv, null); org. omg. CORBA. Object obj. Ref = my. ORB. string_to_object(s 1); sm server = sm. Helper. narrow(obj. Ref); Фрагмент client 2. java. . . Java RMI & CORBA 27
Додаток Java RMI & CORBA 28
CORBA-сервер (варіант 2) sm 1 Impl sm = new sm 1 Impl(); org. omg. CORBA. Object ref = rootpoa. servant_to_reference(sm); sm 1 href = sm 1 Helper. narrow(ref); . . . System. out. println("Server_Corba ready and waiting. . . "); System. out. println("ob. to. String - " + sm. to. String()); System. out. println("obref. to. String - " + href. to. String()); System. out. println("Stringified. IOR - " + orb. object_to_string(href)); Server_Corba. java (фрагменти) Java RMI & CORBA to. String: – об'єктне посилання – Stringified. IOR 29
import javax. naming. Initial. Context; import javax. naming. Context; public class Sm 1 Server { RMI/IIOP-сервер public Sm 1 Server() { try { Sm 1 sm = new Sm 1 Impl(); Context initial. Naming. Context = new Initial. Context(); initial. Naming. Context. rebind("Sm. Service", sm ); System. out. println("RMI/IIOP: Sm 1 Server ready. . . "); System. out. print("ob. to. String - " + sm. to. String()); } catch (Exception e) { System. out. println("Fail: " + e); } Sm 1 Server. java } public static void main(String args[]) { new Sm 1 Server(); } } Java RMI & CORBA 30
- Slides: 30