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.spi.ManagedConnectionFactory;
 import java.util.Set;
This class represents the abstraction of a 1.0 compliant rar. It holds the ra.xml (connector decriptor) values, class loader used to to load the Resource adapter class and managed connection factory and module name (rar) to which it belongs. It is also the base class for ActiveOutboundResourceAdapter(a 1.5 compliant outbound rar).

Author(s):
Srikanth P, Binod PG
 
 
 public class ActiveResourceAdapterImpl implements ActiveResourceAdapter {
 
     protected ConnectorDescriptor desc_;
     protected String moduleName_;
     protected ClassLoader jcl_;
     protected ConnectorRuntime connectorRuntime_ = null;
 
     private static Logger _logger = LogDomains.getLogger(ActiveResourceAdapterImpl.class.);
     private StringManager localStrings =
             StringManager.getManager(ActiveResourceAdapterImpl.class);

    
Constructor.

Parameters:
desc Connector Descriptor. Holds the all ra.xml values
moduleName Name of the module i.e rar Name. Incase of embedded resource adapters its name will be appName#rarName
jcl Classloader used to load the ResourceAdapter and managed connection factory class. values to domain.xml.
    public void init(javax.resource.spi.ResourceAdapter raConnectorDescriptor desc,
                                         String moduleNameClassLoader jclthrows ConnectorRuntimeException {
        this. = desc;
         = moduleName;
         = jcl;
         = ConnectorRuntime.getRuntime();
         = ConnectorDDTransformUtils.getConnectionDefs();
    }
    public ActiveResourceAdapterImpl(){
    }


    
    public String getModuleName() {
        return ;
    }

    
It initializes the resource adapter. It also creates the default pools and resources of all the connection definitions.

Throws:
ConnectorRuntimeException This exception is thrown if the ra.xml is invalid or the default pools and resources couldn't be created
    public void setup() throws ConnectorRuntimeException {
        if ( == null || . != 1) {
            .log(."rardeployment.invalid_connector_desc");
            String i18nMsg = .getString("ccp_adm.invalid_connector_desc");
            throw new ConnectorRuntimeException(i18nMsg);
        }
        if (isServer() && !isSystemRar()) {
            createAllConnectorResources();
        }
        if(.isLoggable(.)) {
            .log(."Completed Active Resource adapter setup");
        }
    }

    
Check if the execution environment is appserver runtime or application client container.

Returns:
boolean if the environment is appserver runtime
    protected boolean isServer() {
        if (.isServer()) {
            return true;
        } else {
            return false;
        }
    }

    
Creates both the default connector connection pools and resources

Throws:
ConnectorRuntimeException when unable to create resources
    protected void createAllConnectorResources() throws ConnectorRuntimeException {
        try {
            if (.getSunDescriptor() != null && .getSunDescriptor().getResourceAdapter() != null) {
                // sun-ra.xml exists
                String jndiName = (String.getSunDescriptor().
                        getResourceAdapter().getValue(.);
                if (jndiName == null || jndiName.equals("")) {
                    // jndiName is empty, do not create duplicate pools, use setting in sun-ra.xml
                    createDefaultConnectorConnectionPools(true);
                } else {
                    // jndiName is not empty, so create duplicate pools, both default and sun-ra.xml
                    createSunRAConnectionPool();
                    createDefaultConnectorConnectionPools(false);
                }
            } else {
                // sun-ra.xml doesn't exist, so create default pools
                createDefaultConnectorConnectionPools(false);
            }
            // always create default connector resources
            createDefaultConnectorResources();
        } catch (ConnectorRuntimeException cre) {
            //Connector deployment should _not_ fail if default connector
            //connector pool and resource creation fails.
            .log(."rardeployment.defaultpoolresourcecreation.failed"cre);
            if(.isLoggable(.)) {
                .log(."Error while trying to create the default connector" +
                    "connection pool and resource"cre);
            }
        } catch (Exception e) {
            //Connector deployment should _not_ fail if default connector
            //connector pool and resource creation fails.
            .log(."rardeployment.defaultpoolresourcecreation.failed"e);
            if(.isLoggable(.)) {
                .log(."Error while trying to create the default connector" +
                    "connection pool and resource"e);
            }
        }
    }

    
Deletes both the default connector connection pools and resources
    protected void destroyAllConnectorResources() {
        if (!(isSystemRar())) {
            deleteDefaultConnectorResources();
            deleteDefaultConnectorConnectionPools();
            // Added to ensure clean-up of the Sun RA connection pool
            if (.getSunDescriptor() != null &&
                    .getSunDescriptor().getResourceAdapter() != null) {
                // sun-ra.xml exists
                String jndiName = (String.getSunDescriptor().
                        getResourceAdapter().getValue(.);
                if (jndiName == null || jndiName.equals("")) {
                    // jndiName is empty, sunRA pool not created, so don't need to delete
                } else {
                    // jndiName is not empty, need to delete pool
                    deleteSunRAConnectionPool();
                }
            }
        }
    }
    protected boolean isSystemRar(String moduleName) {
        return ConnectorsUtil.belongsToSystemRA(moduleName);
    }

    
Deletes the default connector connection pools.
    protected void deleteDefaultConnectorConnectionPools() {
        for (ConnectionDefDescriptor aConnectionDefs_ : ) {
            String connectionDefName = aConnectionDefs_.getConnectionFactoryIntf();
            String resourceJndiName = .getDefaultPoolName(connectionDefName);
            try {
                PoolInfo poolInfo = new PoolInfo(resourceJndiName);
                .deleteConnectorConnectionPool(poolInfo);
            } catch (ConnectorRuntimeException cre) {
                .log(."rar.undeployment.default_pool_delete_fail"resourceJndiName);
            }
        }
    }

    
Deletes the default connector resources.
    protected void deleteDefaultConnectorResources() {
        for (ConnectionDefDescriptor aConnectionDefs_ : ) {
            String connectionDefName = aConnectionDefs_.getConnectionFactoryIntf();
            String resourceJndiName = .getDefaultResourceName(connectionDefName);
            try {
                ResourceInfo resourceInfo = new ResourceInfo(resourceJndiName);
                .deleteConnectorResource(resourceInfo);
            } catch (ConnectorRuntimeException cre) {
                .log(."rar.undeployment.default_resource_delete_fail"resourceJndiName);
                if(.isLoggable(.)) {
                    .log(."Error while trying to delete the default connector resource"cre);
                }
            }
        }
    }

    
uninitializes the resource adapter. It also destroys the default pools and resources
    public void destroy() {
        if (isServer()) {
            destroyAllConnectorResources();
        }
    }

    
Returns the Connector descriptor which represents/holds ra.xml

Returns:
ConnectorDescriptor Representation of ra.xml.
        return ;
    }

    
    public boolean handles(ConnectorDescriptor cdString moduleName) {
        boolean canHandle = false;
        boolean adminObjectsDefined = false;
        Set adminObjects = cd.getAdminObjects();
        if (adminObjects != null && adminObjects.size() > 0) {
            adminObjectsDefined = true;
        }
        /*
        this class can handle Connector 1.0 Spec. compliant RAR
        criteria for 1.0 RAR :
          * No inbound artifacts
          * No admin-objects
          * There should be only one connection-definition
          * RA Class should not be present (equivalent to "")
        */
        if(!cd.getInBoundDefined() && !adminObjectsDefined &&
                (cd.getOutBoundDefined() && cd.getOutboundResourceAdapter().getConnectionDefs().size() < 2
                        && "".equals(cd.getResourceAdapterClass()))
                ){
            canHandle = true;
        }
        return canHandle;
    }

    
    public ManagedConnectionFactory[] createManagedConnectionFactories(
            ConnectorConnectionPool ccpClassLoader jcl) {
        throw new UnsupportedOperationException("This operation is not supported");
    }


    
Creates managed Connection factory instance.

Parameters:
ccp Connector connection pool which contains the pool properties and ra.xml values pertaining to managed connection factory class. These values are used in MCF creation.
jcl Classloader used to managed connection factory class.
Returns:
ManagedConnectionFactory created managed connection factory instance
    public ManagedConnectionFactory createManagedConnectionFactory(
            ConnectorConnectionPool ccpClassLoader jcl) {
        final String mcfClass = ccp.getConnectorDescriptorInfo().getManagedConnectionFactoryClass();
        try {
            ManagedConnectionFactory mcf = null;
            mcf = instantiateMCF(mcfClassjcl);
            if (mcf instanceof ConfigurableTransactionSupport) {
                TransactionSupport ts = ConnectionPoolObjectsUtils.getTransactionSupport(
                                ccp.getTransactionSupport());
                ((ConfigurableTransactionSupport)mcf).setTransactionSupport(ts);
            }
            SetMethodAction setMethodAction = new SetMethodAction
                    (mcfccp.getConnectorDescriptorInfo().getMCFConfigProperties());
            setMethodAction.run();
            if(.isLoggable(.)) {
                .log(."Created MCF object : "mcfClass);
            }
            return mcf;
        } catch (ClassNotFoundException Ex) {
            .log(."rardeployment.class_not_found"new Object[]{mcfClassEx.getMessage()});
            if(.isLoggable(.)) {
                .log(."rardeployment.class_not_found"Ex);
            }
            return null;
        } catch (InstantiationException Ex) {
            .log(."rardeployment.class_instantiation_error"new Object[]{mcfClassEx.getMessage()});
            if(.isLoggable(.)) {
                .log(."rardeployment.class_instantiation_error"Ex);
            }
            return null;
        } catch (IllegalAccessException Ex) {
            .log(."rardeployment.illegalaccess_error"new Object[]{mcfClassEx.getMessage()});
            if(.isLoggable(.)) {
                .log(."rardeployment.illegalaccess_error"Ex);
            }
            return null;
        } catch (Exception Ex) {
            .log(."rardeployment.mcfcreation_error"new Object[]{mcfClassEx.getMessage()});
            if(.isLoggable(.)) {
                .log(."rardeployment.mcfcreation_error"Ex);
            }
            return null;
        }
    }

    
sets the logWriter for the MCF being instantiated.
Resource Adapter implementer can make use of this logWriter

Parameters:
mcf ManagedConnectionFactory
    private void setLogWriter(ManagedConnectionFactory mcf) {
        PrintWriterAdapter adapter = new PrintWriterAdapter(ConnectorRuntime.getRuntime().getResourceAdapterLogWriter());
        try {
            mcf.setLogWriter(adapter);
        } catch (Exception e) {
            Object[] params = new Object[]{mcf.getClass().getName(), e.toString()};
            .log(."rardeployment.logwriter_error"params);
            if(.isLoggable(.)) {
                .log(."Unable to set LogWriter for ManagedConnectionFactory : " + mcf.getClass().getName(), e);
            }
        }
    }
    protected ManagedConnectionFactory instantiateMCF(String mcfClassClassLoader loader)
            throws Exception {
        ManagedConnectionFactory mcf = null;
        if ( != null) {
            mcf = (ManagedConnectionFactory) .loadClass(mcfClass).newInstance();
        } else if (loader != null) {
            mcf = (ManagedConnectionFactory) loader.loadClass(mcfClass).newInstance();
        } else {
            //mcf = (ManagedConnectionFactory) Class.forName(mcfClass).newInstance();
            mcf = (ManagedConnectionFactory)Thread.currentThread().getContextClassLoader().loadClass(mcfClass).newInstance();
        }
        setLogWriter(mcf);
        return mcf;
    }


    
Creates default connector resource

Throws:
ConnectorRuntimeException when unable to create connector resources
    protected void createDefaultConnectorResources()
            throws ConnectorRuntimeException {
        for (ConnectionDefDescriptor descriptor : ) {
            String connectionDefName = descriptor.getConnectionFactoryIntf();
            String resourceName = .getDefaultResourceName(connectionDefName);
            String poolName = .getDefaultPoolName(connectionDefName);
            PoolInfo poolInfo = new PoolInfo(poolName);
            ResourceInfo resourceInfo = new ResourceInfo(resourceName);
            .createConnectorResource(resourceInfopoolInfonull);
            .addDefaultResourceName(resourceName);
            if(.isLoggable(.)) {
                .log(."Created default connector resource [ " + resourceName + " ] " );
            }
        }
    }

    
Creates default connector connection pool

Parameters:
useSunRA whether to use default pool settings or settings in sun-ra.xml
Throws:
ConnectorRuntimeException when unable to create connector connection pools
    protected void createDefaultConnectorConnectionPools(boolean useSunRA)
            throws ConnectorRuntimeException {
        for (ConnectionDefDescriptor descriptor : ) {
            String poolName = .getDefaultPoolName(descriptor.getConnectionFactoryIntf());
            PoolInfo poolInfo = new PoolInfo(poolName);
            ConnectorDescriptorInfo connectorDescriptorInfo =
                    ConnectorDDTransformUtils.getConnectorDescriptorInfo(descriptor);
            connectorDescriptorInfo.setRarName();
            connectorDescriptorInfo.setResourceAdapterClassName(.getResourceAdapterClass());
            ConnectorConnectionPool connectorPoolObj;
            // if useSunRA is true, then create connectorPoolObject using settings
            // from sunRAXML
            if (useSunRA) {
                connectorPoolObj =
                        ConnectionPoolObjectsUtils.createSunRaConnectorPoolObject(poolInfo);
            } else {
                connectorPoolObj =
                        ConnectionPoolObjectsUtils.createDefaultConnectorPoolObject(poolInfo);
            }
            connectorPoolObj.setConnectorDescriptorInfo(connectorDescriptorInfo);
            .createConnectorConnectionPool(connectorPoolObj);
            if(.isLoggable(.)) {
                .log(."Created default connection pool [ "poolInfo + " ] ");
            }
        }
    }

    
Creates connector connection pool pertaining to sun-ra.xml. This is only for 1.0 complient rars.

Throws:
ConnectorRuntimeException Thrown when pool creation fails.
    private void createSunRAConnectionPool() throws ConnectorRuntimeException {
        String defaultPoolName = .getDefaultPoolName(
                [0].getConnectionFactoryIntf());
        String sunRAPoolName = defaultPoolName + .;
        PoolInfo poolInfo = new PoolInfo(sunRAPoolName);
        ConnectorDescriptorInfo connectorDescriptorInfo =
                ConnectorDDTransformUtils.getConnectorDescriptorInfo([0]);
        connectorDescriptorInfo.setRarName();
        connectorDescriptorInfo.setResourceAdapterClassName(.getResourceAdapterClass());
        ConnectorConnectionPool connectorPoolObj =
                ConnectionPoolObjectsUtils.createSunRaConnectorPoolObject(poolInfo);
        connectorPoolObj.setConnectorDescriptorInfo(connectorDescriptorInfo);
        .createConnectorConnectionPool(connectorPoolObj);
        if(.isLoggable(.)) {
            .log(."Created SUN-RA connection pool:"poolInfo);
        }
        String jndiName = (String.getSunDescriptor().
                getResourceAdapter().getValue(.);
        ResourceInfo resourceInfo = new ResourceInfo(jndiName);
        .createConnectorResource(resourceInfopoolInfonull);
        if(.isLoggable(.)) {
            .log(."Created SUN-RA connector resource : "resourceInfo);
        }
    }

    
Added to clean up the connector connection pool pertaining to sun-ra.xml. This is only for 1.0 complient rars.
    private void deleteSunRAConnectionPool() {
        String defaultPoolName = .getDefaultPoolName(
                [0].getConnectionFactoryIntf());
        String sunRAPoolName = defaultPoolName + .;
        PoolInfo poolInfo = new PoolInfo(sunRAPoolName);
        try {
            .deleteConnectorConnectionPool(poolInfo);
        } catch (ConnectorRuntimeException cre) {
            .log(."rar.undeployment.sun_ra_pool_delete_fail"poolInfo);
        }
    }

    
Returns the class loader that is used to load the RAR.

Returns:
ClassLoader object.
    public ClassLoader getClassLoader() {
        return ;
    }

    
Retrieves the resource adapter java bean.

Returns:
ResourceAdapter
    public javax.resource.spi.ResourceAdapter getResourceAdapter() {
        throw new UnsupportedOperationException("1.0 RA will not have ResourceAdapter bean");
    }
New to GrepCode? Check out our FAQ X