Dynamic Skeleton Interface (DSI) を使用すると、インタフェースの詳細が事前に (コンパイル時に) わかっていないサーバントオブジェクトをサーバが提供できます。DSI を使用した場合、サーバは、IDL インタフェース定義からコンパイルされたスケルトンコードを使用する代わりにオペレーション呼び出しを動的に作成します。
CORBA クライアントの要求を COM サーバが理解できる形式に変換するには、動的サーバントを実装します。この変換処理を行うコードはすべて記述する必要があります。これを典型的な静的オブジェクト呼び出しと比較してみます。サーバは、呼び出されるインタフェース用のスケルトン (コンパイルされたもの) にアクセスできます。このスケルトンは、IDL インタフェース定義を idltojava
コンパイラでコンパイルすることにより生成されます。ORB は、要求を受け取ると、このスケルトンコードを使ってサーバ側でオペレーション引数を組み立て、結果を送り返します。
次のインタフェースについて検討してみます。
module HelloApp { interface Hello { string printHelloArgs(in string arg1, in short arg2); }; };
次に示すのは、上記インタフェース用の動的サーバントの骨組みとなるコンパイル可能なコードです。メソッド要求を解析する基本部分だけが実装されているため、変換処理を実装したコードを挿入する必要があります。
// Copyright and License import HelloApp.*; import java.util.*; import org.omg.CosNaming.*; import org.omg.CosNaming.NamingContextPackage.*; import java.io.*; import org.omg.CORBA.*; // servant must extend DynamicImplementation class HelloServant extends DynamicImplementation { // store the repository ID for the implemented interface static String[] myIDs = {"IDL:JavaIDL/DSIExample:1.0"}; ORB orb; // create a reference to the ORB HelloServant(ORB orb) { this.orb = orb; } // must implement invoke() for handling requests public void invoke(ServerRequest request) { try { System.out.println("DSI: invoke called, op = "+request.op_name()); // create an NVList to hold the parameters NVList nvlist = orb.create_list(0); // need an if statement like this for each method name if (request.op_name().equals("printHelloArgs") == true) { // need an Any for each argument Any any1 = orb.create_any(); any1.insert_string(""); nvlist.add_value("arg1", any1, ARG_IN.value); Any any2 = orb.create_any(); any2.insert_string(""); nvlist.add_value("arg2", any2, ARG_IN.value); // pass the NVList to the request to get values request.params(nvlist); System.err.println("Argument 1: In value: " + nvlist.item(0).value().extract_string()); System.err.println("Argument 2: In value: " + nvlist.item(1).value().extract_short()); TypeCode result_tc = orb.get_primitive_tc(TCKind.tk_void); Any result_any = orb.create_any(); result_any.type(result_tc); request.result(result_any); } } catch (Exception ex) { ex.printStackTrace(); System.out.println("DSIExample: Exception thrown: " + ex); } } // implement an _ids method to return repository ID of interface public String[] _ids() { return myIDs; } // HelloServer implemented in the usual fashion public class HelloServer { public static void main(String args[]) { try{ // create and initialize the ORB ORB orb = ORB.init(args, null); // create servant and register it with the ORB HelloServant helloRef = new HelloServant(orb); orb.connect(helloRef); OutputStream f = new FileOutputStream(System.getProperty("user.home")+System.getProperty("file.separator")+"DSI.ior"); DataOutputStream out = new DataOutputStream(f); String ior = orb.object_to_string(helloRef); out.writeBytes(ior); out.close(); System.out.println("IOR is "+ ior); // wait for invocations from clients java.lang.Object sync = new java.lang.Object(); synchronized (sync) { sync.wait(); } } catch (Exception e) { System.err.println("ERROR: " + e); e.printStackTrace(System.out); } } }
ホーム |
Copyright © 1995-98 Sun Microsystems, Inc. All Rights Reserved.