Interstage Application Server アプリケーション作成ガイド (CORBAサービス編)
目次 索引 前ページ次ページ

第6章 アプリケーションの開発(Java言語)> 6.16 サーバアプリケーションのプログラミング例

6.16.4 Servant Locator使用例 (ユーザインスタンス管理方式)

(1)IDL定義

  module ODsample{
      interface    intf{
          readonly  attribute  long  value; 
          void add( in  long  a ); 
          void sub( in  long  b ); 
      };
      interface Factory{
          intf   create( in  string  userid ); 
          void   destroy( in  intf  obj ); 
      };
  };

(2)ネーミングサービスの登録

  OD_or_adm -c IDL:ODsample/Factory:1.0 -a Imple_POAsample4 -n ODsample::POAsample4

(3)アプリケーション構成概要図

(4)クライアントアプリケーション

  import java.io.*;
  import org.omg.CORBA.*; 
  import org.omg.CosNaming.*; 
  import ODsample.*; 

  public class Client {
      public static void main( String args[] ) {

          try {
              // ORBの生成と初期化
              ORB Orb = ORB.init( args, null ); 

              // ネーミングサービスのオブジェクトリファレンスの取得
              org.omg.CORBA.Object _tmpObj =
                                 Orb.resolve_initial_references( "NameService" ); 
              NamingContextExt Cos = NamingContextExtHelper.narrow( _tmpObj ); 

              // ネーミングサービスのresolveメソッドを発行し、
              // サーバアプリケーションのオブジェクトリファレンスの獲得
              String NCid   = 
                            new String( "ODsample::POAsample4" ); // オブジェクト名
              String NCkind = new String( "" );         // オブジェクトタイプ
              NameComponent nc =  new NameComponent( NCid, NCkind ); 
              NameComponent NCo[] = { nc };
              org.omg.CORBA.Object  Obj = Cos.resolve( NCo ); 

              // サーバアプリケーションのインタフェースクラスの獲得
              Factory target = FactoryHelper.narrow( Obj ); 

              int  in; 
              String line = null; 
              String userid = null; 

              try {
                  System.out.print( "USERID => " ); 
                  userid =
                    new BufferedReader( new InputStreamReader( System.in ) ).readLine();
                  if( userid.length() == 0 ) 
                      System.exit( 255 ); 

                  // FactoryでインタフェースアプリのIORを作成する
                  intf _intf = target.create( userid ); 

                  // サーバアプリケーションのメソッド呼出し
                  System.out.println( "value = " + _intf.value() ); 

                  System.out.print( "add => " ); 
                  line =
                    new BufferedReader( new InputStreamReader( System.in ) ).readLine();
                  in = Integer.parseInt( line ); 

                  // サーバアプリケーションのメソッド呼出し
                  _intf.add( in ); 
                  System.out.println( "value = " + _intf.value() ); 

                  System.out.print( "sub => " ); 
                  line =
                    new BufferedReader( new InputStreamReader( System.in ) ).readLine();
                  in = Integer.parseInt( line ); 

                  // サーバアプリケーションのメソッド呼出し
                  _intf.sub( in ); 
                  System.out.println( "value = " + _intf.value() ); 

                  target.destroy( _intf ); 
              }
              catch ( java.lang.NumberFormatException e ) {
                  System.exit(255); 
              }
          }
          catch( org.omg.CORBA.SystemException se ) {
              System.out.println( "ERROR : " + se.getClass().getName()
                   + " : minor = 0x" + java.lang.Integer.toHexString(se.minor) );
               System.exit( 255 );
          }
          catch( org.omg.CORBA.UserException ue ) {
              System.out.println( "ERROR : " + ue.getClass().getName() );
              System.exit( 255 );
          }
          catch ( Exception e ) {
              System.err.println( "ERROR : " + e ); 
              System.exit( 255 ); 
          }
      }
  }

(5)サーバアプリケーション

  import org.omg.CORBA.*; 
  import org.omg.PortableServer.*; 
  import java.util.*; 
  import ODsample.*; 

  // ユーザアプリケーション:メイン処理クラス
  public class Server {
      public static void main( String args[] ) {

          try {
              // ORBの生成と初期化
              ORB Orb = ORB.init( args, null ); 

              // RootPOAのオブジェクトリファレンスの取得
              org.omg.CORBA.Object _tmpObj =
                           Orb.resolve_initial_references( "RootPOA" ); 
              // RootPOAのPOAオブジェクト獲得
              POA rootPOA = POAHelper.narrow( _tmpObj ); 

              // Factoryインタフェース用のPOA作成
              // ポリシリスト作成
              org.omg.CORBA.Policy factory_policies[] = new org.omg.CORBA.Policy[4]; 
              factory_policies[0] = rootPOA.create_servant_retention_policy(
                                   ServantRetentionPolicyValue.NON_RETAIN ); 
              factory_policies[1] = rootPOA.create_request_processing_policy(
                                   RequestProcessingPolicyValue.USE_DEFAULT_SERVANT ); 
              factory_policies[2] = rootPOA.create_id_assignment_policy(
                                   IdAssignmentPolicyValue.SYSTEM_ID ); 
              factory_policies[3] = rootPOA.create_id_uniqueness_policy(
                                   IdUniquenessPolicyValue.MULTIPLE_ID ); 
              POA factory_POA = rootPOA.create_POA( "IDL:ODsample/Factory:1.0",
                                                  null, 
                                                  factory_policies ); 

              // インタフェース用のPOA作成
              // ポリシリスト作成
              org.omg.CORBA.Policy inf_policies[] = new org.omg.CORBA.Policy[4]; 
              inf_policies[0] = rootPOA.create_servant_retention_policy(
                                   ServantRetentionPolicyValue.NON_RETAIN ); 
              inf_policies[1] = rootPOA.create_request_processing_policy(
                                   RequestProcessingPolicyValue.USE_SERVANT_MANAGER ); 
              inf_policies[2] = rootPOA.create_id_assignment_policy(
                                   IdAssignmentPolicyValue.USER_ID ); 
              inf_policies[3] = rootPOA.create_id_uniqueness_policy(
                                   IdUniquenessPolicyValue.UNIQUE_ID ); 
              String inf_adapter_name = "IDL:ODsample/intf:1.0";
                                        //インタフェースリポジトリID
              POA childPOA = rootPOA.create_POA( "childPOA", null, inf_policies ); 

              // FactoryServantの生成
              Servant svt = new FactoryServant( childPOA ); 
              // Factoryインタフェース用POAのDefault Servantに設定
              factory_POA.set_servant( svt ); 

              // ServantLocatorの生成
              ServantLocator svtloc = new UserServantLocator();
              // インタフェース用POAにServantManagerとして登録
              childPOA.set_servant_manager( svtloc ); 

              // POAマネージャの獲得
              POAManager poamanager = rootPOA.the_POAManager();

              // POAマネージャのactivate
              poamanager.activate();
              Orb.run();
          }
          catch( org.omg.CORBA.SystemException se ) {
              System.out.println( "ERROR : " + se.getClass().getName()
                   + " : minor = 0x" + java.lang.Integer.toHexString(se.minor) );
               System.exit( 255 );
          }
          catch( org.omg.CORBA.UserException ue ) {
              System.out.println( "ERROR : " + ue.getClass().getName() );
              System.exit( 255 );
          }
          catch ( Exception e ) {
              System.err.println( "ERROR : " + e ); 
              System.exit( 255 ); 
          }
      }
  }

  // FactoryServant:Factoryメソッド実装クラス(スケルトンを継承)
  class FactoryServant extends FactoryPOA {

      static java.util.Hashtable tbl = new java.util.Hashtable();  // 管理テーブル
      private POA poa = null; 

      // コンストラクタ
      public FactoryServant( POA poa ) {
          this.poa = poa; 
      }

  // JDK/JRE1.3以前を使用する場合
  //    public intf create( java.lang.String userid ) {
  //        intf       ior;   // UserServantのオブジェクトリファレンス

  //        try{
  //            // オブジェクトリファレンスの生成
  //            org.omg.CORBA.Object _tmpObj = this.poa.create_reference_with_id(
  //                  userid.getBytes(),
  //                  "IDL:ODsample/intf:1.0" ); 
  //            ior = intfHelper.narrow( _tmpObj ); 
  //        }
  //        catch( org.omg.CORBA.UserException e ) {
  //            System.err.println( "create error: " + e ); 
  //            return( null ); 
  //        }

  //        return( ior ); 

  //    }

  // JDK/JRE1.4を使用する場合
      public intf create( java.lang.String userid ) {
          intf       ior;   // UserServantのオブジェクトリファレンス
  
          // オブジェクトリファレンスの生成
          org.omg.CORBA.Object _tmpObj = this.poa.create_reference_with_id(
                userid.getBytes(),
                "IDL:ODsample/intf:1.0" );
          ior = intfHelper.narrow( _tmpObj );
  
          return( ior );
  
      }

      public void destroy( intf obj ) 
      {
          try{
              // オブジェクトリファレンスからオブジェクトIDを求める
              byte oid[] = this.poa.reference_to_id( obj ); 

              // Servantを管理テーブルから削除
              FactoryServant.tbl.remove(  new ObjKey( oid )  ); 
          }
          catch( org.omg.CORBA.UserException e ) {
              System.err.println( "destroy error: " + e ); 
          }
      }
  }
  // Servantロケータ:Servant生成クラス(ServantLocatorを継承)
  // JDK/JRE1.3以前を使用する場合
  // class UserServantLocator extends ServantLocator {
  // JDK/JRE1.4を使用する場合
  class UserServantLocator extends LocalObject implements ServantLocator {

      public Servant preinvoke(
                 byte[] oid, 
                 org.omg.PortableServer.POA adapter, 
                 java.lang.String operation, 
                 org.omg.PortableServer.ServantLocatorPackage.CookieHolder cookie ) {

          // Servantを検索する。みつからなければnewで生成する
          Servant svt =  (Servant)FactoryServant.tbl.get( new ObjKey( oid ) ); 
          if ( svt == null ) {
              svt = new UserServant();
              FactoryServant.tbl.put( new ObjKey(oid), svt ); 
          }

          // Cookieの作成
          cookie.value = new ObjKey(oid); 

          return( svt ); 
      }

      public void postinvoke( byte[] oid, 
                            org.omg.PortableServer.POA adapter, 
                            java.lang.String operation, 
                            java.lang.Object cookie, 
                            Servant the_servant ) {

          // 変数の初期化を行う。
          the_servant = null; 
      }
  }

  // ObjKey
  class ObjKey{
      private byte[]  key; 

      public ObjKey( byte[] _key ) {
          key = _key; 
      }

      public int hashCode() {
          return (int)key[0]; 
      }

      public boolean equals( java.lang.Object comp ) {
          int i = key.length; 
          if ( i != ((ObjKey)comp).key.length ) {
              return false; 
          }
          for ( i--; i>=0; i-- ) {
              if ( key[i] != ((ObjKey)comp).key[i] ) {
                  return false; 
              }
          }

          return true; 
      }
  }

  // Servant:メソッド実装クラス(スケルトンを継承)
  class UserServant extends intfPOA {

      private int    value = 0; 

      public int value() {
          return( this.value ); 
      }

      public void add( int a ) {
          this.value += a; 
      }

      public void sub( int b ) {
          this.value -= b; 
      }
  }

(6)例外情報の獲得

 サーバアプリケーションで例外を獲得する方法は、クライアントアプリケーションの例外処理と同様です。詳細については、“クライアントアプリケーションの例外処理”を参照してください。


目次 索引 前ページ次ページ

All Rights Reserved, Copyright(C) 富士通株式会社 2005