home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / TRIAL / JBUILDER / JVISBRKR.Z / CreditApprovalDispenserImpl.java < prev    next >
Encoding:
Java Source  |  1998-05-08  |  7.4 KB  |  188 lines

  1. /*
  2.  * Copyright (c) 1997-1998 Borland International, Inc. All Rights Reserved.
  3.  * 
  4.  * This SOURCE CODE FILE, which has been provided by Borland as part
  5.  * of a Borland product for use ONLY by licensed users of the product,
  6.  * includes CONFIDENTIAL and PROPRIETARY information of Borland.  
  7.  *
  8.  * USE OF THIS SOFTWARE IS GOVERNED BY THE TERMS AND CONDITIONS 
  9.  * OF THE LICENSE STATEMENT AND LIMITED WARRANTY FURNISHED WITH
  10.  * THE PRODUCT.
  11.  *
  12.  * IN PARTICULAR, YOU WILL INDEMNIFY AND HOLD BORLAND, ITS RELATED
  13.  * COMPANIES AND ITS SUPPLIERS, HARMLESS FROM AND AGAINST ANY CLAIMS
  14.  * OR LIABILITIES ARISING OUT OF THE USE, REPRODUCTION, OR DISTRIBUTION
  15.  * OF YOUR PROGRAMS, INCLUDING ANY CLAIMS OR LIABILITIES ARISING OUT OF
  16.  * OR RESULTING FROM THE USE, MODIFICATION, OR DISTRIBUTION OF PROGRAMS
  17.  * OR FILES CREATED FROM, BASED ON, AND/OR DERIVED FROM THIS SOURCE
  18.  * CODE FILE.
  19.  */
  20. //--------------------------------------------------------------------------------------------------
  21. // CORBA Reference Application
  22. // Copyright (c) 1997 by Borland International, All Rights Reserved
  23. //
  24. // Credit Approval Dispenser Class Implementation.
  25. //--------------------------------------------------------------------------------------------------
  26.  
  27. package borland.reference.creditapproval.server;
  28.  
  29. import com.objectspace.jgl.*;
  30. import java.util.*;
  31. import borland.reference.creditapproval.CORBAInterface.*;
  32. import borland.reference.creditapproval.server.Res;
  33.  
  34. /**
  35. * CreditApprovalDispenserImpl implements the IDL-defined CreditApprovalDispenser
  36. * interface. This class creates a pool of CreditApproval objects and stores their
  37. * references in an array of CreditApprovalStatus objects. It registers each newly
  38. * created CreditApproval object with the ORB.  CreditApproval objects are issued to
  39. * clients as requested.  When a client is done with the object, the object is placed
  40. * back into the available pool for use by another client.
  41. */
  42. public class CreditApprovalDispenserImpl extends _sk_CreditApprovalDispenser {
  43.  
  44.   private int objectCount = 0;
  45.   private final boolean CONNECT_ON_DEMMAND = true;
  46.   private final boolean GENERATE_RANDOM_DATA = true;
  47.   private final long SECURE_OBJECT_TIMEOUT = 30l;   // in seconds
  48.   private Deque availableObjectQueue = new Deque();
  49.   private Deque reservedObjectQueue = new Deque();
  50.   private Deque requestQueue = new Deque();
  51.   private int enteredCount = 0;
  52.   ResourceBundle res = Res.getBundle("borland.reference.creditapproval.server.Res");
  53.   /**
  54.   * Construct a persistently named object that allocates a pool of available
  55.   * CreditApproval objects.
  56.   */
  57.   public CreditApprovalDispenserImpl(java.lang.String name, int instanceCount) {
  58.     super(name);
  59.  
  60.     try {
  61.       // Initialize the ORB so that the CreditApproval objects can be regisetered
  62.       org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init();
  63.       org.omg.CORBA.BOA boa = orb.BOA_init();
  64.  
  65.       for (int loop = 0; loop < instanceCount; loop++ ) {
  66.  
  67.         // Create using a local variable - if creation throws an exception,
  68.         // this instance will be automatically destroyed.
  69.         CreditApprovalImpl creditApprovalObject =
  70.                    new CreditApprovalImpl( "Credit Approval Object " + (loop + 1),
  71.                                 CONNECT_ON_DEMMAND, GENERATE_RANDOM_DATA);
  72.  
  73.         // Register the Object with the ORB
  74.         boa.obj_is_ready( creditApprovalObject );
  75.  
  76.         // The object was created successfully - add it to the object pool
  77.         availableObjectQueue.pushBack(creditApprovalObject);
  78.         objectCount++;
  79.  
  80.       } // end for loop
  81.     }
  82.     catch( Exception e) {
  83.       e.printStackTrace();
  84.     }
  85.     System.out.println("\n\r" + res.getString("Credit_Approval"));
  86.     System.out.println("----------------------");
  87.     System.out.println(res.getString("Object_Pool_Count") + ": " + objectCount );
  88.     System.out.println("    " + res.getString("Server_Status") + ": " + res.getString("Awaiting_Client"));
  89.   }
  90.  
  91.   /**
  92.   * Find an available object from the object pool and its reference.
  93.   * Return null if none are available.
  94.   */
  95.   public CreditApproval reserveCreditApprovalObject()
  96.          throws CreditApprovalException {
  97.  
  98.     CreditApproval creditApprovalObject;
  99.  
  100.     // Attempt to secure one of the Approval Objects from the Object Pool
  101.     creditApprovalObject = secureApprovalObject();
  102.  
  103.     // If an objcet was not available, register a request for one
  104.     // and wait SECURE_OBJECT_TIMEOUT seconds for one to become
  105.     // available.
  106.     if (creditApprovalObject == null ) {
  107.       CreditApprovalRequest requestObject = registerRequest();
  108.       long end = System.currentTimeMillis() + SECURE_OBJECT_TIMEOUT * 1000l;
  109.  
  110.       // Wait until the timeout period has expired OR an object is secured
  111.       while( requestObject.creditApprovalObject == null &&
  112.              System.currentTimeMillis() < end);
  113.  
  114.       // Since an object could not be secured, remove the request from the request queue.
  115.       if ( requestObject.creditApprovalObject == null )
  116.          removeRequest( requestObject );
  117.  
  118.       creditApprovalObject = requestObject.creditApprovalObject;
  119.     }
  120.  
  121.     // An approval object could not be secured. Throw an exception
  122.     if (creditApprovalObject == null)
  123.       throw new CreditApprovalException(
  124.             res.getString("Server_Busy"));
  125.  
  126.     // Return null to indicate that an available object was not found.
  127.     return creditApprovalObject;
  128.   }
  129.  
  130.   /**
  131.   *  Create a request object and place it on the request queue. The
  132.   * releaseCreditApprovalObject function checks this queue. If the
  133.   * queue is not empty, a released object is secured for the first object
  134.   * in the queue.
  135.   */
  136.   private synchronized CreditApprovalRequest registerRequest() {
  137.     CreditApprovalRequest requestObject = new CreditApprovalRequest();
  138.     requestQueue.pushBack( requestObject );
  139.     return requestObject;
  140.   }
  141.  
  142.   /**
  143.   * Removes the specified request from the request queue.
  144.   */
  145.   private synchronized void removeRequest(CreditApprovalRequest request) {
  146.       // Make sure the request was not satisfied
  147.     if (request.creditApprovalObject == null)
  148.         requestQueue.remove( request );
  149.   }
  150.  
  151.   /**
  152.   * Find an available object from the object pool and its reference.
  153.   * Return null if none are available.
  154.   */
  155.   private synchronized CreditApproval secureApprovalObject()
  156.          throws CreditApprovalException {
  157.  
  158.     if (!availableObjectQueue.isEmpty()) {
  159.       CreditApproval approvalObject = (CreditApproval) availableObjectQueue.popFront();
  160.       reservedObjectQueue.pushBack( approvalObject );
  161.       return approvalObject;
  162.     }
  163.  
  164.     // Return null to indicate that an available object was not found.
  165.     return null;
  166.   }
  167.  
  168.   /**
  169.   * A client is Releasing a reserved object. Find it in the pool and set
  170.   * its reserved flag to false.
  171.   */
  172.   public synchronized void releaseCreditApprovalObject( CreditApproval creditApprovalObject )
  173.               throws CreditApprovalException {
  174.  
  175.     // If requests for objects are pending, provide the first request
  176.     // with this object
  177.     if (!requestQueue.isEmpty()) {
  178.       CreditApprovalRequest firstRequest = (CreditApprovalRequest) requestQueue.popFront();
  179.       firstRequest.creditApprovalObject = creditApprovalObject;
  180.       return;
  181.     }
  182.  
  183.     availableObjectQueue.pushBack( creditApprovalObject );
  184.     reservedObjectQueue.remove( creditApprovalObject );
  185.   }
  186. }
  187.  
  188.