Search in sources :

Example 16 with ServiceRegistry

use of org.jboss.msc.service.ServiceRegistry in project wildfly by wildfly.

the class AbstractDataSourceAdd method secondRuntimeStep.

static void secondRuntimeStep(OperationContext context, ModelNode operation, ManagementResourceRegistration datasourceRegistration, ModelNode model, boolean isXa) throws OperationFailedException {
    final ServiceTarget serviceTarget = context.getServiceTarget();
    final ModelNode address = operation.require(OP_ADDR);
    final String dsName = PathAddress.pathAddress(address).getLastElement().getValue();
    final String jndiName = JNDI_NAME.resolveModelAttribute(context, model).asString();
    final ServiceRegistry registry = context.getServiceRegistry(true);
    final List<ServiceName> serviceNames = registry.getServiceNames();
    final boolean elytronEnabled = ELYTRON_ENABLED.resolveModelAttribute(context, model).asBoolean();
    final ServiceName dataSourceServiceName = context.getCapabilityServiceName(Capabilities.DATA_SOURCE_CAPABILITY_NAME, dsName, DataSource.class);
    final ServiceController<?> dataSourceController = registry.getService(dataSourceServiceName);
    final ExceptionSupplier<CredentialSource, Exception> credentialSourceExceptionExceptionSupplier = dataSourceController.getService() instanceof AbstractDataSourceService ? ((AbstractDataSourceService) dataSourceController.getService()).getCredentialSourceSupplierInjector().getOptionalValue() : null;
    final ExceptionSupplier<CredentialSource, Exception> recoveryCredentialSourceExceptionExceptionSupplier = dataSourceController.getService() instanceof AbstractDataSourceService ? ((AbstractDataSourceService) dataSourceController.getService()).getRecoveryCredentialSourceSupplierInjector().getOptionalValue() : null;
    final boolean jta;
    if (isXa) {
        jta = true;
        final ModifiableXaDataSource dataSourceConfig;
        try {
            dataSourceConfig = xaFrom(context, model, dsName, credentialSourceExceptionExceptionSupplier, recoveryCredentialSourceExceptionExceptionSupplier);
        } catch (ValidateException e) {
            throw new OperationFailedException(ConnectorLogger.ROOT_LOGGER.failedToCreate("XaDataSource", operation, e.getLocalizedMessage()));
        }
        final ServiceName xaDataSourceConfigServiceName = XADataSourceConfigService.SERVICE_NAME_BASE.append(dsName);
        final XADataSourceConfigService xaDataSourceConfigService = new XADataSourceConfigService(dataSourceConfig);
        final ServiceBuilder<?> builder = serviceTarget.addService(xaDataSourceConfigServiceName, xaDataSourceConfigService);
        // add dependency on security domain service if applicable
        final DsSecurity dsSecurityConfig = dataSourceConfig.getSecurity();
        if (dsSecurityConfig != null) {
            final String securityDomainName = dsSecurityConfig.getSecurityDomain();
            if (!elytronEnabled && securityDomainName != null) {
                builder.requires(SECURITY_DOMAIN_SERVICE.append(securityDomainName));
            }
        }
        // add dependency on security domain service if applicable for recovery config
        if (dataSourceConfig.getRecovery() != null) {
            final Credential credential = dataSourceConfig.getRecovery().getCredential();
            if (credential != null) {
                final String securityDomainName = credential.getSecurityDomain();
                if (!RECOVERY_ELYTRON_ENABLED.resolveModelAttribute(context, model).asBoolean() && securityDomainName != null) {
                    builder.requires(SECURITY_DOMAIN_SERVICE.append(securityDomainName));
                }
            }
        }
        int propertiesCount = 0;
        for (ServiceName name : serviceNames) {
            if (xaDataSourceConfigServiceName.append("xa-datasource-properties").isParentOf(name)) {
                final ServiceController<?> xaConfigPropertyController = registry.getService(name);
                XaDataSourcePropertiesService xaPropService = (XaDataSourcePropertiesService) xaConfigPropertyController.getService();
                if (!ServiceController.State.UP.equals(xaConfigPropertyController.getState())) {
                    propertiesCount++;
                    xaConfigPropertyController.setMode(ServiceController.Mode.ACTIVE);
                    builder.addDependency(name, String.class, xaDataSourceConfigService.getXaDataSourcePropertyInjector(xaPropService.getName()));
                } else {
                    throw new OperationFailedException(ConnectorLogger.ROOT_LOGGER.serviceAlreadyStarted("Data-source.xa-config-property", name));
                }
            }
        }
        if (propertiesCount == 0) {
            throw ConnectorLogger.ROOT_LOGGER.xaDataSourcePropertiesNotPresent();
        }
        builder.install();
    } else {
        final ModifiableDataSource dataSourceConfig;
        try {
            dataSourceConfig = from(context, model, dsName, credentialSourceExceptionExceptionSupplier);
        } catch (ValidateException e) {
            throw new OperationFailedException(ConnectorLogger.ROOT_LOGGER.failedToCreate("DataSource", operation, e.getLocalizedMessage()));
        }
        jta = dataSourceConfig.isJTA();
        final ServiceName dataSourceCongServiceName = DataSourceConfigService.SERVICE_NAME_BASE.append(dsName);
        final DataSourceConfigService configService = new DataSourceConfigService(dataSourceConfig);
        final ServiceBuilder<?> builder = serviceTarget.addService(dataSourceCongServiceName, configService);
        // add dependency on security domain service if applicable
        final DsSecurity dsSecurityConfig = dataSourceConfig.getSecurity();
        if (dsSecurityConfig != null) {
            final String securityDomainName = dsSecurityConfig.getSecurityDomain();
            if (!elytronEnabled && securityDomainName != null) {
                builder.requires(SECURITY_DOMAIN_SERVICE.append(securityDomainName));
            }
        }
        for (ServiceName name : serviceNames) {
            if (dataSourceCongServiceName.append("connection-properties").isParentOf(name)) {
                final ServiceController<?> connPropServiceController = registry.getService(name);
                ConnectionPropertiesService connPropService = (ConnectionPropertiesService) connPropServiceController.getService();
                if (!ServiceController.State.UP.equals(connPropServiceController.getState())) {
                    connPropServiceController.setMode(ServiceController.Mode.ACTIVE);
                    builder.addDependency(name, String.class, configService.getConnectionPropertyInjector(connPropService.getName()));
                } else {
                    throw new OperationFailedException(ConnectorLogger.ROOT_LOGGER.serviceAlreadyStarted("Data-source.connectionProperty", name));
                }
            }
        }
        builder.install();
    }
    final ServiceName dataSourceServiceNameAlias = AbstractDataSourceService.SERVICE_NAME_BASE.append(jndiName).append(Constants.STATISTICS);
    if (dataSourceController != null) {
        if (!ServiceController.State.UP.equals(dataSourceController.getState())) {
            final boolean statsEnabled = STATISTICS_ENABLED.resolveModelAttribute(context, model).asBoolean();
            DataSourceStatisticsService statsService = new DataSourceStatisticsService(datasourceRegistration, statsEnabled);
            final ServiceBuilder statsServiceSB = serviceTarget.addService(dataSourceServiceName.append(Constants.STATISTICS), statsService);
            statsServiceSB.addAliases(dataSourceServiceNameAlias);
            statsServiceSB.requires(dataSourceServiceName);
            statsServiceSB.addDependency(CommonDeploymentService.getServiceName(ContextNames.bindInfoFor(jndiName)), CommonDeployment.class, statsService.getCommonDeploymentInjector());
            statsServiceSB.setInitialMode(ServiceController.Mode.PASSIVE);
            statsServiceSB.install();
            dataSourceController.setMode(ServiceController.Mode.ACTIVE);
        } else {
            throw new OperationFailedException(ConnectorLogger.ROOT_LOGGER.serviceAlreadyStarted("Data-source", dsName));
        }
    } else {
        throw new OperationFailedException(ConnectorLogger.ROOT_LOGGER.serviceNotAvailable("Data-source", dsName));
    }
    final DataSourceReferenceFactoryService referenceFactoryService = new DataSourceReferenceFactoryService();
    final ServiceName referenceFactoryServiceName = DataSourceReferenceFactoryService.SERVICE_NAME_BASE.append(dsName);
    final ServiceBuilder<?> referenceBuilder = serviceTarget.addService(referenceFactoryServiceName, referenceFactoryService).addDependency(dataSourceServiceName, DataSource.class, referenceFactoryService.getDataSourceInjector());
    referenceBuilder.install();
    final ContextNames.BindInfo bindInfo = ContextNames.bindInfoFor(jndiName);
    final BinderService binderService = new BinderService(bindInfo.getBindName());
    final ServiceBuilder<?> binderBuilder = serviceTarget.addService(bindInfo.getBinderServiceName(), binderService).addDependency(referenceFactoryServiceName, ManagedReferenceFactory.class, binderService.getManagedObjectInjector()).addDependency(bindInfo.getParentContextServiceName(), ServiceBasedNamingStore.class, binderService.getNamingStoreInjector()).addListener(new LifecycleListener() {

        private volatile boolean bound;

        public void handleEvent(final ServiceController<? extends Object> controller, final LifecycleEvent event) {
            switch(event) {
                case UP:
                    {
                        if (jta) {
                            SUBSYSTEM_DATASOURCES_LOGGER.boundDataSource(jndiName);
                        } else {
                            SUBSYSTEM_DATASOURCES_LOGGER.boundNonJTADataSource(jndiName);
                        }
                        bound = true;
                        break;
                    }
                case DOWN:
                    {
                        if (bound) {
                            if (jta) {
                                SUBSYSTEM_DATASOURCES_LOGGER.unboundDataSource(jndiName);
                            } else {
                                SUBSYSTEM_DATASOURCES_LOGGER.unBoundNonJTADataSource(jndiName);
                            }
                        }
                        break;
                    }
                case REMOVED:
                    {
                        SUBSYSTEM_DATASOURCES_LOGGER.debugf("Removed JDBC Data-source [%s]", jndiName);
                        break;
                    }
            }
        }
    });
    binderBuilder.setInitialMode(ServiceController.Mode.ACTIVE);
    binderBuilder.install();
}
Also used : ValidateException(org.jboss.jca.common.api.validator.ValidateException) DataSourceStatisticsService(org.jboss.as.connector.services.datasources.statistics.DataSourceStatisticsService) LifecycleListener(org.jboss.msc.service.LifecycleListener) ServiceBuilder(org.jboss.msc.service.ServiceBuilder) BinderService(org.jboss.as.naming.service.BinderService) LifecycleEvent(org.jboss.msc.service.LifecycleEvent) CredentialSource(org.wildfly.security.credential.source.CredentialSource) ContextNames(org.jboss.as.naming.deployment.ContextNames) Credential(org.jboss.jca.common.api.metadata.common.Credential) DsSecurity(org.jboss.jca.common.api.metadata.ds.DsSecurity) ServiceTarget(org.jboss.msc.service.ServiceTarget) OperationFailedException(org.jboss.as.controller.OperationFailedException) OperationFailedException(org.jboss.as.controller.OperationFailedException) ValidateException(org.jboss.jca.common.api.validator.ValidateException) ServiceName(org.jboss.msc.service.ServiceName) ServiceBasedNamingStore(org.jboss.as.naming.ServiceBasedNamingStore) ServiceRegistry(org.jboss.msc.service.ServiceRegistry) ModelNode(org.jboss.dmr.ModelNode)

Example 17 with ServiceRegistry

use of org.jboss.msc.service.ServiceRegistry in project wildfly by wildfly.

the class XaDataSourcePropertyAdd method performRuntime.

@Override
protected void performRuntime(OperationContext context, ModelNode operation, ModelNode recoveryEnvModel) throws OperationFailedException {
    final String configPropertyValue = XADATASOURCE_PROPERTY_VALUE.resolveModelAttribute(context, recoveryEnvModel).asString();
    final ModelNode address = operation.require(OP_ADDR);
    PathAddress path = PathAddress.pathAddress(address);
    final String dsName = path.getElement(path.size() - 2).getValue();
    final String configPropertyName = PathAddress.pathAddress(address).getLastElement().getValue();
    ServiceName serviceName = XADataSourceConfigService.SERVICE_NAME_BASE.append(dsName).append("xa-datasource-properties").append(configPropertyName);
    final ServiceRegistry registry = context.getServiceRegistry(true);
    final ServiceName dataSourceConfigServiceName = XADataSourceConfigService.SERVICE_NAME_BASE.append(dsName);
    final ServiceController<?> dataSourceConfigController = registry.getService(dataSourceConfigServiceName);
    if (dataSourceConfigController == null || !((XaDataSource) dataSourceConfigController.getValue()).isEnabled()) {
        final ServiceTarget serviceTarget = context.getServiceTarget();
        final XaDataSourcePropertiesService service = new XaDataSourcePropertiesService(configPropertyName, configPropertyValue);
        ServiceController<?> controller = serviceTarget.addService(serviceName, service).setInitialMode(ServiceController.Mode.NEVER).install();
    } else {
        context.reloadRequired();
    }
}
Also used : ServiceName(org.jboss.msc.service.ServiceName) PathAddress(org.jboss.as.controller.PathAddress) ServiceTarget(org.jboss.msc.service.ServiceTarget) ServiceRegistry(org.jboss.msc.service.ServiceRegistry) ModelNode(org.jboss.dmr.ModelNode) XaDataSource(org.jboss.jca.common.api.metadata.ds.XaDataSource)

Example 18 with ServiceRegistry

use of org.jboss.msc.service.ServiceRegistry in project wildfly by wildfly.

the class ConnectionPropertyAdd method performRuntime.

@Override
protected void performRuntime(OperationContext context, ModelNode operation, ModelNode recoveryEnvModel) throws OperationFailedException {
    final String configPropertyValue = CONNECTION_PROPERTY_VALUE.resolveModelAttribute(context, recoveryEnvModel).asString();
    final ModelNode address = operation.require(OP_ADDR);
    PathAddress path = PathAddress.pathAddress(address);
    final String jndiName = path.getElement(path.size() - 2).getValue();
    final String configPropertyName = PathAddress.pathAddress(address).getLastElement().getValue();
    ServiceName serviceName = DataSourceConfigService.SERVICE_NAME_BASE.append(jndiName).append("connection-properties").append(configPropertyName);
    final ServiceRegistry registry = context.getServiceRegistry(true);
    final ServiceName dataSourceConfigServiceName = DataSourceConfigService.SERVICE_NAME_BASE.append(jndiName);
    final ServiceController<?> dataSourceConfigController = registry.getService(dataSourceConfigServiceName);
    if (dataSourceConfigController == null || !((DataSource) dataSourceConfigController.getValue()).isEnabled()) {
        final ServiceTarget serviceTarget = context.getServiceTarget();
        final ConnectionPropertiesService service = new ConnectionPropertiesService(configPropertyName, configPropertyValue);
        serviceTarget.addService(serviceName, service).setInitialMode(ServiceController.Mode.NEVER).install();
    } else {
        context.reloadRequired();
    }
}
Also used : ServiceName(org.jboss.msc.service.ServiceName) PathAddress(org.jboss.as.controller.PathAddress) ServiceTarget(org.jboss.msc.service.ServiceTarget) ServiceRegistry(org.jboss.msc.service.ServiceRegistry) ModelNode(org.jboss.dmr.ModelNode) DataSource(org.jboss.jca.common.api.metadata.ds.DataSource)

Example 19 with ServiceRegistry

use of org.jboss.msc.service.ServiceRegistry in project wildfly by wildfly.

the class EJB3SubsystemDefaultEntityBeanOptimisticLockingWriteHandler method updateOptimisticLocking.

void updateOptimisticLocking(final OperationContext context, final ModelNode model) throws OperationFailedException {
    final ModelNode enabled = EJB3SubsystemRootResourceDefinition.DEFAULT_ENTITY_BEAN_OPTIMISTIC_LOCKING.resolveModelAttribute(context, model);
    final ServiceRegistry serviceRegistry = context.getServiceRegistry(true);
    ServiceController<?> existingService = serviceRegistry.getService(SERVICE_NAME);
    // if a default optimistic locking config is installed, remove it
    if (existingService != null) {
        context.removeService(existingService);
    }
    if (enabled.isDefined()) {
        final Service<Boolean> newDefaultPoolConfigService = new ValueService<Boolean>(new ImmediateValue<Boolean>(enabled.asBoolean()));
        ServiceController<?> newController = context.getServiceTarget().addService(SERVICE_NAME, newDefaultPoolConfigService).install();
    }
}
Also used : ServiceRegistry(org.jboss.msc.service.ServiceRegistry) ModelNode(org.jboss.dmr.ModelNode) ValueService(org.jboss.msc.service.ValueService)

Example 20 with ServiceRegistry

use of org.jboss.msc.service.ServiceRegistry in project wildfly by wildfly.

the class StrictMaxPoolDerivedSizeReadHandler method executeRuntimeStep.

@Override
protected void executeRuntimeStep(final OperationContext context, final ModelNode operation) throws OperationFailedException {
    final String poolName = PathAddress.pathAddress(operation.get(ModelDescriptionConstants.OP_ADDR)).getLastElement().getValue();
    ServiceName serviceName = context.getCapabilityServiceName(StrictMaxPoolResourceDefinition.STRICT_MAX_POOL_CONFIG_CAPABILITY_NAME, poolName, StrictMaxPoolConfigService.class);
    final ServiceRegistry registry = context.getServiceRegistry(true);
    ServiceController<?> sc = registry.getService(serviceName);
    if (sc != null) {
        StrictMaxPoolConfigService smpc = (StrictMaxPoolConfigService) sc.getService();
        if (smpc != null) {
            context.getResult().set(smpc.getDerivedSize());
            return;
        }
    }
    throw EjbLogger.ROOT_LOGGER.cannotReadStrictMaxPoolDerivedSize(serviceName);
}
Also used : ServiceName(org.jboss.msc.service.ServiceName) ServiceRegistry(org.jboss.msc.service.ServiceRegistry) StrictMaxPoolConfigService(org.jboss.as.ejb3.component.pool.StrictMaxPoolConfigService)

Aggregations

ServiceRegistry (org.jboss.msc.service.ServiceRegistry)75 ServiceName (org.jboss.msc.service.ServiceName)52 ModelNode (org.jboss.dmr.ModelNode)22 PathAddress (org.jboss.as.controller.PathAddress)15 ServiceTarget (org.jboss.msc.service.ServiceTarget)15 OperationFailedException (org.jboss.as.controller.OperationFailedException)13 ServiceController (org.jboss.msc.service.ServiceController)12 ActiveMQServer (org.apache.activemq.artemis.core.server.ActiveMQServer)8 ServiceBuilder (org.jboss.msc.service.ServiceBuilder)8 OperationContext (org.jboss.as.controller.OperationContext)6 Resource (org.jboss.as.controller.registry.Resource)6 AttributeDefinition (org.jboss.as.controller.AttributeDefinition)5 Activation (org.jboss.jca.common.api.metadata.resourceadapter.Activation)5 OperationStepHandler (org.jboss.as.controller.OperationStepHandler)4 PathElement (org.jboss.as.controller.PathElement)4 EmbeddedCacheManager (org.infinispan.manager.EmbeddedCacheManager)3 DefaultAccessTimeoutService (org.jboss.as.ejb3.component.DefaultAccessTimeoutService)3 DeploymentUnit (org.jboss.as.server.deployment.DeploymentUnit)3 ArrayList (java.util.ArrayList)2 BridgeConfiguration (org.apache.activemq.artemis.core.config.BridgeConfiguration)2