サーバー・コード

サーバー・アプリケーションでは、リモート・オブジェクトのインスタンスを作成し、ネーミング・サービスに公開する必要があります。 Java™ Naming and Directory Interface (JNDI) は、標準インターフェースのセットを定義します。 このインターフェースは、ネーミング・サービスの照会や、ネーミング・サービスへのオブジェクトのバインドに使用されます。

共通オブジェクト・リクエスト・ブローカー・アーキテクチャー (CORBA) 環境の CosNaming サービスをネーミング・サービスの実装にすることができます。 CosNaming サービスは、ネーミング・サービスの集合で、 CORBA によって定義された一連のインターフェースとして実装されます。 あるいは、 リモート・メソッド呼び出し (RMI) (JRMP) アプリケーションの RMI レジストリーを使用して ネーミング・サービスを実装できます。 CORBA および RMI で、JNDI を使用できます。 使用した場合、サーバー実装が使用されるネーミング・サービスに非依存になります。 例えば、以下のコードを使用してネーミング・サービスを取得し、そこにオブジェクト参照をバインドできます。
Context ctx = new InitialContext(...); 	// get hold of the initial context 
ctx.bind("sample", sampleReference);		// bind the reference to the name "sample"	 	
Object obj = ctx.lookup("sample");		// obtain the reference
どのネーミング実装が使用されているかをアプリケーションに通知するには、以下のいずれかの Java プロパティーを設定する必要があります。
java.naming.factory.initial
javax.naming.Context.INITIAL_CONTEXT_FACTORY とも定義されます。このプロパティーは、ネーミング・サービス・プロバイダーの初期コンテキスト・ファクトリーのクラス名を指定します。 RMI レジストリーの場合、クラス名は com.sun.jndi.rmi.registry.RegistryContextFactory です。 CosNaming サービスの場合、クラス名は com.sun.jndi.cosnaming.CNCtxFactoryです。
java.naming.provider.url
このプロパティーはルート・ネーミング・コンテキスト、オブジェクト・リクエスト・ブローカー (ORB)、またはその両方を構成します。 ネーミング・サービスが異なるホストに保管されている場合に使用され、以下のような異なる URI スキームをとることができます。
  • rmi
  • corbaname
  • corbaloc
  • IOR
  • iiop
  • iiopname
例:
rmi://[<host>[:<port>]][/<initial_context>] for RMI registry    
iiop://[<host>[:<port>]][/<cosnaming_name>] for COSNaming
その環境の前のプロパティーを取得するには、以下のようなコードを使用します。
Hashtable env = new Hashtable(); 	
Env.put(Context.INITIAL_CONTEXT_FACTORY, 		
       "com.sun.jndi.cosnaming.CNCtxFactory");
そして、ハッシュ・テーブルを InitialContext のコンストラクターに引数として渡します。

例えば、RMI (JRMP) を使用する場合は、サーバントのインスタンスを作成してから、前述の手順に従ってこの参照をネーミング・サービスにバインドします。

ただし、CORBA (Java IDL) を使用する場合は、ORB を作成する必要があるため、追加の作業を行う必要があります。 ORB では、サーバント参照をリモート呼び出しで使用できるようにする必要があります。 この仕組みは通常、ORB のオブジェクト・アダプターにより制御されます。
public class Server {
	public static void main (String args []) {
		try {
			ORB orb = ORB.init(args, null);
			
			// Get reference to the root poa & activate the POAManager 
			POA poa = (POA)orb.resolve_initial_references("RootPOA"); 
			poa.the_POAManager().activate(); 
			
			// Create a servant and register with the ORB
			SampleImpl sample = new SampleImpl();			
			sample.setORB(orb); 

			// TIE model ONLY
			// create a tie, with servant being the delegate and
			// obtain the reference ref for the tie
			SamplePOATie tie = new SamplePOATie(sample, poa); 
			Sample ref = tie._this(orb);

			// Inheritance model ONLY
			// get object reference from the servant 
			org.omg.CORBA.Object ref = poa.servant_to_reference(sample);
      Sample ref = SampleHelper.narrow(ref);

			// bind the object reference ref to the naming service using JNDI
	   		..........(see previous code) .....
			orb.run();
		}
		catch(Exception e) {}
	}
}
RMI-IIOP の場合:
public class Server {
	public static void main (String args []) {
		try {
			ORB orb = ORB.init(args, null);
			
			// Get reference to the root poa & activate the POAManager 
			POA poa = (POA)orb.resolve_initial_references("RootPOA"); 
			poa.the_POAManager().activate();

			// Create servant and its tie
			SampleImpl sample = new SampleImpl();
			_SampleImpl_Tie tie = (_SampleImpl_Tie)Util.getTie(sample); 
			
			// get an usable object reference 
			org.omg.CORBA.Object ref = poa.servant_to_reference((Servant)tie);

			// bind the object reference ref to the naming service using JNDI
	   		..........(see previous code) .....
		}
		catch(Exception e) {}
	}
}

以前のポータブル・オブジェクト・アダプター (POA) サーバー・コードを使用するには、 -iiop -poa オプションを一緒に使用して、 rmic がタイを生成できるようにする必要があります。 POAを使わない場合、RMI(IIOP)サーバーコードを削減して、サーバント(SampleImpl sample = new SampleImpl())をインスタンス化できます。次に、RMI(JRMP)環境で通常行われるように、サーバントをネーミングサービスにバインドします。 この場合、-iiop オプションのみを使用して、rmic により RMI-IIOP タイを生成できるようにする必要があります。 -iiopを省略すると、RMI (JRMP) スケルトンが生成されます。

RMI-IIOP オブジェクトをご使用のサーバーにエクスポートする場合、必ずしも JRMP と IIOP のいずれかを選択する必要はありません。 JRMP クライアントおよび IIOP クライアントをサポートするシングル・サーバー・オブジェクトが必要な場合は、RMI-IIOP オブジェクトを JRMP と IIOP に同時にエクスポートできます。 RMI-IIOP 用語では、このアクションは二重エクスポートと呼ばれます。

RMI クライアントの例:
public class SampleClient { 	 
  public static void main(String [] args) {  	    
    try{ 			 		
      Sample sampleref
      //Look-up the naming service using JNDI and get the reference 			
            ......... 		
      // Invoke method   		
      System.out.println(sampleRef.message()); 	    
    } 	    
    catch(Exception e) {} 	  
  } 	
}  
CORBA クライアントの例:
public class SampleClient {
	public static void main (String [] args) {
		try {
			ORB orb = ORB.init(args, null);
			// Look up the naming service using JNDI
			......
			// Narrowing the reference to the correct class
			Sample sampleRef = SampleHelper.narrow(o);
			// Method Invocation
			System.out.println(sampleRef.message());
		}
		catch(Exception e) {}
	}
}
RMI-IIOP クライアントの例:
public class SampleClient {
	public static void main (String [] args) {
		try{
			ORB orb = ORB.init(args, null);
			// Retrieving reference from naming service
			........
			// Narrowing the reference to the correct class
			Sample sampleRef = (Sample)PortableRemoteObject.narrow(o, Sample.class);
			// Method Invocation
			System.out.println(sampleRef.message());
		}
		catch(Exception e) {}
	}
}