home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-05-08 | 7.4 KB | 188 lines |
- /*
- * Copyright (c) 1997-1998 Borland International, Inc. All Rights Reserved.
- *
- * This SOURCE CODE FILE, which has been provided by Borland as part
- * of a Borland product for use ONLY by licensed users of the product,
- * includes CONFIDENTIAL and PROPRIETARY information of Borland.
- *
- * USE OF THIS SOFTWARE IS GOVERNED BY THE TERMS AND CONDITIONS
- * OF THE LICENSE STATEMENT AND LIMITED WARRANTY FURNISHED WITH
- * THE PRODUCT.
- *
- * IN PARTICULAR, YOU WILL INDEMNIFY AND HOLD BORLAND, ITS RELATED
- * COMPANIES AND ITS SUPPLIERS, HARMLESS FROM AND AGAINST ANY CLAIMS
- * OR LIABILITIES ARISING OUT OF THE USE, REPRODUCTION, OR DISTRIBUTION
- * OF YOUR PROGRAMS, INCLUDING ANY CLAIMS OR LIABILITIES ARISING OUT OF
- * OR RESULTING FROM THE USE, MODIFICATION, OR DISTRIBUTION OF PROGRAMS
- * OR FILES CREATED FROM, BASED ON, AND/OR DERIVED FROM THIS SOURCE
- * CODE FILE.
- */
- //--------------------------------------------------------------------------------------------------
- // CORBA Reference Application
- // Copyright (c) 1997 by Borland International, All Rights Reserved
- //
- // Credit Approval Dispenser Class Implementation.
- //--------------------------------------------------------------------------------------------------
-
- package borland.reference.creditapproval.server;
-
- import com.objectspace.jgl.*;
- import java.util.*;
- import borland.reference.creditapproval.CORBAInterface.*;
- import borland.reference.creditapproval.server.Res;
-
- /**
- * CreditApprovalDispenserImpl implements the IDL-defined CreditApprovalDispenser
- * interface. This class creates a pool of CreditApproval objects and stores their
- * references in an array of CreditApprovalStatus objects. It registers each newly
- * created CreditApproval object with the ORB. CreditApproval objects are issued to
- * clients as requested. When a client is done with the object, the object is placed
- * back into the available pool for use by another client.
- */
- public class CreditApprovalDispenserImpl extends _sk_CreditApprovalDispenser {
-
- private int objectCount = 0;
- private final boolean CONNECT_ON_DEMMAND = true;
- private final boolean GENERATE_RANDOM_DATA = true;
- private final long SECURE_OBJECT_TIMEOUT = 30l; // in seconds
- private Deque availableObjectQueue = new Deque();
- private Deque reservedObjectQueue = new Deque();
- private Deque requestQueue = new Deque();
- private int enteredCount = 0;
- ResourceBundle res = Res.getBundle("borland.reference.creditapproval.server.Res");
- /**
- * Construct a persistently named object that allocates a pool of available
- * CreditApproval objects.
- */
- public CreditApprovalDispenserImpl(java.lang.String name, int instanceCount) {
- super(name);
-
- try {
- // Initialize the ORB so that the CreditApproval objects can be regisetered
- org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init();
- org.omg.CORBA.BOA boa = orb.BOA_init();
-
- for (int loop = 0; loop < instanceCount; loop++ ) {
-
- // Create using a local variable - if creation throws an exception,
- // this instance will be automatically destroyed.
- CreditApprovalImpl creditApprovalObject =
- new CreditApprovalImpl( "Credit Approval Object " + (loop + 1),
- CONNECT_ON_DEMMAND, GENERATE_RANDOM_DATA);
-
- // Register the Object with the ORB
- boa.obj_is_ready( creditApprovalObject );
-
- // The object was created successfully - add it to the object pool
- availableObjectQueue.pushBack(creditApprovalObject);
- objectCount++;
-
- } // end for loop
- }
- catch( Exception e) {
- e.printStackTrace();
- }
- System.out.println("\n\r" + res.getString("Credit_Approval"));
- System.out.println("----------------------");
- System.out.println(res.getString("Object_Pool_Count") + ": " + objectCount );
- System.out.println(" " + res.getString("Server_Status") + ": " + res.getString("Awaiting_Client"));
- }
-
- /**
- * Find an available object from the object pool and its reference.
- * Return null if none are available.
- */
- public CreditApproval reserveCreditApprovalObject()
- throws CreditApprovalException {
-
- CreditApproval creditApprovalObject;
-
- // Attempt to secure one of the Approval Objects from the Object Pool
- creditApprovalObject = secureApprovalObject();
-
- // If an objcet was not available, register a request for one
- // and wait SECURE_OBJECT_TIMEOUT seconds for one to become
- // available.
- if (creditApprovalObject == null ) {
- CreditApprovalRequest requestObject = registerRequest();
- long end = System.currentTimeMillis() + SECURE_OBJECT_TIMEOUT * 1000l;
-
- // Wait until the timeout period has expired OR an object is secured
- while( requestObject.creditApprovalObject == null &&
- System.currentTimeMillis() < end);
-
- // Since an object could not be secured, remove the request from the request queue.
- if ( requestObject.creditApprovalObject == null )
- removeRequest( requestObject );
-
- creditApprovalObject = requestObject.creditApprovalObject;
- }
-
- // An approval object could not be secured. Throw an exception
- if (creditApprovalObject == null)
- throw new CreditApprovalException(
- res.getString("Server_Busy"));
-
- // Return null to indicate that an available object was not found.
- return creditApprovalObject;
- }
-
- /**
- * Create a request object and place it on the request queue. The
- * releaseCreditApprovalObject function checks this queue. If the
- * queue is not empty, a released object is secured for the first object
- * in the queue.
- */
- private synchronized CreditApprovalRequest registerRequest() {
- CreditApprovalRequest requestObject = new CreditApprovalRequest();
- requestQueue.pushBack( requestObject );
- return requestObject;
- }
-
- /**
- * Removes the specified request from the request queue.
- */
- private synchronized void removeRequest(CreditApprovalRequest request) {
- // Make sure the request was not satisfied
- if (request.creditApprovalObject == null)
- requestQueue.remove( request );
- }
-
- /**
- * Find an available object from the object pool and its reference.
- * Return null if none are available.
- */
- private synchronized CreditApproval secureApprovalObject()
- throws CreditApprovalException {
-
- if (!availableObjectQueue.isEmpty()) {
- CreditApproval approvalObject = (CreditApproval) availableObjectQueue.popFront();
- reservedObjectQueue.pushBack( approvalObject );
- return approvalObject;
- }
-
- // Return null to indicate that an available object was not found.
- return null;
- }
-
- /**
- * A client is Releasing a reserved object. Find it in the pool and set
- * its reserved flag to false.
- */
- public synchronized void releaseCreditApprovalObject( CreditApproval creditApprovalObject )
- throws CreditApprovalException {
-
- // If requests for objects are pending, provide the first request
- // with this object
- if (!requestQueue.isEmpty()) {
- CreditApprovalRequest firstRequest = (CreditApprovalRequest) requestQueue.popFront();
- firstRequest.creditApprovalObject = creditApprovalObject;
- return;
- }
-
- availableObjectQueue.pushBack( creditApprovalObject );
- reservedObjectQueue.remove( creditApprovalObject );
- }
- }
-
-