20.3.1 コードサンプル

20.3.2 コード一覧」では,それぞれのインタセプタAPIメソッドは単純にインプリメントされており,これは標準出力に情報メッセージを出力します。

Borland Enterprise Server VisiBrokerインストレーションのexamples/vbe/interceptorsディレクトリには次の四つのアプリケーションの例があります。

<この項の構成>
(1) クライアント-サーバインタセプタのサンプル
(2) ServiceResolverInterceptorのサンプル(Java)

(1) クライアント-サーバインタセプタのサンプル

サンプルを実行するには,通常どおりにファイルをコンパイルします。そして,サーバとクライアントを次に示すように起動します。

Javaの場合

prompt>vbj -Dvbroker.orb.dynamicLibs=SampleServerLoader Server
prompt>vbj -Dvbroker.orb.dynamicLibs=SampleClientLoader Client Kate

Borland Enterprise Server VisiBroker ORBサービスに,ServiceLoaderインタフェースをインプリメントする二つのクラスを指定します。
VisiBroker 3.xで使用したServiceInitクラスは,ServiceLoaderおよびServiceResolverInterceptorという二つのインタフェースをインプリメントすることによって置き換えられています。この方法のサンプルについては,「20.3.1(2) ServiceResolverInterceptorのサンプル(Java)」を参照してください。

表20-2にインタセプタの例の実行結果を示します。クライアントとサーバによる実行が順を追って示してあります。

表20-2 インタセプタの例の実行結果

クライアントサーバ

========> SampleServerLoader:Interceptors
loaded
========> In POA / . Nothing to do.
========> In POA bank_agent_poa,1
ServerRequest interceptor installed
Stub [repository_id=IDL:Bank/
AccountManager:1.0,key=ServiceId [service=/
bank_agent
_poa,id={11 bytes:
[B ][a ][n ][k ][M ][a ][n ][a ][g ][e ][r ]}]] is ready.

Bind Interceptors loaded
========> SampleBindInterceptor bind
========> SampleBindInterceptor
bind_succeeded
========> SampleClientInterceptor id
MyClientInterceptor preinvoke_premarshal
=> open
========> SampleClientInterceptor id
MyClientInterceptor preinvoke_postmarshal

========> SampleServerInterceptor id
MyServerInterceptor preinvoke => open
Created john's account:
Stub [repository_id=IDL:Bank/
Account:1.0,key=TransientId [
poaName=/,id={4 bytes:
(0)(0)(0)(0)},sec=0,usec=0 ]]

========> SampleClientInterceptor id
MyClientInterceptor postinvoke
========> SampleBindInterceptor bind
========> SampleBindInterceptor
bind_succeeded
========> SampleClientInterceptor id
MyClientInterceptor preinvoke_premarshal =>
balance

========> SampleClientInterceptor id
MyClientInterceptor preinvoke_postmarshal

========>SampleServerInterceptor id
MyServerInterceptor postinvoke_premarshal
========>SampleServerInterceptor id
MyServerInterceptor postinvoke_postmarshal

========>SampleClientInterceptor id
MyClientInterceptor postinvoke
The balance in john's account is $245.64

OADは実行されていないので,bind()メソッドの呼び出しは失敗し,サーバが処理を続行します。クライアントはアカウントオブジェクトにバインドしてから,balance()メソッドを起動します。このリクエストはサーバが受信して処理し,結果がクライアントに戻されます。クライアントは結果を出力します。

コードと結果のサンプルに示したように,クライアントとサーバの両方のインタセプタは,それぞれのプロセスの開始時点でインストールされます。インタセプタの登録についての情報については,「20.2.5 Borland Enterprise Server VisiBroker ORBへのインタセプタの登録」を参照してください。

(2) ServiceResolverInterceptorのサンプル(Java)

次のコードでServiceLoaderインタフェースのインプリメント方法の例を示します。

import com.inprise.vbroker.properties.*;
import com.inprise.vbroker.interceptor.*;
import com.inprise.vbroker.InterceptorExt.*;

public final class UtilityServiceLoader implements ServiceLoader,
  ServiceResolverInterceptor {
  private com.inprise.vbroker.orb.ORB _orb = null;
  private String[ ] __serviceNames = { "TimeService",
                                              "WeatherService"};

  public void init(org.omg.CORBA.ORB orb) {
     //Just in case they are needed by resolve()
     _orb =(com.inprise.vbroker.orb.ORB)orb;

     PropertyManager pm =_orb.getPropertyManager();
     //use the PropertyManager to query property settings
     //if needed (not used in this example)

     /****Installing the Initial Reference *****/
     InterceptorManagerControl control =_orb.interceptorManager();
     ServiceResolverInterceptorManager manager =
        (ServiceResolverInterceptorManager)control.get_manager
                 ("ServiceResolver");
     for (int i =0;i <_serviceNames.length;i++){
        manager.add(_serviceNames [i ],this);
     }
     /****end of installation ***/

     if (_orb.debug)
        _orb.println("UtilityServices package has been
         initialized");
  }

  public void init_complete(org.omg.CORBA.ORB orb){
     //can be used for post-initialization processing if desired
  }

  public void shutdown(org.omg.CORBA.ORB orb){
     _orb =null;
     _serviceNames =null;
  }

  public org.omg.CORBA.Object resolve(java.lang.String service){
     org.omg.CORBA.Object srv =null;
     byte[ ] serviceId =service.getBytes();
     try {
        if (service =="TimeService"){
           srv =UtilityServices.TimeServiceHelper.bind(_orb,
              "/time_service_poa",serviceId);
        }
        else if (service =="WeatherService"){
           srv =UtilityServices.WeatherServiceHelper.bind(_orb,
              "/weather_service_poa",serviceId);
        }
     }catch (org.omg.CORBA.SystemException e){
        if (_orb.debug)
           _orb.println("UtilityServices package resolve error:"+e);
        srv =null;
     }

      return srv;
  }
}