Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

J2SE 1.4 breathes new life into the CORBA community, Part 3

Create enterprise-level apps with the POA

  • Print
  • Feedback

Page 3 of 7

package bank;
import org.omg.CORBA.*;
import org.omg.PortableServer.*;
import java.util.List;
import java.util.Iterator;
public class BankImpl extends bank.BankPOA
{
  public bank.Account openAccount (bank.AccountInformation accInfo) throws 
           bank.UnknownException
  {
    try
    {
      DBUtils.createAccount(accInfo);
      POA accountPOA = this._poa().find_POA("AccountPOA",true);
      String accNum = accInfo.accNum;
      Account theAccount = AccountHelper.narrow(
        accountPOA.create_reference_with_id(accNum.getBytes(),
          "IDL:bank/Account:1.0"));
        return theAccount;
      }
      catch(Exception e)
      {
        throw new UnknownException(e.getMessage());
      }
  }
  public void closeAccount (String accNum) throws bank.UnknownException
  {
    try
    {
      DBUtils.removeAccount(accNum);
      POA accountPOA = this._poa().find_POA("AccountPOA",true);
      accountPOA.deactivate_object(accNum.getBytes());
    }
    catch(org.omg.PortableServer.POAPackage.ObjectNotActive e)
    {
    }
    catch(Exception e)
    {
      throw new UnknownException(e.getMessage());
    }
  }
  public bank.Account[] findAccounts (bank.AccountInformation accInfo) throws bank.UnknownException
  {
    try
    {
      // Find the Accounts and return references
      List accountNumbers = DBUtils.findAccounts(accInfo);
      POA accountPOA = this._poa().find_POA("AccountPOA",true);
      Account[] accounts = new Account[accountNumbers.size()];
      Iterator it = accountNumbers.iterator();
      int i=0;
      while(it.hasNext())
      {
        accounts[i] = AccountHelper.narrow(
          accountPOA.create_reference_with_id(((String)it.next()).getBytes(),
          "IDL:bank/Account:1.0"));
        i++;
      }
      return accounts;
    }
    catch(Exception e)
    {
      throw new UnknownException(e.getMessage());
    }
  }
}


Implement the Account object

As I mentioned above, the AccountPOA creates and manages account objects (and references). I will discuss three different ways to configure the AccountPOA and the resulting account servants. I list the three approaches below:

  1. Use a servant activator
  2. Use a servant locator
  3. Use a default servant


Obviously, depending on how creative you are and how much time you have, you can create a solution using a combination of these three methods, but I will not discuss that here.

The above three solutions correspond to the USE_SERVANT_MANAGER and USE_DEFAULT_SERVANT values of the request processing policy values. The third (and default) value, USE_ACTIVE_OBJECT_MAP_ONLY, does not really work here since we would have to create all the account objects during startup—not a very scalable solution when you consider that a typical bank could have several thousand accounts. Why create an account object (and thus consume computing resources) that might not be accessed more than once a month? I used this same line of thinking in the bank servant implementation when I decided to return object references rather than object instances in the openAccount() and findAccount() methods. When you return an object reference, the POA does not add an entry to the active object map, even if the POA has the RETAIN policy. The POA adds such an entry only when a servant has instantiated for that object reference.

Use a servant activator

When the POA has the RETAIN policy, it uses servant managers called servant activators. Remember from Part 2 that the RETAIN policy implies that the POA will maintain the association between the servant instance and an activated object in the active object map. When the POA receives a request, it extracts the object ID from the request and checks the active object map to see if a servant already exists that can service that object ID's request. If it cannot find a servant, then the POA invokes the incarnate() method on the servant activator (if it has been registered), passing in the object ID as a parameter. The POA expects this method to return a valid servant instance; it then forwards the request to the valid servant instance and puts the instance in the active object map. When the POA is shutting down or when a specific object reference is being deactivated, the etherealize() method on the servant activator will be called (in the case of the POA shutting down, the etherealize() method will be called many times; once for each object reference). Any object-specific cleanup can be handled here.

  • Print
  • Feedback

Resources