4.8 オブジェクトを再利用する

次の「Joltリモート・サービスを拡張する(extendSample.java)」は、JoltRemoteServiceクラスをサブクラス化する1つの方法を示しています。この例では、JoltRemoteServiceクラスをサブクラス化してTransferServiceクラスを作成します。TransferServiceクラスはJoltRemoteServiceクラスを拡張し、Oracle TuxedoのBANKAPPのTRANSFERサービスを利用するTransfer機能を追加しています。

次のリストでは、Java言語のextendsキーワードが使用されています。extendsキーワードは、Javaでベース(親)クラスをサブクラス化するために使用されています。次のコードは、JoltRemoteServiceを拡張する方法の1つを示しています。

Joltリモート・サービスを拡張する(extendSample.java)のリスト

/* Copyright 1999 Oracle Systems, Inc. All Rights Reserved */
import java.net.*;
import java.io.*;
import bea.jolt.*;
/*
 * This Jolt sample code fragment illustrates how to customize
 * JoltRemoteService. It uses the Java language “extends” mechanism
 */
class TransferService extends JoltRemoteService
{
     public String fromBal;
     public String toBal;
     public TransferService( JoltSession session )
     {
        super(“TRANSFER”, session);
     }
public String doxfer( int fromAcctNum, int toAcctNum, String amount )
{
     /* Clear any previous input parameters */
     this.clear();
     /* Set the input parameters */
     this.setIntItem(“ACCOUNT_ID”, 0, fromAcctNum);
     this.setIntItem(“ACCOUNT_ID”, 1, toAcctNum);  
     this.setString(“SAMOUNT”, amount );
try
{
      /* Invoke the transfer service. */
      this.call(null);
      /* Get the output parameters */
      fromBal = this.getStringItemDef(“SBALANCE”, 0, null);
      if (fromBal == null)
          return “No balance from Account “ +
          fromAcctNum;
      toBal = this.getStringItemDef(“SBALANCE”, 1, null);
      if (toBal == null)
          return “No balance from Account “ + toAcctNum;
      return null;
}
catch (ApplicationException e)
{
      /* The transaction failed, return the reason */
      return this.getStringDef(“STATLIN”, “Unknown reason”);
       }
   }
}
class extendSample
{
      public static void main( String args[] )
      {
           JoltSession s_session;
           String host;   
           short port;
           TransferService xfer;
           String failure;

           if (args.length != 2)
           {
               System.err.println(“Usage: reuseSample host port”);
           System.exit(1);
           }
           /* Get the host name and port number for initialization. */
           host = args[0];
           port = (short)Integer.parseInt(args[1]);
           /* Prepare to connect to the Tuxedo domain. */
           JoltSessionAttributes attr = new JoltSessionAttributes();
           attr.setString(attr.APPADDRESS,”//”+ host+”:” + port);

           String username = null;
           String userrole = “sw-developer”;
           String applpasswd = null;
           String userpasswd = null;
           /* Check what authentication level has been set. */
           switch (attr.checkAuthenticationLevel())
           {
               case JoltSessionAttributes.NOAUTH:
                   break;
               case JoltSessionAttributes.APPASSWORD:
                    applpasswd = “secret8”;
                    break;
               case JoltSessionAttributes.USRPASSWORD:
                    username = “myName”;   
                    userpasswd = “BEA#1”;      
                    applpasswd = “secret8”;
                    break;
}
/* Logon now without any idle timeout (0). */
/* The network connection is retained until logoff. */
attr.setInt(attr.IDLETIMEOUT, 0);
s_session = new JoltSession(attr, username, userrole,
userpasswd, applpasswd);
/*
 * TransferService extends from JoltRemoteService and uses the
 * standard Oracle Tuxedo BankApp TRANSFER service. We invoke this
 * service twice with different parameters. Note, we assume
 * that “s_session” is initialized somewhere before.
 */
xfer = new TransferService(s_session);
if ((failure = xfer.doxfer(10000, 10001, “500.00”)) != null)
    System.err.println(“Tranasaction failed: “ + failure);
else
{
    System.out.println(“Transaction is done.”);
    System.out.println(“From Acct Balance: “+xfer.fromBal);
    System.out.println(“ To Acct Balance: “+xfer.toBal);
}
if ((failure = xfer.doxfer(51334, 40343, “$123.25”)) != null)
    System.err.println(“Tranasaction failed: “ + failure);
else
{
      System.out.println(“Transaction is done.”);
      System.out.println(“From Acct Balance: “+xfer.fromBal);
      System.out.println(“ To Acct Balance: “+xfer.toBal);
}
}
}