// -*-java-*- // // naming.java // // Arturo Crespo, Gerard Rodriguez // // This package implements a CORBA naming service using the http binding // service of the Infobus // // How to use (an example of a naming context is: // "dl/Services/InfoProcessing"): // * Clients: // // Initialize the ORB and read the IOR from the httpserver // org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args, null); // myInterface proxy = myInterface.myHelper.narrow(naming.GetObject("myNamingContext", "myName",orb)); // // * Servers: // // Initialize the Visigenic ORB and BOA // org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args ,null); // org.omg.CORBA.BOA boa = orb.BOA_init(); // // Create Proxy and make it ready // myImpl proxy = new myImpl("my"); // boa.obj_is_ready(proxy); // // Publish in the Infobus and start event loop // naming.Publish("myNamingContext", "myName",proxy); // boa.impl_is_ready(); // // // When all done, withraw from naming service: // naming.Withdraw("myNamingContext", "myName"); // // To do: handle types properly. now the caller to getobject need to // narrow down the object manually. Is this really needed? // import java.lang.*; import java.net.*; import java.io.*; public class naming { public static String bindingPath = "http://grunion.stanford.edu/cgi-bin/testbed/cosnaming2/cosnaming.py"; public static String typeID; public static String type; private static String GetType (String typeID) { int pos = typeID.indexOf(':'); if ( pos == -1) { return ""; } try { String type = typeID.substring(pos + 1, typeID.length()); pos = type.indexOf(':'); if (pos == -1) { return type; } type = type.substring(0,pos); pos = type.indexOf("/"); if (pos == -1) return type; type = type.replace('/','.'); return type; } catch(Exception e) { return ""; } } public static void Publish (String namingContext, String name, org.omg.CORBA.Object obj) { try { org.omg.CORBA.ORB orb = obj._orb(); String objString = orb.object_to_string(obj); typeID = obj._repository_id(); System.out.println("TypeID = " + typeID); type = GetType(typeID); System.out.println("Type = " + type); URL url = new URL(bindingPath + "?method=Bind&pName=" + name + "&pContext=" + namingContext + "&pSBH="+objString + "&pTypeID="+ typeID + "&pType="+ type); URLConnection connection=url.openConnection(); InputStream in = connection.getInputStream(); BufferedReader d = new BufferedReader(new InputStreamReader(in)); String status = d.readLine(); status = status.trim(); if (!(status.equals("Success"))) { System.out.println("Cannot bind:" + status); } else { System.out.println("Published!!!!"); } } catch (Exception e) { System.err.println(e); } } public static void Withdraw (String namingContext, String name) { try { URL url = new URL(bindingPath + "?method=UnBind&pName=" + name + "&pContext=" + namingContext ); URLConnection connection=url.openConnection(); InputStream in = connection.getInputStream(); BufferedReader d = new BufferedReader(new InputStreamReader(in)); String status = d.readLine(); status = status.trim(); if (!(status.equals("Success"))) { System.out.println("Cannot unbind:" + status); } else { System.out.println("Binding Withdrawn"); } } catch (Exception e) { System.err.println(e); } } public static org.omg.CORBA.Object GetObject(String namingContext, String name, org.omg.CORBA.ORB orb) { // get the object from the http server String sbh=""; try { URL url = new URL("http://grunion.stanford.edu/cgi-bin/testbed/cosnaming2/cosnaming.py?method=Resolve&pContext="+namingContext+"&pName="+name); URLConnection connection=url.openConnection(); InputStream in = connection.getInputStream(); BufferedReader d = new BufferedReader(new InputStreamReader(in)); sbh = d.readLine(); if (sbh.indexOf("CannotProceed") != -1) { System.out.println("Unable to resolve binding"); return null; } } catch (Exception e) { System.out.println(e); } return orb.string_to_object(sbh); } }