!--a11y-->
Developing the Remote Client 
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:
· Define the InitialContext factory and naming provider URL as context environment properties
· Create an InitialContext using the above environment properties
· Obtain a reference to the remote object by looking it up from the naming using the InitialContext
· Narrow the object reference to the bean’s home interface
· Create an instance of the bean using its home interface
· Call business methods of the bean remotely
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.
...
1. First, create the Client class using the New ® Class function from the context menu of the default package:

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) |
2. Open the Client.java class for editing.
3. Enter the following import statements necessary for the Client class:
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; |
4. Start developing the main method of the class by declaring the following variables:
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 SAP J2EE Engine’s Naming System.
5. Define the environment properties that are to be used for InitialContext creation:
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 SAP J2EE Engine’s factory implementation that is provided with the com.sap.engine.services.jndi.CosNamingInitialContextFactoryImpl class.
6. Create an InitialContext with the environment properties specified in the previous step:
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; } |
7. Lookup the UtilCallback bean using its JNDI name (the jndiName variable). The object that the lookup method of the InitialContext returns represents a reference to the bean:
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; } |
8. Narrow the object reference received in the previous step to the home interface of the UtilCallback bean:
// 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; } |
9. Use the create() method of the UtilCallback bean’s home interface to create a bean’s instance:
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.
10. Call the reverseString method of the UtilCallback bean. Print the result to the system output screen:
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; } |
11. Call the squareRoot method of the UtilCallback bean. Print the result of the method to the system output screen:
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; } |
12. Call the isPrime method of the UtilCallback bean. However, before you actually call this method, you must do some preliminary work to activate the callback object and get the reference to it:
a. Initialize the local Object Request Broker (ORB):
ORB orb = ORB.init(args, null); |
b. Activate the POAManager:
// Get reference to rootPOA and activate the POAManager POA rootPOA = POAHelper.narrow(orb.resolve_initial_references("RootPOA")); rootPOA.the_POAManager().activate(); |
c. Create an instance of the NotificatorServant object:
// Create servant NotificatorServant notificatorObject = new NotificatorServant(); |
d. Use the POAManager to get the reference to the callback object from the servant:
// 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; }
} } |
