290 likes | 421 Views
Java/CORBA. CSE525. JDK Sidebars. Avoiding the name service Applets and CORBA with JDK. Avoiding the name service. Corba objects need only a “handle” or reference to be utilized. The job of the naming service is to look up and return the reference for the object.
E N D
Java/CORBA CSE525
JDK Sidebars • Avoiding the name service • Applets and CORBA with JDK.
Avoiding the name service • Corba objects need only a “handle” or reference to be utilized. • The job of the naming service is to look up and return the reference for the object. • References to objects can be “stringified” in order to be dealt with as text. • An example: • IOR:000000000000002849444c3a6f6d672e6f72672f436f734e616d696e672f4e616d696e67436f6e746578743a312e300000000001000000000000004000010000000000166368757263682e656e672e61756275726e2e6564750081810000001cafabcafe0000000235d310b300000000000000080000000000000000
Avoiding the name service • The server can write the stringified reference to a file HelloServant HelloRef = new HelloServant(); orb.connect(HelloRef); String ior = orb.object_to_string(HelloRef); String filename = System.getProperty("user.home") + System.getProperty("file.separator") + "HelloIOR"; FileOutputStream fos = new FileOutputStream(filename); PrintStream ps = new PrintStream(fos); ps.print(ior); ps.close(); • and mail the file to a client
Avoiding the name service • The client can read the stringified reference from a file String filename = System.getProperty("user.home") + System.getProperty("file.separator") + "HelloIOR"; FileInputStream fis = new FileInputStream(filename); DataInputStream dis = new DataInputStream(fis); String ior = dis.readLine(); org.omg.CORBA.Object obj = orb.string_to_object(ior); Hello HelloRef = HelloHelper.narrow(obj); • and use the reference. String Hello = HelloRef.sayHello();
Applets and CORBA with JDK. • Clients can be applets. • Access to a server that is not the http server of the applet may complicate even further issues such as access to servers behind firewalls.
Applets and CORBA with JDK. <!-- HelloApplet.html --> <HTML><BODY> <hr> <applet code="HelloApplet.class" height=200 width = 200> </applet> </hr> </BODY></HTML>
Applets and CORBA with JDK. import HelloApp.*; import org.omg.CosNaming.*; import org.omg.CosNaming.NamingContextPackage.*; import org.omg.CORBA.*; import java.awt.Graphics; import java.util.Properties;
// create and initialize the ORB ORB orb = ORB.init(this, props); //ORB orb = ORB.init(this, null); // get the root naming context org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService"); NamingContext ncRef = NamingContextHelper.narrow(objRef); // resolve the Object Reference in Naming NameComponent nc = new NameComponent("Hello", ""); NameComponent path[] = {nc}; Hello HelloRef = HelloApp.HelloHelper.narrow(ncRef.resolve(path)); // call the Hello server object and print results message = HelloRef.sayHello(); } catch (Exception e) { System.out.println("HelloApplet exception: " + e.getMessage()); e.printStackTrace(System.out);} }
public class HelloApplet extends java.applet.Applet { public void init() { try { // set up properties Properties props = new Properties(); props.put("org.omg.CORBA.ORBInitialHost", "church.eng.auburn.edu"); props.put("org.omg.CORBA.ORBInitialPort", "36767");
/* and the paint() method */ public void paint(Graphics g) { g.drawString(message, 25, 50); } String message = ""; }
Exceptions • CORBA exceptions are both system exceptions and user defined exceptions. • Differences between CORBA exception objects and Java exceptions objects are handled by the idltojava mapping. • A user defined exception is indicated in IDL using the “raises” keyword rather than “throws”.
module HelloApp { interface Hello { exception cantWriteFile {}; exception cantReadFile{}; string sayHello( in string message) raises (cantWriteFile); string lastMessage() raises (cantReadFile); } }; /* this IDL defines an object that returns * hello world!!! but also writes the parameter * to a file. Asking for lastMessage returns the * last message sent to the object */
IDL Compiler results • Compiling the previous module adds more java classes to the HelloApp directory • cantReadFile.java • cantReadFilehelper.java • cantReadFileHolder.java • cantWriteFile.java • cantWriteFilehelper.java • cantWriteFileHolder.java
// The service class needs to be changed // imports as before . . . class HelloServant extends _HelloImplBase { private int msglength = 0; public String sayHello(String msg) throws HelloApp.HelloPackage.cantWriteFile { try { synchronized (this) //avoid concurrent file access { File helloFile = new File(“helloStorage.txt”); FileOutputStream fos = new FileOutputStream(helloFile); msglength = msg.length(); byte[] buf = new byte[msglength] msg.getBytes(0,msglength,buf,0); fos.write(but); fos.close(); } catch (Exception e) {throw new HelloApp.HelloPackage.cantWriteFile();} return “\nHelloWorld !!\n”; }
// public String lastMessage() throws HelloApp.HelloPackage.cantReadFile { try { synchronized (this) //avoid concurrent file access { if (msglength == 0) return “”; File helloFile = new File(“helloStorage.txt”); FileInputStream fis = new FileInputStream(helloFile); byte[] buf = new byte[msglength]; int n = fis.read(buf); fis.close(); return new String(buf); } catch (Exception e) {throw new HelloApp.HelloPackage.cantReadFile();} } }
Implementing the client • No changes are required of the client. The clients main() contained a try block that catches CORBA system and user defined exceptions. • CORBA clients should always be prepared for system exceptions. This mechanism handles user defined exceptions as well.
CORBA exceptions • All CORBA exceptions have the following IDL description. exception <somename> { unsigned long minor; CompletionStatus completed; } • User exceptions become subtypes of java.lang.Exception via the org.omg.CORBA.UserException class.
Hello World with Callback • Clients often need to have the server call the client back when information merits. For example, a spreadsheet client needs updating whenever the value of a stock changes.
Here’s how it works • The IDL defines two classes, • one that specifies the object that understands the sayHello( ) message • another that specifies the object that understands the callback( ) • The client when it sends the sayHello() message sends a callback object to the servant. The client implements and registers this object with the ORB.
Here’s how it works • When the client calls the server it passes the handle to the servant. The servant uses this handle to invoke the callback on the client. • Here’s the IDL module HelloApp { interface Hello { string sayHello(in HelloCallback objRef, in string message); }; interface HelloCallback { void callback(in string message); }; };
// Servant changes class HelloServant extends _HelloImplBase { public String sayHello( HelloCallback callobj, String msg) { callobj.callback(msg); // call client back return “\nHello World !!\n”; } } // Client implements the callback class HelloCallbackServant extends _HelloCallbackImplBase { public void callback(String notification) { System.out.println(notification); } } public class HelloClient { public static void main(String args[]) {
//client now must create an object and register it //with its ORB . . . unchanged client code elided here HelloCallbackServant helloCallbackRef = new HelloCallbackServant(); orb.connect(helloCallbackRef); . . . and this is all the changes to the client.
When Servant classes need to inherit from something • Servant classes must inherit from some ImplBase class generated by the idltojava compiler. • The Java language allows a class only one superclass. How can a servant inherit from other Java classes? • A technique. . .delegate the work at runtime to another idltojava generated class.
Recall Threads • class A extends Thread { } • class B extends C implements Runnable{ Thread t = new Thread(this);
In General • class A extends B , C { // not Java • class D extends B { public D( A anA) {//constructor } . . } //assume can’t change B //or no need for D • class A extends C implements BInterface{ public A( D aD) { // constructor
This technique OK for Servants • want class Servant extends _HelloImplBase, Whatever • built in to idltojava is a switch -ftie to generate the interface class • idltojava -ftie Hello.idl /* new servant code */ class HelloBasic{ public String sayHello() { return “Hello”;} } class HelloServant extends HelloBasic implements _HelloOperations { };
Server Code does HelloServant servant = new HelloServant( ); Hello helloRef = new _HelloTie(servant); orb.connect(helloRef); . . .