VisiBroker for Java Developer’s Guide : Using the tie mechanism

Using the tie mechanism
This section describes how the tie mechanism may be used to integrate existing Java code into a distributed object system. This section will enable you to create a delegation implementation or to provide implementation inheritance.
How does the tie mechanism work?
Object implementation classes normally inherit from a servant class generated by the idl2java compiler. The servant class, in turn, inherits from org.omg.PortableServer.Servant. When it is not convenient or possible to alter existing classes to inherit from the VisiBroker servant class, the tie mechanism offers an attractive alternative.
The tie mechanism provides object servers with a delegator implementation class that inherits from org.omg.PortableServer.Servant . The delegator implementation does not provide any semantics of its own. The delegator implementation simply delegates every request it receives to the real implementation class, which can be implemented separately. The real implementation class is not required to inherit from org.omg.PortableServer::.Servant .
With using the tie mechanism, two additional files are generated from the IDL compiler:
<interface_name>POATie defers implementation of all IDL defined methods to a delegate. The delegate implements the interface <interface_name>Operations. Legacy implementations can be trivially extended to implement the operations interface and in turn delegate to the real implementation.
<interface_name>Operations defines all of the methods that must be implemented by the object implementation. This interface acts as the delegate object for the associated <interface_name>POATie class when the tie mechanism is used.
Example program
Location of an example program using the tie mechanism
A version of the Bank example using the tie mechanism can be found in:
<install_dir>\vbroker\examples\basic\bank_tie
Changes to the server class
The following code sample shows the modifications to the Server class. Note the extra step of creating an instance of AccountManagerManagerPOATie.
import org.omg.PortableServer.*;

public class Server {
public static void main(String[] args) {
try {
// Initialize the ORB.
org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args,null);
// get a reference to the root POA
POA rootPoa = POAHelper.narrow(
orb.resolve_initial_references("RootPOA"));
// Create policies for our persistent POA
org.omg.CORBA.Policy[] policies = {
rootPoa.create_lifespan_policy(LifespanPolicyValue.PERSISTENT)
};
// Create myPOA with the right policies
POA myPOA = rootPoa.create_POA("bank_agent_poa",
rootPoa.the_POAManager(), policies);
// Create the tie which delegates to an instance of AccountManagerImpl
Bank.AccountManagerPOATie tie =
new Bank.AccountManagerPOATie(new AccountManagerImpl(rootPoa));
// Decide on the ID for the servant
byte[] managerId = "BankManager".getBytes();
// Activate the servant with the ID on myPOA
myPOA.activate_object_with_id(managerId, tie);
// Activate the POA manager
rootPoa.the_POAManager().activate();
System.out.println("Server is ready.");
// Wait for incoming requests
orb.run();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Changes to the AccountManager
The changes made to the AccountManager class (in comparison with the Bank_agent example) include:
AccountManagerImpl no longer extends Bank.AccountManagerPOA.
When a new Account is to be created, an AccountPOATie is also created and initialized.
import org.omg.PortableServer.*;
import java.util.*;

public class AccountManagerImpl implements
Bank.AccountManagerOperations {
public AccountManagerImpl(POA poa) {
_accountPOA = poa;
}
public synchronized Bank.Account open(String name) {
// Lookup the account in the account dictionary.
Bank.Account account = (Bank.Account) _accounts.get(name);
// If there was no account in the dictionary, create one.
if (account == null) {
// Make up the account's balance, between 0 and 1000 dollars.
float balance = Math.abs(_random.nextInt()) % 100000 / 100f;
// Create an account tie which delegate to an instance of AccountImpl
Bank.AccountPOATie tie =
new Bank.AccountPOATie(new AccountImpl(balance));
try {
// Activate it on the default POA which is root POA for
// this servant
account =
Bank.AccountHelper.narrow(_accountPOA.servant_to_reference(tie));
}
catch (Exception e) {
e.printStackTrace();
}
// Print out the new account.
System.out.println("Created " + name +
"'s account: " + account);
// Save the account in the account dictionary.
_accounts.put(name, account);
}
// Return the account.
return account;
}
private Dictionary _accounts = new Hashtable();
private Random _random = new Random();
private POA _accountPOA = null;
}
Changes to the Account class
The changes made to the Account class (in comparison with the Bank example) are that it no longer extends Bank.AccountPOA.
// Server.java
public class AccountImpl implements Bank.AccountOperations {
public AccountImpl(float balance) {
_balance = balance;
}
public float balance() {
return _balance;
}
private float _balance;
}
Building the tie example
The instructions described in “Developing an example application with VisiBroker” are also valid for building the tie example.