poaimpl.java
来自「java jdk 1.4的源码」· Java 代码 · 共 1,329 行 · 第 1/3 页
JAVA
1,329 行
/* * @(#)POAImpl.java 1.88 03/01/23 * * Copyright 2003 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */package com.sun.corba.se.internal.POA;import org.omg.CORBA.*;import org.omg.CORBA.portable.*;import org.omg.PortableServer.*;import org.omg.PortableServer.POAPackage.*;import org.omg.PortableServer.ServantLocatorPackage.*;import com.sun.corba.se.internal.ior.IIOPProfileTemplate ;import com.sun.corba.se.internal.ior.ObjectKeyTemplate ;import com.sun.corba.se.internal.ior.POAObjectKeyTemplate ;import com.sun.corba.se.internal.ior.IORTemplate ;import com.sun.corba.se.internal.ior.ObjectId ;import com.sun.corba.se.internal.ior.IIOPAddress ;import com.sun.corba.se.internal.ior.IIOPAddressImpl ;import com.sun.corba.se.internal.ior.POAView ;import com.sun.corba.se.internal.ior.POAId ;import com.sun.corba.se.internal.ior.POAIdPOAView ;import org.omg.IOP.TAG_INTERNET_IOP ;import com.sun.corba.se.internal.core.*;import com.sun.corba.se.internal.corba.*;import com.sun.corba.se.internal.orbutil.ORBUtility; //d11638import com.sun.corba.se.internal.orbutil.ORBConstants; //d11638import java.util.*;/** * POAImpl is the implementation of the Portable Object Adapter. It * contains an implementation of the POA interfaces specified in * orbos/97-05-15. * */public class POAImpl extends org.omg.CORBA.LocalObject implements POA, POAView { protected POAORB orb; private int numLevels; private POAId poaId ; private String name; private POAManagerImpl manager; private POAImpl parent; private AdapterActivator activator; private ServantManager servantManager; private Servant defaultServant; private Policies policies; protected ActiveObjectMap activeObjectMap; protected Map children; protected int scid ; private Integer sysIdCounter = new Integer(0); private int nInvocations = 0; // pending invocations on this POA protected IORTemplate iortemp; protected byte[] adapterId ; // adapterActivatorCV has to be null // see its use in find_POA() and enter() private java.lang.Object adapterActivatorCV = null; // Wait on this CV for // AdapterActivator upcalls to complete private java.lang.Object invokeCV = new java.lang.Object(); // Wait on this CV for all active // invocations to complete // beingDestroyedCV has to be null // see its use in destroy() and enter() private java.lang.Object beingDestroyedCV = null; // Wait on this CV for the destroy // method to complete doing its work // thread local variable to store a boolean to detect deadlock in POA.destroy(). // Note: This doesn't cover the case where an application starts more than one // thread which call POA.destroy. This variable could be extended to maintain // the current POA the thread is destroying so that the destroy threads don't // get in the way of each other. protected ThreadLocal isDestroying = new ThreadLocal () { protected java.lang.Object initialValue() { return Boolean.FALSE; } }; private boolean destroyed = false; private boolean etherealizeFlag = false; // to check if the value has be // set already if destroy is called multiple times private boolean etherealizeValue = false; // to hold value from first call to // destroy, and this value is used for subsequent destroy calls // XXX temporary for timing: DO NOT RELEASE WITH THIS FLAG SET! private boolean normalState = false ; public POAImpl(String name, POAManagerImpl manager, Policies policies, POAImpl parent, AdapterActivator activator, POAORB orb) { this.policies = policies; this.name = name; this.manager = manager; this.parent = parent; this.activator = activator; this.orb = orb; pre_initialize(); if( parent != null ) { // If this is not the Root POA, notify POAORB that IORInterceptors // should be invoked now. orb.invokeIORInterceptors( this ); } post_initialize() ; } public POAImpl makePOA( String name, POAManagerImpl manager, Policies policies, POAImpl parent, AdapterActivator activator, POAORB orb ) { return new POAImpl( name, manager, policies, parent, activator, orb ) ; } protected void pre_initialize() { if (parent == null) numLevels = 0 ; else numLevels = parent.getNumLevels() + 1 ; if (policies.retainServants()) activeObjectMap = new ActiveObjectMap(policies.isMultipleIds()); children = new HashMap(); manager.addPOA(this); if (policies.isSingleThreaded()) throw new org.omg.CORBA.NO_IMPLEMENT( "Single threaded model not implemented"); // Construct the IORTemplate final POAORB poaorb = (POAORB)orb; int serverid; int port ; if ( policies.isTransient() ) { serverid = poaorb.getTransientServerId(); port = poaorb.getServerEndpoint().getPort() ; if (policies.servantCachingAllowed()) scid = ORBConstants.SCTransientSCID ; else scid = ORBConstants.TransientSCID ; } else { serverid = poaorb.getPersistentServerId(); port = poaorb.getPersistentServerPort(EndPoint.IIOP_CLEAR_TEXT); if (policies.servantCachingAllowed()) scid = ORBConstants.SCPersistentSCID ; else scid = ORBConstants.PersistentSCID ; } String host = poaorb.getServerEndpoint().getHostName() ; IIOPAddress addr = new IIOPAddressImpl( host, port ) ; // get the orb-id corresponding this ORB for putting in the Object Key String orbId = poaorb.getORBId(); poaId = new POAIdPOAView( this ) ; // Construct the objectKey ObjectKeyTemplate oktemp = new POAObjectKeyTemplate( scid, serverid, orbId, poaId ) ; adapterId = oktemp.getAdapterId( orb ) ; GIOPVersion version = orb.getGIOPVersion() ; IIOPProfileTemplate temp = new StandardIIOPProfileTemplate( addr, version.major, version.minor, oktemp, null, orb ) ; iortemp = new IORTemplate() ; iortemp.add( temp ) ; } protected void post_initialize() { iortemp.makeImmutable() ; } public final Policies getPolicies() { return policies; } protected synchronized boolean hasServantInMap(Servant servant){ if (activeObjectMap != null ) //Has retain policy set. return activeObjectMap.contains(servant); else //Has non-retain policy set. return false; } protected POA create_POA(String name, POAManagerImpl manager, Policies policies) throws AdapterAlreadyExists, InvalidPolicy { if (children.get(name) != null) adapterAlreadyExists(); if (manager == null) manager = new POAManagerImpl(orb); POAImpl child = makePOA(name, manager, policies, this, null, orb); children.put(name, child); return child; } protected void removeChild(String name) { children.remove(name); } // XXX visible for ServantCachingPOAClientSC void enter() throws POADestroyed { if (destroyed) { // Avoid deadlock if this is the thread that is processing the POA.destroy // because this is the only thread that can notify waiters on beingDestroyedCV if (beingDestroyedCV != null && isDestroying.get() == Boolean.FALSE) { try { synchronized (beingDestroyedCV) { beingDestroyedCV.wait(); } } catch (InterruptedException ex) { } } throw new POADestroyed(); } if (parent != null && parent.adapterActivatorCV != null) { try { synchronized (parent.adapterActivatorCV) { parent.adapterActivatorCV.wait(); } } catch (InterruptedException ex) { } } synchronized (this) { nInvocations++; } manager.enter(); } // XXX visible for ServantCachingPOAClientSC void exit() { synchronized (this) { nInvocations--; if ((nInvocations == 0) && destroyed) { synchronized (invokeCV) { invokeCV.notifyAll(); } } } manager.exit(); } /** Called from the subcontract to get the servant to dispatch this * invocation to. The actual servant dispatch code is in the subcontract. */ public Servant getServant( byte[] id, CookieHolder cookieHolder, String operation, com.sun.corba.se.internal.core.ServerRequest serverRequest) throws ForwardRequest, POADestroyed { enter(); orb.getCurrent().addThreadInfo(this, id, cookieHolder, operation); // This must be set just after "enter" and the thread stack // has been pushed so if an exception occurs before this point then // the exception reply will not try to do returnServant. // serverRequest may be null in the rmi-iiop isLocal case. if (serverRequest != null) { serverRequest.setExecuteReturnServantInResponseConstructor(true); } Servant servant = internalGetServant(id, cookieHolder, operation); orb.getCurrent().setServant(servant); return servant; } /** Called from the subcontract to let this POA cleanup after an * invocation. Note: If getServant was called, then returnServant * MUST be called, even in the case of exceptions. This may be * called multiple times for a single request. */ public void returnServant() { if ( !policies.retainServants() && policies.useServantManager() && orb.getCurrent().preInvokeCalled() && !orb.getCurrent().postInvokeCalled()) { if (servantManager == null || ! (servantManager instanceof ServantLocator) ) { return; } ServantLocator locator = (ServantLocator)servantManager; POACurrent c = orb.getCurrent(); try { locator.postinvoke(c.getObjectId(), c.getPOA(), c.getOperation(), c.getCookieHolder().value, c.getServant()); } finally { orb.getCurrent().setPostInvokeCalled(); } } } /** Called in GenericPOAServerSC rmi-iiop isLocal case. */ public void returnServantAndRemoveThreadInfo() { try { returnServant(); } finally { removeThreadInfo(); } } /** Called from the subcontract to let this POA cleanup after an * invocation. Note: If getServant was called, then returnServant * MUST be called, even in the case of exceptions. This MUST * be called only once for a single request. */ public void removeThreadInfo() { exit(); orb.getCurrent().removeThreadInfo(); } private Servant internalGetServant(byte[] id, CookieHolder cookieHolder, String operation) throws ForwardRequest, POADestroyed { boolean isSpecial = SpecialMethod.isSpecialMethod(operation); if (policies.retainServants() && policies.useActiveMapOnly()) { // CHANGED (RAM J) 08/07/2000 raise OBJECT_NOT_EXIST exception // if servant not found in the activeObjectMap. Previously, // we were returning null, which is incorrect. //return activeObjectMap.get(id); Servant servant = activeObjectMap.get(id); if (servant == null) { if (! isSpecial) { objectNotExist(MinorCodes.NULL_SERVANT, CompletionStatus.COMPLETED_NO); } else { return null; } } return servant; } else if (policies.retainServants() && policies.useServantManager()) { Servant servant = activeObjectMap.get(id); if ( servant == null) { if (servantManager == null) objAdapter(MinorCodes.POA_NO_SERVANT_MANAGER, CompletionStatus.COMPLETED_NO); if (!(servantManager instanceof ServantActivator)) objAdapter(MinorCodes.POA_BAD_SERVANT_MANAGER, CompletionStatus.COMPLETED_NO); synchronized (servantManager) { // Check again if the servant exists in activeObjectMap: // this is necessary because two threads could call // the first activeObjectMap.get(id) simultaneously // and get nulls returned. We cant hold the // activeObjectMap lock during the incarnate call // because incarnate may take too long. servant = activeObjectMap.get(id); if (servant == null) { ServantActivator activator = (ServantActivator) servantManager; servant = activator.incarnate(id, this); if (servant == null) { if (! isSpecial) { throw new OBJ_ADAPTER(MinorCodes.NULL_SERVANT, CompletionStatus.COMPLETED_NO); } else { return null; } } // here check for unique_id policy, and if the servant // is already registered, for a different ID, then throw // OBJ_ADAPTER exception, else activate it. Section 11.3.5.1 // 99-10-07.pdf if (policies.isUniqueIds()) { // check if the servant already is associated with some // id if (activeObjectMap.contains(servant)) { objAdapter(MinorCodes.POA_SERVANT_NOT_UNIQUE, CompletionStatus.COMPLETED_NO); } } activate(id, servant); } } } return servant; } else if (policies.retainServants() && policies.useDefaultServant()) { Servant servant = activeObjectMap.get(id); if (servant != null) return servant; else if (defaultServant != null) return defaultServant; else objAdapter(MinorCodes.POA_NO_DEFAULT_SERVANT, CompletionStatus.COMPLETED_NO); } else if (!policies.retainServants() && policies.useServantManager())
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?