VisiBroker for Java Developer’s Guide : Server basics

Server basics
This section outlines the tasks that are necessary to set up a server to receive client requests.
Overview
The basic steps that you'll perform in setting up your server are:
This section describes each task in a global manner to give you an idea of what you must consider. The specifics of each step are dependent on your individual requirements.
Initializing the VisiBroker ORB
As stated in the previous section, the VisiBroker ORB provides a communication link between client requests and object implementations. Each application must initialize the VisiBroker ORB before communicating with it as follows:
// Initialize the VisiBroker ORB.
org.omg.CORBA.ORB orb=org.omg.CORBA.ORB.init(args,null);
Creating the POA
Early versions of the CORBA object adapter (the Basic Object Adapter, or BOA) did not permit portable object server code. A new specification was developed by the OMG to address these issues and the Portable Object Adapter (POA) was created.
A discussion of the POA can be quite extensive. This section introduces you to some of the basic features of the POA. For detailed information, see “Using POAs” and the OMG specification.
In basic terms, the POA (and its components) determine which servant should be invoked when a client request is received, and then invokes that servant. A servant is a programming object that provides the implementation of an abstract object. A servant is not a CORBA object.
One POA (called the rootPOA) is supplied by each VisiBroker ORB. You can create additional POAs and configure them with different behaviors. You can also define the characteristics of the objects the POA controls.
The steps to setting up a POA with a servant include:
Some of these steps may be different for your application.
Obtaining a reference to the root POA
All server applications must obtain a reference to the root POA to manage objects or to create new POAs.
//2. Get a reference to the root POA
org.omg.CORBA.Object obj = orb.resolve_initial_reference("RootPOA");
// Narrow the object reference to a POA reference
POA rootPoa = org.omg.PortableServer.POAHelper.narrow(obj);
You can obtain a reference to the root POA by using resolve_initial_references which returns a value of type CORBA::Object. You are responsible for narrowing the returned object reference to the desired type, which is PortableServer::POA in the above example.
You can then use this reference to create other POAs, if needed.
Creating the child POA
The root POA has a predefined set of policies that cannot be changed. A policy is an object that controls the behavior of a POA and the objects the POA manages. If you need a different behavior, such as different lifespan policy, you will need to create a new POA.
POAs are created as children of existing POAs using create_POA. You can create as many POAs as you think are required.
Note
Child POAs do not inherit the policies of their parent POAs.
In the following example, a child POA is created from the root POA and has a persistent lifespan policy. The POA Manager for the root POA is used to control the state of this child POA.
// Create policies for our persistent POA
org.omg.CORBA.Policy[] policies =
{rootPoa.create_lifespan_policy(
LifespanPolicyValue.PERSISTENT)
};
// Create myPOA with the right policies
POA myPOA = rootPoa.create_POA( "bank_agent_poa", rootPoa.the_POAManager(),
policies );
Implementing servant methods
IDL has a syntax similar to Java and can be used to define modules, interfaces, data structures, and more. When you compile IDL that contains an interface, a class is generated which serves as the base class for your servant. For example, in the Bank.IDL file, there is an AccountManager interface:
module Bank{
interface Account {
float balance();
};
interface AccountManager {
Account open (in string name);
};
};
The following shows the AccountManager implementation on the server side.
AccountManagerPOA.java is created and serves as the skeleton code (implementation base code) for the AccountManager object implementation on the server side, as follows:
import org.omg.PortableServer.*;
import java.util.*;
public class AccountManagerImpl extends Bank.AccountManagerPOA {
public synchronized Bank.Account open(String name) {
// Lookup the account in the account dictionary.
Bank.Account account = (Bank.Account) _accounts.get(name);
// If there was no account in the dictionary, create one.
if(account == null) {
// Make up the account's balance, between 0 and 1000 dollars.
float balance = Math.abs(_random.nextInt()) % 100000 / 100f;
// Create the account implementation, given the balance.
AccountImpl accountServant = new AccountImpl(balance);
try {
// Activate it on the default POA which is root POA for this
servant
account = Bank.AccountHelper.narrow(_default_POA().
servant_to_reference(accountServant));
} catch (Exception e) {
e.printStackTrace();
}
// Print out the new account.
System.out.println("Created " + name + "'s account: " + account);
// Save the account in the account dictionary.
_accounts.put(name, account);
}
// Return the account.
return account;
}
private Dictionary _accounts = new Hashtable();
private Random _random = new Random();
}
Creating and Activating the Servant
The AccountManager implementation must be created and activated in the server code. In this example, AccountManager is activated with activate_object_with_id, which passes the object ID to the Active Object Map where it is recorded. The Active Object Map is simply a table that maps IDs to servants. This approach ensures that this object is always available when the POA is active and is called explicit object activation.
// 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
myPOA.activate_object_with_id(managerId, managerServant);
Activating the POA
The last step is to activate the POA Manager associated with your POA. By default, POA Managers are created in a holding state. In this state, all requests are routed to a holding queue and are not processed. To allow requests to be dispatched, the POA Manager associated with the POA must be changed from the holding state to an active state. A POA Manager is simply an object that controls the state of the POA (whether requests are queued, processed, or discarded.) A POA Manager is associated with a POA during POA creation. You can specify a POA Manager to use, or let the system create a new one for you by passing a null value as the POA Manager name in create_POA()).
// Activate the POA Manager
rootPOA.the_POAManager().activate();
Activating objects
In the preceding section, there was a brief mention of explicit object activation. There are several ways in which objects can be activated:
Explicit: All objects are activated upon server start-up via calls to the POA
On-demand: The servant manager activates an object when it receives a request for a servant not yet associated with an object ID
Implicit: Objects are implicitly activated by the server in response to an operation by the POA, not by any client request
Default servant: The POA uses the default servant to process the client request
A complete discussion of object activation is in “Using POAs”. For now, just be aware that there are several means of activating objects.
Waiting for client requests
Once your POA is set up, you can wait for client requests by using orb.run(). This process will run until the server is terminated.
// Wait for incoming requests.
orb.run();
Complete example
The sample below shows the complete example code.
// Server.java
import org.omg.PortableServer.*;
public class Server {
public static void main(String[] args) {
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 policies for our persistent POA
org.omg.CORBA.Policy[] policies = {
rootPOA.create_lifespan_policy(LifespanPolicyValue.PERSISTENT)
};
// Create myPOA with the right policies
POA myPOA = rootPOA.create_POA( "bank_agent_poa", rootPOA.the_POAManager(),
policies );
// 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
myPOA.activate_object_with_id(managerId, managerServant);
// Activate the POA manager
rootPOA.the_POAManager().activate();
System.out.println(myPOA.servant_to_reference(managerServant) + " is ready.");
// Wait for incoming requests
orb.run();
} catch (Exception e) {
e.printStackTrace();
}
}