下面列出了怎么用org.omg.CORBA.Any的API类实例代码及写法,或者点击链接到github查看源代码。
public void from_any (org.omg.CORBA.Any value)
throws org.omg.DynamicAny.DynAnyPackage.TypeMismatch,
org.omg.DynamicAny.DynAnyPackage.InvalidValue
{
if (status == STATUS_DESTROYED) {
throw wrapper.dynAnyDestroyed() ;
}
if ((any != null) && (! any.type().equal(value.type()))) {
throw new TypeMismatch();
}
// If the passed Any does not contain a legal value
// (such as a null string), the operation raises InvalidValue.
Any tempAny = null;
try {
tempAny = DynAnyUtil.copy(value, orb);
} catch (Exception e) {
throw new InvalidValue();
}
if ( ! DynAnyUtil.isInitialized(tempAny)) {
throw new InvalidValue();
}
any = tempAny;
}
/** This is the implementation of standard API defined in org.omg.CORBA.ORB
* class. This method finds the Policy Factory for the given Policy Type
* and instantiates the Policy object from the Factory. It will throw
* PolicyError exception, If the PolicyFactory for the given type is
* not registered.
* _REVISIT_, Once Policy Framework work is completed, Reorganize
* this method to com.sun.corba.se.spi.orb.ORB.
*/
public org.omg.CORBA.Policy create_policy(int type, org.omg.CORBA.Any val)
throws org.omg.CORBA.PolicyError
{
if( val == null ) {
nullParam( );
}
if( policyFactoryTable == null ) {
throw new org.omg.CORBA.PolicyError(
"There is no PolicyFactory Registered for type " + type,
BAD_POLICY.value );
}
PolicyFactory factory = (PolicyFactory)policyFactoryTable.get(
new Integer(type) );
if( factory == null ) {
throw new org.omg.CORBA.PolicyError(
" Could Not Find PolicyFactory for the Type " + type,
BAD_POLICY.value);
}
org.omg.CORBA.Policy policy = factory.create_policy( type, val );
return policy;
}
/** This is the implementation of standard API defined in org.omg.CORBA.ORB
* class. This method finds the Policy Factory for the given Policy Type
* and instantiates the Policy object from the Factory. It will throw
* PolicyError exception, If the PolicyFactory for the given type is
* not registered.
* _REVISIT_, Once Policy Framework work is completed, Reorganize
* this method to com.sun.corba.se.spi.orb.ORB.
*/
public org.omg.CORBA.Policy create_policy(int type, org.omg.CORBA.Any val)
throws org.omg.CORBA.PolicyError
{
if( val == null ) {
nullParam( );
}
if( policyFactoryTable == null ) {
throw new org.omg.CORBA.PolicyError(
"There is no PolicyFactory Registered for type " + type,
BAD_POLICY.value );
}
PolicyFactory factory = (PolicyFactory)policyFactoryTable.get(
new Integer(type) );
if( factory == null ) {
throw new org.omg.CORBA.PolicyError(
" Could Not Find PolicyFactory for the Type " + type,
BAD_POLICY.value);
}
org.omg.CORBA.Policy policy = factory.create_policy( type, val );
return policy;
}
public void set_to_no_active_member ()
throws org.omg.DynamicAny.DynAnyPackage.TypeMismatch
{
if (status == STATUS_DESTROYED) {
throw wrapper.dynAnyDestroyed() ;
}
// _REVISIT_ How does one check for "entire range of discriminator values"?
if (defaultIndex() != -1) {
throw new TypeMismatch();
}
checkInitComponents();
Any discriminatorAny = getAny(discriminator);
// erase the discriminators value so that it does not correspond
// to any of the unions case labels
discriminatorAny.type(discriminatorAny.type());
index = 0;
currentMemberIndex = NO_INDEX;
// Necessary to guarantee OBJECT_NOT_EXIST in member()
currentMember.destroy();
currentMember = null;
components[0] = discriminator;
representations = REPRESENTATION_COMPONENTS;
}
public synchronized org.omg.CORBA.Policy create_policy( int type,
org.omg.CORBA.Any val ) throws org.omg.CORBA.PolicyError
{
checkShutdownState() ;
return pihandler.create_policy( type, val ) ;
}
protected boolean initializeComponentsFromAny() {
// This typeCode is of kind tk_sequence.
TypeCode typeCode = any.type();
int length;
TypeCode contentType = getContentType();
InputStream input;
try {
input = any.create_input_stream();
} catch (BAD_OPERATION e) {
return false;
}
length = input.read_long();
components = new DynAny[length];
anys = new Any[length];
for (int i=0; i<length; i++) {
// _REVISIT_ Could use read_xxx_array() methods on InputStream for efficiency
// but only for primitive types
anys[i] = DynAnyUtil.extractAnyFromStream(contentType, input, orb);
try {
// Creates the appropriate subtype without copying the Any
components[i] = DynAnyUtil.createMostDerivedDynAny(anys[i], orb, false);
} catch (InconsistentTypeCode itc) { // impossible
}
}
return true;
}
/**
* Convert the given any into a CDR encapsulated octet sequence. Only
* the data is stored. The type code is not.
*/
public byte[] encode_value( Any data )
throws InvalidTypeForEncoding
{
if( data == null )
throw wrapper.nullParam() ;
return encodeImpl( data, false );
}
public void write_any(Any any)
{
if ( any == null )
throw wrapper.nullParam(CompletionStatus.COMPLETED_MAYBE);
write_TypeCode(any.type());
any.write_value(parent);
}
public void unmarshalReply(InputStream is)
{
// First unmarshal the return value if it is not void
if ( _result != null ) {
Any returnAny = _result.value();
TypeCode returnType = returnAny.type();
if ( returnType.kind().value() != TCKind._tk_void )
returnAny.read_value(is, returnType);
}
// Now unmarshal the out/inout args
try {
for ( int i=0; i<_arguments.count() ; i++) {
NamedValue nv = _arguments.item(i);
switch( nv.flags() ) {
case ARG_IN.value:
break;
case ARG_OUT.value:
case ARG_INOUT.value:
Any any = nv.value();
any.read_value(is, any.type());
break;
}
}
}
catch ( org.omg.CORBA.Bounds ex ) {
// Cannot happen since we only iterate till _arguments.count()
}
}
public Any get_boxed_value()
throws org.omg.DynamicAny.DynAnyPackage.InvalidValue
{
if (isNull) {
throw new InvalidValue();
}
checkInitAny();
return any;
}
public void insert_any(org.omg.CORBA.Any value)
throws org.omg.DynamicAny.DynAnyPackage.TypeMismatch,
org.omg.DynamicAny.DynAnyPackage.InvalidValue
{
if (status == STATUS_DESTROYED) {
throw wrapper.dynAnyDestroyed() ;
}
if (any.type().kind().value() != TCKind._tk_any)
throw new TypeMismatch();
any.insert_any(value);
}
static AnyImpl convertToNative(ORB orb, Any any) {
if (any instanceof AnyImpl) {
return (AnyImpl)any;
} else {
AnyImpl anyImpl = new AnyImpl(orb, any);
anyImpl.typeCode = TypeCodeImpl.convertToNative(orb, anyImpl.typeCode);
return anyImpl;
}
}
/**
* See the description of the <a href="#anyOps">general Any operations.</a>
*/
public void insert_any(Any a)
{
//debug.log ("insert_any");
typeCode = orb.get_primitive_tc(TCKind._tk_any);
object = a;
stream = null;
isInitialized = true;
}
private int currentUnionMemberIndex(Any discriminatorValue) {
int memberCount = memberCount();
Any memberLabel;
for (int i=0; i<memberCount; i++) {
memberLabel = memberLabel(i);
if (memberLabel.equal(discriminatorValue)) {
return i;
}
}
if (defaultIndex() != -1) {
return defaultIndex();
}
return NO_INDEX;
}
public void setServerPIExceptionInfo( Any exception )
{
if( !hasServerInterceptors ) return;
ServerRequestInfoImpl info = peekServerRequestInfoImplStack();
info.setDSIException( exception );
}
static AnyImpl convertToNative(ORB orb, Any any) {
if (any instanceof AnyImpl) {
return (AnyImpl)any;
} else {
AnyImpl anyImpl = new AnyImpl(orb, any);
anyImpl.typeCode = TypeCodeImpl.convertToNative(orb, anyImpl.typeCode);
return anyImpl;
}
}
public void set_exception(Any exc)
{
// except can be called by the DIR at any time (CORBA 2.2 section 6.3).
if ( exc == null )
throw _wrapper.setExceptionCalledNullArgs() ;
// Ensure that the Any contains a SystemException or a
// UserException. If the UserException is not a declared exception,
// the client will get an UNKNOWN exception.
TCKind kind = exc.type().kind();
if ( kind != TCKind.tk_except )
throw _wrapper.setExceptionCalledBadType() ;
_exception = exc;
// Inform Portable interceptors of the exception that was set
// so sending_exception can return the right value.
_orb.getPIHandler().setServerPIExceptionInfo( _exception );
// The user can only call arguments once and not at all after
// set_exception. (internal flags ensure this). However, the user
// can call set_exception multiple times. Therefore, we only
// invoke receive_request the first time set_exception is
// called (if they haven't already called arguments).
if( !_exceptionSet && !_paramsCalled ) {
// We need to invoke intermediate points here.
_orb.getPIHandler().invokeServerPIIntermediatePoint();
}
_exceptionSet = true;
// actual marshaling of the reply msg header and exception takes place
// after the DSI returns control to the ORB.
}
/**
* This method sets the slot data at the given slot id (index).
*/
public void set_slot( int id, Any data ) throws InvalidSlot
{
// First check whether the slot is allocated
// If not, raise the invalid slot exception
if( id >= theSlotData.length ) {
throw new InvalidSlot();
}
dirtyFlag = true;
theSlotData[id] = data;
}
static DynAny createMostDerivedDynAny(Any any, ORB orb, boolean copyValue)
throws org.omg.DynamicAny.DynAnyFactoryPackage.InconsistentTypeCode
{
if (any == null || ! DynAnyUtil.isConsistentType(any.type()))
throw new org.omg.DynamicAny.DynAnyFactoryPackage.InconsistentTypeCode();
switch (any.type().kind().value()) {
case TCKind._tk_sequence:
return new DynSequenceImpl(orb, any, copyValue);
case TCKind._tk_struct:
return new DynStructImpl(orb, any, copyValue);
case TCKind._tk_array:
return new DynArrayImpl(orb, any, copyValue);
case TCKind._tk_union:
return new DynUnionImpl(orb, any, copyValue);
case TCKind._tk_enum:
return new DynEnumImpl(orb, any, copyValue);
case TCKind._tk_fixed:
return new DynFixedImpl(orb, any, copyValue);
case TCKind._tk_value:
return new DynValueImpl(orb, any, copyValue);
case TCKind._tk_value_box:
return new DynValueBoxImpl(orb, any, copyValue);
default:
return new DynAnyBasicImpl(orb, any, copyValue);
}
}
/**
* See the description of the <a href="#anyOps">general Any operations.</a>
*/
public Any extract_any()
{
//debug.log ("extract_any");
checkExtractBadOperation( TCKind._tk_any ) ;
return (Any)object;
}
private void addComponent(int i, String memberName, Any memberAny, DynAny memberDynAny) {
components[i] = memberDynAny;
names[i] = (memberName != null ? memberName : "");
nameValuePairs[i].id = memberName;
nameValuePairs[i].value = memberAny;
nameDynAnyPairs[i].id = memberName;
nameDynAnyPairs[i].value = memberDynAny;
if (memberDynAny instanceof DynAnyImpl)
((DynAnyImpl)memberDynAny).setStatus(STATUS_UNDESTROYABLE);
}
public void insert_any(org.omg.CORBA.Any value)
throws org.omg.DynamicAny.DynAnyPackage.TypeMismatch,
org.omg.DynamicAny.DynAnyPackage.InvalidValue
{
if (status == STATUS_DESTROYED) {
throw wrapper.dynAnyDestroyed() ;
}
if (index == NO_INDEX)
throw new org.omg.DynamicAny.DynAnyPackage.InvalidValue();
DynAny currentComponent = current_component();
if (DynAnyUtil.isConstructedDynAny(currentComponent))
throw new org.omg.DynamicAny.DynAnyPackage.TypeMismatch();
currentComponent.insert_any(value);
}
/**
* Static method for writing a CORBA standard exception to an Any.
* @param any The Any to write the SystemException into.
*/
public static void insertSystemException(SystemException ex, Any any) {
OutputStream out = any.create_output_stream();
ORB orb = (ORB)(out.orb());
String name = ex.getClass().getName();
String repID = ORBUtility.repositoryIdOf(name);
out.write_string(repID);
out.write_long(ex.minor);
out.write_long(ex.completed.value());
any.read_value(out.create_input_stream(),
getSystemExceptionTypeCode(orb, repID, name));
}
public void from_any (org.omg.CORBA.Any value)
throws org.omg.DynamicAny.DynAnyPackage.TypeMismatch,
org.omg.DynamicAny.DynAnyPackage.InvalidValue
{
if (status == STATUS_DESTROYED) {
throw wrapper.dynAnyDestroyed() ;
}
clearData();
super.from_any(value);
representations = REPRESENTATION_ANY;
index = 0;
}
/** This is called from the ORB after the DynamicImplementation.invoke
* returns. Here we set the result if result() has not already been called.
* @return the exception if there is one (then ORB will not call
* marshalReplyParams()) otherwise return null.
*/
public Any checkResultCalled()
{
// Two things to be checked (CORBA 2.2 spec, section 6.3):
// 1. Unless it calls set_exception(), the DIR must call arguments()
// exactly once, even if the operation signature contains
// no parameters.
// 2. Unless set_exception() is called, if the invoked operation has a
// non-void result type, set_result() must be called exactly once
// before the DIR returns.
if ( _paramsCalled && _resultSet ) // normal invocation return
return null;
else if ( _paramsCalled && !_resultSet && !_exceptionSet ) {
try {
// Neither a result nor an exception has been set.
// Assume that the return type is void. If this is not so,
// the client will throw a MARSHAL exception while
// unmarshaling the return value.
TypeCode result_tc = _orb.get_primitive_tc(
org.omg.CORBA.TCKind.tk_void);
_resultAny = _orb.create_any();
_resultAny.type(result_tc);
_resultSet = true;
return null;
} catch ( Exception ex ) {
throw _wrapper.dsiResultException(
CompletionStatus.COMPLETED_MAYBE, ex ) ;
}
} else if ( _exceptionSet )
return _exception;
else {
throw _wrapper.dsimethodNotcalled(
CompletionStatus.COMPLETED_MAYBE ) ;
}
}
public Any get_boxed_value()
throws org.omg.DynamicAny.DynAnyPackage.InvalidValue
{
if (isNull) {
throw new InvalidValue();
}
checkInitAny();
return any;
}
/**
* Decode the given octet sequence into an any based on a CDR
* encapsulated octet sequence. The type code is expected not to appear
* in the octet sequence, and the given type code is used instead.
*/
public Any decode_value( byte[] data, TypeCode tc )
throws FormatMismatch, TypeMismatch
{
if( data == null )
throw wrapper.nullParam() ;
if( tc == null )
throw wrapper.nullParam() ;
return decodeImpl( data, tc );
}
public static Any createAny(ORB orb) {
Any value = orb.create_any();
if ("com.sun.corba.se.impl.corba.AnyImpl".equals(value.getClass().getName())) {
value = createFixedAny(orb, value);
}
return value;
}
/**
* This method sets the slot data at the given slot id (index).
*/
public void set_slot( int id, Any data ) throws InvalidSlot
{
// First check whether the slot is allocated
// If not, raise the invalid slot exception
if( id >= theSlotData.length ) {
throw new InvalidSlot();
}
dirtyFlag = true;
theSlotData[id] = data;
}
public void set_elements (org.omg.CORBA.Any[] value)
throws org.omg.DynamicAny.DynAnyPackage.TypeMismatch,
org.omg.DynamicAny.DynAnyPackage.InvalidValue
{
if (status == STATUS_DESTROYED) {
throw wrapper.dynAnyDestroyed() ;
}
checkValue(value);
components = new DynAny[value.length];
anys = value;
// We know that this is of kind tk_sequence or tk_array
TypeCode expectedTypeCode = getContentType();
for (int i=0; i<value.length; i++) {
if (value[i] != null) {
if (! value[i].type().equal(expectedTypeCode)) {
clearData();
// _REVISIT_ More info
throw new TypeMismatch();
}
try {
// Creates the appropriate subtype without copying the Any
components[i] = DynAnyUtil.createMostDerivedDynAny(value[i], orb, false);
//System.out.println(this + " created component " + components[i]);
} catch (InconsistentTypeCode itc) {
throw new InvalidValue();
}
} else {
clearData();
// _REVISIT_ More info
throw new InvalidValue();
}
}
index = (value.length == 0 ? NO_INDEX : 0);
// Other representations are invalidated by this operation
representations = REPRESENTATION_COMPONENTS;
}