HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Interfacing Transaction Services"
Vol. 3, Issue 3, p. 52

	

Listing 1: MCM Order Validation System: Conceptual-level Set of Java Interfaces.
 
public interface IItem 
{ 
  public boolean ValidateAttributes(); 
  public boolean ValidateRules(); 
} 

public interface IOrder 
{ 
  public boolean AddItem(IItem Item); 
  public boolean Submit(IItem Item); 
} 

public interface IDispatcher 
{ 
  public boolean Schedule(); 
  public boolean InformDemandPlanning(); 
} 

Listing 2: MCM Order Validation System: IDL Definitions.
 
FILE: MCMUtils.idl 
//MCMUtils.idl 

import "oaidl.idl"; 

//typedefs 
#ifndef _PERSISTID 
#define _PERSISTID 
  typedef BSTR PERSISTID; 
#endif _PERSISTID 

//interfaces 
[ 
  object, 
  uuid(AA573A21-90D2-11d1-B744-00609770DE26), //interface ID 
  dual, 
  helpstring("IPersistObject Interface"), //string desc. 
  pointer_default(unique) 
] 
interface IPersistObject : IDispatch 
{ 
  HRESULT GetPersistRef([out, retval] PERSISTID* pRef); 
  HRESULT Load( [in] const BSTR Storage); 
  HRESULT Save(void); 
  HRESULT IsDirty([out, retval]BSTR* pDirty); 
} 

MCMValidate.idl 

//MCMValidate.idl 

//Custom Properties for Java and MTS 
#include <MtxAttr.h> 
#include <JavaAttr.h> 

import "oaidl.idl"; //std. defs 
import "MCMUtils.idl"; // contains IPersistObject def. 

//interfaces 
[ 
  object, 
  uuid(F6E42BE2-8DE9-11d1-8813-0080C72ABA92), //Iterface ID 
  dual, 
  helpstring("IItem Interface"), 
  pointer_default(unique) 
] 
interface IItem : IDispatch 
{ 
  //Validate method, POST: item attributes and rules were validated,  calls SetComplte()/SetAbort() 
  HRESULT Validate(); 
} 

[ 
  object, 
  uuid(16E009A1-8DEA-11d1-8813-0080C72ABA92), //Iterface ID 
  dual, 
  helpstring("IOrder Interface"), 
  pointer_default(unique) 
] 
interface IOrder : IDispatch 
{ 
  //AddItem method, POST: item is added to order   
  HRESULT AddItem([in] PERSISTID ItemID); 

  //Submit method, POST: order was submitted, calls SetComplte()/SetAbort() 
  HRESULT Submit(void); 
} 

[ 
  object, 
  uuid(41E5D4E1-8DEA-11d1-8813-0080C72ABA92), //Iterface ID 
  dual, 
  helpstring("IDispatcher Interface"), 
  pointer_default(unique) 
] 
interface IDispatcher : IDispatch 
{ 
  //Schedule method, POST: item attributes and rules were validated, calls SetComplte()/SetAbort() 
  HRESULT Schedule([in] PERSISTID ItemID); 

  //InformDemandPlanning method, POST: order was sent to demand planning, calls SetComplte()/SetAbort() 
  HRESULT InformDemandPlanning([in] PERSISTID ItemID); 
} 

[ 
  uuid(6A505140-8DE9-11d1-8813-0080C72ABA92), //LibID 
  version(1.0), //version number 
  helpstring("MCM Order Validation Library") //string desc. 
] 
library MCMValidateLib 
{ 
  importlib("stdole2.tlb"); //std. defs 
  importlib("stdole32.tlb"); //std. defs 
  [ 
    uuid(41E5D4E2-8DEA-11d1-8813-0080C72ABA92),  //COM class ID 
    helpstring("CItem class"), 
    JAVACLASS("MCMValidateImpl.Item"), //Java impl. class 
    PROGID("MCMValidate.Item.1"), //Prog ID 
    TRANSACTION_REQUIRED //MTS trans. indicator 
  ] 
  coclass CItem 
  { 
    [default] interface IItem; 
    interface IPersistObject; 
  } 

  [ 
    uuid(6A505141-8DE9-11d1-8813-0080C72ABA92), //COM class ID 
    helpstring("COrder class"), 
    JAVACLASS("MCMValidateImpl.Order"),  //Java impl. class 
    PROGID("MCMValidate.Order.1"), //Prog ID 
    TRANSACTION_REQUIRED //MTS trans. indicator 
  ] 
  coclass COrder 
  { 
    [default] interface IOrder; 
    interface IPersistObject; 
  } 

  [ 
    uuid(6A505145-8DE9-11d1-8813-0080C72ABA92), //COM class ID 
    helpstring("CDispatcher class"), 
    JAVACLASS("MCMValidateImpl.Dispatcher"), //Java impl. class 
    PROGID("MCMValidate.Dipatcher.1"), //Prog ID 
    TRANSACTION_REQUIRED //MTS trans. indicator 
  ] 
  coclass CDispatcher 
  { 
    [default] interface IDispatcher; 
  } 
} 

Listing 3: MCM Order Validation System: Batch Build File.
 
FILE: MCMBuild.bat 
REM MCMBuild.bat 

REM Compile IDL 
del MCMValidate.tlb 
midl MCMValidate.idl 

REM Generate Java mapping 
jactivex /javatlb /d . MCMValidate.tlb 

REM Compile all Java classes 
jvc mcmvalidate\*.java MCMValidateImpl\*.java MCMUtils\*.java 

REM generate MTS Dll 
exegen /d /r /out:MCMValidate.dll MCMValidate.tlb mcmvalidate\*.class MCMValidateImpl\*.class MCMUtils\*.class 

Listing 4: MCM Order Validation System: Java Implementation Classes.
 
FILE: MCMValidateImpl\PersistManager.java 
//PersistManager.java 

package MCMUtils; //MCMValidate Utilities package 

import MCMValidateImpl.*; //MCMValidate impl. classes 
import mcmvalidate.*; //MCMValidate Jactivex gnerated 

//COM Std. packages 
import com.ms.com.*; 
import com.ms.com.IUnknown; 
import com.ms.com.Variant; 

public class PersistManager 
{ 
  public static IPersistObject BindToObject(String PersistRef) 
  { 
    IPersistObject obj = null; 

    //add your favorite persistence algorithm here 
    //create a new instance, e.g. obj = (IPersistObject) new CItem(); 
    //load state, e.g. obj.Load( žstorageIDÓ); 

    return obj; 
  } 
} 

FILE: MCMValidateImpl \Dispatcher.java 
//Dispatcher.java 

package MCMValidateImpl; 

import MCMUtils.*; 
import mcmvalidate.*; 
import com.ms.mtx.*; //MTS package 

//std. COM packages 
import com.ms.com.*; 
import com.ms.com.IUnknown; 
import com.ms.com.Variant; 

public class Dispatcher implements IObjectControl, IDispatcher 
{ 
  private static final String CLSID = "6A505145-8DE9-11d1-8813-0080C72ABA92"; 
  private IObjectContext m_context; 

  public boolean CanBePooled() //IObjectControl 
  { 
    //allow object pooling 
    return true; 
  } 
  public void Activate() //IObjectControl 
  { 
  } 
  public void Deactivate() //IObjectControl 
  { 
  } 
  public void Schedule( String ItemID ) //IDispatcher 
  { 
    boolean bSuccess = false; 
    try 
    { 
      m_context = MTx.GetObjectContext(); 

      //execute scheduling alogorithm 
      //add business code here 
      bSuccess = true; 
    } 
    finally 
    { 
      //commit changes and recycle instance 
      if ( bSuccess ) 
   m_context.SetComplete(); 
 else 
   m_context.SetAbort();   
    }; 
  } 
  public void InformDemandPlanning( String ItemID ) //IDispatcher 
  { 
    boolean bSuccess = false; 
    try 
    { 
      m_context = MTx.GetObjectContext(); 

      //submit item attributes to demand planning system 
      //add business code here 
      bSuccess = true; 
    } 
    finally 
    { 
      //commit changes and recycle instance 
      if ( bSuccess ) 
   m_context.SetComplete(); 
 else 
   m_context.SetAbort();   
    }; 
  } 
} 

FILE: MCMValidateImpl\Item.java 
//Item.java 

package MCMValidateImpl; 

import MCMUtils.*; 
import mcmvalidate.*; 
import com.ms.mtx.*; //MTS package 

//std. COM packages 
import com.ms.com.*; 
import com.ms.com.IUnknown; 
import com.ms.com.Variant; 

public class Item implements IObjectControl, IPersistObject, IItem 
{ 
  private static final String CLSID = "41E5D4E2-8DEA-11d1-8813-0080C72ABA92"; 
  private IObjectContext m_context; 
  private boolean m_OK; 

  //persistence attributes 
  private String m_strRef; 
  private String m_storageID; 
  private boolean m_i_am_dirty; 

  //item attributes 
  protected String m_name; 
  protected long m_productID;  
  protected long m_width; 
  protected long m_length; 

  //constructor 
  public Item() 
  { 
  } 
  public boolean CanBePooled() //IObjectControl 
  { 
    //allow object pooling 
    return true; 
  } 
  public void Activate() //IObjectControl 
  { 
    //reset on activation 
    m_i_am_dirty = true; 
    m_strRef = ""; 
    m_storageID = ""; 
  } 
  public void Deactivate() //IObjectControl 
  { 
  } 
  public String IsDirty() //IObjectControl 
  { 
    return ( new Boolean(m_i_am_dirty).toString() ); 
  } 
  public String GetPersistRef() //IpersistObject 
  { 
    //return reference that can be used to restore object's state 
    return m_strRef; 
  } 
  public void Save() //IpersistObject 
  { 
    //save the object state into persistent storage: StorageID 
    //add code here 
    return; 
  } 
  public void Load( String StorageID ) //IpersistObject 
  { 
    //load the object state from persistent storage 
    //add code here 
    return; 
  } 
  protected boolean ValidateAttributes() 
  { 
    boolean success = false; 

    //perform validation logic 
    //add code here 

    //signal change of state 
    m_i_am_dirty =  true; 

    return success; 
  } 
  protected boolean ValidateRules()  
  { 
    boolean success = false; 

    //perform validation logic 
    //add code here 

    //signal change of state 
    m_i_am_dirty =  true; 

    return success; 
  } 
  public void Validate() throws ComSuccessException //IItem 
  { 
    boolean success = false; 
    try 
    { 
      m_context = MTx.GetObjectContext(); 

      //perform validations 
      if ( ValidateAttributes() && ValidateRules() ) 
        success = true; 
      else 
        throw new ComSuccessException( "Invalid Item." ); 
    } 
    finally 
    { 
      //save state, if necessary 
      if ( m_i_am_dirty ) 
        Save(); 

      if ( success ) 
        m_context.SetComplete(); 
      else 
        m_context.SetComplete(); 
    }; 
  } 
} 

FILE: MCMValidateImpl\Order.java 
//Order.java 

package MCMValidateImpl; 

import MCMUtils.*; 
import mcmvalidate.*; 
import com.ms.mtx.*; //MTS package 

//std. COM packages 
import com.ms.com.*; 
import com.ms.com.IUnknown; 
import com.ms.com.Variant; 

public class Order implements IObjectControl, IPersistObject, IOrder 
{ 
  private static final String CLSID = "6A505141-8DE9-11d1-8813-0080C72ABA92"; 

  private IItem m_item; 
  private String m_itemID; 
  private IObjectContext m_context; 
  private IDispatcher m_dispatcher; 
  private boolean m_submit; 

  //persistence attributes 
  private String m_strRef; 
  private String m_storageID; 
  private boolean m_i_am_dirty; 

  //constructor 
  public Order() 
  { 
  } 

  public boolean CanBePooled() //IObjectControl 
  { 
    //allow object pooling 
    return true; 
  } 
  public void Activate() //IObjectControl 
  { 
    //reset instance variables 
    m_submit = false; 
    m_dispatcher = null; 
    m_item = null; 
    m_itemID = ""; 
    m_context = null; 
    m_i_am_dirty = false; 
    m_strRef = ""; 
    m_storageID = ""; 
    return; 
  } 
  public void Deactivate() //IObjectControl 
  { 
  } 
  public String IsDirty() //IPersistObject 
  { 
    return ( new Boolean(m_i_am_dirty).toString() ); 
  } 
  public String GetPersistRef() //IPersistObject 
  { 
    //return persistent reference 
    return m_strRef; 
  } 
  public void Save() //IPersistObject 
  { 
    //save state into persistent storage 
    //add code here 
    return; 
  } 
  public void Load( String StorageID ) //IPersistObject 
  { 
    //load state from persistent storage 
    //add code here 
    return; 
  } 
  public void AddItem( String ItemID ) throws ComSuccessException 
  { 
    if( ItemID.length() == 0 ) 
    { 
      throw new ComSuccessException ("Invalid ItemID."); 
    } 
    else 
    { 
 m_itemID = ItemID; 
 m_i_am_dirty = true; 
 return; 
    }; 
  } 
  public void Submit() throws ComSuccessException 
  { 
    boolean success = false; 
    try 
    { 
      m_context = MTx.GetObjectContext(); 
      if ( m_submit ) 
      { 
         //order has already been submitted 
    throw new ComSuccessException ("Order already submitted."); 
      }; 
      if( m_itemID.length() == 0 ) 
 { 
   //nothing to submit 
   throw new ComSuccessException ("No item on order."); 
 }; 
  
 //load an instance of Item 
 m_item = (IItem) PersistManager.BindToObject( m_itemID ); 

      m_item.Validate(); //m_item instance recycled 

      //create instance of Dispatcher within current context 
      m_dispatcher = (IDispatcher) m_context.CreateInstance(CDispatcher.clsid, IDispatcher.iid); 

      m_dispatcher.Schedule(m_itemID); //instance recycled 
      m_dispatcher.InformDemandPlanning(m_itemID); //instance recycled 

      m_i_am_dirty = true; //signal change of state 
      m_submit = true; 
 success = true; 
      return; 
    } 
    finally 
    { 
      //save state before calling SetComplete()/SetAbort() 
      if ( m_i_am_dirty ) 
        Save(); 

      //Without the following calls, the objects used here 
      //would not get released until after the next time 
      //the Java garbage collector runs.  
      //This calls could be omitted. 
      if (m_dispatcher != null)  
   ComLib.release( m_dispatcher ); 
      if (m_dispatcher != null)  
        ComLib.release( m_item ); 

      if ( success ) 
        m_context.SetComplete(); 
      else 
        m_context.SetAbort(); 
    }; 
  } 
}
 
      
 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.