📄 creditapprovaldispenserimpl.java
字号:
/**
* Copyright (c) 1996-2004 Borland Software Corporation. All Rights Reserved.
*
* This SOURCE CODE FILE, which has been provided by Borland Software as part
* of a Borland Software product for use ONLY by licensed users of the product,
* includes CONFIDENTIAL and PROPRIETARY information of Borland Software.
*
* 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 SOFTWARE, 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.
*/
//------------------------------------------------------------------------------
// Copyright (c) 1996-2004 Borland Software Corporation. All Rights Reserved.
//------------------------------------------------------------------------------
package com.borland.samples.creditapproval.server;
import java.util.*;
import com.borland.samples.creditapproval.CORBAInterface.*;
/**
* 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 CreditApprovalDispenserPOA {
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 LinkedList availableObjectQueue = new LinkedList();
private LinkedList reservedObjectQueue = new LinkedList();
private LinkedList requestQueue = new LinkedList();
private int enteredCount = 0;
ResourceBundle res = Res.getBundle("com.borland.samples.creditapproval.server.Res");
/**
* Construct a persistently named object that allocates a pool of available
* CreditApproval objects.
* @param instanceCount int
*/
public CreditApprovalDispenserImpl(int instanceCount) {
try {
// Initialize the ORB so that the CreditApproval objects can be regisetered
org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init( (String[])null, null );
org.omg.CORBA.Object obj = orb.resolve_initial_references("RootPOA");
org.omg.PortableServer.POA rootPOA = org.omg.PortableServer.POAHelper.narrow(obj);
org.omg.CORBA.Any any = orb.create_any();
com.inprise.vbroker.PortableServerExt.BindSupportPolicyValueHelper.insert(any, com.inprise.vbroker.PortableServerExt.BindSupportPolicyValue.BY_INSTANCE);
org.omg.CORBA.Policy bsPolicy = orb.create_policy(com.inprise.vbroker.PortableServerExt.BIND_SUPPORT_POLICY_TYPE.value, any);
org.omg.CORBA.Policy[] policies = {
rootPOA.create_lifespan_policy(org.omg.PortableServer.LifespanPolicyValue.PERSISTENT),
bsPolicy,
};
org.omg.PortableServer.POA poa = rootPOA.create_POA("credit_approval_factory_poa", rootPOA.the_POAManager(), policies);
for (int loop = 0; loop < instanceCount; loop++ ) {
// Create using a local variable - if creation throws an exception,
// this instance will be automatically destroyed.
CreditApprovalImpl creditApprovalServant =
new CreditApprovalImpl(CONNECT_ON_DEMMAND, GENERATE_RANDOM_DATA);
// Make an ID for the credit approval Servant
byte [] creditApprovalID = ("CreditApprovalObject" + (loop + 1)).getBytes();
// Activate the servant with the ID
poa.activate_object_with_id(creditApprovalID, creditApprovalServant);
rootPOA.the_POAManager().activate();
// The object was created successfully - add it to the object pool
availableObjectQueue.addLast(CreditApprovalHelper.narrow(poa.servant_to_reference(creditApprovalServant)));
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.
*
* @throws CreditApprovalException creditapprovalexception
* @return CreditApproval
*/
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.
* @return CreditApprovalRequest
*/
private synchronized CreditApprovalRequest registerRequest() {
CreditApprovalRequest requestObject = new CreditApprovalRequest();
requestQueue.addLast( requestObject );
return requestObject;
}
/**
* Removes the specified request from the request queue.
* @param request CreditApprovalRequest creditapprovalrequest
*
*/
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.
* @throws CreditApprovalException creditapproval
* @return CreditApproval
*/
private synchronized CreditApproval secureApprovalObject()
throws CreditApprovalException {
if (!availableObjectQueue.isEmpty()) {
CreditApproval approvalObject = (CreditApproval) availableObjectQueue.removeFirst();
reservedObjectQueue.addLast( 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.
* @param creditApprovalObject CreditApproval
* @throws CreditApprovalException creditapprovalexception
*/
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.removeFirst();
firstRequest.creditApprovalObject = creditApprovalObject;
return;
}
availableObjectQueue.addLast( creditApprovalObject );
reservedObjectQueue.remove( creditApprovalObject );
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -