📄 tij0196.html
字号:
<html><body>
<table width="100%"><tr>
<td>
<a href="http://www.bruceeckel.com/javabook.html">Bruce Eckel's Thinking in Java</a>
</td>
<td align="right">
<a href="tij_c.html">Contents</a> | <a href="tij0195.html">Prev</a> | <a href="tij0197.html">Next</a>
</td>
</tr></table>
<hr>
<H2 ALIGN=LEFT>
CORBA</H2>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">In
large, distributed applications, your needs might not be satisfied by the
preceding approaches. For example, you might want to interface with legacy
datastores, or you might need services from a server object regardless of its
physical location. These situations require some form of Remote Procedure Call
(RPC), and possibly language independence. This is where CORBA can help.
</FONT><P></DIV><DIV ALIGN=LEFT><A NAME="Index3120"></A><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">CORBA
is not a language feature; it’s an integration technology. It’s a
specification that vendors can follow to implement CORBA-compliant integration
products. CORBA is part of the OMG’s effort to define a standard
framework for distributed, language-independent object interoperability.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">CORBA
supplies the ability to make remote procedure calls into Java objects and
non-Java objects, and to interface with legacy systems in a
location-transparent way. Java adds networking support and a nice
object-oriented language for building graphical and non-graphical applications.
The Java and <A NAME="Index3121"></A>OMG
object model map nicely to each other; for example, both Java and CORBA
implement the interface concept and a reference object model.
</FONT><a name="_Toc408018842"></a><P></DIV>
<A NAME="Heading619"></A><H3 ALIGN=LEFT>
CORBA
Fundamentals
</H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
object interoperability specification developed by the OMG is commonly referred
to as the Object Management Architecture (OMA). The OMA defines two components:
the Core Object Model and the OMA Reference Architecture. The Core Object Model
states the basic concepts of object, interface, operation, and so on. (CORBA is
a refinement of the Core Object Model.) The OMA Reference Architecture defines
an underlying infrastructure of services and mechanisms that allow objects to
interoperate. The OMA Reference Architecture includes the Object Request Broker
(ORB), Object Services (also known as CORBAservices), and common facilities.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
ORB is the communication bus by which objects can request services from other
objects, regardless of their physical location. This means that what looks like
a method call in the client code is actually a complex operation. First, a
connection with the server object must exist, and to create a connection the
ORB must know where the server implementation code resides. Once the connection
is established, the method arguments must be marshaled, i.e. converted in a
binary stream to be sent across a network. Other information that must be sent
are the server machine name, the server process, and the identity of the server
object inside that process. Finally, this information is sent through a
low-level wire protocol, the information is decoded on the server side, and the
call is executed. The ORB hides all of this complexity from the programmer and
makes the operation almost as simple as calling a method on local object.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">There
is no specification for how an ORB Core should be implemented, but to provide a
basic compatibility among different vendors’ ORBs, the OMG defines a set
of services that are accessible through standard interfaces.
</FONT><P></DIV>
<A NAME="Heading620"></A><H4 ALIGN=LEFT>
CORBA
Interface Definition Language (IDL)
</H4>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">CORBA
is designed for language transparency: a client object can call methods on a
server object of different class, regardless of the language they are
implemented with. Of course, the client object must know the names and
signatures of methods that the server object exposes. This is where IDL comes
in. The CORBA IDL is a language-neutral way to specify data types, attributes,
operations, interfaces, and more. The IDL syntax is similar to the C++ or Java
syntax. The following table shows the correspondence between some of the
concepts common to three languages that can be specified through CORBA IDL:
</FONT><P></DIV>
<DIV ALIGN=LEFT><TABLE BORDER>
<COLGROUP>
<COL width="83">
<COL width="92">
<COL width="100">
</COLGROUP>
<TR VALIGN="TOP">
<TD WIDTH=83 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>CORBA
IDL
</B></FONT><P></DIV>
</TD>
<TD WIDTH=92 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Java</B></FONT><P></DIV>
</TD>
<TD WIDTH=100 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>C++</B></FONT><P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD WIDTH=83 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Module</FONT><P></DIV>
</TD>
<TD WIDTH=92 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Package</FONT><P></DIV>
</TD>
<TD WIDTH=100 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Namespace</FONT><P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD WIDTH=83 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Interface</FONT><P></DIV>
</TD>
<TD WIDTH=92 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Interface</FONT><P></DIV>
</TD>
<TD WIDTH=100 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Pure
abstract class
</FONT><P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD WIDTH=83 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Method</FONT><P></DIV>
</TD>
<TD WIDTH=92 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Method</FONT><P></DIV>
</TD>
<TD WIDTH=100 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Member
function
</FONT><P></DIV>
</TD>
</TR>
</TABLE></DIV>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
inheritance concept is supported as well, using the colon operator as in C++.
The programmer writes an IDL description of the attributes, methods, and
interfaces that will be implemented and used by the server and clients. The IDL
is then compiled by a vendor-provided IDL/Java compiler, which reads the IDL
source and generates Java code.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
IDL compiler is an extremely useful tool: it doesn’t just generate a Java
source equivalent of the IDL, it also generates the code that will be used to
marshal method arguments and to make remote calls. This code, called the stub
and skeleton code, is organized in multiple Java source files and is usually
part of the same Java package.
</FONT><P></DIV>
<A NAME="Heading621"></A><H4 ALIGN=LEFT>
The
naming service
</H4>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
naming service is one of the fundamental CORBA services. A CORBA object is
accessed through a reference, a piece of information that’s not
meaningful for the human reader. But references can be assigned
programmer-defined, string names. This operation is known as
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>stringifying
the reference
</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
and one of the OMA components, the Naming Service, is devoted to performing
string-to-object and object-to-string conversion and mapping. Since the Naming
Service acts as a telephone directory that both servers and clients can consult
and manipulate, it runs as a separate process. Creating an object-to-string
mapping is called
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>binding
an object
</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
and removing the mapping is called
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>unbinding</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
Getting an object reference passing a string is called
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>resolving
the name
</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">For
example, on startup, a server application could create a server object, bind
the object into the name service, and then wait for clients to make requests. A
client first obtains a server object reference, resolving the string name, and
then can make calls into the server using the reference.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Again,
the Naming Service specification is part of CORBA, but the application that
implements it is provided by the ORB vendor. The way you get access to the
Naming Service functionality can vary from vendor to vendor.
</FONT><a name="_Toc408018843"></a><P></DIV>
<A NAME="Heading622"></A><H3 ALIGN=LEFT>
An
example
</H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
code shown here will not be elaborate because different ORBs have different
ways to access CORBA services, so examples are vendor specific. (The example
below uses JavaIDL, a free product from Sun that comes with a light-weight ORB,
a naming service, and a IDL-to-Java compiler.) In addition, since Java is young
and still evolving, not all CORBA features are present in the various
Java/CORBA products.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">We
want to implement a server, running on some machine, that can be queried for
the exact time. We also want to implement a client that asks for the exact
time. In this case we’ll be implementing both programs in Java, but we
could also use two different languages (which often happens in real situations).
</FONT><P></DIV>
<A NAME="Heading623"></A><H4 ALIGN=LEFT>
Writing
the IDL source
</H4>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
first step is to write an IDL description of the services provided. This is
usually done by the server programmer, who is then free to implement the server
in any language in which a CORBA IDL compiler exists. The IDL file is
distributed to the client side programmer and becomes the bridge between
languages.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
example below shows the IDL description of our exact time server:
</FONT><P></DIV>
<font color="#990000"><PRE>module RemoteTime {
<font color="#0000ff">interface</font> ExactTime {
string getTime();
};
};</PRE></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">This
is a declaration of the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>ExactTime</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
interface inside the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>RemoteTime</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
namespace. The interface is made up of one single method the gives back the
current time in
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>string</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
format.
</FONT><P></DIV>
<A NAME="Heading624"></A><H4 ALIGN=LEFT>
Creating
stubs and skeletons
</H4>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
second step is to compile the IDL to create the Java stub and skeleton code
that we’ll use for implementing the client and the server. The tool that
comes with the JavaIDL product is <A NAME="Index3122"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>idltojava</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">:
</FONT><P></DIV><DIV ALIGN=LEFT><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">idltojava
–fserver –fclient RemoteTime.idl
</FONT></TT><P></DIV><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
two flags tell
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>idltojava</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
to generate code for both the stub and the skeleton.
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Idltojava</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
generates a Java package named after the IDL module,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>RemoteTime</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
and the generated Java files are put in the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>RemoteTime</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
subdirectory.
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>_ExactTimeImplBase.java
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">is
the skeleton that we’ll use to implement the server object, and
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>_ExactTimeStub.java</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
will be used for the client. There are Java representations of the IDL
interface in
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>ExactTime.java</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and a couple of other support files used, for example, to facilitate access to
the naming service operations.
</FONT><P></DIV>
<A NAME="Heading625"></A><H4 ALIGN=LEFT>
Implementing
the server and the client
</H4>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Below
you can see the code for the server side. The server object implementation is
in the
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -