このページでは、次の内容について説明します。
このプログラム例のコンパイル方法と実行方法については、「Hello World の構築方法と実行方法」を参照してください。ソースコードも用意されています。
インタフェース定義 (
Hello.idl
)
次の OMG IDL は、文字列を返す sayHello() というオペレーションが 1 つだけ含まれた CORBA オブジェクトを記述したものです。
module HelloApp { interface Hello { string sayHello(); }; };
この IDL インタフェースは、次のコマンドでコンパイルします。
idltojava Hello.idlこれにより、
HelloApp
サブディレクトリに次の 5 つのファイルが生成されます。
HelloServer.java
ファイルと HelloClient.java
ファイルで実装します。
HelloServer.java
)ここで紹介するサーバは、サーバントとサーバの 2 つのクラスで構成されます。サーバントである HelloServant は、Hello IDL インタフェースの実装です。つまり、Hello の各インスタンスは、HelloServant のインスタンスにより実装されます。このサーバントは、idltojava コンパイラにより上記の IDL から生成される _HelloImplBase のサブクラスです。サーバントには、IDL のオペレーションごとに 1 つのメソッドが含まれます。この例では sayHello() メソッドだけが含まれます。サーバントメソッドは、Java の通常のメソッドと変わりはありません。引数や結果の整列化など、ORB 関連処理を行うコードは、サーバとスタブで実装します。
サーバクラスにはサーバの main() メソッドが含まれます。この main() メソッドでは、次の処理を行います。
// Copyright and License import HelloApp.*; import org.omg.CosNaming.*; import org.omg.CosNaming.NamingContextPackage.*; import org.omg.CORBA.*; class HelloServant extends _HelloImplBase { public String sayHello() { return "¥nHello world !!¥n"; } } 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.connect(helloRef); // get the root naming context org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService"); NamingContext ncRef = NamingContextHelper.narrow(objRef); // bind the Object Reference in Naming NameComponent nc = new NameComponent("Hello", ""); NameComponent path[] = {nc}; ncRef.rebind(path, helloRef); // 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); } } }
HelloClient.java
)このアプリケーションクライアントでは、次の処理を行います。
// Copyright and License import HelloApp.*; import org.omg.CosNaming.*; import org.omg.CORBA.*; public class HelloClient { public static void main(String args[]) { try{ // create and initialize the ORB ORB orb = ORB.init(args, null); // get the root naming context org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService"); NamingContext ncRef = NamingContextHelper.narrow(objRef); // resolve the Object Reference in Naming NameComponent nc = new NameComponent("Hello", ""); NameComponent path[] = {nc}; Hello helloRef = HelloHelper.narrow(ncRef.resolve(path)); // call the Hello server object and print results String hello = helloRef.sayHello(); System.out.println(hello); } catch (Exception e) { System.out.println("ERROR : " + e) ; e.printStackTrace(System.out); } } }
HelloApplet.java
)このアプレットクライアントでは、次の処理を行います。
// Copyright and License import HelloApp.*; import org.omg.CosNaming.*; import org.omg.CosNaming.NamingContextPackage.*; import org.omg.CORBA.*; import java.awt.Graphics; public class HelloApplet extends java.applet.Applet { public void init() { try { // create and initialize the ORB ORB orb = ORB.init(this, null); // get the root naming context org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService"); NamingContext ncRef = NamingContextHelper.narrow(objRef); // resolve the Object Reference in Naming NameComponent nc = new NameComponent("Hello", ""); NameComponent path[] = {nc}; Hello helloRef = HelloHelper.narrow(ncRef.resolve(path)); // call the Hello server object and print results message = helloRef.sayHello(); } catch (Exception e) { System.out.println("HelloApplet exception: " + e.getMessage()); e.printStackTrace(System.out); } } public void paint(Graphics g) { g.drawString(message, 25, 50); } String message = ""; }
このプログラム例のソースコードは、examples/hello ディレクトリにあります。以下の説明では、Java IDL ネームサーバ用にポート 1050 を使用できることを前提としています。必要であれば、別のポートに変更してください。なお、1024 以下のポートを使用する場合は、UNIX マシンであればスーパーユーザの権限が、Windows95 および NT であれば管理者の権限が必要です。また、以下の説明ではパス名でスラッシュ (/) を使用していますが、Windows95 および NT ではバックスラッシュ (¥) に置き換える必要があります。
注 : Hello World を構築し実行するには、idltojava
が必要です。idltojava
は、JDC (Java Developer Connection) Web ページから無償でダウンロードできます。このページにアクセスするには、JDC のメンバでなければなりません。JDC のメンバでない場合は、JDC に登録する必要があります。登録料は無料です。ダウンロードページは次のとおりです。
http://developer.java.sun.com/developer/earlyAccess/jdk12/idltojava.html拡張子「.bin」のファイルがダウンロードされるので、このファイルを実行 (コマンド行で完全なファイル名を入力) して解凍してください。解凍しないと、
idltojava
コンパイラを使用できません。
cd docs/guide/idl/examples/hello
idltojava Hello.idl
javac *.java HelloApp/*.java
tnameserv -ORBInitialPort 1050&
java HelloServer -ORBInitialPort 1050
java HelloClient -ORBInitialPort 1050
ホーム |
Copyright © 1995-98 Sun Microsystems, Inc. All Rights Reserved.