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.service;
 
 
 import  javax.resource.ResourceException;
 import  javax.resource.spi.ResourceAdapter;
 import  javax.resource.spi.ResourceAdapterAssociation;
 import java.util.*;
 
This is resource adapter admin service. It creates, deletes Resource adapter and also the resource adapter configuration updation.

Author(s):
Binod P.G, Srikanth P, Aditya Gore, Jagadish Ramu
 
 
     private ExecutorService execService =
     Executors.newCachedThreadPool(new ThreadFactory() {
            public Thread newThread(Runnable r) {
              Thread th = new Thread(r);
              th.setDaemon(true);
              return th;
            }
     });   

    
Default constructor
 
     public ResourceAdapterAdminServiceImpl() {
         super();
     }

    
Destroys/deletes the Active resource adapter object from the connector container. Active resource adapter abstracts the rar deployed.

Parameters:
moduleName Name of the rarModule to destroy/delete
Throws:
ConnectorRuntimeException if the deletion fails
 
     private void destroyActiveResourceAdapter(String moduleNamethrows ConnectorRuntimeException {
        ResourcesUtil resutil = ResourcesUtil.createInstance();
        if (resutil == null) {
            ConnectorRuntimeException cre =
                    new ConnectorRuntimeException("Failed to get ResourcesUtil object");
            .log(."rardeployment.resourcesutil_get_failure"moduleName);
            .log(.""cre);
            throw cre;
        }
        if (!stopAndRemoveActiveResourceAdapter(moduleName)) {
            ConnectorRuntimeException cre =
                    new ConnectorRuntimeException("Failed to remove Active Resource Adapter");
            .log(."rardeployment.ra_removal_registry_failure"moduleName);
            .log(.""cre);
            throw cre;
        }
        unbindConnectorDescriptor(moduleName);
    }
    private void unbindConnectorDescriptor(String moduleNamethrows ConnectorRuntimeException {
        if(ConnectorRuntime.getRuntime().isServer()){
            try {
                String descriptorJNDIName = ConnectorAdminServiceUtils.
                        getReservePrefixedJNDINameForDescriptor(moduleName);
                .getNamingManager().getInitialContext().unbind(descriptorJNDIName);
                if(.isLoggable(.)){
                    .finest("ResourceAdapterAdminServiceImpl :: destroyActiveRA "
                        + moduleName + " removed descriptor " + descriptorJNDIName);
                }
            } catch (NamingException ne) {
                if(.isLoggable(.)){
                    .log(."rardeployment.connector_descriptor_jndi_removal_failure"moduleName);
                }
            }
        }
    }

    
Creates Active resource Adapter which abstracts the rar module. During the creation of ActiveResourceAdapter, default pools and resources also are created.

Parameters:
connectorDescriptor object which abstracts the connector deployment descriptor i.e rar.xml and sun-ra.xml.
moduleName Name of the module
moduleDir Directory where rar module is exploded.
loader Classloader to use
Throws:
ConnectorRuntimeException if creation fails.
    public void createActiveResourceAdapter(ConnectorDescriptor connectorDescriptor,
                                            String moduleNameString moduleDirClassLoader loader)
            throws ConnectorRuntimeException {
        synchronized (.getLockObject(moduleName)) {
            if (.isLoggable(.)) {
                .fine("ResourceAdapterAdminServiceImpl :: createActiveRA "
                        + moduleName + " at " + moduleDir);
            }
            ActiveResourceAdapter activeResourceAdapter = .getActiveResourceAdapter(moduleName);
            if (activeResourceAdapter != null) {
                if(.isLoggable(.)) {
                    .log(."rardeployment.resourceadapter.already.started"moduleName);
                }
                return;
            }
            //TODO V3 works fine ?
            if (loader == null) {
                try {
                    loader = connectorDescriptor.getClassLoader();
                } catch (Exception ex) {
                    if(.isLoggable(.)) {
                        .log(."No classloader available with connector descriptor");
                    }
                    loader = null;
                }
            }
            ConnectorRuntime connectorRuntime = ConnectorRuntime.getRuntime();
            ModuleDescriptor moduleDescriptor = null;
            Application application = null;
            if(.isLoggable(.)) {
                .fine("ResourceAdapterAdminServiceImpl :: createActiveRA "
                    + moduleName + " at " + moduleDir + " loader :: " + loader);
            }
            //class-loader can not be null for standalone rar as deployer should have provided one.
            //class-laoder can (may) be null for system-rars as they are not actually deployed.
            //TODO V3 don't check for system-ra if the resource-adapters are not loaded before recovery
            // (standalone + embedded)
            if (loader == null && ConnectorsUtil.belongsToSystemRA(moduleName)) {
                if (connectorRuntime.isServer()) {
                    loader = connectorRuntime.getSystemRARClassLoader(moduleName);
                }
            } else {
                connectorDescriptor.setClassLoader(null);
                moduleDescriptor = connectorDescriptor.getModuleDescriptor();
                application = connectorDescriptor.getApplication();
                connectorDescriptor.setModuleDescriptor(null);
                connectorDescriptor.setApplication(null);
            }
            try {
                activeResourceAdapter =
                        connectorRuntime.getActiveRAFactory().
                                createActiveResourceAdapter(connectorDescriptormoduleNameloader);
                if(.isLoggable(.)) {
                    .fine("ResourceAdapterAdminServiceImpl :: createActiveRA " +
                        moduleName + " at " + moduleDir +
                        " adding to registry " + activeResourceAdapter);
                }
                .addActiveResourceAdapter(moduleNameactiveResourceAdapter);
                if(.isLoggable(.)) {
                    .fine("ResourceAdapterAdminServiceImpl:: createActiveRA " +
                        moduleName + " at " + moduleDir
                        + " env =server ? " + (connectorRuntime.isServer()));
                }
                if (connectorRuntime.isServer()) {
                    //Update RAConfig in Connector Descriptor and bind in JNDI
                    //so that ACC clients could use RAConfig
                    updateRAConfigInDescriptor(connectorDescriptormoduleName);
                    String descriptorJNDIName = ConnectorAdminServiceUtils.getReservePrefixedJNDINameForDescriptor(moduleName);
                    if(.isLoggable(.)) {
                        .fine("ResourceAdapterAdminServiceImpl :: createActiveRA "
                            + moduleName + " at " + moduleDir
                            + " publishing descriptor " + descriptorJNDIName);
                    }
                    .getNamingManager().publishObject(descriptorJNDINameconnectorDescriptortrue);
                    activeResourceAdapter.setup();
                    String securityWarningMessage=
                        connectorRuntime.getSecurityPermissionSpec(moduleName);
                    // To i18N.
                    if (securityWarningMessage != null) {
                        .log(.securityWarningMessage);
                    }
                }
            } catch (NullPointerException npEx) {
                ConnectorRuntimeException cre =
                        new ConnectorRuntimeException("Error in creating active RAR");
                cre.initCause(npEx);
                .log."rardeployment.nullPointerException"moduleName);
                .log(.""cre);
                throw cre;
            } catch (NamingException ne) {
                ConnectorRuntimeException cre =
                        new ConnectorRuntimeException("Error in creating active RAR");
                cre.initCause(ne);
                .log(."rardeployment.jndi_publish_failure");
                .log(.""cre);
                throw cre;
            } finally {
                if (moduleDescriptor != null) {
                    connectorDescriptor.setModuleDescriptor(moduleDescriptor);
                    connectorDescriptor.setApplication(application);
                    connectorDescriptor.setClassLoader(loader);
                }
            }
        }
    }

    
Updates the connector descriptor of the connector module, with the contents of a resource adapter config if specified. This modified ConnectorDescriptor is then bound to JNDI so that ACC clients while configuring a non-system RAR could get the correct merged configuration. Any updates to resource-adapter config while an ACC client is in use is not transmitted to the client dynamically. All such changes would be visible on ACC client restart.
    private void updateRAConfigInDescriptor(ConnectorDescriptor connectorDescriptor,
                                            String moduleName) {
        ResourceAdapterConfig raConfig =
                ConnectorRegistry.getInstance().getResourceAdapterConfig(moduleName);
        List<PropertyraConfigProps = null;
        if (raConfig != null) {
            raConfigProps = raConfig.getProperty();
        }
        if(.isLoggable(.)) {
            .fine("current RAConfig In Descriptor " + connectorDescriptor.getConfigProperties());
        }
        if (raConfigProps != null) {
            Set mergedProps = ConnectorDDTransformUtils.mergeProps(
                    raConfigPropsconnectorDescriptor.getConfigProperties());
            Set actualProps = connectorDescriptor.getConfigProperties();
            actualProps.clear();
            actualProps.addAll(mergedProps);
            if(.isLoggable(.)) {
                .fine("updated RAConfig In Descriptor " + connectorDescriptor.getConfigProperties());
            }
        }
    }


    
Creates Active resource Adapter which abstracts the rar module. During the creation of ActiveResourceAdapter, default pools and resources also are created.

Parameters:
moduleDir Directory where rar module is exploded.
moduleName Name of the module
Throws:
ConnectorRuntimeException if creation fails.
    public void createActiveResourceAdapter(String moduleDirString moduleNameClassLoader loader)
            throws ConnectorRuntimeException {
        synchronized (.getLockObject(moduleName)){
            ActiveResourceAdapter activeResourceAdapter =
                    .getActiveResourceAdapter(moduleName);
            if (activeResourceAdapter != null) {
                if(.isLoggable(.)) {
                    .log(."rardeployment.resourceadapter.already.started"moduleName);
                }
                return;
            }
            if (ConnectorsUtil.belongsToSystemRA(moduleName)) {
                moduleDir = ConnectorsUtil.getSystemModuleLocation(moduleName);
            }
            ConnectorDescriptor connectorDescriptor = ConnectorDDTransformUtils.getConnectorDescriptor(moduleDirmoduleName);
            if (connectorDescriptor == null) {
                ConnectorRuntimeException cre = new ConnectorRuntimeException("Failed to obtain the connectorDescriptor");
                .log(."rardeployment.connector_descriptor_notfound"moduleName);
                .log(.""cre);
                throw cre;
            }
            createActiveResourceAdapter(connectorDescriptormoduleNamemoduleDirloader);
        }
    }


    
associates the given instance of ResourceAdapterAssociation with the ResourceAdapter java-bean of the specified RAR

Parameters:
rarName resource-adapter-name
raa Object that is an instance of ResourceAdapterAssociation
Throws:
ResourceException when unable to associate the RA Bean with RAA instance.
    public void associateResourceAdapter(String rarName, ResourceAdapterAssociation raa)
            throws ResourceException {
        ResourceAdapter ra = ConnectorRegistry.getInstance().
                getActiveResourceAdapter(rarName).getResourceAdapter();
        if(ra != null){
            raa.setResourceAdapter(ra);
        }else{
            throw new ResourceException("RA Bean [ "+rarName+" ] not available");
        }
    }


    
Stops the resourceAdapter and removes it from connector container/ registry.

Parameters:
moduleName Rarmodule name.
Returns:
true it is successful stop and removal of ActiveResourceAdapter false it stop and removal fails.
    private boolean stopAndRemoveActiveResourceAdapter(String moduleName) {
        ActiveResourceAdapter acr = null;
        if (moduleName != null) {
            acr = .getActiveResourceAdapter(moduleName);
        }
        if (acr != null) {
            sendStopToResourceAdapter(acr);
/*
            // remove the system rar from class loader chain.
            if(ConnectorsUtil.belongsToSystemRA(moduleName)) {
                ConnectorClassFinder ccf =
                        (ConnectorClassFinder)ConnectorRegistry.getInstance().
                                getActiveResourceAdapter(moduleName).getClassLoader();
                ConnectorRuntime connectorRuntime = ConnectorRuntime.getRuntime();
                DelegatingClassLoader ccl = connectorRuntime.getConnectorClassLoader();
                boolean systemRarCLRemoved = ccl.removeDelegate(ccf);
                if(_logger.isLoggable(Level.FINE)){
                    _logger.log(Level.FINE, "System RAR [ "+moduleName+" ] removed from " +
                        "classloader chain : " + systemRarCLRemoved);
                }
            }
*/
            .removeLockObject(moduleName);
            return .removeActiveResourceAdapter(moduleName);
        }
        return true;
    }

    
Checks if the rar module is already reployed.

Parameters:
moduleName Rarmodule name
Returns:
true if it is already deployed. false if it is not deployed.
    public boolean isRarDeployed(String moduleName) {
        ActiveResourceAdapter activeResourceAdapter =
                .getActiveResourceAdapter(moduleName);
        return activeResourceAdapter != null;
    }

    
Calls the stop method for all J2EE Connector 1.5/1.0 spec compliant RARs
    public void stopAllActiveResourceAdapters() {
        ActiveResourceAdapter[] resourceAdapters =
                ConnectorRegistry.getInstance().getAllActiveResourceAdapters();
        //stop system-rars after stopping all other rars.
        Set<ActiveResourceAdaptersystemRAs = new HashSet<ActiveResourceAdapter>();
        List<FuturerarExitStatusList = new ArrayList<Future>();
        for (ActiveResourceAdapter resourceAdapter : resourceAdapters) {
            if(!ConnectorsUtil.belongsToSystemRA(resourceAdapter.getModuleName())){
                RAShutdownHandler handler = new RAShutdownHandler(resourceAdapter.getModuleName());
                rarExitStatusList.add(.submit(handler));
            }else{
                systemRAs.add(resourceAdapter);
            }
        }
        for(Future futurerarExitStatusList){
            try {
                future.get();
            } catch (InterruptedException e) {
                //ignore as the child task will log any failures
            } catch (ExecutionException e) {
                //ignore as the child task will log any failures
            }
        }
        rarExitStatusList.clear();
        for(ActiveResourceAdapter resourceAdapter : systemRAs){
            RAShutdownHandler handler = new RAShutdownHandler(resourceAdapter.getModuleName());
            rarExitStatusList.add(.submit(handler));
        }
        for(Future futurerarExitStatusList){
            try {
                future.get();
            } catch (InterruptedException e) {
                //ignore as the child task will log any failures
            } catch (ExecutionException e) {
                //ignore as the child task will log any failures
            }
        }
    }

    
stop the active resource adapter (runtime)

Parameters:
raName resource-adapter name
    public void stopActiveResourceAdapter(String raName) {
        if(.isLoggable(.)) {
            .log(."Stopping RA : "raName);
        }
        try {
            destroyActiveResourceAdapter(raName);
        } catch (ConnectorRuntimeException cre) {
            Object params[] = new Object[]{raNamecre.getMessage()};
            .log(."unable.to.stop.ra"params);
            if(.isLoggable(.)) {
                .log(."unable to stop resource adapter [ " + raName + " ]"cre);
            }
        }
    }

    
add the resource-adapter-config

Parameters:
rarName resource-adapter name
raConfig resource-adapter-config
Throws:
ConnectorRuntimeException
    public void addResourceAdapterConfig(String rarNameResourceAdapterConfig raConfig)
        throws ConnectorRuntimeException {
        if (rarName != null && raConfig != null) {
            .addResourceAdapterConfig(rarNameraConfig);
            reCreateActiveResourceAdapter(rarName);
        }
    }

    
Delete the resource adapter configuration to the connector registry

Parameters:
rarName resource-adapter-name
Throws:
ConnectorRuntimeException when unable to remove RA Config.
    public void deleteResourceAdapterConfig(String rarNamethrows ConnectorRuntimeException {
        if (rarName != null) {
            .removeResourceAdapterConfig(rarName);
            reCreateActiveResourceAdapter(rarName);
        }
    }

    
The ActiveResourceAdapter object which abstract the rar module is recreated in the connector container/registry. All the pools and resources are killed. But the infrastructure to create the pools and and resources is untouched. Only the actual pool is killed.

Parameters:
moduleName rar module Name.
Throws:
ConnectorRuntimeException if recreation fails.
    public void reCreateActiveResourceAdapter(String moduleName)
            throws ConnectorRuntimeException {
        ConnectorRuntime runtime = ConnectorRuntime.getRuntime();
        if (isRarDeployed(moduleName)) {
            if(!ConnectorsUtil.belongsToSystemRA(moduleName)){
                ConnectorApplication app = .getConnectorApplication(moduleName);
                app.undeployResources();
                stopAndRemoveActiveResourceAdapter(moduleName);
                String moduleDir = ConnectorsUtil.getLocation(moduleName);
                createActiveResourceAdapter(moduleDirmoduleNameapp.getClassLoader());
                .getConnectorApplication(moduleName).deployResources();
            }else{
             Collection<Resourceresources =
                     getResourcesUtil().filterConnectorResources(getResourcesUtil().getGlobalResources(), moduleNametrue);
                runtime.getGlobalResourceManager().undeployResources(resources);
                stopAndRemoveActiveResourceAdapter(moduleName);
                String moduleDir = ConnectorsUtil.getLocation(moduleName);
                createActiveResourceAdapter(moduleDirmoduleName,
                        runtime.getSystemRARClassLoader(moduleName));
                runtime.getGlobalResourceManager().deployResources(resources);
            }
        }
     /*   //No need to deploy the .rar, it may be a case where rar is not deployed yet
        //Also, when the rar is started, RA-Config is anyway used
        else {
            ConnectorApplication app = _registry.getConnectorApplication(moduleName);
            createActiveResourceAdapter(moduleDir, moduleName, app.getClassLoader());
            _registry.getConnectorApplication(moduleName).deployResources();
        }*/
    }

    
Calls the stop method for all RARs

Parameters:
resourceAdapterToStop ra to stop
            resourceAdapterToStop) {
        Runnable rast = new RAShutdownTask(resourceAdapterToStop);
        String raName =  resourceAdapterToStop.getModuleName();
        Long timeout = ConnectorRuntime.getRuntime().getShutdownTimeout();
        Future future = null;
        boolean stopSuccessful = false;
        try {
            if(.isLoggable(.)) {
                .log(."scheduling stop for RA [ " + raName +" ] ");
            }
            future = .submit(rast);
            future.get(timeout.);
            if(.isLoggable(.)) {
                .log(."stop() Complete for active 1.5 compliant RAR " +
                    "[ "raName  +" ]");
            }
            stopSuccessful = true;
        } catch (TimeoutException e) {
            Object params[] = new Object[]{raNamee};
            .log(."ra.stop.timeout"params);
            cancelTask(futuretrueraName);
        } catch(Exception e){
            Object params[] = new Object[]{raNamee};
            .log(."ra.stop.failed"params);
            cancelTask(futuretrueraName);
        }
        if (stopSuccessful) {
            .log(."ra.stop-successful"raName);
        } else {
            .log(."ra.stop-unsuccessful"raName);
        }
    }
    private void cancelTask(Future futureboolean interruptIfRunningString raName){
        if(future != null){
            if(!(future.isCancelled()) && !(future.isDone())){
                boolean cancelled = future.cancel(interruptIfRunning);
                .log(."cancelling the shutdown of RA [ " + raName +" ] status : " + cancelled);
            } else {
                .log(."shutdown of RA [ " + raName +" ] is either already complete or already cancelled");
            }
        }
    }
    private static class RAShutdownTask implements Runnable {
        private ActiveResourceAdapter ra;
        public RAShutdownTask(ActiveResourceAdapter ratoBeShutDown) {
            super();
            this. = ratoBeShutDown;
        }
        public void run() {
            if(.isLoggable(.)) {
                .log(."Calling RA [ " + .getModuleName() + " ] shutdown ");
            }
            this..destroy();
        }
    }
    private class RAShutdownHandler implements Runnable {
        private String moduleName;
        public RAShutdownHandler(String moduleName){
            this. = moduleName;
        }
        public void run(){
            stopActiveResourceAdapter();     
        }
    }
New to GrepCode? Check out our FAQ X