Java realization

Instructor's Guide


Introduction Interface Broker C++ Java Prolog Configure Conclusions
The Java realization of the Hello Universe example has the same structure as the C++ realization. See slide java-realization.

Java realization


slide: Java realization

Broker

The IDL-Java binding seems to be somewhat better standardized, possibly because it is of a later date. So the advantage of using a broker abstraction lies primarily in the shorthands it provides, and the similarity with the C++ realization.
  import org.omg.CORBA.*; 
broker.java
import java.io.*; public class broker { boolean _fl = false; java.applet.Applet _applet = null; public broker() { } public broker(boolean fl) { _fl = fl; } public broker(java.applet.Applet x) { _applet = x; init(null); } public void init(String[] args) { if (_applet == null) { _orb = ORB.init(args,new java.util.Properties()); if (!_fl) _boa = _orb.BOA_init(args,new java.util.Properties()); } else _orb = ORB.init(_applet, null); } public int operator() {
run -- server-only
if (!_fl) _boa.impl_is_ready(null); return 0; // OK } .... };

slide: Java -- broker

The only (real) difference with the C++ broker class is the possible existence of an applet. Somewhat unfortunately, when the (client) object is an applet, the initialization of the ORB must be done in a different way.

Client

The client in Java must, also, create and initialize a broker object, and create a reference to the world server object.

Java -- client


  package universe;
  
  import org.omg.CORBA.*; 
client.java
import java.io.*; import hush.broker;
see broker.java
public class client { public static void main(String args[]) { broker _broker = new broker(true); // true means client only try { _broker.init(args);
init orb
org.omg.CORBA.Object obj = _broker.object("world.ref"); world world = worldHelper.narrow(obj); if (world == null) throw new RuntimeException(); System.out.println("Enter 'h' (hello) or 'x' (exit):"); ... // do some requests to the world } catch(...) { ... } } };

slide: Java -- client

In Java, an extra object interfaceHelper is created to allow for casting the object that is created from a string object identifier to its actual object type, world in this case.

Server

The server provides access to the (server) object that implements the world interface. After creating and initializing a broker object, it creates an instance of a world server and writes its object identifier to the file world.ref. In addition it writes a HTML file that contains an applet that has a (string) reference to the server. See the online version or the Orbacus documentation for details.

Java -- server


  package universe; 
server.java
import org.omg.CORBA.*; import java.io.*; import hush.broker; // see broker.java public class server { public static void main(String args[]) { broker _broker = new broker(); try { _broker.init(args);
create orb en boa;
world_srv p = new world_srv();
create impl object
_broker.refout("world.ref",p);
write ref
create world.htm
_broker.html("world.htm",p, " code=universe/applet.class " + "width=500 height=300"); _broker.operator(); // run == boa.impl_is_ready(null); } catch(SystemException ex) { _broker.print(ex); System.exit(1); } System.exit(0); } }
After creating the object and its reference files, the server invokes the method impl_is_ready, through the broker's operator.

The implementation of the actual world server is as simple as its C++ peer.

Java -- server implementation


  package universe; 
world_srv.java
import org.omg.CORBA.*; public class world_srv extends _worldImplBase { public void hello() { System.out.println("Hello World!"); } public void ask(String msg) { System.out.println(msg); } public String tell() { String s = new String("ok"); return s; } public void halt() { System.exit(0); } }
Note that dealing with strings is significantly simpler in Java, because of Java's higher-level built-in String.

Discussion

Observe that the Java and C++ realizations are very similar in structure and also very similar in code. Nevertheless, what does not become evident in this small example is the bookkeeping that needs to be done in C++ for managing the objects created. Since Java supports garbage collection, the programmer needs to do no such bookkeeping, whereas in C++ one has to use either reference counting or the special _var type, that acts as a smart pointer which does the reference counting automatically. See any CORBA documentation for details.