题目1.Java版CORBA程序1——HelloWorld
题目2.JAVA版CORBA程序2——Counter
helloworld
module sample{
interface HelloWorld{
wstring sayHello();
};
};
package sample;
/**
* sample/_HelloWorldStub.java .
* 由IDL-to-Java 编译器 (可移植), 版本 "3.2"生成
* 从HelloWorld.idl
* 2019年5月16日 星期四 下午05时03分56秒 CST
*/
public class _HelloWorldStub extends org.omg.CORBA.portable.ObjectImpl implements sample.HelloWorld
{
public String sayHello ()
{
org.omg.CORBA.portable.InputStream $in = null;
try {
org.omg.CORBA.portable.OutputStream $out = _request ("sayHello", true);
$in = _invoke ($out);
String $result = $in.read_wstring ();
return $result;
} catch (org.omg.CORBA.portable.ApplicationException $ex) {
$in = $ex.getInputStream ();
String _id = $ex.getId ();
throw new org.omg.CORBA.MARSHAL (_id);
} catch (org.omg.CORBA.portable.RemarshalException $rm) {
return sayHello ( );
} finally {
_releaseReply ($in);
}
} // sayHello
// Type-specific CORBA::Object operations
private static String[] __ids = {
"IDL:sample/HelloWorld:1.0"};
public String[] _ids ()
{
return (String[])__ids.clone ();
}
private void readObject (java.io.ObjectInputStream s) throws java.io.IOException
{
String str = s.readUTF ();
String[] args = null;
java.util.Properties props = null;
org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init (args, props);
try {
org.omg.CORBA.Object obj = orb.string_to_object (str);
org.omg.CORBA.portable.Delegate delegate = ((org.omg.CORBA.portable.ObjectImpl) obj)._get_delegate ();
_set_delegate (delegate);
} finally {
orb.destroy() ;
}
}
private void writeObject (java.io.ObjectOutputStream s) throws java.io.IOException
{
String[] args = null;
java.util.Properties props = null;
org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init (args, props);
try {
String str = orb.object_to_string (this);
s.writeUTF (str);
} finally {
orb.destroy() ;
}
}
} // class _HelloWorldStub
package sample;
/**
* sample/HelloWorld.java .
* 由IDL-to-Java 编译器 (可移植), 版本 "3.2"生成
* 从HelloWorld.idl
* 2019年5月16日 星期四 下午05时03分56秒 CST
*/
public interface HelloWorld extends HelloWorldOperations, org.omg.CORBA.Object, org.omg.CORBA.portable.IDLEntity
{
} // interface HelloWorld
package sample;
import sample.*;
import org.omg.CosNaming.*;
import org.omg.CORBA.*;
public class HelloWorldClient {
public static void main(String args[]) {
try{
ORB orb = ORB.init(args, null);
org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
NamingContext ncRef = NamingContextHelper.narrow(objRef);
NameComponent nc = new NameComponent("Hello","");
NameComponent path[] = {nc};
HelloWorld helloWorld = HelloWorldHelper.narrow(ncRef.resolve(path));
String hello = helloWorld.sayHello();
System.out.println(hello);
} catch (Exception e) {
System.out.println("ERROR : " + e) ;
e.printStackTrace(System.out);
}
}
}
package sample;
/**
* sample/HelloWorldHelper.java .
* 由IDL-to-Java 编译器 (可移植), 版本 "3.2"生成
* 从HelloWorld.idl
* 2019年5月16日 星期四 下午05时03分56秒 CST
*/
abstract public class HelloWorldHelper
{
private static String _id = "IDL:sample/HelloWorld:1.0";
public static void insert (org.omg.CORBA.Any a, sample.HelloWorld that)
{
org.omg.CORBA.portable.OutputStream out = a.create_output_stream ();
a.type (type ());
write (out, that);
a.read_value (out.create_input_stream (), type ());
}
public static sample.HelloWorld extract (org.omg.CORBA.Any a)
{
return read (a.create_input_stream ());
}
private static org.omg.CORBA.TypeCode __typeCode = null;
synchronized public static org.omg.CORBA.TypeCode type ()
{
if (__typeCode == null)
{
__typeCode = org.omg.CORBA.ORB.init ().create_interface_tc (sample.HelloWorldHelper.id (), "HelloWorld");
}
return __typeCode;
}
public static String id ()
{
return _id;
}
public static sample.HelloWorld read (org.omg.CORBA.portable.InputStream istream)
{
return narrow (istream.read_Object (_HelloWorldStub.class));
}
public static void write (org.omg.CORBA.portable.OutputStream ostream, sample.HelloWorld value)
{
ostream.write_Object ((org.omg.CORBA.Object) value);
}
public static sample.HelloWorld narrow (org.omg.CORBA.Object obj)
{
if (obj == null)
return null;
else if (obj instanceof sample.HelloWorld)
return (sample.HelloWorld)obj;
else if (!obj._is_a (id ()))
throw new org.omg.CORBA.BAD_PARAM ();
else
{
org.omg.CORBA.portable.Delegate delegate = ((org.omg.CORBA.portable.ObjectImpl)obj)._get_delegate ();
sample._HelloWorldStub stub = new sample._HelloWorldStub ();
stub._set_delegate(delegate);
return stub;
}
}
public static sample.HelloWorld unchecked_narrow (org.omg.CORBA.Object obj)
{
if (obj == null)
return null;
else if (obj instanceof sample.HelloWorld)
return (sample.HelloWorld)obj;
else
{
org.omg.CORBA.portable.Delegate delegate = ((org.omg.CORBA.portable.ObjectImpl)obj)._get_delegate ();
sample._HelloWorldStub stub = new sample._HelloWorldStub ();
stub._set_delegate(delegate);
return stub;
}
}
}
package sample;
/**
* sample/HelloWorldHolder.java .
* 由IDL-to-Java 编译器 (可移植), 版本 "3.2"生成
* 从HelloWorld.idl
* 2019年5月16日 星期四 下午05时03分56秒 CST
*/
public final class HelloWorldHolder implements org.omg.CORBA.portable.Streamable
{
public sample.HelloWorld value = null;
public HelloWorldHolder ()
{
}
public HelloWorldHolder (sample.HelloWorld initialValue)
{
value = initialValue;
}
public void _read (org.omg.CORBA.portable.InputStream i)
{
value = sample.HelloWorldHelper.read (i);
}
public void _write (org.omg.CORBA.portable.OutputStream o)
{
sample.HelloWorldHelper.write (o, value);
}
public org.omg.CORBA.TypeCode _type ()
{
return sample.HelloWorldHelper.type ();
}
}
package sample;
/**
* sample/HelloWorldOperations.java .
* 由IDL-to-Java 编译器 (可移植), 版本 "3.2"生成
* 从HelloWorld.idl
* 2019年5月16日 星期四 下午05时03分56秒 CST
*/
public interface HelloWorldOperations
{
String sayHello ();
} // interface HelloWorldOperations
package sample;
/**
* sample/HelloWorldPOA.java .
* 由IDL-to-Java 编译器 (可移植), 版本 "3.2"生成
* 从HelloWorld.idl
* 2019年5月16日 星期四 下午05时03分56秒 CST
*/
public abstract class HelloWorldPOA extends org.omg.PortableServer.Servant
implements sample.HelloWorldOperations, org.omg.CORBA.portable.InvokeHandler
{
// Constructors
private static java.util.Hashtable _methods = new java.util.Hashtable ();
static
{
_methods.put ("sayHello", new java.lang.Integer (0));
}
public org.omg.CORBA.portable.OutputStream _invoke (String $method,
org.omg.CORBA.portable.InputStream in,
org.omg.CORBA.portable.ResponseHandler $rh)
{
org.omg.CORBA.portable.OutputStream out = null;
java.lang.Integer __method = (java.lang.Integer)_methods.get ($method);
if (__method == null)
throw new org.omg.CORBA.BAD_OPERATION (0, org.omg.CORBA.CompletionStatus.COMPLETED_MAYBE);
switch (__method.intValue ())
{
case 0: // sample/HelloWorld/sayHello
{
String $result = null;
$result = this.sayHello ();
out = $rh.createReply();
out.write_wstring ($result);
break;
}
default:
throw new org.omg.CORBA.BAD_OPERATION (0, org.omg.CORBA.CompletionStatus.COMPLETED_MAYBE);
}
return out;
} // _invoke
// Type-specific CORBA::Object operations
private static String[] __ids = {
"IDL:sample/HelloWorld:1.0"};
public String[] _all_interfaces (org.omg.PortableServer.POA poa, byte[] objectId)
{
return (String[])__ids.clone ();
}
public HelloWorld _this()
{
return HelloWorldHelper.narrow(
super._this_object());
}
public HelloWorld _this(org.omg.CORBA.ORB orb)
{
return HelloWorldHelper.narrow(
super._this_object(orb));
}
} // class HelloWorldPOA
package sample;
import sample.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
import org.omg.CORBA.portable.*;
import org.omg.PortableServer.*;
class HelloWorldServant extends HelloWorldPOA{ //对象实现类
public String sayHello(){
return "\nHello World!\n"+"软工1704"+"王伟";
}
}
public class HelloWorldServer{ //服务程序
public static void main(String args[]){
try{
//初始化ORB
ORB orb = ORB.init(args, null);
//取根POA的引用
org.omg.CORBA.Object poaobj = orb.resolve_initial_references ("RootPOA");
org.omg.PortableServer.POA rootPOA = org.omg.PortableServer.POAHelper.narrow(poaobj);
org.omg.PortableServer.POAManager manager = rootPOA.the_POAManager();
//创建伺服对象
HelloWorldServant objRef = new HelloWorldServant();
HelloWorld obj = objRef._this(orb);
//绑定命名服务
NamingContext ncRef = NamingContextHelper.narrow(orb.resolve_initial_references("NameService"));
NameComponent nc = new NameComponent("Hello", "");
NameComponent path[] = {nc};
ncRef.rebind(path, obj);
//激活POA管理器
manager.activate();
//等待处理客户程序的请求
System.out.println("HelloWorld is running!");
orb.run();
}catch (Exception e) {
System.err.println("ERROR: " + e);
e.printStackTrace(System.out);
}
}
}
2
自加减
module CounterApp{
interface Counter{
readonly attribute long value;
void inc();
void dec();
};
};
package CounterApp;
/**
* CounterApp/_CounterStub.java .
* 由IDL-to-Java 编译器 (可移植), 版本 "3.2"生成
* 从counter.idl
* 2019年5月16日 星期四 下午05时18分42秒 CST
*/
public class _CounterStub extends org.omg.CORBA.portable.ObjectImpl implements CounterApp.Counter
{
public int value ()
{
org.omg.CORBA.portable.InputStream $in = null;
try {
org.omg.CORBA.portable.OutputStream $out = _request ("_get_value", true);
$in = _invoke ($out);
int $result = $in.read_long ();
return $result;
} catch (org.omg.CORBA.portable.ApplicationException $ex) {
$in = $ex.getInputStream ();
String _id = $ex.getId ();
throw new org.omg.CORBA.MARSHAL (_id);
} catch (org.omg.CORBA.portable.RemarshalException $rm) {
return value ( );
} finally {
_releaseReply ($in);
}
} // value
public void inc ()
{
org.omg.CORBA.portable.InputStream $in = null;
try {
org.omg.CORBA.portable.OutputStream $out = _request ("inc", true);
$in = _invoke ($out);
return;
} catch (org.omg.CORBA.portable.ApplicationException $ex) {
$in = $ex.getInputStream ();
String _id = $ex.getId ();
throw new org.omg.CORBA.MARSHAL (_id);
} catch (org.omg.CORBA.portable.RemarshalException $rm) {
inc ( );
} finally {
_releaseReply ($in);
}
} // inc
public void dec ()
{
org.omg.CORBA.portable.InputStream $in = null;
try {
org.omg.CORBA.portable.OutputStream $out = _request ("dec", true);
$in = _invoke ($out);
return;
} catch (org.omg.CORBA.portable.ApplicationException $ex) {
$in = $ex.getInputStream ();
String _id = $ex.getId ();
throw new org.omg.CORBA.MARSHAL (_id);
} catch (org.omg.CORBA.portable.RemarshalException $rm) {
dec ( );
} finally {
_releaseReply ($in);
}
} // dec
// Type-specific CORBA::Object operations
private static String[] __ids = {
"IDL:CounterApp/Counter:1.0"};
public String[] _ids ()
{
return (String[])__ids.clone ();
}
private void readObject (java.io.ObjectInputStream s) throws java.io.IOException
{
String str = s.readUTF ();
String[] args = null;
java.util.Properties props = null;
org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init (args, props);
try {
org.omg.CORBA.Object obj = orb.string_to_object (str);
org.omg.CORBA.portable.Delegate delegate = ((org.omg.CORBA.portable.ObjectImpl) obj)._get_delegate ();
_set_delegate (delegate);
} finally {
orb.destroy() ;
}
}
private void writeObject (java.io.ObjectOutputStream s) throws java.io.IOException
{
String[] args = null;
java.util.Properties props = null;
org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init (args, props);
try {
String str = orb.object_to_string (this);
s.writeUTF (str);
} finally {
orb.destroy() ;
}
}
} // class _CounterStub
package CounterApp;
import CounterApp.*;
import java.util.*;
import java.io.*;
import org.omg.CORBA.*;
import org.omg.CosNaming.*;
public class Client {
public static void main(String[] args){
try{
ORB orb = ORB.init(args, null);
org.omg.CORBA.Object obj = orb.resolve_initial_references("NameService");
NamingContext ncRef = NamingContextHelper.narrow(obj);
NameComponent nc = new NameComponent("Count","");
NameComponent path[] = {nc};
String ref = null;
try{
Scanner reader = new Scanner(new File("Counter.ref"));
ref = reader.nextLine();
}catch(IOException ex){
System.out.println("File error: "+ex.getMessage());
System.exit(2);
}
obj = orb.string_to_object(ref);
if(obj == null){
System.out.println("Invalid IOR");
System.exit(4);
}
Counter c = null;
try{
c = CounterHelper.narrow(obj);
}catch(BAD_PARAM ex){
System.out.println("Narrowing failed");
System.exit(3);
}
int inp = -1;
do{
System.out.print("Counter value: "+c.value()+"\nAction(+/-/e)?");
System.out.flush();
do{
try{
inp = System.in.read();
}catch(IOException ioe){}
}while(inp != '+' && inp != '-' && inp != 'e');
if(inp == '+')
c.inc();
else if(inp == '-')
c.dec();
}while(inp != 'e');
}catch(Exception ex){
System.out.println("Exception: "+ex.getMessage());
System.exit(1);
}
}
}
package CounterApp;
/**
* CounterApp/Counter.java .
* 由IDL-to-Java 编译器 (可移植), 版本 "3.2"生成
* 从counter.idl
* 2019年5月16日 星期四 下午05时18分42秒 CST
*/
public interface Counter extends CounterOperations, org.omg.CORBA.Object, org.omg.CORBA.portable.IDLEntity
{
} // interface Counter
package CounterApp;
/**
* CounterApp/CounterHelper.java .
* 由IDL-to-Java 编译器 (可移植), 版本 "3.2"生成
* 从counter.idl
* 2019年5月16日 星期四 下午05时18分42秒 CST
*/
abstract public class CounterHelper
{
private static String _id = "IDL:CounterApp/Counter:1.0";
public static void insert (org.omg.CORBA.Any a, CounterApp.Counter that)
{
org.omg.CORBA.portable.OutputStream out = a.create_output_stream ();
a.type (type ());
write (out, that);
a.read_value (out.create_input_stream (), type ());
}
public static CounterApp.Counter extract (org.omg.CORBA.Any a)
{
return read (a.create_input_stream ());
}
private static org.omg.CORBA.TypeCode __typeCode = null;
synchronized public static org.omg.CORBA.TypeCode type ()
{
if (__typeCode == null)
{
__typeCode = org.omg.CORBA.ORB.init ().create_interface_tc (CounterApp.CounterHelper.id (), "Counter");
}
return __typeCode;
}
public static String id ()
{
return _id;
}
public static CounterApp.Counter read (org.omg.CORBA.portable.InputStream istream)
{
return narrow (istream.read_Object (_CounterStub.class));
}
public static void write (org.omg.CORBA.portable.OutputStream ostream, CounterApp.Counter value)
{
ostream.write_Object ((org.omg.CORBA.Object) value);
}
public static CounterApp.Counter narrow (org.omg.CORBA.Object obj)
{
if (obj == null)
return null;
else if (obj instanceof CounterApp.Counter)
return (CounterApp.Counter)obj;
else if (!obj._is_a (id ()))
throw new org.omg.CORBA.BAD_PARAM ();
else
{
org.omg.CORBA.portable.Delegate delegate = ((org.omg.CORBA.portable.ObjectImpl)obj)._get_delegate ();
CounterApp._CounterStub stub = new CounterApp._CounterStub ();
stub._set_delegate(delegate);
return stub;
}
}
public static CounterApp.Counter unchecked_narrow (org.omg.CORBA.Object obj)
{
if (obj == null)
return null;
else if (obj instanceof CounterApp.Counter)
return (CounterApp.Counter)obj;
else
{
org.omg.CORBA.portable.Delegate delegate = ((org.omg.CORBA.portable.ObjectImpl)obj)._get_delegate ();
CounterApp._CounterStub stub = new CounterApp._CounterStub ();
stub._set_delegate(delegate);
return stub;
}
}
}
package CounterApp;
/**
* CounterApp/CounterHolder.java .
* 由IDL-to-Java 编译器 (可移植), 版本 "3.2"生成
* 从counter.idl
* 2019年5月16日 星期四 下午05时18分42秒 CST
*/
public final class CounterHolder implements org.omg.CORBA.portable.Streamable
{
public CounterApp.Counter value = null;
public CounterHolder ()
{
}
public CounterHolder (CounterApp.Counter initialValue)
{
value = initialValue;
}
public void _read (org.omg.CORBA.portable.InputStream i)
{
value = CounterApp.CounterHelper.read (i);
}
public void _write (org.omg.CORBA.portable.OutputStream o)
{
CounterApp.CounterHelper.write (o, value);
}
public org.omg.CORBA.TypeCode _type ()
{
return CounterApp.CounterHelper.type ();
}
}
package CounterApp;
import CounterApp.*;
public class CounterImpl extends CounterPOA {
private int count;
public CounterImpl(){
count = 0;
}
public void inc(){
count++;
}
public void dec(){
count --;
}
public int value(){
return count;
}
}
package CounterApp;
/**
* CounterApp/CounterOperations.java .
* 由IDL-to-Java 编译器 (可移植), 版本 "3.2"生成
* 从counter.idl
* 2019年5月16日 星期四 下午05时18分42秒 CST
*/
public interface CounterOperations
{
int value ();
void inc ();
void dec ();
} // interface CounterOperations
package CounterApp;
/**
* CounterApp/CounterPOA.java .
* 由IDL-to-Java 编译器 (可移植), 版本 "3.2"生成
* 从counter.idl
* 2019年5月16日 星期四 下午05时18分42秒 CST
*/
public abstract class CounterPOA extends org.omg.PortableServer.Servant
implements CounterApp.CounterOperations, org.omg.CORBA.portable.InvokeHandler
{
// Constructors
private static java.util.Hashtable _methods = new java.util.Hashtable ();
static
{
_methods.put ("_get_value", new java.lang.Integer (0));
_methods.put ("inc", new java.lang.Integer (1));
_methods.put ("dec", new java.lang.Integer (2));
}
public org.omg.CORBA.portable.OutputStream _invoke (String $method,
org.omg.CORBA.portable.InputStream in,
org.omg.CORBA.portable.ResponseHandler $rh)
{
org.omg.CORBA.portable.OutputStream out = null;
java.lang.Integer __method = (java.lang.Integer)_methods.get ($method);
if (__method == null)
throw new org.omg.CORBA.BAD_OPERATION (0, org.omg.CORBA.CompletionStatus.COMPLETED_MAYBE);
switch (__method.intValue ())
{
case 0: // CounterApp/Counter/_get_value
{
int $result = (int)0;
$result = this.value ();
out = $rh.createReply();
out.write_long ($result);
break;
}
case 1: // CounterApp/Counter/inc
{
this.inc ();
out = $rh.createReply();
break;
}
case 2: // CounterApp/Counter/dec
{
this.dec ();
out = $rh.createReply();
break;
}
default:
throw new org.omg.CORBA.BAD_OPERATION (0, org.omg.CORBA.CompletionStatus.COMPLETED_MAYBE);
}
return out;
} // _invoke
// Type-specific CORBA::Object operations
private static String[] __ids = {
"IDL:CounterApp/Counter:1.0"};
public String[] _all_interfaces (org.omg.PortableServer.POA poa, byte[] objectId)
{
return (String[])__ids.clone ();
}
public Counter _this()
{
return CounterHelper.narrow(
super._this_object());
}
public Counter _this(org.omg.CORBA.ORB orb)
{
return CounterHelper.narrow(
super._this_object(orb));
}
} // class CounterPOA
package CounterApp;
import CounterApp.*;
import java.io.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
import org.omg.CORBA.portable.*;
import org.omg.PortableServer.*;
public class Server {
public static void main(String[] args){
try{
ORB orb = ORB.init(args, null);
org.omg.CORBA.Object poaobj = orb.resolve_initial_references ("RootPOA");
org.omg.PortableServer.POA rootPOA = org.omg.PortableServer.POAHelper.narrow(poaobj);
org.omg.PortableServer.POAManager manager = rootPOA.the_POAManager();
CounterImpl c_impl = new CounterImpl();
Counter c = c_impl._this(orb);
NamingContext ncRef = NamingContextHelper.narrow(orb.resolve_initial_references("NameService"));
NameComponent nc = new NameComponent("Count", "");
NameComponent path[] = {nc};
ncRef.rebind(path, c);
FileOutputStream file = new FileOutputStream("Counter.ref");
PrintWriter writer = new PrintWriter(file);
String ref = orb.object_to_string(c);
writer.println(ref);
writer.flush();
file.close();
System.out.println("Server started."+" Stop:Ctrl-c");
rootPOA.the_POAManager().activate();
orb.run();
}catch(IOException ex){
System.out.println("File error:"+ex.getMessage());
System.exit(2);
}catch(Exception ex){
System.out.println("Exception: "+ex.getMessage());
System.exit(1);
}
}
}