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 7 of 7

  // Create the AccountPOA and set its default servant (NON_RETAIN Policy)
  policies[0] = 
rootPOA.create_lifespan_policy(LifespanPolicyValue.PERSISTENT);
  policies[1] = 
    rootPOA.create_request_processing_policy(RequestProcessingPolicyValue.
          USE_DEFAULT_SERVANT);
  policies[2] = rootPOA.create_servant_retention_policy(ServantRetentionPolicyValue.
          NON_RETAIN);
  POA accountPOA = rootPOA.create_POA("AccountPOA",null,policies);
  AccountDefaultServantImpl defaultAcc = new bank.AccountDefaultServantImpl();
  rootPOA.activate_object(defaultAcc);
  accountPOA.set_servant(defaultAcc);
  • Make the bank object reference available to clients: The BankPOA creates a bank reference with the object ID CORBABank. This reference is then provided to clients in the CORBA naming service under three names: BankServer, BankServer2, and BankServer3 corresponding to the servant activator, servant locator, and default servant solutions, respectively. We provide the reference to clients the same way we did for Part 2's Hello World example.

    The source code's BankServer.java shows the code for the server that uses the servant activator with AccountPOA. Listing BankServer2.java shows the code for the server that uses the servant locator with AccountPOA. Listing BankServer3.java shows the code for the server that uses the default servant with AccountPOA.

  • Activate the POAs and wait for client requests: Once again, no rocket science here. We activate the POA the same way we did in Part 2's Hello World example.


  • Use servertool to register the CORBA servers

    Since the bank and account object references are persistent references, J2SE 1.4 requires me to register my server implementations using servertool, which comes bundled with J2SE 1.4. The relevant commands are shown below. Note that these commands assume that J2SE 1.4 is installed on the E drive in a folder called j2sdk1.4.0_01 and that the bank example source code is compiled into the E:\dev\Corba\Bank\classes directory:

    orbd -ORBInitialPort 1050 -ORBInitialHost localhost
    E:\j2sdk1.4.0_01\bin\servertool.exe orbd -ORBInitialPort 1050 -ORBInitialHost localhost
    register -server bank.BankServer -applicationName BankServer -classpath E:\dev\Corba\Bank    classes -args "-ORBInitialPort 1050 -ORBInitialHost localhost"
    register -server bank.BankServer2 -applicationName BankServer2 -classpath E:\dev\Corba\Bank\classes -args "-ORBInitialPort 1050 -ORBInitialHost localhost"
    register -server bank.BankServer3 -applicationName BankServer3 -classpath E:\dev\Corba\Bank\classes -args "-ORBInitialPort 1050 -ORBInitialHost localhost"
    


    As shown in the code above, I start ORBD (Object Request Broker Daemon), which is the J2SE 1.4 persistent naming service, and then use servertool to register BankServer, BankServer2, and BankServer3. Each registration command requires me to provide the classpath and any startup command-line parameters. Obviously, you will have to compile the Java source code before you can register. Compiling (running idlj and javac) the bank application proves no different from compiling Part 2's Hello World application. So I'll leave that as an exercise for you.

    A test client

    BankClient, shown below, is a generic client that can access and test all three solutions discussed above:

    package bank.test;
    import bank.Bank;
    import bank.BankHelper;
    import bank.Account;
    import bank.AccountHelper;
    import bank.AccountInformation;
    import bank.AccountType;
    import org.omg.CORBA.*;
    import org.omg.CosNaming.*;
    import org.omg.CosNaming.NamingContextPackage.*;
    public class BankClient
    {
      public static void main(String args[])
      {
        try
        {
          // Create and initialize the ORB
            ORB orb = ORB.init(args, null);
          // Get the root naming context
          org.omg.CORBA.Object objRef =
            orb.resolve_initial_references("NameService");
            
          // Use NamingContextExt instead of NamingContext. This is
          // part of the Interoperable Naming Service.
          NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
          // Resolve the object reference in Naming
          String name = "BankServer";
          System.out.println(args.length);
          if(args.length == 5)
            name = args[4];
          Bank theBank = BankHelper.narrow(ncRef.resolve_str(name));
          System.out.println("Obtained a handle on server object: " + theBank);
          AccountInformation accInfo = new AccountInformation();
          accInfo.accNum="";
          accInfo.accType = AccountType.NIL;
          Account[] accounts = theBank.findAccounts(accInfo);
          for(int i=0; i<accounts.length; i++)
            System.out.println("Account Number " + ((Account)accounts[i]).accountNumber());
          accounts[0].deposit((float)1.1);
        }
        catch(bank.UnknownException e)
        {
          System.out.print(e.reason);
        }
        catch(bank.InvalidOperationException e)
        {
          System.out.print(e.reason);
        }
        catch (Exception e)
        {
          System.out.println("ERROR : " + e) ;
            e.printStackTrace(System.out);
        }
      }
    }
    


    The program takes one parameter: the server to access, which is BankServer, BankServer2, or BankServer3. The default is BankServer. This parameter is actually the fifth one on the command line since the first four are J2SE 1.4 ORB-related parameters as shown in the example command line below:

      java test.BankClient -ORBInitialHost localhost -ORBInitialPort 1050 BankServer2
    


    The program then finds all accounts and deposits .10 in the first account found. So, before you run this program, ensure you have at least one account in your database.

    Until next time

    As promised, this article has been hands-on, with wide coverage of various POA programming aspects. Based on the knowledge gained from Parts 2 and 3 of this series, you should be in a good position to start using the POA in your own projects. I will conclude the series in Part 4, another hands-on article, where I cover two important topics: the Interoperable Naming Service and portable interceptors.

    About the author

    Tarak Modi has been architecting scalable, high-performance, distributed applications for more than seven years and is currently a senior specialist with North Highland, a management and technology consulting company. His professional experience includes hardcore C++ and Java programming; working with Microsoft technologies such as COM (Component Object Model), MTS (Microsoft Transaction Server), and COM+; Java-based technologies including J2EE; and CORBA. He is also coauthor of Professional Java Web Services (Wrox Press, 2002; ISBN 1861003757). To find out more about him, visit his personal Website at http://www.tekNirvana.com.
    • Print
    • Feedback

    Resources