1 Hello IDLインタフェース
前のHello WorldアプリケーションからのIDLを使用します。
// hello.idl
module helloWorld
{
interface Hello
{
| string sayHello();
};
};
idl2java -poa -compile -ds gensrc -d ../../lib hello.idlidl2javaコンパイラは、gensrcディレクトリにPOAのJavaコードを生成し、生成されたJavaコードを../../libディレクトリにコンパイルします。次のクラスはhelloWorldJavaパッケージで作成されます。すべてのJavaクラス命はIDLインタフェースのHelloの名前から取得されます。
Hello // Java interface corresponding to IDL interface HelloOperations // methods on the Java interface HelloHelper // helper methods for narrow, read, write HelloHolder // holder for out and inout parameters HelloPOA // inheritance based POA Skeleton HelloPOATie // delegation based POA Skeleton _HelloStub // Stub
package poaHello;
public class HelloImpl extends helloWorld.HelloPOA
{
public String sayHello()
{
| return "Hello World!\n";
}
}
HelloPOAを拡張することにより、サーバントはorg.omg.PortableServer.Servantになります。また、org.omg.CORBA.Objectではないので、CORBAオブジェクトをパラメータとして使用するorb.object_to_stringのようなAPIを使用する前に、POA APIを使用してオブジェクト参照を作成する必要があります。
4 Hello Worldサーバ
サーバはORBを作成し、サーバントを作成し、ルートPOAを使用して、オブジェクト参照を作成し、それを文字列化して書き出し、呼び出し待ちをします。
ルートPOAはパラメータとして「RootPOA」でorb.resolve_initial_referencesを使用して、ORBから取得できるローカル性が制約されたオブジェクトです。
package poaHello;
import util.Util;
import org.omg.CORBA.ORB;
import org.omg.PortableServer.POA;
import org.omg.PortableServer.Servant;
public class helloServer
{
public static void main(String[] args)
{
| try {
| |
| | // create the jBroker ORB
| | ORB orb = ORB.init(args, null);
| |
| | // create a servant
| | Servant hello = new HelloImpl();
| |
| | // get the root POA
| | POA rootPOA = (POA) orb.resolve_initial_references("RootPOA");
| |
| | // create a stringified object reference
| | String helloIOR = orb.object_to_string(
| | rootPOA.servant_to_reference(hello));
| |
| | // write the stringified object reference
| | Util.writeIOR(helloIOR, "ior", true);
| |
| | // activate the Root POA
| | rootPOA.the_POAManager().activate();
| |
| | // wait for invocations
| | System.out.println("waiting for invocations ...");
| | orb.run();
| |
| } catch (Exception ex) {
| | ex.printStackTrace();
| }
}
}
rootPOA.the_POAManaget().activateステートメントは任意です。orb.run()は自動的にルートPOAを有効にします。
package helloWorld;
import util.Util;
import org.omg.CORBA.ORB;
public class helloClient
{
public static void main(String[] args)
{
| try {
| |
| | // create the jBroker ORB
| | ORB orb = ORB.init(args, null);
| |
| | // read the stringified object reference
| | String helloIOR = Util.readIOR("ior");
| |
| | // narrow the stringified object
| | Hello hello = HelloHelper.narrow(orb.string_to_object(helloIOR));
| |
| | // invoke method on the object
| | System.out.println(hello.sayHello());
| |
| } catch (Exception ex) {
| | ex.printStackTrace();
| }
}
}
6.1 Helloの実装
HelloImpl2のサンプルからのhelloWorldを引き続き使用できます。package helloWorld; public class HelloImpl2 implements HelloOperations { public String sayHello() { | return "Hello World!\n"; } }6.2 Hello Worldサーバ
ここでは、サーバントの作成手順が前とは異なります。TIEオブジェクトは実際にHelloPOAを拡張します。package poaHello; import util.Util; import org.omg.CORBA.ORB; import org.omg.PortableServer.POA; import org.omg.PortableServer.Servant; import helloWorld.HelloImpl2; import helloWorld.HelloPOATie; public class helloServer2 { public static void main(String[] args) { | try { | | | | // create the jBroker ORB | | ORB orb = ORB.init(args, null); | | | | // create a servant | | Servant hello = new HelloPOATie(new HelloImpl2()); | | | | // get the root POA | | POA rootPOA = (POA) orb.resolve_initial_references("RootPOA"); | | | | // create a stringified object reference | | String helloIOR = orb.object_to_string( | | rootPOA.servant_to_reference(hello)); | | | | // write the stringified object reference | | Util.writeIOR(helloIOR, "ior", true); | | | | // activate the Root POA | | rootPOA.the_POAManager().activate(); | | | | // wait for invocations | | System.out.println("waiting for invocations ..."); | | orb.run(); | | | } catch (Exception ex) { | | ex.printStackTrace(); | } } }
| Copyright © 2000-2003, Novell, Inc.All rights reserved. |