FileDocCategorySizeDatePackage
ConnectionManagerImpl.javaAPI DocGlassfish v2 API17218Fri Jun 01 05:56:50 BST 2007com.sun.enterprise.connectors

ConnectionManagerImpl

public class ConnectionManagerImpl extends Object implements Serializable, ConnectionManager
author
Tony Ng

Fields Summary
protected String
jndiName
protected String
poolName
static Logger
_logger
protected String
rarName
protected com.sun.enterprise.deployment.ResourcePrincipal
defaultPrin
Constructors Summary
public ConnectionManagerImpl(String poolName)


       
        this.poolName = poolName;
    
Methods Summary
public java.lang.ObjectallocateConnection(ManagedConnectionFactory mcf, ConnectionRequestInfo cxRequestInfo)


        return this.allocateConnection( mcf, cxRequestInfo, jndiName); 
    
public java.lang.ObjectallocateConnection(ManagedConnectionFactory mcf, ConnectionRequestInfo cxRequestInfo, java.lang.String jndiNameToUse)

        return this.allocateConnection( mcf, cxRequestInfo, jndiNameToUse, null );
    
public java.lang.ObjectallocateConnection(ManagedConnectionFactory mcf, ConnectionRequestInfo cxRequestInfo, java.lang.String jndiNameToUse, java.lang.Object conn)

        validatePool();
        PoolManager poolmgr = Switch.getSwitch().getPoolManager();
	boolean resourceShareable = true;

	ResourceReferenceDescriptor ref =  poolmgr.getResourceReference(jndiNameToUse);
	if (ref != null) {
	    String shareableStr = ref.getSharingScope();

	    // MQ resource adapter doesnt support connection sharing. Disabling it
	    // for the moment.
            if (shareableStr.equals(ref.RESOURCE_UNSHAREABLE)) {
                resourceShareable = false;
            }
        }

        if (ref == null) {
            _logger.log(Level.FINE,"poolmgr.no_resource_reference",jndiNameToUse);
            return internalGetConnection(mcf, defaultPrin, cxRequestInfo, 
                resourceShareable, jndiNameToUse, conn, true);
        }
        String auth = ref.getAuthorization();
	
        if (auth.equals(ref.APPLICATION_AUTHORIZATION) ) {
	    if (cxRequestInfo == null ) {
                StringManager localStrings =
                    StringManager.getManager(ConnectionManagerImpl.class);
	        String msg = localStrings.getString(
		    "con_mgr.null_userpass");
	        throw new ResourceException( msg );
	    }
            ConnectorRuntime.getRuntime().switchOnMatching(rarName, poolName);
            return internalGetConnection(mcf, null, cxRequestInfo,
                resourceShareable, jndiNameToUse, conn, false );
        } else {
            ResourcePrincipal prin =null;
            Set principalSet =null;
            Principal callerPrincipal = null;
            SecurityContext securityContext = null;
            ConnectorRuntime connectorRuntime = ConnectorRuntime.getRuntime();
            if(connectorRuntime.isServer() && 
             (securityContext = SecurityContext.getCurrent()) != null &&
	     (callerPrincipal = securityContext.getCallerPrincipal()) != null &&
	     (principalSet = securityContext.getPrincipalSet()) != null) {
                AuthenticationService authService = 
                    connectorRuntime.getAuthenticationService(rarName,poolName);
                if(authService != null) {
                    prin = (ResourcePrincipal)authService.mapPrincipal(
                            callerPrincipal, principalSet);
                }
            }
            
            if (prin == null) { 
                prin = ref.getResourcePrincipal();
                if (prin == null) {
                    _logger.log(Level.FINE,"default-resource-principal not" +
		                "specified for " + jndiNameToUse + ". Defaulting to" +
				" user/password specified in the pool");

                    prin = defaultPrin;
                } else if (!prin.equals(defaultPrin)){
                    ConnectorRuntime.getRuntime().switchOnMatching(rarName, poolName);
                }
            }
            return internalGetConnection(mcf, prin, cxRequestInfo,
                resourceShareable, jndiNameToUse, conn, false);
        }
   
    
public java.lang.ObjectallocateNonTxConnection(ManagedConnectionFactory mcf, ConnectionRequestInfo cxRequestInfo)
Allocate a non transactional connection. This connection, even if acquired in the context of an existing transaction, will never be associated with a transaction The typical use case may be to check the original contents of an EIS when a transacted connection is changing the contents, and the tx is yet to be committed. We create a ResourceSpec for a non tx connection with a name ending in __nontx. This is to maintain uniformity with the scheme of having __pm connections. If one were to create a resource with a jndiName ending with __nontx the same functionality might be achieved.

 
        String localJndiName = jndiName;

	if (_logger.isLoggable(Level.FINE)) {
	    _logger.fine("Allocating NonTxConnection");
	}

	//If a resource has been created with __nontx, we don't want to
	//add it again.
	//Otherwise we need to add __nontx at the end to ensure that the
	//mechanism to check for the correct resource manager still works
	//We do the addition if and only if we are getting this call
	//from a normal datasource and not a __nontx datasource.
        if ( ! jndiName.endsWith( ConnectorConstants.NON_TX_JNDI_SUFFIX ) ) {
	    localJndiName = jndiName + ConnectorConstants.NON_TX_JNDI_SUFFIX ;
	    
	    if (_logger.isLoggable(Level.FINE)) {
	        _logger.fine("Adding __nontx to jndiname");
	    }
	} else {
	    if (_logger.isLoggable(Level.FINE)) {
	        _logger.fine("lookup happened from a __nontx datasource directly");
	    }
	}

        return allocateConnection( mcf, cxRequestInfo, localJndiName);


    
public java.lang.StringgetJndiName()

        return jndiName;
    
public java.lang.StringgetRarName()

        return rarName;
    
public voidinitialize()

    
        Switch sw = Switch.getSwitch();
	if (sw.getContainerType() == ConnectorConstants.NON_ACC_CLIENT) { 
            //Invocation from a non-ACC client
	    try {
	        // Initialize Transaction service. By now the ORB must have
		// been created during the new InitialContext() call using
		// the ORBManager
		PEORBConfigurator.initTransactionService(null, new Properties() );

	        if (sw.getTransactionManager() == null) { 
		    sw.setTransactionManager(new J2EETransactionManagerOpt());
		}
                // There wont be any invocation manager. So, treat this as a system 
	        // resource.
	        jndiName = ResourceInstaller.getPMJndiName(jndiName);
	    } catch(Exception e) {
	        throw (ConnectorRuntimeException) 
                    new ConnectorRuntimeException(e.getMessage()).initCause(e);
	    }
	}
        
        ConnectorRuntime runtime = ConnectorRuntime.getRuntime();
        ManagedConnectionFactory mcf = runtime.obtainManagedConnectionFactory(poolName);
	ConnectorRegistry registry = ConnectorRegistry.getInstance();
        PoolMetaData pmd = registry.getPoolMetaData( poolName );
        defaultPrin = pmd.getResourcePrincipal();

    
protected java.lang.ObjectinternalGetConnection(ManagedConnectionFactory mcf, com.sun.enterprise.deployment.ResourcePrincipal prin, ConnectionRequestInfo cxRequestInfo, boolean shareable, java.lang.String jndiNameToUse, java.lang.Object conn, boolean isUnknownAuth)

        try {
            PoolManager poolmgr = Switch.getSwitch().getPoolManager();
	    ConnectorRegistry registry = ConnectorRegistry.getInstance();
            PoolMetaData pmd = registry.getPoolMetaData( poolName );
            
            ResourceSpec spec = new ResourceSpec(jndiNameToUse, 
                    ResourceSpec.JNDI_NAME, pmd);
	    spec.setConnectionPoolName(this.poolName);
	    ManagedConnectionFactory freshMCF = pmd.getMCF();

        if(_logger.isLoggable(Level.INFO)){
            if (! freshMCF.equals(mcf)) {
                _logger.info("conmgr.mcf_not_equal");
            }
        }
            ConnectorDescriptor desc = registry.getDescriptor(rarName);

            Subject subject = null;
            ClientSecurityInfo info = null;
            boolean subjectDefined = false;
            if (isUnknownAuth && rarName.equals(ConnectorConstants.DEFAULT_JMS_ADAPTER)
                  && !(pmd.isAuthCredentialsDefinedInPool())) {
                 //System.out.println("Unkown Auth - pobably nonACC client");
                 //Unknown authorization. This is the case for standalone java clients,
                 //where the authorization is neither container nor component
                 //managed. In this case we associate an non-null Subject with no
                 //credentials, so that the RA can either use its own custom logic
                 //for figuring out the credentials. Relevant connector spec section
                 //is 9.1.8.2.
                 //create non-null Subject associated with no credentials
                 //System.out.println("RAR name "+ rarName);
                subject = ConnectionPoolObjectsUtils.createSubject(mcf, null);
            } else {
                if (prin == null) {
                    info = new ClientSecurityInfo(cxRequestInfo);
                } else {
                    info = new ClientSecurityInfo(prin);
                    //If subject is defined, don't attempt to redefine it
                    if (subject == null) {
                        if (prin.equals(defaultPrin)) {
                           subject = pmd.getSubject();
                        } else {
                           subject = ConnectionPoolObjectsUtils.createSubject(mcf, prin);
                        }
                    }
                }
            }

	    int txLevel = pmd.getTransactionSupport();
	    if (_logger.isLoggable(Level.FINE)) {
	        _logger.fine( "ConnectionMgr: poolName " + poolName +
		    "  txLevel : " + txLevel );
	    }
            
            ResourceAllocator alloc = null;
            if ( conn != null ) {
                spec.setConnectionToAssociate( conn );
            }
            
            switch (txLevel) {
            
            case ConnectorConstants.NO_TRANSACTION_INT:
                alloc =
                    new NoTxConnectorAllocator(poolmgr, mcf, spec,
                                               subject, cxRequestInfo, info,
                                               desc);
                return poolmgr.getResource(spec, alloc, info);
                
            case ConnectorConstants.LOCAL_TRANSACTION_INT:
                if (!shareable) {
                    StringManager localStrings =
                        StringManager.getManager(ConnectionManagerImpl.class);
		    String i18nMsg = localStrings.getString(
		        "con_mgr.resource_not_shareable");
		    throw new ResourceAllocationException( i18nMsg );	
                }
                alloc = 
                    new LocalTxConnectorAllocator(poolmgr, mcf, spec,
                                                  subject, cxRequestInfo,
                                                  info, desc);
                return poolmgr.getResource(spec, alloc, info);
            case ConnectorConstants.XA_TRANSACTION_INT:
                if (rarName.equals(ConnectorRuntime.DEFAULT_JMS_ADAPTER)) {
                    shareable = false;
                }
		spec.markAsXA();
                alloc = 
                    new ConnectorAllocator(poolmgr, mcf, spec, 
                                           subject, cxRequestInfo, info, desc,
                                           shareable);
                return poolmgr.getResource(spec, alloc, info);
            default:
                StringManager localStrings =
                    StringManager.getManager(ConnectionManagerImpl.class);
	        String i18nMsg = localStrings.getString(
		    "con_mgr.illegal_tx_level",  txLevel+ " ");
                throw new IllegalStateException(i18nMsg);
            }

        } catch (PoolingException ex) {
             Object[]  params = new Object[]{poolName, ex.getMessage()};
            _logger.log(Level.WARNING,"poolmgr.get_connection_failure",params);
            StringManager localStrings =
                StringManager.getManager(ConnectionManagerImpl.class);
            String i18nMsg = localStrings.getString( 
	        "con_mgr.error_creating_connection", ex.getMessage() );
            ResourceAllocationException rae = new ResourceAllocationException(
	        i18nMsg );
	    rae.initCause( ex );
            throw rae;
        }
    
public voidsetJndiName(java.lang.String jndiName)

        this.jndiName = jndiName;
    
public voidsetPoolName(java.lang.String poolName)

        this.poolName = poolName;
    
public voidsetRarName(java.lang.String _rarName)

        rarName = _rarName;
    
private voidvalidatePool()

        ConnectorRegistry registry = ConnectorRegistry.getInstance();
        if (registry.getPoolMetaData(poolName) == null){
            StringManager localStrings = 
                StringManager.getManager(ConnectionManagerImpl.class);
            String msg = localStrings.getString("con_mgr.no_pool_meta_data");
            throw new ResourceException(poolName + ": " + msg);
            }