Interstage Application Server アプリケーション作成ガイド (CORBAサービス編) |
目次
索引
![]() ![]() |
第6章 アプリケーションの開発(Java言語) | > 6.18 データ型に対するマッピング |
IDL言語でany型anyを指定した場合、Java言語ではorg.omg.CORBA.Anyクラスとorg.omg.CORBA.AnyHolderクラスになります。
org.omg.CORBA.Anyクラスは、any型を操作するためのメソッド、およびインスタンスメンバが定義されています。本クラスは、inパラメタおよび復帰値を扱う場合に使用します。
以下の表にorg.omg.CORBA.Anyクラス中で定義されているメンバの意味を示します。
org.omg.CORBA.Anyクラスのメンバ |
意味 |
insert_xxx() |
xxx型の要素の値を挿入します。 |
extract_xxx() |
xxx型の要素の値を取り出します。 |
type() |
TypeCodeの参照/設定。 |
org.omg.CORBA.AnyHolderクラスは、以下のようにインスタンスメンバ、デフォルトコンストラクタおよびコンストラクタが定義されています。本クラスは、outやinoutパラメタを受け渡す場合に使用します。
< org.omg.CORBA.AnyHolderクラス>
final public class AnyHolder { public Any value; public AnyHolder() { } public AnyHolder(Any initial){...} }
以下の表にorg.omg.CORBA.AnyHolderクラス中で定義されているメンバの意味を示します。
org.omg.CORBA.AnyHolderクラスのメンバ |
意味 |
value |
要素の値。 |
デフォルトコンストラクタ |
valueメンバへの設定なしで、インスタンスを生成するために使用します。 |
コンストラクタ |
パラメタで指定された値をvalueメンバに設定します。 |
IDL言語での定義が以下のように定義されていた場合について、アプリケーションプログラムの例を示します。
【IDL言語】
module ODsample{ struct sample1 { long para1; string para2; }; struct sample2 { char para1; float para2; }; struct sample3 { char para1; double para2; }; interface anytest{ any op1(in any any1, out any any2, inout any any3 ); }; };
これをJava言語で記述すると以下のようになります。
【Java言語】
<インタフェース>
package ODsample; public interface anytestOperations { public org.omg.CORBA.Any op1(org.omg.CORBA.Any any1, org.omg.CORBA.AnyHolder any2, org.omg.CORBA.AnyHolder any3 ); }
<ユーザ定義型クラス>
package ODsample; final public class sample1 implements org.omg.CORBA.portable.IDLEntity { public int para1; public java.lang.String para2; public sample1() {} public sample1(int para1, java.lang.String para2){...} } package ODsample; final public class sample2 implements org.omg.CORBA.portable.IDLEntity { public char para1; public float para2; public sample2() {} public sample2(char para1, float para2) {...} } package ODsample; final public class sample3 implements org.omg.CORBA.portable.IDLEntity { public char para1; public double para2; public sample3() {} public sample3(char para1, double para2){...} }
<ユーザ定義型Holderクラス>
package ODsample; public class sample1Holder implements org.omg.CORBA.portable.Streamable { public ODsample.sample1 value; public sample1Holder(){} public sample1Holder(ODsample.sample1 value) {...} public void _read(org.omg.CORBA.portable.InputStream i) {...} public void _write(org.omg.CORBA.portable.OutputStream o) {...} public org.omg.CORBA.TypeCode _type() {...} } package ODsample; public class sample2Holder implements org.omg.CORBA.portable.Streamable { public ODsample.sample2 value; public sample2Holder() {} public sample2Holder(ODsample.sample2 value) {...} public void _read(org.omg.CORBA.portable.InputStream i) {...} public void _write(org.omg.CORBA.portable.OutputStream o) {...} public org.omg.CORBA.TypeCode _type() {...} } package ODsample; public class sample3Holder implements org.omg.CORBA.portable.Streamable { public ODsample.sample3 value; public sample3Holder() {} public sample3Holder(ODsample.sample3 value) {...} public void _read(org.omg.CORBA.portable.InputStream i) {...} public void _write(org.omg.CORBA.portable.OutputStream o) {...} public org.omg.CORBA.TypeCode _type() {...} }
<ユーザ定義型Helperクラス>
package ODsample; public class sample1Helper { public static void insert(org.omg.CORBA.Any a, ODsample.sample1 value){...} public static ODsample.sample1 extract(org.omg.CORBA.Any a){...} public static String id(){...} public java.lang.Object read_Object(org.omg.CORBA.portable.InputStream istream){...} public void write_Object(org.omg.CORBA.portable.OutputStream ostream, Object value){...} public java.lang.String get_id(){...} public org.omg.CORBA.TypeCode get_type(){...} private static org.omg.CORBA.TypeCode _type; synchronized public static org.omg.CORBA.TypeCode type(){...} public static ODsample.sample1 read(org.omg.CORBA.portable.InputStream istream){...} public static void write(org.omg.CORBA.portable.OutputStream ostream, ODsample.sample1 value){...} } package ODsample; public class sample2Helper { public static void insert(org.omg.CORBA.Any a, ODsample.sample2 value){...} public static ODsample.sample2 extract(org.omg.CORBA.Any a){...} public static String id(){...} public java.lang.Object read_Object(org.omg.CORBA.portable.InputStream istream){...} public void write_Object(org.omg.CORBA.portable.OutputStream ostream, Object value){...} public java.lang.String get_id( ){...} public org.omg.CORBA.TypeCode get_type(){...} private static org.omg.CORBA.TypeCode _type; synchronized public static org.omg.CORBA.TypeCode type(){...} public static ODsample.sample2 read(org.omg.CORBA.portable.InputStream istream){...} public static void write(org.omg.CORBA.portable.OutputStream ostream, ODsample.sample2 value){...} } package ODsample; public class sample3Helper { public static void insert(org.omg.CORBA.Any a, ODsample.sample3 value){...} public static ODsample.sample3 extract(org.omg.CORBA.Any a){...} public static String id(){...} public java.lang.Object read_Object(org.omg.CORBA.portable.InputStream istream){...} public void write_Object(org.omg.CORBA.portable.OutputStream ostream, Object value){...} public java.lang.String get_id( ){...} public org.omg.CORBA.TypeCode get_type(){...} private static org.omg.CORBA.TypeCode _type; synchronized public static org.omg.CORBA.TypeCode type(){...} public static ODsample.sample3 read(org.omg.CORBA.portable.InputStream istream){...} public static void write(org.omg.CORBA.portable.OutputStream ostream, ODsample.sample3 value){...} }
inパラメタのany型のデータ領域は、org.omg.CORBA.ORB.create_any()メソッドで初期化して獲得します。なお、inout、outパラメタの場合は、org.omg.CORBA.AnyHolderクラスを使用してデータ領域を割り当てます。
any型にデータを代入するためには、Long、Short等の基本データ型の場合にはorg.omg.CORBA.Anyクラスの基本データ型のinsertメソッドを使用し、その他のユーザ定義型の場合にはユーザ定義型Helperクラスのinsertメソッドを使用します。これにより、値とTypeCodeの両方を同時に設定することができます。
any型からデータを取り出すためには、Long、Short等の基本データ型の場合にはorg.omg.CORBA.Anyクラスの基本データ型のextractメソッドを使用します。その他のユーザ定義型の場合にはユーザ定義型Helperクラスのextractメソッドで取り出したユーザ定義型のインスタンスを使用します。
import org.omg.CORBA.*; import ODsample.*; public class anyClient { public static void main(String args[]) { // ORBの前処理 // オブジェクトリファレンスの獲得 : try{ int int_tmp = 1; String str_tmp = "in_str:data"; // 構造体ODsample.sample1インスタンスの作成 sample1 smp1 = new sample1( int_tmp, str_tmp ); // inパラメタ用変数 org.omg.CORBA.Any in = org.omg.CORBA.ORB.init().create_any(); // org.omg.CORBA.Anyクラスにsample1の値、typeを設定 sample1Helper.insert( in, smp1 ); // outパラメタ用変数 org.omg.CORBA.AnyHolder outHolder = new org.omg.CORBA.AnyHolder(); char char_tmp = 'x'; float float_tmp = 0.003f; // 構造体ODsample.sample2インスタンスの作成 sample2 smp2 = new sample2( char_tmp, float_tmp ); org.omg.CORBA.Any any_tmp = org.omg.CORBA.ORB.init().create_any(); // org.omg.CORBA.Anyクラスにsample2の値、typeを設定 sample2Helper.insert( any_tmp, smp2 ); // inoutパラメタ用変数 org.omg.CORBA.AnyHolder inoutHolder = new org.omg.CORBA.AnyHolder( any_tmp ); // 復帰値用変数 org.omg.CORBA.Any result; // サーバアプリケーションのメソッド呼出し result = target.op1( in, outHolder, inoutHolder ); // メソッドの結果表示 // org.omg.CORBA.Anyクラスからsample3の値を取り出す System.out.println( sample3Helper.extract(result).para1 ); System.out.println( sample3Helper.extract(result).para2 ); // org.omg.CORBA.AnyHolderクラスからsample1の値を取り出す System.out.println( sample1Helper.extract(outHolder.value).para1 ); System.out.println( sample1Helper.extract(outHolder.value).para2 ); // org.omg.CORBA.AnyHolderクラスからsample2の値を取り出す System.out.println( sample2Helper.extract(inoutHolder.value).para1 ); System.out.println( sample2Helper.extract(inoutHolder.value).para2 ); } catch (java.lang.Exception e) { //エラー処理 : } } }
inout、outパラメタを参照する場合、Long、Short等の基本データ型の場合にはorg.omg.CORBA.Anyクラスの基本データ型のextractメソッドを使用します。その他のユーザ定義型の場合にはユーザ定義型Helperクラスのextractメソッドで取り出したユーザ定義型のインスタンスを使用します。
inout、outパラメタにデータを代入するためには、Long、Short等の基本データ型の場合にはorg.omg.CORBA.Anyクラスの基本データ型のinsertメソッドを使用します。その他のユーザ定義型の場合にはユーザ定義型Helperクラスのinsertメソッドを使用して設定したany型クラスをinout、outパラメタのHolderクラスのvalueに設定します。
import org.omg.CORBA.*; import org.omg.PortableServer.*; import ODsample.*; // Servantクラス // サーバアプリケーションのメソッド class anyServant extends anytestPOA { public org.omg.CORBA.Any op1( org.omg.CORBA.Any in, org.omg.CORBA.AnyHolder outHolder, org.omg.CORBA.AnyHolder inoutHolder ) { // 入力パラメタの表示 // org.omg.CORBA.Anyクラスからsample1の値を取り出す System.out.println( sample1Helper.extract(in).para1 ); System.out.println( sample1Helper.extract(in).para2 ); // org.omg.CORBA.AnyHolderクラスからsample2の値を取り出す System.out.println( sample2Helper.extract(inoutHolder.value).para1 ); System.out.println( sample2Helper.extract(inoutHolder.value).para2 ); // 出力パラメタの設定 int int_tmp = 12345; String str_tmp = "out_str:data"; // 構造体ODsample.sample1インスタンスの作成 sample1 smp1 = new sample1( int_tmp, str_tmp ); org.omg.CORBA.Any any_tmp1 = org.omg.CORBA.ORB.init().create_any(); // org.omg.CORBA.Anyクラスにsample1の値、typeを設定 sample1Helper.insert( any_tmp1, smp1 ); // outパラメタの設定 outHolder.value = any_tmp1; // inoutパラメタを取り出し、構造体ODsample.sample2インスタンスの作成 sample2 smp2 = sample2Helper.extract(inoutHolder.value); smp2.para1 = 'z'; // inoutパラメタ値を変更 smp2.para2 = smp2.para2 + 0.007f; // inoutパラメタ値を変更 org.omg.CORBA.Any any_tmp2 = org.omg.CORBA.ORB.init().create_any(); // org.omg.CORBA.Anyクラスにsample2の値、typeを設定 sample2Helper.insert( any_tmp2, smp2 ); // inoutパラメタ値の設定 inoutHolder.value = any_tmp2; // 復帰の設定 char char_tmp = 'R'; double double_tmp = 9E38; // 構造体ODsample.sample3インスタンスの作成 sample3 smp3 = new sample3( char_tmp, double_tmp ); org.omg.CORBA.Any result = org.omg.CORBA.ORB.init().create_any(); // org.omg.CORBA.Anyクラスにsample3の値、typeを設定 sample3Helper.insert( result, smp3 ); // 復帰 return( result ); } } public class anyServer { public static void main( String args[] ) { try { // ORBの前処理 : // POAオブジェクトの生成 : // Servantの生成とそのPOAへの登録 : // POAManagerの活性化 : } catch ( java.lang.Exception e ) { //エラー処理 : } } }
目次
索引
![]() ![]() |