可以调用JCORBA,大大拓展了JCORBA的应用范围,提高了JCORBA重用性和使用的方便性。为了实例介绍的方便性,我们选择了Java应用程序作为其客户端实例。
客户端Java程序源代码如下:
import oracle.oas.jco.*;
import oracle.oas.jndi.*;
import oracle.oas.jndi.oas.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Hashtable;
import javax.naming.*;
import myStack.ServerStackRemote;
public class StackClient
{
public static void main(String args[])
{
int stackSize;
String buf, stackElement;
int n = 0;
String _host = "localhost";
ServerStackRemote s = null;
try {
String _urlStr = args[0];
if (_urlStr != null) {
URL _contactURL = new URL(_urlStr);
_host = _contactURL.getHost();
int _port = _contactURL.getPort();
if (_port != -1)
_host = _host + ":" + String.valueOf(_port);
}
Hashtable _env = new Hashtable();
_env.put(Context.URL_PKG_PREFIXES, "oracle.oas.jndi");
// Initial Context
Context _initialContext = new InitialContext(_env);
// Lookup
String _name = "oas://" + _host + "/myStack/ServerStack";
s = (ServerStackRemote) _initialContext.lookup(_name);
System.out.println();
System.out.print("Enter server stack size: ");
BufferedReader d = new BufferedReader(
new InputStreamReader(System.in));
buf = d.readLine();
stackSize = Integer.parseInt(buf);
s.setStackSize(stackSize);
System.out.println("---> Server stack size is currently " +
s.getStackSize());
for(n = 0;;n++) {
System.out.print("Enter stack element (RET to quit): ");
stackElement = d.readLine();
if (stackElement.length() == 0)
break;
s.push(stackElement);
}
System.out.println("---> Server stack currently has the following " +
n + " elements:");
System.out.println();
for (int i = n-1; i >= 0; i--)
System.out.println(" " + s.pop());
} // end try
catch (myStack.StackException se) {
System.out.println("Stack exception occurred.");
System.out.println(se);
}
catch (java.io.IOException ioe) {
System.out.println("The following error occurred while trying to read
input.");
System.out.println(ioe);
}
catch (NamingException ne) {
System.out.print("Communication error: ");
System.out.println(ne.getMessage());
ne.printStackTrace();
}
finally {
if (s != null)
s.destroy(); // from JCORemote
}
System.exit(0);
}
}
为了使客户端和服务器建立连接,还需将ServerStackRemote.class和StackException.class 复制到 ..\Stack\classes_cnt\myStack目录之下。
编译 StackClient.java 为 StackClient.class,并按第七节的方式产生客户端的JAR文件,JDK命令行如下:
prompt>javac -d ..\Stack\classes_cnt\myStack StackClient.java
prompt>cd ..\Stack\classes_cnt
prompt>jar cvf client.jar ..\Stack\classes_cnt\myStack\*.class
客户端JAR文件产生完成。
Oracle应用服务器实现Java CORBA(6)
时间:2010-09-22
十一、运行客户端程序调用JCORBA应用
至此,我们终于可以运行我们的JCORBA应用了,命令行如下:
prompt> java StackClient http://appservermachine:port
a |