home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 February / CHIP_2_98.iso / software / pelne / optionp / mts4.cab / Account.VJ_Account_GetReceipt.java < prev    next >
Text File  |  1997-11-14  |  7KB  |  191 lines

  1. // Filename: GetReceipt.java
  2. //
  3. // Description: Declaration of GetReceipt
  4. //
  5. // This file is provided as part of the Microsoft Transaction Server
  6. // Software Development Kit
  7. //
  8. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT 
  9. // WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, 
  10. // INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
  11. // OF MERCHANTABILITY AND/OR FITNESS FOR A  PARTICULAR 
  12. // PURPOSE.
  13. //
  14. // Copyright (C) 1997 Microsoft Corporation, All rights reserved
  15.  
  16. package Account;
  17.  
  18. import com.ms.mtx.*;
  19. import com.ms.com.*;
  20.  
  21. import accountlib.*;
  22.  
  23. public class GetReceipt implements IGetReceipt {
  24.     
  25.     private static final String CLSID = "ab077646-e902-11d0-b5be-00c04fb957d8";
  26.  
  27.     // GetNextReceipt() performs error handling for TrueGetNextReceipt().  If TrueGetNextReceipt() 
  28.     // throws an exception, then GetNextReceipt() will call SetAbort() and pass the exception up 
  29.     // to the caller.  Otherwise, GetNextReceipt() will simply call SetComplete() and return.
  30.  
  31.     public int GetNextReceipt () throws ComFailException {
  32.  
  33.         boolean bSuccess = false;
  34.         int result;
  35.  
  36.  
  37.         // First of all, get the object context
  38.         IObjectContext ctxObject = MTx.GetObjectContext();
  39.  
  40.         try {
  41.             // Call the true function
  42.             result = trueGetNextReceipt ();
  43.  
  44.             bSuccess = true;
  45.             return result;
  46.         }
  47.  
  48.         // Upon exit, always call SetComplete if happy, or SetAbort if unhappy.
  49.         // We do this because we never save state across method calls.
  50.         finally {
  51.             if (bSuccess)
  52.                 ctxObject.SetComplete();
  53.             else
  54.                 ctxObject.SetAbort();
  55.         }
  56.     }
  57.  
  58.  
  59.     // trueGetNextReceipt() is the function that performs the actual work for the Account class.
  60.     // If an error occurs during execution, it will throw a ComFailException for GetNextReceipt()
  61.     // to handle.
  62.  
  63.     // For exposition purposes, two versions of this function are given. You can uncomment either one
  64.     // of these routines.
  65.  
  66.     // The first version of trueGetNextReceipt uses the MTS Shared Property Manager to hold the
  67.     // shared state. The Shared Property Manager allows one to share state across all instances of
  68.     // components that are in the same package, regardless of what language and tools were used to
  69.     // implement each component. It is also hard to create race conditions with the Shared Property Manager.
  70.     // This example also shows that the interface the the Shared Property Manager is not particularly
  71.     // friendly to the Java programmer.
  72.     
  73.     // The second version of trueGetNextReceipt uses Java static variables to hold the shared state,
  74.     // and a static synchronized method to access and update them. This works well if every component
  75.     // that accesses the shared state is written in Java. Correctly writing code like this also
  76.     // requires a full understanding of static members and methods, of synchronized methods, and of
  77.     // how the two interact. It's relatively easy to get code like this wrong, but this example is
  78.     // simple enough that the code is attractive in this case.
  79.  
  80.  
  81.     // trueGetNextReceipt using MTS Shared Properties:
  82.     private int trueGetNextReceipt () throws ComFailException {
  83.         
  84.         ISharedPropertyGroupManager spmMgr = null;
  85.         ISharedPropertyGroup spmGroup = null;        
  86.         ISharedProperty spmPropNextReceipt = null;
  87.         ISharedProperty spmPropMaxNum = null;
  88.         
  89.         IUpdateReceipt objReceiptUpdate = null;
  90.  
  91.         try {
  92.             // Create SPM group manager
  93.             spmMgr = (ISharedPropertyGroupManager) MTx.GetObjectContext().CreateInstance 
  94.                 (SharedPropertyGroupManager.clsid, ISharedPropertyGroupManager.iid);
  95.  
  96.             // Prepare primnitive types that can be passed in by reference, as per the 
  97.             // function declarations generated by the Java Type Library Wizard for the 
  98.             // "Shared Property Manager Type Library."
  99.             boolean bExists[] = new boolean[1];
  100.             int iLockMode [] = new int [1];
  101.             int iReleaseMode [] = new int [1];
  102.             
  103.             iLockMode [0] = ISharedPropertyGroupManager.LOCKMODE_METHOD;
  104.             iReleaseMode [0] = ISharedPropertyGroupManager.RELEASEMODE_PROCESS;
  105.             
  106.             // Create the shared property group
  107.             spmGroup = spmMgr.CreatePropertyGroup ("Receipt", iLockMode, iReleaseMode, bExists);
  108.             
  109.             // Create the properties
  110.             spmPropNextReceipt = spmGroup.CreateProperty("Next", bExists);
  111.             spmPropMaxNum = spmGroup.CreateProperty("MaxNum", bExists);
  112.             
  113.             // Call GetNextReceipt() if necessary
  114.             Variant vRet = new Variant();
  115.             if (spmPropNextReceipt.getValue().getInt() >= spmPropMaxNum.getValue().getInt()) {
  116.                 
  117.                 objReceiptUpdate = (IUpdateReceipt) MTx.GetObjectContext().CreateInstance 
  118.                     (CUpdateReceipt.clsid, IUpdateReceipt.iid);
  119.  
  120.                 int iRet = objReceiptUpdate.Update ();
  121.                 
  122.                 // Update Next
  123.                 vRet.putInt (iRet);
  124.                 spmPropNextReceipt.putValue (vRet);
  125.                 
  126.                 // Incremement MaxNum by 100
  127.                 vRet.putInt (iRet + 100);
  128.                 spmPropMaxNum.putValue (vRet);
  129.             }
  130.  
  131.             vRet.putInt (spmPropNextReceipt.getValue().getInt() + 1);
  132.             spmPropNextReceipt.putValue (vRet);
  133.             
  134.             // We are finished and happy
  135.             return spmPropNextReceipt.getValue().getInt();
  136.         }
  137.  
  138.         finally {
  139.             // The following code is not strictly necessary. By calling ComLib.release here,
  140.             // the object counts seen in the MTX explorer remain correct. Without these release
  141.             // calls, the objects used here would not get released until after the next time
  142.             // the Java garbage collector runs. No other ill effect would be caused by omitting
  143.             // this code.
  144.  
  145.             // Note that this code will be executed regardless of whether we leave this
  146.             // method via return or via an exception.
  147.             if (spmMgr             != null) ComLib.release (spmMgr);
  148.             if (spmGroup           != null) ComLib.release (spmGroup);
  149.             if (spmPropNextReceipt != null) ComLib.release (spmPropNextReceipt);
  150.             if (spmPropMaxNum      != null) ComLib.release (spmPropMaxNum);
  151.             if (objReceiptUpdate   != null) ComLib.release (objReceiptUpdate);
  152.         }
  153.     }    
  154.  
  155.  
  156. /*
  157.     // trueGetNextReceipt, using Java static variables:
  158.     private static int m_nextReceipt;
  159.     private static int m_maxReceipt;
  160.     private static synchronized int trueGetNextReceipt () throws ComFailException {
  161.  
  162.         IUpdateReceipt objUpdateReceipt = null;
  163.  
  164.         try {
  165.             if (m_nextReceipt >= m_maxReceipt)
  166.             {
  167.                 objUpdateReceipt = (IUpdateReceipt) MTx.GetObjectContext().CreateInstance (
  168.                                             CUpdateReceipt.clsid, IUpdateReceipt.iid);
  169.                 m_nextReceipt = objUpdateReceipt.Update();
  170.                 m_maxReceipt = m_nextReceipt + 100;
  171.             }
  172.  
  173.             return ++m_nextReceipt;
  174.         }
  175.  
  176.         finally {
  177.             // The following code is not strictly necessary. By calling ComLib.release here,
  178.             // the object counts seen in the MTX explorer remain correct. Without these release
  179.             // calls, the objects used here would not get released until after the next time
  180.             // the Java garbage collector runs. No other ill effect would be caused by omitting
  181.             // this code.
  182.  
  183.             // Note that this code will be executed regardless of whether we leave this
  184.             // method via return or via an exception.
  185.             if (objUpdateReceipt != null) ComLib.release (objUpdateReceipt);
  186.         }
  187.     }
  188. */
  189.  
  190. }
  191.