VisiBroker for Java Developer’s Guide : Using URL naming

Using URL naming
This section explains how to use the URL Naming Service which allows you to associate a URL (Uniform Resource Locator) with an object's IOR (Interoperable Object Reference). Once a URL has been bound to an object, client applications can obtain a reference to the object by specifying the URL as a string instead of the object's name. If you want client applications to locate objects without using the osagent or a CORBA Naming Service, specifying a URL is an alternative.
URL Naming Service
The URL Naming Service is a simple mechanism that lets a server object associate its IOR with a URL in the form of a string in a file. Client programs can then locate the object using the URL pointing to the file containing the stringified URL on the web server. The URL Naming Service supports the http URL scheme for registering objects and locating an object by the URL.
This URL name service provides a way to locate objects without using the Smart Agent or a CORBA Naming Service. It enables client applications to locate objects provided by any vendor.
Note
The VisiBroker URL Naming supports whatever form of URL handling that your Java environment supports.
URL Naming Service examples
The code for the URL Naming Service examples are located in your VisiBroker distribution in the following directory:
<install_dir>\examples\vbroker\basic\bank_URL
The following is the IDL specification for this service. IDL sample (WebNaming module)
// WebNaming.idl
#pragma prefix "borland.com"
module
URLNaming {
exception InvalidURL{string reason;};
exception CommFailure{string reason;};
exception ReqFailure{string reason;};
exception AlreadyExists{string reason;};
abstract interface Resolver {
// Read Operations
Object locate(in string url_s)
raises (InvalidURL, CommFailure, ReqFailure);
// Write Operations
void force_register_url(in string url_s, in Object obj)
raises (InvalidURL, CommFailure, ReqFailure);
void register_url(in string url_s, in Object obj)
raises (InvalidURL, CommFailure, ReqFailure, AlreadyExists);
};
};
Registering objects
Object servers register objects by binding to the Resolver and then using the register_url or the force_register_url method to associate a URL with an object's IOR. register_url is used to associate a URL with an object's IOR if no prior association exists. Using the force_register_url method associates a URL with an object's IOR regardless of whether an URL has already been bound to that object. If you use the register_url method under the same circumstances, an AlreadyExists exception is raised.
For an example illustrating the server-side use of this feature, see “URL Naming Service examples”. This example uses force_register_url. For force_register_url to be successful, the web server must be allowed to issue HTTP PUT commands.
Note
To get a reference to the Resolver, use the VisiBroker ORB's resolve_initial_references method, as shown in the example.
...
public class Server {
public static void main(String[] args) {
if (args.length == 0) {
System.out.println("Usage: vbj Server <URL string>");
return;
}
String url = args[0];
try {
// Initialize the ORB.
org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args,null);
// get a reference to the root POA
POA rootPoa =
POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
// Create the servant
AccountManagerImpl managerServant = new AccountManagerImpl();
// Decide on the ID for the servant
byte[] managerId = "BankManager".getBytes();
// Activate the servant with the ID on myPOA
rootPoa.activate_object_with_id(managerId, managerServant);
// Activate the POA manager
rootPoa.the_POAManager().activate();
// Create the object reference
org.omg.CORBA.Object manager =
rootPoa.servant_to_reference(managerServant);
// Obtain the URLNaming Resolver
Resolver resolver = ResolverHelper.narrow(
orb.resolve_initial_references("URLNamingResolver"));
// Register the object reference (overwrite if exists)
resolver.force_register_url(url, manager);
System.out.println(manager + " is ready.");
// Wait for incoming requests
orb.run();
} catch (Exception e) {
e.printStackTrace();
}
}
}
In this code sample args[0] is of the form:
http://<host_name>:<http_server_port>/<ior_file_path>/<ior_file_name>
The ior_file_name is the user-specified file name where the stringified object reference is stored. The suffix of the ior_file_name must be .ior if the Gatekeeper will be used instead of an HTTP server. An example using the Gatekeeper and its default port number is as follows:
http://mars:15000/URLNaming/Bank_Manager.ior
Locating an object by URL
Client applications do not need to bind to the Resolver, they simply specify the URL when they call the bind method, as shown in the following code sample. The bind accepts the URL as the object name. If the URL is invalid, an InvalidURL exception is raised. The bind method transparently callslocate() for you.
// ResolverClient.java
import com.inprise.vbroker.URLNaming.*;
public class ResolverClient {
public static void main(String[] args) {
if (args.length == 0) {
System.out.println("Usage: vbj Client <URL string> [Account name]");
return;
}
String url = args[0];
try {
// Initialize the ORB.
org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args,null);
For an example of how to use locate(), see the following code sample.
// Obtain the URLNaming Resolver
Resolver resolver = ResolverHelper.narrow(
orb.resolve_initial_references("URLNamingResolver"));
// Locate the object
Bank.AccountManager manager =
Bank.AccountManagerHelper.narrow(resolver.locate(url));
// use args[0] as the account name, or a default.
String name = args.length > 1 ? args[1] : "Jack B. Quick";
// Request the account manager to open a named account.
Bank.Account account = manager.open(name);
// Get the balance of the account.
float balance = account.balance();
// Print out the balance.
System.out.println("The balance in " + name + "'s account is $" + balance);
} catch(Exception e) {
e.printStackTrace();
}
}
}
Obtaining an object reference using the Resolver.locate method:
// Client.java
public class Client {
public static void main(String[] args) {
if (args.length == 0) {
System.out.println("Usage: vbj Client <URL string> [Account name]");
return;
}
String url = args[0];
// Initialize the ORB.
org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args,null);
// Locate the object
Bank.AccountManager manager = Bank.AccountManagerHelper.bind(orb, url);
// use args[0] as the account name, or a default.
String name = args.length > 1 ? args[1] : "Jack B. Quick";
// Request the account manager to open a named account.
Bank.Account account = manager.open(name);
// Get the balance of the account.
float balance = account.balance();
// Print out the balance.
System.out.println("The balance in " + name + "'s account is $" + balance);
}
}