Search in sources :

Example 1 with LifecycleEvent

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

the class InfinispanCacheDeploymentListener method startCache.

@Override
public Wrapper startCache(Classification classification, Properties properties) throws Exception {
    ServiceContainer target = currentServiceContainer();
    String container = properties.getProperty(CONTAINER);
    String cacheType = properties.getProperty(CACHE_TYPE);
    // TODO Figure out how to access CapabilityServiceSupport from here
    ServiceName containerServiceName = ServiceNameFactory.parseServiceName(InfinispanRequirement.CONTAINER.getName()).append(container);
    // need a private cache for non-jpa application use
    String name = properties.getProperty(NAME, UUID.randomUUID().toString());
    ServiceBuilder<?> builder = target.addService(ServiceName.JBOSS.append(DEFAULT_CACHE_CONTAINER, name));
    Supplier<EmbeddedCacheManager> manager = builder.requires(containerServiceName);
    if (CACHE_PRIVATE.equals(cacheType)) {
        // If using a private cache, addCacheDependencies(...) is never triggered
        String[] caches = properties.getProperty(CACHES).split("\\s+");
        for (String cache : caches) {
            builder.requires(ServiceNameFactory.parseServiceName(InfinispanCacheRequirement.CONFIGURATION.getName()).append(container, cache));
        }
    }
    final CountDownLatch latch = new CountDownLatch(1);
    builder.addListener(new LifecycleListener() {

        @Override
        public void handleEvent(final ServiceController<?> controller, final LifecycleEvent event) {
            if (event == LifecycleEvent.UP) {
                latch.countDown();
                controller.removeListener(this);
            }
        }
    });
    ServiceController<?> controller = builder.install();
    // Ensure cache configuration services are started
    latch.await();
    return new CacheWrapper(manager.get(), controller);
}
Also used : LifecycleListener(org.jboss.msc.service.LifecycleListener) EmbeddedCacheManager(org.infinispan.manager.EmbeddedCacheManager) CountDownLatch(java.util.concurrent.CountDownLatch) ServiceContainer(org.jboss.msc.service.ServiceContainer) CurrentServiceContainer(org.jboss.as.server.CurrentServiceContainer) ServiceName(org.jboss.msc.service.ServiceName) LifecycleEvent(org.jboss.msc.service.LifecycleEvent)

Example 2 with LifecycleEvent

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

the class WritableServiceBasedNamingStore method unbind.

public void unbind(final Name name) throws NamingException {
    requireOwner();
    final ServiceName bindName = buildServiceName(name);
    final ServiceController<?> controller = getServiceRegistry().getService(bindName);
    if (controller == null) {
        throw NamingLogger.ROOT_LOGGER.cannotResolveService(bindName);
    }
    final CountDownLatch latch = new CountDownLatch(1);
    controller.addListener(new LifecycleListener() {

        @Override
        public void handleEvent(final ServiceController<?> controller, final LifecycleEvent event) {
            if (event == LifecycleEvent.REMOVED)
                latch.countDown();
        }
    });
    try {
        controller.setMode(ServiceController.Mode.REMOVE);
        latch.await();
    } catch (Exception e) {
        throw namingException("Failed to unbind [" + bindName + "]", e);
    }
}
Also used : ServiceName(org.jboss.msc.service.ServiceName) LifecycleEvent(org.jboss.msc.service.LifecycleEvent) LifecycleListener(org.jboss.msc.service.LifecycleListener) CountDownLatch(java.util.concurrent.CountDownLatch) NamingException(javax.naming.NamingException) OperationNotSupportedException(javax.naming.OperationNotSupportedException) NamingUtils.namingException(org.jboss.as.naming.util.NamingUtils.namingException)

Example 3 with LifecycleEvent

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

the class JMSConnectionFactoryDefinitionInjectionSource method getDefaulResourceAdapter.

static String getDefaulResourceAdapter(DeploymentUnit deploymentUnit) {
    EEModuleDescription eeDescription = deploymentUnit.getAttachment(org.jboss.as.ee.component.Attachments.EE_MODULE_DESCRIPTION);
    if (eeDescription != null) {
        String defaultJndiName = eeDescription.getDefaultResourceJndiNames().getJmsConnectionFactory();
        ContextNames.BindInfo bindInfo = ContextNames.bindInfoFor(defaultJndiName);
        ServiceController binder = deploymentUnit.getServiceRegistry().getService(bindInfo.getBinderServiceName());
        if (binder != null) {
            Object pcf = binder.getService().getValue();
            ServiceController.State currentState = binder.getState();
            CountDownLatch latch = new CountDownLatch(1);
            if (currentState != ServiceController.State.UP) {
                LifecycleListener lifecycleListener = new LifecycleListener() {

                    @Override
                    public void handleEvent(ServiceController<?> controller, LifecycleEvent event) {
                        latch.countDown();
                    }
                };
                try {
                    binder.addListener(lifecycleListener);
                    latch.await();
                } catch (InterruptedException ex) {
                    return null;
                } finally {
                    binder.removeListener(lifecycleListener);
                }
            }
            if (currentState != ServiceController.State.UP) {
                return null;
            }
            // In case of multiple JNDI entries only the 1st is properly bound
            if (pcf != null && pcf instanceof ContextListAndJndiViewManagedReferenceFactory) {
                ManagedReference ref = ((ContextListAndJndiViewManagedReferenceFactory) pcf).getReference();
                Object ra = ref.getInstance();
                if (ra instanceof ActiveMQRAConnectionFactoryImpl) {
                    bindInfo = ContextNames.bindInfoFor(((ActiveMQRAConnectionFactoryImpl) ra).getReference().getClassName());
                    binder = deploymentUnit.getServiceRegistry().getService(bindInfo.getBinderServiceName());
                    if (binder != null) {
                        pcf = binder.getService().getValue();
                    }
                }
            }
            if (pcf != null && pcf instanceof ConnectionFactoryReferenceFactoryService) {
                return ((ConnectionFactoryReferenceFactoryService) pcf).getName();
            }
        }
    }
    return null;
}
Also used : ConnectionFactoryReferenceFactoryService(org.jboss.as.connector.services.resourceadapters.ConnectionFactoryReferenceFactoryService) ManagedReference(org.jboss.as.naming.ManagedReference) LifecycleListener(org.jboss.msc.service.LifecycleListener) CountDownLatch(java.util.concurrent.CountDownLatch) ContextListAndJndiViewManagedReferenceFactory(org.jboss.as.naming.ContextListAndJndiViewManagedReferenceFactory) ActiveMQRAConnectionFactoryImpl(org.apache.activemq.artemis.ra.ActiveMQRAConnectionFactoryImpl) EEModuleDescription(org.jboss.as.ee.component.EEModuleDescription) LifecycleEvent(org.jboss.msc.service.LifecycleEvent) ServiceController(org.jboss.msc.service.ServiceController) ContextNames(org.jboss.as.naming.deployment.ContextNames)

Example 4 with LifecycleEvent

use of org.jboss.msc.service.LifecycleEvent 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 5 with LifecycleEvent

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

the class BinderServiceUtil method installAliasBinderService.

public static void installAliasBinderService(final ServiceTarget serviceTarget, final BindInfo bindInfo, final String alias) {
    final BindInfo aliasBindInfo = ContextNames.bindInfoFor(alias);
    final BinderService aliasBinderService = new BinderService(alias);
    aliasBinderService.getManagedObjectInjector().inject(new AliasManagedReferenceFactory(bindInfo.getAbsoluteJndiName()));
    final ServiceBuilder sb = serviceTarget.addService(aliasBindInfo.getBinderServiceName(), aliasBinderService);
    sb.addDependency(bindInfo.getParentContextServiceName(), ServiceBasedNamingStore.class, aliasBinderService.getNamingStoreInjector());
    sb.requires(bindInfo.getBinderServiceName());
    sb.addListener(new LifecycleListener() {

        private volatile boolean bound;

        @Override
        public void handleEvent(ServiceController<?> controller, LifecycleEvent event) {
            switch(event) {
                case UP:
                    {
                        ROOT_LOGGER.boundJndiName(alias);
                        bound = true;
                        break;
                    }
                case DOWN:
                    {
                        if (bound) {
                            ROOT_LOGGER.unboundJndiName(alias);
                        }
                        break;
                    }
                case REMOVED:
                    {
                        ROOT_LOGGER.debugf("Removed messaging object [%s]", alias);
                        break;
                    }
            }
        }
    });
    sb.install();
}
Also used : BinderService(org.jboss.as.naming.service.BinderService) LifecycleEvent(org.jboss.msc.service.LifecycleEvent) LifecycleListener(org.jboss.msc.service.LifecycleListener) BindInfo(org.jboss.as.naming.deployment.ContextNames.BindInfo) ServiceBuilder(org.jboss.msc.service.ServiceBuilder)

Aggregations

LifecycleEvent (org.jboss.msc.service.LifecycleEvent)22 LifecycleListener (org.jboss.msc.service.LifecycleListener)22 ServiceName (org.jboss.msc.service.ServiceName)11 CountDownLatch (java.util.concurrent.CountDownLatch)8 ContextNames (org.jboss.as.naming.deployment.ContextNames)8 ServiceController (org.jboss.msc.service.ServiceController)7 BinderService (org.jboss.as.naming.service.BinderService)6 ServiceBasedNamingStore (org.jboss.as.naming.ServiceBasedNamingStore)5 ServiceBuilder (org.jboss.msc.service.ServiceBuilder)5 ServiceRegistry (org.jboss.msc.service.ServiceRegistry)3 ExecutorService (java.util.concurrent.ExecutorService)2 DataSourceStatisticsService (org.jboss.as.connector.services.datasources.statistics.DataSourceStatisticsService)2 DataSourceReferenceFactoryService (org.jboss.as.connector.subsystems.datasources.DataSourceReferenceFactoryService)2 ContextListAndJndiViewManagedReferenceFactory (org.jboss.as.naming.ContextListAndJndiViewManagedReferenceFactory)2 ManagedReferenceFactory (org.jboss.as.naming.ManagedReferenceFactory)2 DeploymentUnit (org.jboss.as.server.deployment.DeploymentUnit)2 Module (org.jboss.modules.Module)2 ServiceContainer (org.jboss.msc.service.ServiceContainer)2 StartException (org.jboss.msc.service.StartException)2 CompletableFuture (java.util.concurrent.CompletableFuture)1