Search in sources :

Example 1 with NamePrincipal

use of io.agroal.api.security.NamePrincipal in project hibernate-orm by hibernate.

the class AgroalConnectionProvider method configure.

@Override
@SuppressWarnings("unchecked")
public void configure(Map props) throws HibernateException {
    LOGGER.debug("Configuring Agroal");
    try {
        AgroalPropertiesReader agroalProperties = new AgroalPropertiesReader(CONFIG_PREFIX).readProperties(props);
        agroalProperties.modify().connectionPoolConfiguration(cp -> cp.connectionFactoryConfiguration(cf -> {
            copyProperty(props, AvailableSettings.DRIVER, cf::driverClassName, Function.identity());
            copyProperty(props, AvailableSettings.URL, cf::jdbcUrl, Function.identity());
            copyProperty(props, AvailableSettings.USER, cf::principal, NamePrincipal::new);
            copyProperty(props, AvailableSettings.PASS, cf::credential, SimplePassword::new);
            copyProperty(props, AvailableSettings.AUTOCOMMIT, cf::autoCommit, Boolean::valueOf);
            resolveIsolationSetting(props, cf);
            return cf;
        }));
        agroalDataSource = AgroalDataSource.from(agroalProperties);
    } catch (Exception e) {
        throw new HibernateException(e);
    }
    LOGGER.debug("Agroal Configured");
}
Also used : ConnectionProviderInitiator(org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator) Stoppable(org.hibernate.service.spi.Stoppable) Connection(java.sql.Connection) AvailableSettings(org.hibernate.cfg.AvailableSettings) Configurable(org.hibernate.service.spi.Configurable) Logger(org.jboss.logging.Logger) UnknownUnwrapTypeException(org.hibernate.service.UnknownUnwrapTypeException) AgroalPropertiesReader(io.agroal.api.configuration.supplier.AgroalPropertiesReader) NamePrincipal(io.agroal.api.security.NamePrincipal) Function(java.util.function.Function) AgroalConnectionFactoryConfigurationSupplier(io.agroal.api.configuration.supplier.AgroalConnectionFactoryConfigurationSupplier) Consumer(java.util.function.Consumer) SQLException(java.sql.SQLException) Map(java.util.Map) DataSource(javax.sql.DataSource) AgroalConnectionFactoryConfiguration(io.agroal.api.configuration.AgroalConnectionFactoryConfiguration) SimplePassword(io.agroal.api.security.SimplePassword) HibernateException(org.hibernate.HibernateException) ConnectionProvider(org.hibernate.engine.jdbc.connections.spi.ConnectionProvider) AgroalDataSource(io.agroal.api.AgroalDataSource) HibernateException(org.hibernate.HibernateException) AgroalPropertiesReader(io.agroal.api.configuration.supplier.AgroalPropertiesReader) UnknownUnwrapTypeException(org.hibernate.service.UnknownUnwrapTypeException) SQLException(java.sql.SQLException) HibernateException(org.hibernate.HibernateException)

Example 2 with NamePrincipal

use of io.agroal.api.security.NamePrincipal in project wildfly by wildfly.

the class DataSourceDefinitionInjectionSource method getResourceValue.

// --- //
@Override
public void getResourceValue(ResolutionContext context, ServiceBuilder<?> serviceBuilder, DeploymentPhaseContext phaseContext, Injector<ManagedReferenceFactory> injector) throws DeploymentUnitProcessingException {
    AgroalConnectionFactoryConfigurationSupplier connectionFactoryConfiguration = new AgroalConnectionFactoryConfigurationSupplier();
    try {
        Class<?> providerClass = phaseContext.getDeploymentUnit().getAttachment(MODULE).getClassLoader().loadClass(className);
        if (providerClass != null && !DataSource.class.isAssignableFrom(providerClass) && !Driver.class.isAssignableFrom(providerClass)) {
            throw AgroalLogger.SERVICE_LOGGER.invalidDeploymentConnectionProvider();
        }
        connectionFactoryConfiguration.connectionProviderClass(providerClass);
    } catch (ClassNotFoundException e) {
        throw AgroalLogger.SERVICE_LOGGER.loadClassDeploymentException(e, className);
    }
    for (Map.Entry<String, String> property : properties.entrySet()) {
        connectionFactoryConfiguration.jdbcProperty(property.getKey(), property.getValue());
    }
    if (databaseName != null && !databaseName.isEmpty()) {
        connectionFactoryConfiguration.jdbcProperty(DATABASE_NAME_PROP, databaseName);
    }
    if (description != null && !description.isEmpty()) {
        connectionFactoryConfiguration.jdbcProperty(DESCRIPTION_PROP, description);
    }
    if (serverName != null && !serverName.isEmpty()) {
        connectionFactoryConfiguration.jdbcProperty(SERVER_NAME_PROP, serverName);
    }
    if (portNumber >= 0) {
        connectionFactoryConfiguration.jdbcProperty(PORT_NUMBER_PROP, Integer.toString(portNumber));
    }
    if (loginTimeout >= 0) {
        connectionFactoryConfiguration.jdbcProperty(LOGIN_TIMEOUT_PROP, Integer.toString(loginTimeout));
    }
    if (maxStatements >= 0) {
        connectionFactoryConfiguration.jdbcProperty(MAX_STATEMENTS_PROP, Integer.toString(maxStatements));
    }
    if (url != null && !url.isEmpty()) {
        connectionFactoryConfiguration.jdbcUrl(url);
    }
    if (user != null && !user.isEmpty()) {
        connectionFactoryConfiguration.principal(new NamePrincipal(user));
    }
    if (password != null && !password.isEmpty()) {
        connectionFactoryConfiguration.credential(new SimplePassword(password));
    }
    connectionFactoryConfiguration.jdbcTransactionIsolation(AgroalConnectionFactoryConfiguration.TransactionIsolation.fromLevel(isolationLevel));
    AgroalConnectionPoolConfigurationSupplier connectionPoolConfiguration = new AgroalConnectionPoolConfigurationSupplier();
    connectionPoolConfiguration.connectionFactoryConfiguration(connectionFactoryConfiguration);
    if (initialPoolSize >= 0) {
        connectionPoolConfiguration.initialSize(initialPoolSize);
    }
    if (minPoolSize >= 0) {
        connectionPoolConfiguration.minSize(minPoolSize);
    }
    if (maxPoolSize >= 0) {
        connectionPoolConfiguration.maxSize(maxPoolSize);
    }
    if (maxIdleTime >= 0) {
        connectionPoolConfiguration.reapTimeout(Duration.ofSeconds(maxIdleTime));
    }
    AgroalDataSourceConfigurationSupplier dataSourceConfiguration = new AgroalDataSourceConfigurationSupplier();
    dataSourceConfiguration.connectionPoolConfiguration(connectionPoolConfiguration);
    ContextNames.BindInfo bindInfo = ContextNames.bindInfoForEnvEntry(context.getApplicationName(), context.getModuleName(), context.getComponentName(), !context.isCompUsesModule(), jndiName);
    ServiceName dataSourceServiceName = DATASOURCE_DEFINITION_SERVICE_PREFIX.append(bindInfo.getBinderServiceName().getCanonicalName());
    // This is the service responsible for the JNDI binding, with a dependency on the datasource service that acts as a ManagedReferenceFactory and is used as the injection source
    BinderService binderService = new BinderService(bindInfo.getBindName(), this);
    phaseContext.getServiceTarget().addService(bindInfo.getBinderServiceName(), binderService).addDependency(dataSourceServiceName, ManagedReferenceFactory.class, binderService.getManagedObjectInjector()).addDependency(bindInfo.getParentContextServiceName(), ServiceBasedNamingStore.class, binderService.getNamingStoreInjector()).install();
    ServiceBuilder svcBuilder = phaseContext.getServiceTarget().addService(dataSourceServiceName);
    Supplier<TransactionSynchronizationRegistry> tsrSupplier = null;
    if (transactional) {
        CapabilityServiceSupport css = phaseContext.getDeploymentUnit().getAttachment(Attachments.CAPABILITY_SERVICE_SUPPORT);
        ServiceName tsrName = css.getCapabilityServiceName("org.wildfly.transactions.transaction-synchronization-registry");
        // noinspection unchecked
        tsrSupplier = (Supplier<TransactionSynchronizationRegistry>) svcBuilder.requires(tsrName);
    }
    DataSourceDefinitionService dataSourceService = new DataSourceDefinitionService(bindInfo, transactional, dataSourceConfiguration, tsrSupplier);
    svcBuilder.setInstance(dataSourceService).install();
    serviceBuilder.requires(bindInfo.getBinderServiceName());
    serviceBuilder.addDependency(dataSourceServiceName, ManagedReferenceFactory.class, injector);
}
Also used : AgroalDataSourceConfigurationSupplier(io.agroal.api.configuration.supplier.AgroalDataSourceConfigurationSupplier) NamePrincipal(io.agroal.api.security.NamePrincipal) AgroalConnectionFactoryConfigurationSupplier(io.agroal.api.configuration.supplier.AgroalConnectionFactoryConfigurationSupplier) DataSource(javax.sql.DataSource) ServiceBuilder(org.jboss.msc.service.ServiceBuilder) CapabilityServiceSupport(org.jboss.as.controller.capability.CapabilityServiceSupport) BinderService(org.jboss.as.naming.service.BinderService) AgroalConnectionPoolConfigurationSupplier(io.agroal.api.configuration.supplier.AgroalConnectionPoolConfigurationSupplier) ServiceName(org.jboss.msc.service.ServiceName) ServiceBasedNamingStore(org.jboss.as.naming.ServiceBasedNamingStore) TransactionSynchronizationRegistry(javax.transaction.TransactionSynchronizationRegistry) SimplePassword(io.agroal.api.security.SimplePassword) Map(java.util.Map) ContextNames(org.jboss.as.naming.deployment.ContextNames)

Aggregations

AgroalConnectionFactoryConfigurationSupplier (io.agroal.api.configuration.supplier.AgroalConnectionFactoryConfigurationSupplier)2 NamePrincipal (io.agroal.api.security.NamePrincipal)2 SimplePassword (io.agroal.api.security.SimplePassword)2 Map (java.util.Map)2 DataSource (javax.sql.DataSource)2 AgroalDataSource (io.agroal.api.AgroalDataSource)1 AgroalConnectionFactoryConfiguration (io.agroal.api.configuration.AgroalConnectionFactoryConfiguration)1 AgroalConnectionPoolConfigurationSupplier (io.agroal.api.configuration.supplier.AgroalConnectionPoolConfigurationSupplier)1 AgroalDataSourceConfigurationSupplier (io.agroal.api.configuration.supplier.AgroalDataSourceConfigurationSupplier)1 AgroalPropertiesReader (io.agroal.api.configuration.supplier.AgroalPropertiesReader)1 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 Consumer (java.util.function.Consumer)1 Function (java.util.function.Function)1 TransactionSynchronizationRegistry (javax.transaction.TransactionSynchronizationRegistry)1 HibernateException (org.hibernate.HibernateException)1 AvailableSettings (org.hibernate.cfg.AvailableSettings)1 ConnectionProviderInitiator (org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator)1 ConnectionProvider (org.hibernate.engine.jdbc.connections.spi.ConnectionProvider)1 UnknownUnwrapTypeException (org.hibernate.service.UnknownUnwrapTypeException)1