📄 ch13.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"><HTML><HEAD><SCRIPT LANGUAGE="JavaScript"><!--function popUp(pPage) { var fullURL = document.location; var textURL = fullURL.toString(); var URLlen = textURL.length; var lenMinusPage = textURL.lastIndexOf("/"); lenMinusPage += 1; var fullPath = textURL.substring(0,lenMinusPage); popUpWin = window.open('','popWin','resizable=yes,scrollbars=no,width=525,height=394'); figDoc= popUpWin.document; zhtm= '<HTML><HEAD><TITLE>' + pPage + '</TITLE>'; zhtm += '</HEAD>'; zhtm += '<BODY bgcolor="#FFFFFF">'; zhtm += '<IMG SRC="' + fullPath + pPage + '">'; zhtm += '<P><B>' + pPage + '</B>'; zhtm += '</BODY></HTML>'; window.popUpWin.document.write(zhtm); window.popUpWin.document.close(); // Johnny Jackson 4/28/98 }//--> </SCRIPT> <META NAME="Author" Content="Bryan Flores"> <META HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=iso-8859-1"> <TITLE>Teach Yourself CORBA In 14 Days -- Ch 13 -- Developing for the Internet Using CORBA and Java</TITLE></HEAD><BODY TEXT="#000000" BGCOLOR="#FFFFFF"><CENTER><H1><IMG SRC="../button/sams.gif" WIDTH="171" HEIGHT="66" ALIGN="BOTTOM" BORDER="0"><BR><FONT COLOR="#000077">Teach Yourself CORBA In 14 Days</FONT></H1></CENTER><CENTER><P><A HREF="../ch12/ch12.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../ch14/ch14.htm"><IMGSRC="../button/next.gif" WIDTH="128" HEIGHT="28" ALIGN="BOTTOM" ALT="Next chapter"BORDER="0"></A><A HREF="../index.htm"><IMG SRC="../button/contents.gif" WIDTH="128"HEIGHT="28" ALIGN="BOTTOM" ALT="Contents" BORDER="0"></A> <HR></CENTER><CENTER><H1><FONT COLOR="#000077">Day 13<BR>Developing for the Internet Using CORBA and Java</FONT><A HREF="#Heading1"></A></H1></CENTER><UL> <LI><A HREF="#Heading1">Introducing Java</A> <LI><A HREF="#Heading2">Discovering the Symbiosis Between CORBA and Java</A> <UL> <LI><A HREF="#Heading3">Architectural Similarities</A> <LI><A HREF="#Heading4">CORBA Versus Java RMI</A> <LI><A HREF="#Heading5">Other Considerations</A> </UL> <LI><A HREF="#Heading6">Developing a CORBA Application in Java: An Overview</A> <UL> <LI><A HREF="#Heading7">Using the IDL Compiler</A> <LI><A HREF="#Heading8">Implementing the Server Functionality</A> <LI><A HREF="#Heading9">Implementing the Client Functionality</A> <LI><A HREF="#Heading10">Running the Application</A> </UL> <LI><A HREF="#Heading11">Summary</A> <LI><A HREF="#Heading12">Q&A</A> <LI><A HREF="#Heading13">Workshop</A> <UL> <LI><A HREF="#Heading14">Quiz</A> </UL></UL><P><HR SIZE="4"><CENTER><H1><FONT COLOR="#000077"></FONT></H1></CENTER><P>On Day 12 "Exploring CORBAservices and CORBAfacilities," you first studiedthe various CORBAservices and CORBAfacilities and learned what sort of functionalityis provided by these specifications. You then got a crack at modifying the <TT>Bank</TT>application to use the CORBA Naming Service. By now, the <TT>Bank</TT> applicationhas grown into a complex system.</P><P>Today you'll take a step back with the <TT>Bank</TT> application in terms of functionality,but you'll step out in a completely new direction at the same time. Taking the baseline<TT>Bank</TT> application from Day 9, "Using Callbacks to Add Push Capability,"you'll port the application to Java (not as hard as it might sound). In case you'renot familiar with Java already, this chapter first gives you a picture of what Javais all about, along with insights into the relationship between Java and CORBA. Javaand CORBA are a very good match for each other, and you'll find out why.<H2><A NAME="Heading1"></A><FONT COLOR="#000077">Introducing Java</FONT></H2><P>Java is still a relative newcomer to the computer industry, but this language-cum-platformhas already seen significant increases in maturity and utility, both in the reliabilityof the platform itself (in most cases) and in the availability of development tools.Since its introduction in 1995 (when it metamorphosed from its predecessor, knownas Oak), Java has enjoyed explosive growth, and despite some legal squabbles at thetime of this writing, its growth is continuing.</P><P>For those unfamiliar with the Java language/platform, the best way to learn aboutit is to visit the Web page of the JavaSoft division of Sun Microsystems at <TT>http://www.javasoft.com/</TT>or to peruse one of numerous texts available on the language. What these sourceswill tell you is that Java is characterized by the following:<UL> <LI>An object-oriented language whose syntax resembles a simplified C++; it borrows various concepts from other languages such as Smalltalk and Objective C.<BR> <BR> <LI>A compiler that translates Java source code into a platform-neutral format known as bytecode. The bytecode files, also known as class files, can be distributed to various machines and executed by the Java runtime environment (see the next bullet).<BR> <BR> <LI>A runtime environment that insulates Java applications from the underlying hardware and operating system. To this aspect, Java applications mostly owe their cross-platform portability.<BR> <BR> <LI>A set of class libraries encompassing a variety of capabilities, from file and socket I/O, to multimedia and 3D, to graphical user interface objects.<BR> <BR> <LI>The capability to be run inside a Web browser as an <I>applet</I>. An applet is an application that, in addition to being run inside a Web browser (as a component to a Web page, perhaps), is subject to various security restrictions. Security is a feature built into the Java language that makes this possible and makes Java particularly well suited to Internet-based applications.</UL><P>Java has the potential to make the operating system into a commodity, providinga consistent API across all platforms. Whether this potential will ever be realizedremains to be seen. Java is not perfect, of course, and still must face challengesin the way of performance, portability, and, of course, political opposition. Forthe time being, however, it appears that Java is here to stay, and as it turns out,Java is very well suited for use in CORBA application development.<H2><A NAME="Heading2"></A><FONT COLOR="#000077">Discovering the Symbiosis BetweenCORBA and Java</FONT></H2><P>Why do CORBA and Java make a good match for each other? How is this so? What doesthe CORBA architecture have to offer the Java language/platform, and vice versa?What symbiosis exists between the two? This section discusses some of the factorsthat make development with CORBA and Java an attractive proposition.<H3><A NAME="Heading3"></A><FONT COLOR="#000077">Architectural Similarities</FONT></H3><P>To Java programmers, developing in CORBA feels surprisingly natural and intuitive,almost as if Java and CORBA were designed for each other. (Of course, this isn'tthe case; Java and CORBA have completely separate design goals.) Most responsiblefor the near seamlessness between Java and CORBA are the similarities in architecture:<UL> <LI>CORBA's <TT>module</TT> construct, which groups together interfaces that perform related tasks, is analogous to Java's <TT>package</TT> construct.<BR> <BR> <LI>Java's garbage-collected memory management model eases application development in general, and the development of CORBA applications is no exception. Such niceties as not having to manage strings passed across CORBA interfaces make the CORBA developer's life just a little bit easier.<BR> <BR> <LI>Java's exception model also works well with CORBA. Unlike C++, Java forces methods to handle exceptions or explicitly throw them (and declare this in the method signature). Therefore, CORBA exceptions are easier to use without the worry of exceptions falling through the cracks.<BR> <BR> <LI>Last, but perhaps the most convenient, is the almost direct correspondence between Java's use of interfaces and CORBA's. Mapping CORBA interfaces to C++ classes can become hairy at times; the mapping to Java interfaces is much cleaner.</UL><H3><A NAME="Heading4"></A><FONT COLOR="#000077">CORBA Versus Java RMI</FONT></H3><P>Those familiar with Java are already aware of its Remote Method Interface (RMI)feature. The functionality provided by RMI is very CORBA-like; indeed, both CORBAand RMI share the common goal of enabling the development of distributed, object-orientedapplications. Other than some superficial architectural similarities, CORBA and RMIare quite different, and, thus, each is better suited for different purposes.</P><P>One major difference between CORBA and RMI is that, whereas CORBA is language-independent,RMI is limited to Java only. This is a major consideration when choosing an architecture,even for an application implemented entirely in Java. Although such an applicationcan use RMI, an architect must consider the possible need for a system to interactin the future with other, non-Java applications. However, Sun has indicated thatlater versions of RMI will be interoperable with IIOP (CORBA's Internet Inter-ORBProtocol) so interoperability between the RMI and CORBA architectures might not befar away.</P><P>CORBA also holds an advantage over RMI in terms of robustness. Not only has CORBAhad a number of years head start on RMI, but also CORBA provides a more enterprise-readysolution. Whereas RMI consists of a communications layer and simple naming services,CORBA is part of an architecture that offers many more services valuable for developingenterprise-class applications. (Review Chapter 12 for an overview of the servicesand facilities provided by the Object Management Architecture.) Offering capabilitiessuch as hierarchical naming services, transaction management facilities, event services,and a wealth of vertical market facilities, CORBA holds a clear advantage over RMIin terms of robustness.</P><P>CORBA does not hold all the cards, though; RMI currently has at least one significantadvantage over the CORBA architecture. Because RMI is a Java-only technology, itcan take advantage of features of the Java platform. Most notably, RMI integratesseamlessly with Java's Object Serialization technology, enabling objects to be passedby value (as well as by reference) between remote components. In addition, not onlycan objects be passed by value, but also, because Java employs a platform-independentbytecode, new classes can be sent across the network via RMI for use by client orserver components. The capability to dynamically (at runtime) introduce new classesinto a system opens up a wealth of potential for new breeds of applications.<H3><A NAME="Heading5"></A><FONT COLOR="#000077">Other Considerations</FONT></H3><P>Other factors exist that lend credibility to the marriage of Java and CORBA. Perhapsone of the most compelling features of Java is its portability--the capability torun on various types of computer hardware and operating systems. One characteristicyou're certain to find in a distributed application is the need to run on a varietyof hardware and OS platforms. Also, there is likely greater diversity on the clientend of the application, where numerous types of desktop machines abound--from low-endNetwork Computers, to midrange PCs, to high-end UNIX workstations. Certainly, beingable to deliver client applications on all potential end-user platforms is a boonto developers of enterprise-class applications. This is what Java offers to CORBA:the capability to write a client-side application once and run it on a multitudeof platforms. (As Java continues to make strides in performance and robustness, itwill see more use on the server side of distributed applications, but that's anotherstory.)</P><P>Not only does CORBA benefit from Java by gaining cross-platform client applications,but also Java gains from CORBA. The benefit to Java is that CORBA offers cross-languageinteroperability. Recall that Java's Remote Method Invocation facility works onlybetween Java applications; to communicate with non-Java applications, developersmust implement their own communications layer, possibly involving low-level networksockets. CORBA makes this unnecessary, providing an object-oriented abstraction thatenables Java applications to communicate with applications written in almost anylanguage.<H2><A NAME="Heading6"></A><FONT COLOR="#000077">Developing a CORBA Application inJava: An Overview</FONT></H2><P>Developing a CORBA application in Java is not unlike developing the same applicationin C++. You still use an IDL compiler to translate IDL definitions into server skeletonsand client stubs; the only difference is that the compiler generates Java code ratherthan C++ code. The process, however, remains the same.</P><P>To develop the Java version of the <TT>Bank</TT> application presented in thischapter, you need a few additional tools:<UL> <LI>A CORBA product that supports Java. The example in this chapter (and the next, "Web-Enabling the <TT>Bank</TT> Example with Java") uses Visigenic Software's VisiBroker for Java, but you can also use other products, such as Sun's Java IDL or IONA Technologies' OrbixWeb, with little or no modification to the provided code.<BR> <BR> <LI>A Java development tool. This can be an Integrated Development Environment (IDE) such as Microsoft's Visual J++ or Symantec's Café or Visual Café, or it can be the Java Development Kit (JDK) from Sun. Depending on the CORBA product you choose, you need a tool that supports JDK 1.1 or higher, but you might be able to get by with JDK 1.0.2.<BR> <BR> <LI>A machine (or machines) capable of running these products. Chances are, though, that if you have the products, you probably have the machine, too.</UL><P>When you have all these items, you're ready to begin developing in Java.<H3><A NAME="Heading7"></A><FONT COLOR="#000077">Using the IDL Compiler</FONT></H3><P>As already mentioned, the use of the IDL compiler is exactly the same when you'rewriting a CORBA application in C++ or in Java. The command to run the IDL compiler,though, might be different. For the compiler included with VisiBroker for Java, usethe following command:</P><PRE><FONT COLOR="#0066FF"><TT>idl2java -package idlGlobal <I>filename</I></TT></FONT></PRE><P>where <I><TT>filename</TT></I> is the name of the IDL file to be compiled. Also,here the <TT>-package idlGlobal</TT> switch tells the IDL compiler to place the generatedcode into a directory named <TT>idlGlobal</TT>, and the generated Java classes willbe placed in the <TT>idlGlobal</TT> package. The VisiBroker IDL compiler will generatecode for both servers and clients.<BLOCKQUOTE> <P><HR><B>Warning: </B>Before attempting to use the Java IDL compiler, make sure you have installed the product according to the vendor's documentation. In particular, you need to set the <TT>PATH</TT> and <TT>CLASSPATH</TT> environment variables. For example, VisiBroker requires <I><TT>VisiBroker-directory</TT></I><TT>/vbj30.jar</TT> to be added to the <TT>CLASSPATH</TT>. If you encounter errors when trying to use the IDL compiler, consult your vendor's documentation. <HR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -