「20.3.2 コード一覧」では,それぞれのインタセプタAPIメソッドは単純にインプリメントされており,これは標準出力に情報メッセージを出力します。
Borland Enterprise Server VisiBrokerインストレーションのexamples/vbe/interceptorsディレクトリには次の四つのアプリケーションの例があります。
サンプルを実行するには,通常どおりにファイルをコンパイルします。そして,サーバとクライアントを次に示すように起動します。
prompt>vbj -Dvbroker.orb.dynamicLibs=SampleServerLoader Server
prompt>vbj -Dvbroker.orb.dynamicLibs=SampleClientLoader Client Kate
表20-2にインタセプタの例の実行結果を示します。クライアントとサーバによる実行が順を追って示してあります。
表20-2 インタセプタの例の実行結果
クライアント | サーバ |
---|---|
========> SampleServerLoader:Interceptors | |
Bind Interceptors loaded | |
========> SampleServerInterceptor id | |
========> SampleClientInterceptor id | |
========>SampleServerInterceptor id | |
========>SampleClientInterceptor id |
OADは実行されていないので,bind()メソッドの呼び出しは失敗し,サーバが処理を続行します。クライアントはアカウントオブジェクトにバインドしてから,balance()メソッドを起動します。このリクエストはサーバが受信して処理し,結果がクライアントに戻されます。クライアントは結果を出力します。
コードと結果のサンプルに示したように,クライアントとサーバの両方のインタセプタは,それぞれのプロセスの開始時点でインストールされます。インタセプタの登録についての情報については,「20.2.5 Borland Enterprise Server VisiBroker ORBへのインタセプタの登録」を参照してください。
次のコードで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;
}
}