How can I recognize that the object is instance of an interface when passed in parameter ? [Corba, Java]

1k views Asked by At

My IDL looks like:

interface TransactionResource {
    void prepare() raises (NotPreparedException);
    void commit() raises(TransactionException);
    void rollback() raises(TransactionException);
};
interface ManageDemand : TransactionResource {
    string createDemand(in string demand);
};

interface ManageAccount : TransactionResource {
    string createDemand(in string demand);
};

I create the ManageDemand distributed object on Server , I make it persistent, and it's reachable through the CORBALOC address.

On my client I have:

Object obj = orb.string_to_object(url.toString());
TransactionResource transactionResource = null;
if (obj._is_a("IDL:transaction/ManageDemand:1.0")){
    transactionResource = ManageDemandHelper.narrow(obj);
} else {
    transactionResource = ManageAccountHelper.narrow(obj);
}

When I try to test if the transactionResource object (distributed reference) is an instance of ManageDemand, the result is true.

But if I invoke Transaction object, defined like this:

interface Transaction {
    ProxyStream addResource(in TransactionResource resource);
}

And I pass in parameter the transactionResource distributed reference, in this method when I test if this resource is an instance of ManageDemand, the result is false.

What I have to do, to recognize that this transactionResource is instance of ManageDemand on the addResource method?

1

There are 1 answers

0
jawee On

I don't know how you test if the transactionResource object(TransactionResource resource) is an instance of ManageDemand, in the 'addResource' method of your Transaction Servant implementation.
I guess maybe, you test it just like below:

if(resource instanceof ManageDemand ) {
  System.out.println("Yes, ManageDemand");
} else {
  System.out.println("No, ManageDemand");
}

If yes, then you wont' get the correct answer, since the 'resource' object obtained from the client is "_TransactionResourceStub" class, therefore, you can't use the 'instanceof' method to test.
However, you still can use the '_is_a' method to test it ,like below:

if (resource._is_a("IDL:transaction/ManageAccount:1.0")){
    System.out.println("Yes");
} else {
    System.out.println("No");
}

Then, you'll get the correct answer.