Object Models and Java
by Jean-Marie Chauvet and Marc Lerman


Example 1:

(a)
javatlb Account.tlb

(b)
javareg /register /class:Account_Impl 
             /clsid:{ 3b377243-C3FF-11D0-A14F-0060B000632C }

Example 2:
(a)
orbixdw

(b)
putit account <full-path>\server.exe

Example 3:

(a)
account.Ref accountRef  = null;
try{
    accountRef = account._bind( ":account", "pc48" );
}
catch (IE.Iona.Orbix2.CORBA.SystemException se) {
    System.out.println("Unexpected exception:" );
    System.out.println(se.toString());
    return;
}

(b)
try{
    accountRef.makeWithdrawal( amount );
}
catch (IE.Iona.Orbix2.CORBA.SystemException se) {
    //  Handle exception here
    return;
}


Example 4:

(a)
import horb.orb.*;
//
private HorbURL m_url = new HorbURL("horb://" + "-" );
private     Account m_HORBAccount = new Account_Proxy( m_url );

(b)
m_HORBAccount.makeDeposit( 100.00 );


Listing One
[ uuid(3b377230-C3FF-11D0-A14F-0060B000632C), version(1.0),
  helpstring("Account OLE Control module"), control ]
library ACCOUNTLib
{
    importlib(STDOLE_TLB);
    importlib(STDTYPE_TLB);

    //  Primary dispatch interface for CAccountCtrl

    [ uuid(3b377231-C3FF-11D0-A14F-0060B000632C),
      helpstring("Dispatch interface for Account Control"), hidden ]
    dispinterface _DAccount
    {
        properties:
            // NOTE - ClassWizard will maintain property information here.
            //    Use extreme caution when editing this section.
            //{{AFX_ODL_PROP(CAccountCtrl)
            [id(1)] double balance;
            //}}AFX_ODL_PROP

        methods:
            // NOTE - ClassWizard will maintain method information here.
            //    Use extreme caution when editing this section.
            //{{AFX_ODL_METHOD(CAccountCtrl)
            [id(2)] void makeDeposit(double amount);
            [id(3)] void makeWithdrawal(double amount);
            [id(4)] VARIANT queryBalance();
            //}}AFX_ODL_METHOD
    };
    //  Class information for CAccountCtrl

    [ uuid(3b377233-C3FF-11D0-A14F-0060B000632C),
      helpstring("Account Control"), control ]
    coclass Account
    {
        [default] dispinterface _DAccount;
    };
    //{{AFX_APPEND_ODL}}
};

Listing Two
void AccountCtrl::deposit(double amount) 
{
    m_balance += amount;
}
void AccountCtrl::withdraw(double amount) 
{
    m_balance -= amount;
}
double AccountCtrl::queryBalance() 
{
    return m_balance;
}
double AccountCtrl::GetBalance() 
{
    return m_balance;
}
void AccountCtrl::SetBalance(double newValue) 
{
    m_balance = newValue;
    SetModifiedFlag();
}

Listing Three
public class account/Account extends java.lang.Object
{
}
public interface account/_DAccountEvents extends com.ms.com.IUnknown
{
}
public interface account/_DAccount extends com.ms.com.IUnknown
{
    public abstract void makeWithdrawal(double);
    public abstract double getbalance();
    public abstract void putbalance(double);
    public abstract void makeDeposit(double);
    public abstract double queryBalance();
}

Listing Four
import  com.*;
import  account.*;

public class Account_Impl implements _DAccount{
    private static double balance = 0.0;
    public  void makeWithdrawal( double amount ){
        balance -= amount;
    }
    public  double getbalance(){
        return balance;
    }
    public  void putbalance( double amount){
        balance = amount;
    }
    public  void makeDeposit( double amount ){
        balance += amount;
    }
    public double queryBalance(){
       return balance;
    }
}

Listing Five
interface   account{
    readonly    attribute   double  balance;
    void        makeDeposit( in double amount );
    void        makeWithdrawal( in double amount );
};

Listing Six
virtual double balance (CORBA_Environment&) {
     return m_balance; }
  virtual void makeDeposit (double amount, CORBA_Environment&) {
     m_balance += amount; }
  virtual void makeWithdrawal (double amount, CORBA_Environment&) {
    m_balance -= amount; }

Listing Seven
int main() {
//
  // Create server application object
  account* server = new TIE_account(account_impl) (new account_impl());
//
  // Export the server to the network
  IT_TRY {
      CORBA_Orbix.impl_is_ready("account",IT_X);
  }
  IT_CATCHANY {
     cout << IT_X << endl;
  } IT_ENDTRY
  server->_release ();
return 0;
}

Listing Eight
package account;
public interface Ref extends IE.Iona.Orbix2.CORBA.Object.Ref {
    public double balance()
        throws IE.Iona.Orbix2.CORBA.SystemException;
    public void makeDeposit(double amount)
        throws IE.Iona.Orbix2.CORBA.SystemException;
    public void makeWithdrawal(double amount)
        throws IE.Iona.Orbix2.CORBA.SystemException;
}

Listing Nine
public void makeWithdrawal(double amount)
        throws IE.Iona.Orbix2.CORBA.SystemException
    {
        IE.Iona.Orbix2.CORBA.Request _mb = 
              new IE.Iona.Orbix2.CORBA.Request(this, "makeWithdrawal",false);
        _mb.insertDouble(amount);
        try {
            _mb.invoke();
            _mb.extractOutParams();
        } catch (IE.Iona.Orbix2.CORBA.SystemException _ex) {
            throw _ex;
        } catch (IE.Iona.Orbix2.CORBA.CORBAException _ex) {
            throw new IE.Iona.Orbix2.CORBA.UNKNOWN(12001, 
                          IE.Iona.Orbix2.CORBA.CompletionStatus.MAYBE);
        }
    }

Listing Ten
<html>
<head>
<title>JavaAtm</title>
</head>
<script language=VBScript>
<!--
sub Window_OnLoad
    document.JavaAtm.setOLEAccount OLEAccount
end sub
-->
</script>
<body><body>
<hr>
This is an applet written in Java using MS/Visual J++ 1.1<P>
<applet
    code=JavaAtm.class
    name=JavaAtm
    width=320
    height=240 >
</applet>
<hr>
This is an Account control written in C++ with MS/Visual C++ v4.2<P>
<object
   classid="clsid:58738CB3-C4AB-11D0-A14F-0060B000632C"
   id=OLEAccount
   width=160
   height=180
>
</object>
<P>
<a href="javaatm.java">The source.</a>
</body>
</html>

Listing Eleven
    private HorbURL m_url = new HorbURL("horb://" + "-" );
    private _DAccount   m_HORBacc = new Account_Proxy( m_url );
    private _DAccount   m_javaOLEacc = (_DAccount)new Account();
    private _DAccount   m_localacc = new Account_Impl();
    private _DAccount   m_oleacc;
    private _DAccount   m_orbixacc = (_DAccount)new account();

Listing Twelve
public void setOLEAccount( Object oleacc ){
       m_oleacc = (_DAccount)new Account( oleacc );
}

Listing Thirteen
private void handleDeposit( _DAccount acc, 
double amount, TextField outBalance, String s ){
  acc.makeDeposit( amount );
  outBalance.setText( s + acc.queryBalance() );
}
public boolean action(Event evt, Object what){
if(evt.target == m_deposit){
    Double  D = new Double(m_amount.getText());
    handleDeposit(m_oleacc,D.doubleValue(),m_balance,"OCX Balance is: $" );
    handleDeposit(m_javaOLEacc,D.doubleValue(),m_combalance,"Java/COM Balance is: $");
    handleDeposit(m_HORBacc,D.doubleValue(),m_corbabalance,"Java/HORB Balance is: $");
    handleDeposit(m_localacc,D.doubleValue(),m_javabalance,"Java Balance is: $");
    handleDeposit(m_orbixacc,D.doubleValue(),m_orbixbalance,"Orbix Balance is: $");
    // Event handled
    return true;
    }
  // Event not handled
  return false;
}



6


