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

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

6.16.7 代理方式の実装例(デフォルトインスタンス方式の例)

(1)IDL定義

  module ODsample{
      interface  intf{
          long  add( in  long  a,  in  long  b ); 
      };
  };

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

 動的登録

(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::POAsample1" ); // オブジェクト名
              String NCkind = new String( "" );   // オブジェクトタイプ
              NameComponent nc =  new NameComponent( NCid, NCkind ); 
              NameComponent NCo[] = { nc };
              org.omg.CORBA.Object  Obj = Cos.resolve( NCo ); 

              // サーバアプリケーションのオブジェクトリファレンス獲得
              intf target = intfHelper.narrow( Obj ); 

              int  in1 = 0;     // inパラメタ用変数
              int  in2 = 0;     // inパラメタ用変数
              int  result;      // 復帰値用変数
              String line = null; 

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

                  System.out.print( "in2 => " ); 
                  line = new BufferedReader(
                       new InputStreamReader( System.in ) ).readLine();
                  in2 = Integer.parseInt( line ); 
              }
              catch ( java.lang.NumberFormatException e ) {
                  System.exit( 255 ); 
              }

              // サーバアプリケーションのメソッド呼出し
              result = target.add( in1, in2 ); 

              // メソッドの結果表示
              System.out.println( in1 + " + " + in2 + " = "+ result ); 
          }
          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 org.omg.CosNaming.*; 
  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 ); 

              // インタフェース用のPOA作成
              // ポリシリスト作成
              org.omg.CORBA.Policy policies[] = new org.omg.CORBA.Policy[4]; 
              policies[0] = rootPOA.create_servant_retention_policy(
                            ServantRetentionPolicyValue.NON_RETAIN ); 
              policies[1] = rootPOA.create_request_processing_policy(
                            RequestProcessingPolicyValue.USE_DEFAULT_SERVANT ); 
              policies[2] = rootPOA.create_id_assignment_policy(
                            IdAssignmentPolicyValue.SYSTEM_ID ); 
              policies[3] = rootPOA.create_id_uniqueness_policy(
                            IdUniquenessPolicyValue.MULTIPLE_ID ); 
  
              // Default Servant用のPOAオブジェクトの作成
              POA childPOA = rootPOA.create_POA( "childPOA", null, policies ); 
              // ユーザクラスのインスタンス生成
              UserClass uc = new UserClass();
              // tieオブジェクト作成とユーザクラスの登録
              intfPOATie  tie = new intfPOATie( uc ); 
              // POAのDefault Servantに設定
              childPOA.set_servant( tie ); 

              org.omg.CORBA.Object _bindObj =
              childPOA.create_reference( "IDL:ODsample/intf:1.0" ); 

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

              // サーバアプリケーションのオブジェクトリファレンスの獲得
              String NCid   = new String( "ODsample::POAsample1" ); 
              String NCkind = new String( "" ); 
              NameComponent nc =  new NameComponent( NCid, NCkind ); 
              NameComponent NCo[] = { nc };
              try {
                  Cos.unbind( NCo ); 
              } catch( Exception e ) {
                  ; 
              }
              Cos.bind( NCo, _bindObj ); 

              // POAマネージャの獲得
              POAManager poamanager = childPOA.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 ); 
          }
      }
  }


  //ユーザクラス(インタフェース実装クラス)
  class UserClass 
  extends OtherUserClass
  implements ODsample.intfOperations
  {
      public int add( int a, int b ){ 
          return( a + b ); 
      }
  }

  //他のユーザクラス
  class OtherUserClass
  {
      public int sub( int a, int b ){ 
          return( a - b ); 
      }
  }

(6)例外情報の獲得

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


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

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