@Override
  public void deploy(DeploymentPhaseContext phaseContext) throws DeploymentUnitProcessingException {
    final DeploymentUnit deploymentUnit = phaseContext.getDeploymentUnit();
    final EEModuleDescription moduleDescription =
        deploymentUnit.getAttachment(Attachments.EE_MODULE_DESCRIPTION);

    if (moduleDescription == null) {
      return;
    }

    final ServiceTarget serviceTarget = phaseContext.getServiceTarget();
    // if this is a war we need to bind to the modules comp namespace
    if (DeploymentTypeMarker.isType(DeploymentType.WAR, deploymentUnit)
        || DeploymentTypeMarker.isType(DeploymentType.APPLICATION_CLIENT, deploymentUnit)) {
      final ServiceName moduleContextServiceName =
          ContextNames.contextServiceNameOfModule(
              moduleDescription.getApplicationName(), moduleDescription.getModuleName());
      bindServices(deploymentUnit, serviceTarget, moduleContextServiceName);
    }

    for (ComponentDescription component : moduleDescription.getComponentDescriptions()) {
      if (component.getNamingMode() == ComponentNamingMode.CREATE) {
        final ServiceName compContextServiceName =
            ContextNames.contextServiceNameOfComponent(
                moduleDescription.getApplicationName(),
                moduleDescription.getModuleName(),
                component.getComponentName());
        bindServices(deploymentUnit, serviceTarget, compContextServiceName);
      }
    }
  }
  @Override
  public void deploy(DeploymentPhaseContext phaseContext) throws DeploymentUnitProcessingException {

    final DeploymentUnit deploymentUnit = phaseContext.getDeploymentUnit();
    final DeploymentUnit topLevelDeployment =
        deploymentUnit.getParent() == null ? deploymentUnit : deploymentUnit.getParent();
    final ServiceTarget serviceTarget = phaseContext.getServiceTarget();
    if (!WeldDeploymentMarker.isPartOfWeldDeployment(topLevelDeployment)) {
      return;
    }

    final Collection<ServiceName> dependencies =
        deploymentUnit.getAttachment(Attachments.JNDI_DEPENDENCIES);

    BeanDeploymentArchiveImpl rootBda =
        deploymentUnit.getAttachment(WeldAttachments.DEPLOYMENT_ROOT_BEAN_DEPLOYMENT_ARCHIVE);
    if (rootBda == null) {
      // this archive is not actually a bean archive.
      // then use the top level root bda
      rootBda =
          topLevelDeployment.getAttachment(WeldAttachments.DEPLOYMENT_ROOT_BEAN_DEPLOYMENT_ARCHIVE);
    }
    if (rootBda == null) {
      WeldLogger.ROOT_LOGGER.couldNotFindBeanManagerForDeployment(deploymentUnit.getName());
      return;
    }

    final ServiceName weldServiceName =
        topLevelDeployment.getServiceName().append(WeldBootstrapService.SERVICE_NAME);

    // add the BeanManager service
    final ServiceName beanManagerServiceName = BeanManagerService.serviceName(deploymentUnit);
    BeanManagerService beanManagerService = new BeanManagerService(rootBda.getId());
    serviceTarget
        .addService(beanManagerServiceName, beanManagerService)
        .addDependency(
            weldServiceName, WeldBootstrapService.class, beanManagerService.getWeldContainer())
        .install();
    ;

    final EEModuleDescription moduleDescription =
        deploymentUnit.getAttachment(org.jboss.as.ee.component.Attachments.EE_MODULE_DESCRIPTION);

    if (moduleDescription == null) {
      return;
    }

    // hack to set up a java:comp binding for jar deployments as well as wars
    if (DeploymentTypeMarker.isType(DeploymentType.WAR, deploymentUnit)
        || deploymentUnit.getName().endsWith(".jar")) {
      // bind the bean manager to JNDI
      final ServiceName moduleContextServiceName =
          ContextNames.contextServiceNameOfModule(
              moduleDescription.getApplicationName(), moduleDescription.getModuleName());
      bindBeanManager(
          serviceTarget,
          beanManagerServiceName,
          moduleContextServiceName,
          dependencies,
          phaseContext.getServiceRegistry());
    }

    // bind the bm into java:comp for all components that require it
    for (ComponentDescription component : moduleDescription.getComponentDescriptions()) {
      if (component.getNamingMode() == ComponentNamingMode.CREATE) {
        final ServiceName compContextServiceName =
            ContextNames.contextServiceNameOfComponent(
                moduleDescription.getApplicationName(),
                moduleDescription.getModuleName(),
                component.getComponentName());
        bindBeanManager(
            serviceTarget,
            beanManagerServiceName,
            compContextServiceName,
            dependencies,
            phaseContext.getServiceRegistry());
      }
    }
    deploymentUnit.addToAttachmentList(Attachments.SETUP_ACTIONS, new WeldContextSetup());
  }
  private void startDataSource(
      final AbstractDataSourceService dataSourceService,
      final String jndiName,
      final EEModuleDescription moduleDescription,
      final ResolutionContext context,
      final ServiceTarget serviceTarget,
      final ServiceBuilder valueSourceServiceBuilder,
      final Injector<ManagedReferenceFactory> injector) {

    final ServiceName dataSourceServiceName =
        AbstractDataSourceService.SERVICE_NAME_BASE.append(
            "DataSourceDefinition",
            moduleDescription.getApplicationName(),
            moduleDescription.getModuleName(),
            jndiName);
    final ServiceBuilder<?> dataSourceServiceBuilder =
        Services.addServerExecutorDependency(
                serviceTarget.addService(dataSourceServiceName, dataSourceService),
                dataSourceService.getExecutorServiceInjector(),
                false)
            .addDependency(
                ConnectorServices.IRONJACAMAR_MDR,
                MetadataRepository.class,
                dataSourceService.getMdrInjector())
            .addDependency(
                ConnectorServices.RA_REPOSITORY_SERVICE,
                ResourceAdapterRepository.class,
                dataSourceService.getRaRepositoryInjector())
            .addDependency(ConnectorServices.BOOTSTRAP_CONTEXT_SERVICE.append(DEFAULT_NAME))
            .addDependency(
                ConnectorServices.TRANSACTION_INTEGRATION_SERVICE,
                TransactionIntegration.class,
                dataSourceService.getTransactionIntegrationInjector())
            .addDependency(
                ConnectorServices.MANAGEMENT_REPOSITORY_SERVICE,
                ManagementRepository.class,
                dataSourceService.getManagementRepositoryInjector())
            .addDependency(
                SubjectFactoryService.SERVICE_NAME,
                SubjectFactory.class,
                dataSourceService.getSubjectFactoryInjector())
            .addDependency(
                ConnectorServices.CCM_SERVICE,
                CachedConnectionManager.class,
                dataSourceService.getCcmInjector())
            .addDependency(
                ConnectorServices.JDBC_DRIVER_REGISTRY_SERVICE,
                DriverRegistry.class,
                dataSourceService.getDriverRegistryInjector())
            .addDependency(NamingService.SERVICE_NAME);
    final ContextNames.BindInfo bindInfo =
        ContextNames.bindInfoForEnvEntry(
            context.getApplicationName(),
            context.getModuleName(),
            context.getComponentName(),
            !context.isCompUsesModule(),
            jndiName);

    final DataSourceReferenceFactoryService referenceFactoryService =
        new DataSourceReferenceFactoryService();
    final ServiceName referenceFactoryServiceName =
        DataSourceReferenceFactoryService.SERVICE_NAME_BASE.append(bindInfo.getBinderServiceName());
    final ServiceBuilder<?> referenceBuilder =
        serviceTarget
            .addService(referenceFactoryServiceName, referenceFactoryService)
            .addDependency(
                dataSourceServiceName,
                javax.sql.DataSource.class,
                referenceFactoryService.getDataSourceInjector());

    final BinderService binderService = new BinderService(bindInfo.getBindName(), this);
    final ServiceBuilder<?> binderBuilder =
        serviceTarget
            .addService(bindInfo.getBinderServiceName(), binderService)
            .addDependency(
                referenceFactoryServiceName,
                ManagedReferenceFactory.class,
                binderService.getManagedObjectInjector())
            .addDependency(
                bindInfo.getParentContextServiceName(),
                ServiceBasedNamingStore.class,
                binderService.getNamingStoreInjector())
            .addListener(
                new AbstractServiceListener<Object>() {
                  public void transition(
                      final ServiceController<? extends Object> controller,
                      final ServiceController.Transition transition) {
                    switch (transition) {
                      case STARTING_to_UP:
                        {
                          SUBSYSTEM_DATASOURCES_LOGGER.boundDataSource(jndiName);
                          break;
                        }
                      case START_REQUESTED_to_DOWN:
                        {
                          SUBSYSTEM_DATASOURCES_LOGGER.unboundDataSource(jndiName);
                          break;
                        }
                      case REMOVING_to_REMOVED:
                        {
                          SUBSYSTEM_DATASOURCES_LOGGER.debugf(
                              "Removed JDBC Data-source [%s]", jndiName);
                          break;
                        }
                    }
                  }
                });

    dataSourceServiceBuilder.setInitialMode(ServiceController.Mode.ACTIVE).install();
    referenceBuilder.setInitialMode(ServiceController.Mode.ACTIVE).install();
    binderBuilder.setInitialMode(ServiceController.Mode.ACTIVE).install();

    valueSourceServiceBuilder.addDependency(
        bindInfo.getBinderServiceName(), ManagedReferenceFactory.class, injector);
  }
  @Override
  public void deploy(final DeploymentPhaseContext phaseContext)
      throws DeploymentUnitProcessingException {

    final DeploymentUnit deploymentUnit = phaseContext.getDeploymentUnit();
    final EEModuleDescription moduleDescription =
        deploymentUnit.getAttachment(Attachments.EE_MODULE_DESCRIPTION);
    final Module module =
        deploymentUnit.getAttachment(org.jboss.as.server.deployment.Attachments.MODULE);

    final EjbJarMetaData ejbJarMetaData =
        deploymentUnit.getAttachment(EjbDeploymentAttachmentKeys.EJB_JAR_METADATA);

    ServiceName defaultTimerPersistenceService =
        TimerPersistence.SERVICE_NAME.append(defaultTimerDataStore);
    final Map<String, ServiceName> timerPersistenceServices = new HashMap<String, ServiceName>();
    // if this is an EJB deployment then create an EJB module level TimerServiceRegistry which can
    // be used by the timer services
    // of all EJB components that belong to this EJB module.
    final TimerServiceRegistry timerServiceRegistry =
        EjbDeploymentMarker.isEjbDeployment(deploymentUnit) ? new TimerServiceRegistry() : null;

    if (ejbJarMetaData != null && ejbJarMetaData.getAssemblyDescriptor() != null) {
      List<TimerServiceMetaData> timerService =
          ejbJarMetaData.getAssemblyDescriptor().getAny(TimerServiceMetaData.class);
      if (timerService != null) {
        for (TimerServiceMetaData data : timerService) {
          if (data.getEjbName().equals("*")) {
            defaultTimerPersistenceService =
                TimerPersistence.SERVICE_NAME.append(data.getDataStoreName());
          } else {
            timerPersistenceServices.put(
                data.getEjbName(), TimerPersistence.SERVICE_NAME.append(data.getDataStoreName()));
          }
        }
      }
    }
    final ServiceName finalDefaultTimerPersistenceService = defaultTimerPersistenceService;

    for (final ComponentDescription componentDescription :
        moduleDescription.getComponentDescriptions()) {

      if (componentDescription.isTimerServiceApplicable()) {
        if (componentDescription.isTimerServiceRequired()) {
          // the component has timeout methods, it needs a 'real' timer service
          final String deploymentName;
          if (moduleDescription.getDistinctName() == null
              || moduleDescription.getDistinctName().length() == 0) {
            deploymentName =
                moduleDescription.getApplicationName() + "." + moduleDescription.getModuleName();
          } else {
            deploymentName =
                moduleDescription.getApplicationName()
                    + "."
                    + moduleDescription.getModuleName()
                    + "."
                    + moduleDescription.getDistinctName();
          }

          ROOT_LOGGER.debug(
              "Installing timer service for component " + componentDescription.getComponentName());
          componentDescription
              .getConfigurators()
              .add(
                  new ComponentConfigurator() {
                    @Override
                    public void configure(
                        final DeploymentPhaseContext context,
                        final ComponentDescription description,
                        final ComponentConfiguration configuration)
                        throws DeploymentUnitProcessingException {
                      final EJBComponentDescription ejbComponentDescription =
                          (EJBComponentDescription) description;

                      final ServiceName invokerServiceName =
                          ejbComponentDescription
                              .getServiceName()
                              .append(TimedObjectInvokerImpl.SERVICE_NAME);
                      final TimedObjectInvokerImpl invoker =
                          new TimedObjectInvokerImpl(deploymentName, module);
                      context
                          .getServiceTarget()
                          .addService(invokerServiceName, invoker)
                          .addDependency(
                              componentDescription.getCreateServiceName(),
                              EJBComponent.class,
                              invoker.getEjbComponent())
                          .install();

                      // install the timer create service
                      final ServiceName serviceName =
                          componentDescription
                              .getServiceName()
                              .append(TimerServiceImpl.SERVICE_NAME);
                      final TimerServiceImpl service =
                          new TimerServiceImpl(
                              ejbComponentDescription.getScheduleMethods(),
                              serviceName,
                              timerServiceRegistry);
                      final ServiceBuilder<javax.ejb.TimerService> createBuilder =
                          context.getServiceTarget().addService(serviceName, service);
                      createBuilder.addDependency(
                          TIMER_SERVICE_NAME, Timer.class, service.getTimerInjectedValue());
                      createBuilder.addDependency(
                          componentDescription.getCreateServiceName(),
                          EJBComponent.class,
                          service.getEjbComponentInjectedValue());
                      createBuilder.addDependency(
                          timerServiceThreadPool,
                          ExecutorService.class,
                          service.getExecutorServiceInjectedValue());
                      if (timerPersistenceServices.containsKey(
                          ejbComponentDescription.getEJBName())) {
                        createBuilder.addDependency(
                            timerPersistenceServices.get(ejbComponentDescription.getEJBName()),
                            TimerPersistence.class,
                            service.getTimerPersistence());
                      } else {
                        createBuilder.addDependency(
                            finalDefaultTimerPersistenceService,
                            TimerPersistence.class,
                            service.getTimerPersistence());
                      }
                      createBuilder.addDependency(
                          invokerServiceName,
                          TimedObjectInvoker.class,
                          service.getTimedObjectInvoker());
                      createBuilder.install();
                      ejbComponentDescription.setTimerService(service);
                      // inject the timer service directly into the start service
                      configuration
                          .getStartDependencies()
                          .add(
                              new DependencyConfigurator<ComponentStartService>() {
                                @Override
                                public void configureDependency(
                                    final ServiceBuilder<?> serviceBuilder,
                                    final ComponentStartService service)
                                    throws DeploymentUnitProcessingException {
                                  serviceBuilder.addDependency(serviceName);
                                }
                              });
                    }
                  });
        } else {
          // the EJB is of a type that could have a timer service, but has no timer methods.
          // just bind the non-functional timer service
          componentDescription
              .getConfigurators()
              .add(
                  new ComponentConfigurator() {
                    @Override
                    public void configure(
                        final DeploymentPhaseContext context,
                        final ComponentDescription description,
                        final ComponentConfiguration configuration)
                        throws DeploymentUnitProcessingException {
                      final EJBComponentDescription ejbComponentDescription =
                          (EJBComponentDescription) description;
                      final ServiceName nonFunctionalTimerServiceName =
                          NonFunctionalTimerService.serviceNameFor(ejbComponentDescription);
                      final NonFunctionalTimerService nonFunctionalTimerService;
                      if (ejbComponentDescription instanceof StatefulComponentDescription) {
                        // for stateful beans, use a different error message that gets thrown from
                        // the NonFunctionalTimerService
                        nonFunctionalTimerService =
                            new NonFunctionalTimerService(
                                EjbMessages.MESSAGES.timerServiceMethodNotAllowedForSFSB(
                                    ejbComponentDescription.getComponentName()),
                                timerServiceRegistry);
                      } else {
                        nonFunctionalTimerService =
                            new NonFunctionalTimerService(
                                EjbMessages.MESSAGES.ejbHasNoTimerMethods(), timerServiceRegistry);
                      }
                      // add the non-functional timer service as a MSC service
                      context
                          .getServiceTarget()
                          .addService(nonFunctionalTimerServiceName, nonFunctionalTimerService)
                          .install();
                      // set the timer service in the EJB component
                      ejbComponentDescription.setTimerService(nonFunctionalTimerService);
                      // now we want the EJB component to depend on this non-functional timer
                      // service to start
                      configuration
                          .getStartDependencies()
                          .add(
                              new DependencyConfigurator<ComponentStartService>() {
                                @Override
                                public void configureDependency(
                                    ServiceBuilder<?> serviceBuilder, ComponentStartService service)
                                    throws DeploymentUnitProcessingException {
                                  serviceBuilder.addDependency(nonFunctionalTimerServiceName);
                                }
                              });
                    }
                  });
        }
      }
    }
  }