Java Reference
In-Depth Information
input/output parameters with the keyword
inout
. Because Java always passes
parameters by value, the IDL-to-Java compiler must generate special “Holder”
classes to provide support for
out
and
inout
parameters.
Forexample, the
demo()
operation uses an
inout
parameter of type
CustomData
. The IDL compiler generates a special support class named
CustomDataHolder.
When the implementation class is built, the parameter
passed in the
CustomData
position must actually be a
CustomDataHolder
object. Holder classes are created to “hold” an object of the root type. Thus
CustomDataHolder
holds a
CustomData
object. In that way, a Java imple-
mentation can set the value of the
CustomData
object within the Holder class
for return to the caller. Both
out
and
inout
parameters are handled with Holder
classes. In fact, in a Java implementation, there is little difference between
out
and
inout
parameter types. The only difference is that
inout
parameters are
expected to contain data upon entry to the method implementation while
out
parameters should not. An attempt to retrieve the inner value of an
out
para-
meter results in a runtime failure, as does an attempt to pass an
inout
parameter
whose inner value has not been set.
Our IDL file also defines an exception called
Cor19UserException
.
Exceptions are not “thrown” in IDL terminology, but rather “raised,” as shown in
the example. However, the
raises
keyword maps into the Java
throws
keyword
in the output from the IDL compiler. There are a variety of built-in exception
types, but if a custom type is desired, it must be defined in the IDL file.
The
Cor19Example
interface declares that
method1()
returns
void
,while
add()
returns
long
. There is no
int
keyword in IDL, but when IDL types are
mapped to Java primitive types, an IDL
long
parameter becomes a Java
int
.
One of the first things a novice CORBA programmer must learn is to mentally
map everywhere that a Java
int
would be used to a
long
in the IDL file. When
the Java implementation is built, the parameters and return values identified as
long
in the IDL file appear as
int
types in the Java implementation.
The parameter for
method1()
is an input (
in
) parameter of type
string
.
Note the lowercase
“
s
„
.Lowercase
string
is an IDL type that maps to a Java
String
object. If one accidentally uses uppercase
String
in an IDL file, an
error is seen when the file is processed by the IDL compiler. This mistake is
common, but the solution is simple - just replace the erroneous
String
with
the correct
string
.
It should now be clear that the IDL file shown above defines a CORBA server
that provides the same services as the simple RMI example from Chapter 18 plus
afew more. Other features of IDL not yet described include the use of two kinds of
attributes
and two kinds of constants (CORBA keyword
const
). Attributes map
into instance variables in Java with accessor and mutator (also known as getter
and setter) methods (which must be implemented in the implementation class).
Attributes with the keyword
readonly
have only an accessor method. Read-
only attributes are values that may be changed within the server implementation,
Search WWH ::
Custom Search