Search in sources :

Example 1 with ServicesAttachment

use of org.jboss.as.server.deployment.ServicesAttachment in project wildfly by wildfly.

the class DriverProcessor method undeploy.

/**
 * {@inheritDoc}
 */
@Override
public void undeploy(final DeploymentUnit context) {
    /**
     * https://issues.redhat.com/browse/WFLY-14114
     *
     * This hack allows to deregister all drivers registered by this module. See comments in {@link DriverManagerAdapterProcessor}
     */
    final Module module = context.getAttachment(org.jboss.as.server.deployment.Attachments.MODULE);
    final ServicesAttachment servicesAttachment = context.getAttachment(Attachments.SERVICES);
    if (module != null && servicesAttachment != null) {
        final List<String> driverNames = servicesAttachment.getServiceImplementations(Driver.class.getName());
        if (!driverNames.isEmpty()) {
            try {
                Class<?> driverManagerAdapterClass = module.getClassLoader().loadClass(DriverManagerAdapter.class.getName());
                Method getDriversMethod = driverManagerAdapterClass.getDeclaredMethod("getDrivers");
                Enumeration<Driver> drivers = (Enumeration<Driver>) getDriversMethod.invoke(null, null);
                Method deregisterDriverMethod = driverManagerAdapterClass.getDeclaredMethod("deregisterDriver", Driver.class);
                while (drivers.hasMoreElements()) {
                    Driver driver = drivers.nextElement();
                    if (driverNames.contains(driver.getClass().getName())) {
                        deregisterDriverMethod.invoke(null, driver);
                    }
                }
            } catch (ClassNotFoundException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
                Assert.unreachableCode();
            }
        }
    }
}
Also used : Enumeration(java.util.Enumeration) DriverManagerAdapter(org.jboss.as.connector._drivermanager.DriverManagerAdapter) ServicesAttachment(org.jboss.as.server.deployment.ServicesAttachment) InstalledDriver(org.jboss.as.connector.services.driver.InstalledDriver) Driver(java.sql.Driver) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) Module(org.jboss.modules.Module)

Example 2 with ServicesAttachment

use of org.jboss.as.server.deployment.ServicesAttachment in project wildfly by wildfly.

the class DriverProcessor method deploy.

/**
 * {@inheritDoc}
 */
@Override
public void deploy(final DeploymentPhaseContext phaseContext) throws DeploymentUnitProcessingException {
    final DeploymentUnit deploymentUnit = phaseContext.getDeploymentUnit();
    final Module module = deploymentUnit.getAttachment(Attachments.MODULE);
    final ServicesAttachment servicesAttachment = deploymentUnit.getAttachment(Attachments.SERVICES);
    if (module != null && servicesAttachment != null) {
        final ModuleClassLoader classLoader = module.getClassLoader();
        final List<String> driverNames = servicesAttachment.getServiceImplementations(Driver.class.getName());
        int idx = 0;
        for (String driverClassName : driverNames) {
            try {
                final Class<? extends Driver> driverClass = classLoader.loadClass(driverClassName).asSubclass(Driver.class);
                final Constructor<? extends Driver> constructor = driverClass.getConstructor();
                final Driver driver = constructor.newInstance();
                final int majorVersion = driver.getMajorVersion();
                final int minorVersion = driver.getMinorVersion();
                final boolean compliant = driver.jdbcCompliant();
                if (compliant) {
                    DEPLOYER_JDBC_LOGGER.deployingCompliantJdbcDriver(driverClass, majorVersion, minorVersion);
                } else {
                    DEPLOYER_JDBC_LOGGER.deployingNonCompliantJdbcDriver(driverClass, majorVersion, minorVersion);
                }
                String driverName = deploymentUnit.getName();
                if ((driverName.contains(".") && !driverName.endsWith(".jar")) || driverNames.size() != 1) {
                    driverName += "_" + driverClassName + "_" + majorVersion + "_" + minorVersion;
                }
                InstalledDriver driverMetadata = new InstalledDriver(driverName, driverClass.getName(), null, null, majorVersion, minorVersion, compliant);
                DriverService driverService = new DriverService(driverMetadata, driver);
                phaseContext.getServiceTarget().addService(ServiceName.JBOSS.append("jdbc-driver", driverName.replaceAll("\\.", "_")), driverService).addDependency(ConnectorServices.JDBC_DRIVER_REGISTRY_SERVICE, DriverRegistry.class, driverService.getDriverRegistryServiceInjector()).setInitialMode(Mode.ACTIVE).install();
                if (idx == 0 && driverNames.size() != 1) {
                    // create short name driver service
                    // reset driverName to the deployment unit name
                    driverName = deploymentUnit.getName();
                    driverMetadata = new InstalledDriver(driverName, driverClass.getName(), null, null, majorVersion, minorVersion, compliant);
                    driverService = new DriverService(driverMetadata, driver);
                    phaseContext.getServiceTarget().addService(ServiceName.JBOSS.append("jdbc-driver", driverName.replaceAll("\\.", "_")), driverService).addDependency(ConnectorServices.JDBC_DRIVER_REGISTRY_SERVICE, DriverRegistry.class, driverService.getDriverRegistryServiceInjector()).setInitialMode(Mode.ACTIVE).install();
                }
                idx++;
            } catch (Throwable e) {
                DEPLOYER_JDBC_LOGGER.cannotInstantiateDriverClass(driverClassName, e);
            }
        }
    }
}
Also used : InstalledDriver(org.jboss.as.connector.services.driver.InstalledDriver) ServicesAttachment(org.jboss.as.server.deployment.ServicesAttachment) ModuleClassLoader(org.jboss.modules.ModuleClassLoader) InstalledDriver(org.jboss.as.connector.services.driver.InstalledDriver) Driver(java.sql.Driver) Module(org.jboss.modules.Module) DeploymentUnit(org.jboss.as.server.deployment.DeploymentUnit) DriverService(org.jboss.as.connector.services.driver.DriverService)

Example 3 with ServicesAttachment

use of org.jboss.as.server.deployment.ServicesAttachment in project wildfly by wildfly.

the class PersistenceProviderHandler method deploy.

public static void deploy(final DeploymentPhaseContext phaseContext, final Platform platform) throws DeploymentUnitProcessingException {
    final DeploymentUnit deploymentUnit = phaseContext.getDeploymentUnit();
    final Module module = deploymentUnit.getAttachment(Attachments.MODULE);
    final ServicesAttachment servicesAttachment = deploymentUnit.getAttachment(Attachments.SERVICES);
    if (module != null && servicesAttachment != null) {
        final ModuleClassLoader deploymentModuleClassLoader = module.getClassLoader();
        PersistenceProvider provider;
        // collect list of persistence providers packaged with the application
        final List<String> providerNames = servicesAttachment.getServiceImplementations(PERSISTENCE_PROVIDER_CLASSNAME);
        List<PersistenceProvider> providerList = new ArrayList<PersistenceProvider>();
        for (String providerName : providerNames) {
            try {
                final Class<? extends PersistenceProvider> providerClass = deploymentModuleClassLoader.loadClass(providerName).asSubclass(PersistenceProvider.class);
                final Constructor<? extends PersistenceProvider> constructor = providerClass.getConstructor();
                provider = constructor.newInstance();
                providerList.add(provider);
                JpaLogger.ROOT_LOGGER.tracef("deployment %s is using its own copy of %s", deploymentUnit.getName(), providerName);
            } catch (Exception e) {
                throw JpaLogger.ROOT_LOGGER.cannotDeployApp(e, providerName);
            }
        }
        if (!providerList.isEmpty()) {
            final String adapterClass = deploymentUnit.getAttachment(JpaAttachments.ADAPTOR_CLASS_NAME);
            PersistenceProviderAdaptor adaptor;
            if (adapterClass != null) {
                try {
                    adaptor = (PersistenceProviderAdaptor) deploymentModuleClassLoader.loadClass(adapterClass).newInstance();
                    adaptor.injectJtaManager(new JtaManagerImpl(deploymentUnit.getAttachment(JpaAttachments.TRANSACTION_SYNCHRONIZATION_REGISTRY)));
                    adaptor.injectPlatform(platform);
                    ArrayList<PersistenceProviderAdaptor> adaptorList = new ArrayList<>();
                    adaptorList.add(adaptor);
                    PersistenceProviderDeploymentHolder.savePersistenceProviderInDeploymentUnit(deploymentUnit, providerList, adaptorList);
                } catch (InstantiationException e) {
                    throw JpaLogger.ROOT_LOGGER.cannotCreateAdapter(e, adapterClass);
                } catch (IllegalAccessException e) {
                    throw JpaLogger.ROOT_LOGGER.cannotCreateAdapter(e, adapterClass);
                } catch (ClassNotFoundException e) {
                    throw JpaLogger.ROOT_LOGGER.cannotCreateAdapter(e, adapterClass);
                }
            } else {
                // register the provider (no adapter specified)
                PersistenceProviderDeploymentHolder.savePersistenceProviderInDeploymentUnit(deploymentUnit, providerList, null);
            }
        }
    }
}
Also used : ServicesAttachment(org.jboss.as.server.deployment.ServicesAttachment) ModuleClassLoader(org.jboss.modules.ModuleClassLoader) PersistenceProvider(javax.persistence.spi.PersistenceProvider) ArrayList(java.util.ArrayList) DeploymentUnitProcessingException(org.jboss.as.server.deployment.DeploymentUnitProcessingException) JtaManagerImpl(org.jboss.as.jpa.transaction.JtaManagerImpl) Module(org.jboss.modules.Module) DeploymentUnit(org.jboss.as.server.deployment.DeploymentUnit) PersistenceProviderAdaptor(org.jipijapa.plugin.spi.PersistenceProviderAdaptor)

Aggregations

ServicesAttachment (org.jboss.as.server.deployment.ServicesAttachment)3 Module (org.jboss.modules.Module)3 Driver (java.sql.Driver)2 InstalledDriver (org.jboss.as.connector.services.driver.InstalledDriver)2 DeploymentUnit (org.jboss.as.server.deployment.DeploymentUnit)2 ModuleClassLoader (org.jboss.modules.ModuleClassLoader)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 Enumeration (java.util.Enumeration)1 PersistenceProvider (javax.persistence.spi.PersistenceProvider)1 DriverManagerAdapter (org.jboss.as.connector._drivermanager.DriverManagerAdapter)1 DriverService (org.jboss.as.connector.services.driver.DriverService)1 JtaManagerImpl (org.jboss.as.jpa.transaction.JtaManagerImpl)1 DeploymentUnitProcessingException (org.jboss.as.server.deployment.DeploymentUnitProcessingException)1 PersistenceProviderAdaptor (org.jipijapa.plugin.spi.PersistenceProviderAdaptor)1