| 
			Interstage Application Server アプリケーション作成ガイド (CORBAサービス編)
		 | 
	
	
		
			目次
			索引
			  
		 | 
	
6.16.2 Active Object Map(AOM)使用例 (Factory-1方式)
(1)IDL定義
  module ODsample{
      interface intf{
          readonly  attribute  long  value; 
          void  add( in  long  a ); 
          void  sub( in  long  b ); 
      };
      interface Factory {
          intf   create();
          void   destroy( in  intf  obj ); 
      };
  };
(2)ネーミングサービスの登録
  OD_or_adm -c IDL:ODsample/Factory:1.0 -n ODsample::POAsample5
(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::POAsample5" ); // オブジェクト名
              String NCkind = new String( "" );   // オブジェクトタイプ
              NameComponent nc =  new NameComponent( NCid, NCkind ); 
              NameComponent NCo[] = { nc };
              org.omg.CORBA.Object  Obj = Cos.resolve( NCo ); 
              // Factoryのオブジェクトリファレンス獲得
              Factory target = FactoryHelper.narrow( Obj ); 
              // Factoryでインタフェース用オブジェクトリファレンス作成
              intf _intf = target.create();
              int    in   = 0;    // inパラメタ用変数
              String line = null; 
              try {
                  // サーバアプリケーションのメソッド呼出し
                  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 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 ); 
              // FactoryServantの生成
              Servant svt = new FactoryServant( rootPOA ); 
              // Factoryインタフェース用POAのDefault Servantに設定
              factory_POA.set_servant( svt ); 
              // 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 {
      private POA poa = null; 
      // コンストラクタ
      public FactoryServant( POA poa ) {
          this.poa = poa; 
      }
      public intf create() {
          intf       ior;           // UserServantのオブジェクトリファレンス
          try {
              // Servantの生成
              Servant svt = new UserServant();
              // Servantからオブジェクトリファレンスの生成
              // IMPLICIT_ACTIVATIONポリシなので、AOMへ自動登録される
              org.omg.CORBA.Object _tmpObj = this.poa.servant_to_reference( svt ); 
              ior = intfHelper.narrow( _tmpObj ); 
          }
          catch( org.omg.CORBA.UserException e ) {
              System.err.println( "create error: " + e ); 
              return( null ); 
          }
          return( ior ); 
      }
      public void destroy( intf obj ) {
          try {
              // オブジェクトリファレンスからオブジェクトIDを求める
              byte oid[] = this.poa.reference_to_id( obj ); 
              // Servantをdeactiveにする
              this.poa.deactivate_object( oid );
          }
          catch( org.omg.CORBA.UserException e ) {
              System.err.println( "destroy error: " + e ); 
          }
      }
  }
  // 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) 富士通株式会社 2007