
You must develop a remote client as part of the client-side application that will actually make remote calls to the UtilCallback bean on the server side.
The development of the client code includes the following tasks:
As far as the isPrime method of the UtilCallback bean is concerned, you must perform extra actions before the client can actually call it. These refer to activating the callback object first and getting a reference to it (remember the callback object is passed as a parameter to the isPrime method of the bean!). You activate the callback object using the standard means provided by the CORBA POA architecture.
Enter the following parameters on the New Java Class screen that appears:
| Field | Value |
|---|---|
|
Source Folder |
Client |
|
Package |
Leave empty (equals to default package) |
|
Enclosing type |
Do not select |
|
Name |
Client |
|
Modifiers |
public |
|
Superclass |
Leave empty |
|
Interfaces |
Leave empty |
|
Which method stubs would you like to create? |
Choose public static void main(String[] args) |
import examples.iiop.*; import javax.naming.*; import javax.rmi.PortableRemoteObject; import java.util.*; import org.omg.PortableServer.POA; import org.omg.PortableServer.POAHelper; import org.omg.CORBA.ORB;
InitialContext initial; java.lang.Object objref; String jndiName = "sap.com/CallbackApplication/UtilCallbackBean";
The first one represents the InitialContext object, the second variable represents an object reference that is to be used to call methods on the remote object, and the third one represents the name under which the UtilCallback bean is bound to the J2EE Engine's Naming System.
Properties env = new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.cosnaming.CNCtxFactory");
env.put("java.naming.provider.url", "iiop://127.0.0.1:50007");Here, we use the InitialContext factory implementation provided by Sun Microsystems with the J2EE classes. However, you have the option to use the J2EE Engine's factory implementation that is provided with the com.sap.engine.services.jndi.CosNamingInitialContextFactoryImpl class.
try{
initial = new InitialContext(env);
System.out.println("Got initial context: " + initial + "\n");
} catch (NamingException e) {
System.err.println("Naming exception during InitialContext construction!");
e.printStackTrace();
return;
}try{
System.out.println(jndiName);
objref = initial.lookup(jndiName);
System.out.println("Lookup successful: " + objref + "\n");
}catch(Exception e){
System.err.println("Exception during lookup: " + e.toString());
e.printStackTrace();
return;
}// Declare variables representing bean's home and remote intefaces
UtilCallbackHome home = null;
UtilCallback util = null;
try{
// Narrow the object reference to the bean's home interface
home = (UtilCallbackHome)PortableRemoteObject.narrow(objref, UtilCallbackHome.class);
System.out.println("Narrowing successful: " + home + "\n");
}catch(Exception e){
System.err.println("Exception during narrowing: " + e.toString());
e.printStackTrace();
return;
}try{
// Create an instance of the UtilCallback bean
util = home.create();
}catch(Exception e){
System.err.println("Exception creating remote object: " + e.toString());
e.printStackTrace();
return;
}After this step, you have successfully created the bean's instance and you can now call business methods of the bean.
try{
System.out.println("-----------testing reverseString() method----------------");
String arg = "Hello customer!";
String response = util.reverseString(arg);
System.out.println(response);
}catch(Exception e){
System.err.println("Remote exception: " + e.toString());
e.printStackTrace();
return;
}try{
System.out.println("-----------testing squareRoot(double d) method----------------");
double d = 5.44;
double result = util.squareRoot(d);
String message = "The square root of " + d + " is " + result;
System.out.println(message);
}catch(Exception e){
System.err.println("Remote exception: " + e.toString());
e.printStackTrace();
return;
}ORB orb = ORB.init(args, null);
// Get reference to rootPOA and activate the POAManager
POA rootPOA = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
rootPOA.the_POAManager().activate();// Create servant NotificatorServant notificatorObject = new NotificatorServant();
// Get object reference from the servant org.omg.CORBA.Object ref = rootPOA.servant_to_reference(notificatorObject); // Narrow the reference Notificator callback = NotificatorHelper.narrow(ref);
Now that you have obtained the reference to the callback object, you can call the isPrime method of the bean:
String number = "222222222222222241";
System.out.println("Testing whether " + number + " is a primary one......");
long l = Long.parseLong(number);
boolean result = util.isPrime(l, callback);
System.out.println("I got: " + result);
}catch(Exception e){
System.err.println("Remote exception: " + e.toString());
e.printStackTrace();
return;
Here, the number we perform operations with is "hard-coded" for simplicity. Of course, the code can be easily modified in a way that the number is taken dynamically as the user's input.
You have developed a standalone client that calls methods of the UtilCallback bean remotely. The full source code of the Client class must be the following:
import examples.iiop.*;
import javax.naming.*;
import javax.rmi.PortableRemoteObject;
import java.util.*;
import org.omg.PortableServer.POA;
import org.omg.PortableServer.POAHelper;
import org.omg.CORBA.ORB;
public class Client {
public static void main(String[] args) {
InitialContext initial;
java.lang.Object objref;
String jndiName = "sap.com/CallbackApplication/UtilCallbackBean";
Properties env = new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.cosnaming.CNCtxFactory");
env.put("java.naming.provider.url", "iiop://127.0.0.1:50007");
try{
initial = new InitialContext(env);
System.out.println("Got initial context: " + initial + "\n");
} catch (NamingException e) {
System.err.println("Naming exception during InitialContext construction!");
e.printStackTrace();
return;
}
try{
System.out.println(jndiName);
objref = initial.lookup(jndiName);
System.out.println("Lookup successful: " + objref + "\n");
}catch(Exception e){
System.err.println("Exception during lookup: " + e.toString());
e.printStackTrace();
return;
}
// Declare variables representing bean's home and remote intefaces
UtilCallbackHome home = null;
UtilCallback util = null;
try{
// Narrow the object reference to the bean's home interface
home = (UtilCallbackHome)PortableRemoteObject.narrow(objref, UtilCallbackHome.class);
System.out.println("Narrowing successful: " + home + "\n");
}catch(Exception e){
System.err.println("Exception during narrowing: " + e.toString());
e.printStackTrace();
return;
}
try{
// Create an instance of the UtilCallback bean
util = home.create();
}catch(Exception e){
System.err.println("Exception creating remote object: " + e.toString());
e.printStackTrace();
return;
}
try{
System.out.println("-----------testing reverseString() method----------------");
String arg = "Hello customer!";
String response = util.reverseString(arg);
System.out.println(response);
}catch(Exception e){
System.err.println("Remote exception: " + e.toString());
e.printStackTrace();
return;
}
try{
System.out.println("-----------testing squareRoot(double d) method----------------");
double d = 5.44;
double result = util.squareRoot(d);
String message = "The square root of " + d + " is " + result;
System.out.println(message);
}catch(Exception e){
System.err.println("Remote exception: " + e.toString());
e.printStackTrace();
return;
}
try{
System.out.println("-----------testing isPrime(long l, Notificator callback) method----------------");
ORB orb = ORB.init(args, null);
// Get reference to rootPOA and activate the POAManager
POA rootPOA = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
rootPOA.the_POAManager().activate();
// Create servant
NotificatorServant notificatorObject = new NotificatorServant();
// Get object reference from the servant
org.omg.CORBA.Object ref = rootPOA.servant_to_reference(notificatorObject);
// Narrow the reference
Notificator callback = NotificatorHelper.narrow(ref);
String number = "222222222222222241";
System.out.println("Testing whether " + number + " is a primary one......");
long l = Long.parseLong(number);
boolean result = util.isPrime(l, callback);
System.out.println("I got: " + result);
}catch(Exception e){
System.err.println("Remote exception: " + e.toString());
e.printStackTrace();
return;
}
}
}