⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 corba.html

📁 jdbc书
💻 HTML
📖 第 1 页 / 共 4 页
字号:
This example adds the Java <CODE>Registration</CODE> interface
to an IDL <CODE>registration module</CODE>.  

<PRE>
  module registration {
     interface Registration {
     };
  }
</PRE>

This example adds the Java <CODE>Registration</CODE> interface
to an IDL <CODE>registration module</CODE>, and indicates the
<CODE>Registration</CODE> interface 
inherits from the <CODE>User</CODE> interface.

<PRE>
  module registration {
     interface Registration: User {
     };
  }
</PRE>

<P>
<STRONG>Java methods</STRONG>:
Java methods map to IDL operations. The IDL operation looks similar to a 
Java method except there is no concept of access control. You also have to 
help the IDL compiler by specifying which parameters are <CODE>in</CODE>, 
<CODE>inout</CODE> or <CODE>out</CODE>, defined as follows: 

<UL>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">in - parameter is passed into the method but not changed.</FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">inout - parameter is passed into the method and might be returned changed.</FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">out - parameter might be returned changed.</FONT>
</UL>

This IDL mapping includes the <CODE>Registration</CODE> and 
<CODE>RegistrationHome</CODE> interface methods to
IDL operations using one IDL module type.


<PRE>
module registration {

  interface Registration {
    boolean verifyPassword(in string password);
    string getEmailAddress();
    string getUser();
    long adjustAccount(in double amount);
    double getBalance();
  };

  interface RegistrationHome {
    Registration findByPrimaryKey(
		   in RegistrationPK theuser) 
		   raises (FinderException);
  }
}
</PRE>

<P>
<STRONG>Java Arrays</STRONG>:
Arrays in the Java language are mapped to the IDL <CODE>array</CODE>
or IDL <CODE>sequence</CODE> type using a type definition. 

<P>
This example maps the Java array <CODE>double balances[10]</CODE>
to an IDL <CODE>array</CODE> type of the same size.

<PRE>
typedef double balances[10];
</PRE>

These examples map the Java array <CODE>double balances[10]</CODE>
to an IDL <CODE>sequence</CODE> type.
The first <CODE>typedef sequence</CODE> is an example of
an unbounded sequence, and the second <CODE>typedef sequence</CODE>
has the same size as the array.

<PRE>
typedef sequence&lt;double&gt; balances;
typedef sequence&lt;double,10&gt; balances;
</PRE>

<P>
<STRONG>Java Exception</STRONG>:
Java exceptions are mapped to IDL exceptions. Operations use IDL
<CODE>exceptions</CODE> by specifying them as a <CODE>raises</CODE> type. 

<P>
This example maps the <CODE>CreateException</CODE> from the
auction application to the IDL <CODE>exception</CODE> type,
and adds the IDL <CODE>raises</CODE> type to the operation
as follows. IDL exceptions follow C++ syntax, so instead of throwing an 
exception (as you would in the Java language), the operation raises
an exception.


<PRE>
exception CreateException {
};

interface RegistrationHome {
  RegistrationPK create(
	           in string theuser, 
	           in string password, 
	           in string emailaddress, 
	           in string creditcard) 
	           raises (CreateException);
}
</PRE>

<A NAME="other"></A>
<H4>Other IDL types</H4>

These other basic IDL types do not have an exact equivalent in the 
Java language. Many of these should be familiar if you have
used C or C++. The Java language provides a mapping for these types
so a program written in the Java language can  receive data from programs 
written in C or C++.

<UL>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">IDL <CODE>attribute</CODE> </FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">IDL <CODE>enum</CODE> </FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">IDL <CODE>struct</CODE></FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">IDL <CODE>union</CODE> </FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">IDL <CODE>Any</CODE></FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">IDL <CODE>Principal</CODE></FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">IDL <CODE>Object</CODE></FONT>
</UL>

<P>
<STRONG>IDL attribute</STRONG>:
The IDL <CODE>attribute</CODE> type is similar to the <CODE>get</CODE>
and <CODE>set</CODE> methods used to access fields in the JavaBeans<FONT SIZE="-2"><SUP>TM</SUP></FONT>
software. 

<P>
In the case of a value declared as an IDL attribute, the IDL compiler 
generates two methods of the same name as the IDL attribute. One method
returns the field and the other method sets it.
For example, this <CODE>attribute</CODE> type:

<PRE>
interface RegistrationPK {
  attribute string theuser;
};
</PRE>

defines these methods

<PRE>
//return user
  String theuser(); 
//set user
  void theuser(String arg); 
</PRE>

<P>
<STRONG>IDL enum</STRONG>:
The Java language has an <CODE>Enumeration</CODE> class for 
representing a collection of data. The IDL <CODE>enum</CODE> type 
is different because it is declared as a data type and not a 
data collection.

<P>
The IDL <CODE>enum</CODE> type is a list of values that can be referenced 
by name instead of by their position in the list. In the example, you
can see that referring to an IDL
<CODE>enum</CODE> status code by name is more readable than 
referring to it by its number. This line maps <CODE>static final int</CODE> 
values in the <CODE>final</CODE> class <CODE>LoginError</CODE>.
You can reference the values as you would reference a static field:
<CODE>LoginError.INVALID_USER</CODE>.
 
<PRE>
enum LoginError {
  INVALID_USER, WRONG_PASSWORD, TIMEOUT};
</PRE>

Here is a version of the <CODE>enum</CODE> type that includes
a preceding underscore that can be used in <CODE>switch</CODE> 
statements:

<PRE>
switch (problem) {
  case LoginError._INVALID_USER:
    System.out.println("please login again");
    break;
}
</PRE>

<P>
<STRONG>IDL struct</STRONG>:
An IDL <CODE>struct</CODE> type can be compared to a Java class that
has only fields, which is how it is mapped by the IDL compiler.


<P>
This example declares an IDL <CODE>struct</CODE>. Note that
IDL types can reference other IDL types. In this example
<CODE>LoginError</CODE> is from the <CODE>enum</CODE> type declared above.

<PRE>
struct ErrorHandler {
  LoginError errortype;
  short retries;
};
</PRE>

<P>
<STRONG>IDL union</STRONG>:
An IDL <CODE>union</CODE> can represent one type from a list of types defined 
for that union. The IDL <CODE>union</CODE> maps to a Java class of the same name 
with a <CODE>discriminator</CODE> method used for determining the type of 
this union. 

<P>
This example maps the <CODE>GlobalErrors</CODE> union to a Java
class by the name of <CODE>GlobalErrors</CODE>.  A 
default case <CODE>case: DEFAULT</CODE> could be added to
handle any elements that might be in the <CODE>LoginErrors enum</CODE> type,
and not specified with a <CODE>case</CODE> statement here.

<PRE>
  union GlobalErrors switch (LoginErrors) {
     case: INVALID_USER: string message;
     case: WRONG_PASSWORD: long attempts;
     case: TIMEOUT: long timeout;
  };
</PRE>

In a program written in the Java language, the <CODE>GlobalErrors</CODE> 
union class is created as follows:

<PRE>
  GlobalErrors ge = new GlobalErrors();
  ge.message("please login again");
</PRE>

The <CODE>INVALID_USER</CODE> value is retrieved like this:

<PRE>
  switch (ge.discriminator().value()) {
    case: LoginError._INVALID_USER:
      System.out.println(ge.message);
      break;
  }
</PRE>

<P>
<STRONG>Any type</STRONG>:
If you do not know what type is going to be passed or returned
to an operation, you can use the <CODE>Any</CODE> type mapping,
which can represent any IDL type. The following 
operation returns and passes an unknown type:

<PRE>
  interface RegistrationHome {
    Any customSearch(Any searchField, out count);
  };
</PRE>

To first create a type of <CODE>Any</CODE>, request the type from the 
Object Request Broker (ORB). To set a value in a type of 
<CODE>Any</CODE>, use an <CODE>insert_&lt;type&gt;</CODE> method. To retrieve a 
value, use the <CODE>extract_&lt;type&gt;</CODE> method.

<P>
This example requests an object of type <CODE>Any</CODE>, 
and uses the <CODE>insert_type</CODE> method to set a value.

<PRE>
  Any sfield = orb.create_any();
  sfield.insert_long(34);
</PRE>

The <CODE>Any</CODE> type has an assigned <CODE>TypeCode</CODE> value that 
you can query using <CODE>type().kind().value()</CODE> on the object. The
following example shows a test for the <CODE>TypeCode double</CODE>.
This example includes a reference to the IDL
<CODE>TypeCode</CODE> find out which type the
<CODE>Any</CODE> object contains. The <CODE>TypeCode</CODE> is
used for all objects. You can analyze the type of a CORBA object using
the <CODE>_type()</CODE> or <CODE>type()</CODE> methods as shown here.

<PRE>
public Any customSearch(Any searchField, IntHolder count){
  if(searchField.type().kind().value() == TCKind._tk_double){
// return number of balances greater than supplied amount 
    double findBalance=searchField.extract_double();
</PRE>

<P>

<STRONG>Principal</STRONG>: The <CODE>Principal</CODE>
type identifies the owner of a CORBA object, for
example, a user name. The value can be interrogated from the 
<CODE>request_principal</CODE> field of the CORBA <CODE>RequestHeader</CODE>
class to make the identification.
More comprehensive security and authorization is available in the 
CORBA security service.

<STRONG>Object</STRONG>: The <CODE>Object</CODE> type is a CORBA object. 
If you need to send Java objects, you have to either translate them into an
IDL type or use a mechanism to serialize them when they are transferred.

<A NAME="auction"></A>
<H3>CORBA in the Auction Application</H3>

The container-managed <A HREF="./Code/registration/RegistrationBean.java">RegistrationBean</A>
from the auction application is completely replaced with a standalone CORBA
<A HREF="./Code/corba/RegistrationServer.java">RegistrationServer</A>
that implements the registration service.
The CORBA <CODE>RegistrationServer</CODE> is built by creating and compiling
an IDL mappings file so client programs can communicate with the registration server.

<P>
The <CODE>SellerBean.java</CODE> and <CODE>AuctionServlet.java</CODE>
files are updated to look up the CORBA registration server.

<A NAME="server"></A>
<H4>CORBA RegistrationServer Implementation</H4>

This section describes the
<A HREF="./Code/corba/Registration.idl">Registration.idl</A>
file, which maps the <CODE>RegistrationHome</CODE> and <CODE>Registration</CODE>
remote interfaces from the Enterprise JavaBean auction application
to their IDL equivalents and shows how to compile the <CODE>Registration.idl</CODE>
file into CORBA registration server classes. 

<P>
The CORBA registration server implements the
<CODE>create</CODE> and <CODE>findByPrimaryKey</CODE> methods from the
original <CODE>RegistrationBean.java</CODE> file, and is enhanced with the following two
new methods to help illustrate CORBA callbacks and how to use the
<CODE>Any</CODE> type.

<UL>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><CODE>findLowCreditAccounts(in ReturnResults rr)</CODE>, which uses a 
<A HREF="#call">callback</A> to return a list of accounts with a low balance.</FONT>

<P>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><CODE>any customSearch(in any searchfield, out long count)</CODE>,
which returns a different search result depending on the search 
<A HREF="#any">field type</A> submitted.</FONT>
</UL>

<A NAME="idl"></A>
<H4>IDL Mappings File</H4>

Here is the <A HREF="./Code/corba/Registration.idl">Registration.idl</A>
file that maps the data types and methods used in the 
<CODE>RegistrationHome</CODE> and <CODE>Registration</CODE> programs
to their IDL equivalents.

<PRE>
module registration {

interface Registration {
   boolean verifyPassword(in string password);
   string getEmailAddress();
   string getUser();
   long adjustAccount(in double amount);
   double getBalance();
};

interface RegistrationPK {
   attribute string theuser;
};

enum LoginError {INVALIDUSER, WRONGPASSWORD, TIMEOUT};

exception CreateException {
};

exception FinderException {
};

typedef sequence&lt;Registration&gt; IDLArrayList;

interface ReturnResults  {
  void updateResults(in IDLArrayList results) 
	raises (FinderException);
};

interface RegistrationHome {
  RegistrationPK create(in string theuser, 
			in string password, 
                   	in string emailaddress, 
			in string creditcard) 
                           raises (CreateException);

  Registration findByPrimaryKey(
                 in RegistrationPK theuser) 
		 raises (FinderException);
  void findLowCreditAccounts(in ReturnResults rr) 
		 raises (FinderException);
  any customSearch(in any searchfield, out long count);
};
};
</PRE>

<A NAME="comp"></A>
<H4>Compiling the IDL Mappings File</H4>

The IDL file has to be converted into Java classes that can be used in
the CORBA distributed network. The Java 2 platform compiles
<CODE>.idl</CODE> files using the program <CODE>idltojava</CODE>. This
program will be eventually replaced with the <CODE>idlj</CODE> command.

<P>
The <CODE>-fno-cpp</CODE> arguments indicate there is no C++ compiler
installed.

<PRE>
  idltojava -fno-cpp Registration.idl
</PRE>

Other Java IDL compilers should also work, for example, 
<CODE>jidl</CODE> from ORBacus can generate classes that can be used by the
Java 2 ORB.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -