Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
   *
   * Copyright (c) 1997-2012 Oracle and/or its affiliates. All rights reserved.
   *
   * The contents of this file are subject to the terms of either the GNU
   * General Public License Version 2 only ("GPL") or the Common Development
   * and Distribution License("CDDL") (collectively, the "License").  You
   * may not use this file except in compliance with the License.  You can
  * obtain a copy of the License at
  * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
  * or packager/legal/LICENSE.txt.  See the License for the specific
  * language governing permissions and limitations under the License.
  *
  * When distributing the software, include this License Header Notice in each
  * file and include the License file at packager/legal/LICENSE.txt.
  *
  * GPL Classpath Exception:
  * Oracle designates this particular file as subject to the "Classpath"
  * exception as provided by Oracle in the GPL Version 2 section of the License
  * file that accompanied this code.
  *
  * Modifications:
  * If applicable, add the following below the License Header, with the fields
  * enclosed by brackets [] replaced by your own identifying information:
  * "Portions Copyright [year] [name of copyright owner]"
  *
  * Contributor(s):
  * If you wish your version of this file to be governed by only the CDDL or
  * only the GPL Version 2, indicate your decision by adding "[Contributor]
  * elects to include this software in this distribution under the [CDDL or GPL
  * Version 2] license."  If you don't indicate a single choice of license, a
  * recipient has the option to distribute your version of this file under
  * either the CDDL, the GPL Version 2 or to extend the choice of license to
  * its licensees as provided above.  However, if you add GPL Version 2 code
  * and therefore, elected the GPL Version 2 license, then the option applies
  * only if the new code is made subject to such option by the copyright
  * holder.
  */
 
 package com.sun.enterprise.connectors;
 
 
 import  javax.resource.ResourceException;
 import  javax.resource.spi.*;
 import  javax.resource.spi.IllegalStateException;
 import java.util.Set;

Author(s):
Tony Ng
 
 public class ConnectionManagerImpl implements ConnectionManagerSerializable {
 
     protected String jndiName;
     protected String logicalName;
     protected PoolInfo poolInfo;
     protected ResourceInfo resourceInfo;
 
     private volatile static Logger logger = LogDomains.getLogger(
             ConnectionManagerImpl.class,.);
     private volatile static StringManager localStrings = StringManager.getManager(
             ConnectionManagerImpl.class);
 
     protected String rarName;
 
     private transient BindableResource resourceConfiguration;
 
     protected ResourcePrincipal defaultPrin = null;
 
    public ConnectionManagerImpl(PoolInfo poolInfoResourceInfo resourceInfo) {
        this. = poolInfo;
        this. = resourceInfo;
    }
    public void setJndiName(String jndiName) {
        this. = jndiName;
    }
    public String getJndiName() {
        return ;
    }
    public void setLogicalName(String logicalName) {
        this. = logicalName;
    }
    public String getLogicalName() {
        return ;
    }
/*
    public void setPoolInfo(PoolInfo poolInfo) {
        this.poolInfo = poolInfo;
    }
*/

    
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.

    public Object allocateNonTxConnection(ManagedConnectionFactory mcf,
                                          ConnectionRequestInfo cxRequestInfothrows ResourceException {
        String localJndiName = ;
        logFine("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.
            localJndiName =  + .;
            logFine("Adding __nontx to jndiname");
        } else {
            logFine("lookup happened from a __nontx datasource directly");
        }
        return allocateConnection(mcfcxRequestInfolocalJndiName);
    }
    public Object allocateConnection(ManagedConnectionFactory mcf, ConnectionRequestInfo cxRequestInfo)
            throws ResourceException {
        return this.allocateConnection(mcfcxRequestInfo);
    }
    public Object allocateConnection(ManagedConnectionFactory mcf, ConnectionRequestInfo cxRequestInfo,
                                     String jndiNameToUsethrows ResourceException {
        return this.allocateConnection(mcfcxRequestInfojndiNameToUsenull);
    }
    public Object allocateConnection(ManagedConnectionFactory mcf,
                                     ConnectionRequestInfo cxRequestInfoString jndiNameToUseObject conn)
            throws ResourceException {
        validateResourceAndPool();
        PoolManager poolmgr = ConnectorRuntime.getRuntime().getPoolManager();
        boolean resourceShareable = true;
        ResourceReferenceDescriptor ref =  poolmgr.getResourceReference(jndiNameToUse);
        if (ref != null) {
            String shareableStr = ref.getSharingScope();
            if (shareableStr.equals(ref.RESOURCE_UNSHAREABLE)) {
                resourceShareable = false;
            }
        }
        //TODO V3 refactor all the 3 cases viz, no res-ref, app-auth, cont-auth.
        if (ref == null) {
            if(getLogger().isLoggable(.)) {
                getLogger().log(."poolmgr.no_resource_reference"jndiNameToUse);
            }
            return internalGetConnection(mcfcxRequestInfo,
                    resourceShareablejndiNameToUseconntrue);
        }
        String auth = ref.getAuthorization();
            if (cxRequestInfo == null) {
                String msg = getLocalStrings().getString("con_mgr.null_userpass");
                throw new ResourceException(msg);
            }
            ConnectorRuntime.getRuntime().switchOnMatching();
            return internalGetConnection(mcfnullcxRequestInfo,
                    resourceShareablejndiNameToUseconnfalse);
        } else {
            ResourcePrincipal prin = null;
            Set principalSet = null;
            Principal callerPrincipal = null;
            SecurityContext securityContext = null;
            ConnectorRuntime connectorRuntime = ConnectorRuntime.getRuntime();
            //TODO V3 is SecurityContext.getCurrent() the right way ? Does it need to be injected ?
            if (connectorRuntime.isServer() &&
                    (securityContext = SecurityContext.getCurrent()) != null &&
                    (callerPrincipal = securityContext.getCallerPrincipal()) != null &&
                    (principalSet = securityContext.getPrincipalSet()) != null) {
                AuthenticationService authService =
                        connectorRuntime.getAuthenticationService();
                if (authService != null) {
                    prin = (ResourcePrincipalauthService.mapPrincipal(
                            callerPrincipalprincipalSet);
                }
            }
            if (prin == null) {
                prin = ref.getResourcePrincipal();
                if (prin == null) {
                    if (getLogger().isLoggable(.)) {
                        getLogger().log(."default-resource-principal not"
                                + "specified for " + jndiNameToUse + ". Defaulting to"
                                + " user/password specified in the pool");
                    }
                    prin = ;
                } else if (!prin.equals()) {
                    ConnectorRuntime.getRuntime().switchOnMatching();
                }
            }
            return internalGetConnection(mcfprincxRequestInfo,
                    resourceShareablejndiNameToUseconnfalse);
        }
    }
    protected Object internalGetConnection(ManagedConnectionFactory mcf,
                                           final ResourcePrincipal prin, ConnectionRequestInfo cxRequestInfo,
                                           boolean shareableString jndiNameToUseObject connboolean isUnknownAuth)
            throws ResourceException {
        try {
            PoolManager poolmgr = ConnectorRuntime.getRuntime().getPoolManager();
            ConnectorRegistry registry = ConnectorRegistry.getInstance();
            PoolMetaData pmd = registry.getPoolMetaData();
            ResourceSpec spec = new ResourceSpec(jndiNameToUse,
                    .pmd);
            spec.setPoolInfo(this.);
            ManagedConnectionFactory freshMCF = pmd.getMCF();
            if (getLogger().isLoggable(.)) {
                if (!freshMCF.equals(mcf)) {
                    getLogger().info("conmgr.mcf_not_equal");
                }
            }
            ConnectorDescriptor desc = registry.getDescriptor();
            Subject subject = null;
            ClientSecurityInfo info = null;
            boolean subjectDefined = false;
            if (isUnknownAuth && .equals(.)
                    && !(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(mcfnull);
            } else {
                if (prin == null) {
                    info = new ClientSecurityInfo(cxRequestInfo);
                } else {
                    info = new ClientSecurityInfo(prin);
                    if (prin.equals()) {
                        subject = pmd.getSubject();
                    } else {
                        subject = ConnectionPoolObjectsUtils.createSubject(mcfprin);
                    }
                }
            }
            int txLevel = pmd.getTransactionSupport();
            if (getLogger().isLoggable(.)) {
                logFine("ConnectionMgr: poolName " +  +
                        "  txLevel : " + txLevel);
            }
             if ( conn != null ) {
                 spec.setConnectionToAssociateconn );
             }
            return getResource(txLevelpoolmgrmcfspecsubjectcxRequestInfoinfodescshareable);
        } catch (PoolingException ex) {
            Object[] params = new Object[]{ex};
            getLogger().log(."poolmgr.get_connection_failure"params);
            String i18nMsg = getLocalStrings().getString("con_mgr.error_creating_connection"ex.getMessage());
            ResourceAllocationException rae = new ResourceAllocationException(i18nMsg);
            rae.initCause(ex);
            throw rae;
        }
    }
    private Object getResource(int txLevelPoolManager poolmgr, ManagedConnectionFactory mcfResourceSpec spec,
                               Subject subject, ConnectionRequestInfo cxRequestInfoClientSecurityInfo info,
                               ConnectorDescriptor descboolean shareable)
            throws PoolingException, ResourceAllocationException, IllegalStateException, RetryableUnavailableException {
        ResourceAllocator alloc;
        switch (txLevel) {
            case .:
                alloc = new NoTxConnectorAllocator(poolmgrmcfspecsubjectcxRequestInfoinfodesc);
                break;
            case .:
                alloc = new LocalTxConnectorAllocator(poolmgrmcfspecsubjectcxRequestInfoinfodescshareable);
                break;
            case .:
                if (.equals(.)) {
                    shareable = false;
                }
                spec.markAsXA();
                alloc = new ConnectorAllocator(poolmgrmcfspecsubjectcxRequestInfoinfodescshareable);
                return poolmgr.getResource(specallocinfo);
            
            default:
                String i18nMsg = getLocalStrings().getString("con_mgr.illegal_tx_level"txLevel + " ");
                throw new IllegalStateException(i18nMsg);
        }
        return poolmgr.getResource(specallocinfo);
    }
    public void setRarName(String _rarName) {
         = _rarName;
    }
    public String getRarName() {
        return ;
    }
    /*
    * This method is called from the ConnectorObjectFactory lookup
    * With this we move all the housekeeping work in allocateConnection
    * up-front 
    */
    public void initialize() throws ConnectorRuntimeException {
        ConnectorRuntime runtime = ConnectorRuntime.getRuntime();
        if(runtime.isNonACCRuntime()){
             = ConnectorsUtil.getPMJndiName();
        }
        ConnectorRegistry registry = ConnectorRegistry.getInstance();
        PoolMetaData pmd = registry.getPoolMetaData();
         = pmd.getResourcePrincipal();
    }
    private void validateResourceAndPool() throws ResourceException {
        ResourceInfo resourceInfo = this.;
        ResourcesUtil resourcesUtil = ResourcesUtil.createInstance();
        ConnectorRuntime runtime = ConnectorRuntime.getRuntime();
        ConnectorRegistry registry = ConnectorRegistry.getInstance();
        // adding a perf. optimization check so that "config-bean" is not accessed at all for
        // cases where the resource is enabled (deployed). Only for cases where resource
        // is not available, we look further and determine whether resource/resource-ref
        // are disabled.
        if (!registry.isResourceDeployed(resourceInfo)) {
            if(.isLoggable(.)){
                .log(.,"resourceInfo not found in connector-registry : " + resourceInfo);
            }
            boolean isDefaultResource = false;
            boolean isSunRAResource = false;
            ConnectorDescriptor descriptor = registry.getDescriptor();
            if (descriptor != null) {
                isDefaultResource = descriptor.getDefaultResourcesNames().contains(resourceInfo.getName());
                if (descriptor.getSunDescriptor() != null) {
                    com.sun.enterprise.deployment.runtime.connector.ResourceAdapter rar =
                            descriptor.getSunDescriptor().getResourceAdapter();
                    if (rar != null) {
                        String sunRAJndiName = (String)
                                rar.getValue(.......);
                        isSunRAResource = resourceInfo.getName().equals(sunRAJndiName);
                    }
                }
            }
            if ((runtime.isServer() || runtime.isEmbedded()) &&
                    (!resourceInfo.getName().contains(.) &&
                            (!isDefaultResource) && (!isSunRAResource))) {
                // performance optimization so that resource configuration is not retrieved from
                // resources config bean each time.
                if ( == null) {
                     =
                            (BindableResourceresourcesUtil.getResource(resourceInfoBindableResource.class);
                    if ( == null) {
                        String suffix = ConnectorsUtil.getValidSuffix(resourceInfo.getName());
                        // it is possible that the resource is a __PM or __NONTX suffixed resource used by JPA/EJB Container
                        // check for the enabled status and existence using non-prefixed resource-name
                        if (suffix != null) {
                            String nonPrefixedName = resourceInfo.getName().substring(0, resourceInfo.getName().lastIndexOf(suffix));
                            resourceInfo = new ResourceInfo(nonPrefixedNameresourceInfo.getApplicationName(),
                                    resourceInfo.getModuleName());
                             = (BindableResource)
                                    resourcesUtil.getResource(resourceInfoBindableResource.class);
                        }
                    }
                } else {
                    // we cache the resourceConfiguration for performance optimization.
                    // make sure that appropriate (actual) resourceInfo is used for validation.
                    String suffix = ConnectorsUtil.getValidSuffix(resourceInfo.getName());
                    // it is possible that the resource is a __PM or __NONTX suffixed resource used by JPA/EJB Container
                    // check for the enabled status and existence using non-prefixed resource-name
                    if (suffix != null) {
                        String nonPrefixedName = resourceInfo.getName().substring(0, resourceInfo.getName().lastIndexOf(suffix));
                        resourceInfo = new ResourceInfo(nonPrefixedNameresourceInfo.getApplicationName(),
                                resourceInfo.getModuleName());
                    }
                }
                if ( == null) {
                    throw new ResourceException("No such resource : " + resourceInfo);
                }
                if (!resourcesUtil.isEnabled(resourceInfo)) {
                    throw new ResourceException(resourceInfo + " is not enabled");
                }
            }
        }
        if (registry.getPoolMetaData() == null) {
            String msg = getLocalStrings().getString("con_mgr.no_pool_meta_data");
            throw new ResourceException( + ": " + msg);
        }
    }
    public void logFine(String message) {
        if (getLogger().isLoggable(.)) {
            getLogger().fine(message);
        }
    }
    private static StringManager getLocalStrings() {
        if ( == null) {
            synchronized (ConnectionManagerImpl.class) {
                if ( == null) {
                     = StringManager.getManager(ConnectionManagerImpl.class);
                }
            }
        }
        return ;
    }
    protected static Logger getLogger() {
        if ( == null){
            synchronized(ConnectionManagerImpl.class) {
                if( == null) {
                     = LogDomains.getLogger(ConnectionManagerImpl.class,.);
                }
            }
        }
        return ;
    }
New to GrepCode? Check out our FAQ X