/**
   * Starts the weld container
   *
   * @throws IllegalStateException if the container is already running
   */
  public synchronized void start(final StartContext context) {
    if (started) {
      throw WeldLogger.ROOT_LOGGER.alreadyRunning("WeldContainer");
    }
    started = true;

    WeldLogger.DEPLOYMENT_LOGGER.startingWeldService(deploymentName);
    // set up injected services
    addWeldService(SecurityServices.class, securityServices.getValue());
    addWeldService(TransactionServices.class, weldTransactionServices.getValue());

    if (!deployment.getServices().contains(ExecutorServices.class)) {
      addWeldService(ExecutorServices.class, executorServices.getValue());
    }

    ModuleGroupSingletonProvider.addClassLoaders(
        deployment.getModule().getClassLoader(), deployment.getSubDeploymentClassLoaders());

    ClassLoader oldTccl = WildFlySecurityManager.getCurrentContextClassLoaderPrivileged();
    try {
      WildFlySecurityManager.setCurrentContextClassLoaderPrivileged(
          deployment.getModule().getClassLoader());
      bootstrap.startContainer(deploymentName, environment, deployment);
      WeldProvider.containerInitialized(
          Container.instance(deploymentName), getBeanManager(), deployment);
    } finally {
      WildFlySecurityManager.setCurrentContextClassLoaderPrivileged(oldTccl);
    }
  }
 /**
  * Starts the weld container
  *
  * @throws IllegalStateException if the container is already running
  */
 public synchronized void start() {
   if (started) {
     throw WeldMessages.MESSAGES.alreadyRunning("WeldContainer");
   }
   ModuleGroupSingletonProvider.addClassLoaders(
       deployment.getModule().getClassLoader(), deployment.getSubDeploymentClassLoaders());
   started = true;
   ClassLoader oldTccl = SecurityActions.getContextClassLoader();
   try {
     SecurityActions.setContextClassLoader(deployment.getModule().getClassLoader());
     bootstrap.startContainer(environment, deployment);
     bootstrap.startInitialization();
     bootstrap.deployBeans();
     bootstrap.validateBeans();
     bootstrap.endInitialization();
   } finally {
     SecurityActions.setContextClassLoader(oldTccl);
   }
 }
 /**
  * Gets the {@link BeanManager} for a given bean deployment archive id.
  *
  * @throws IllegalStateException if the container is not running
  * @throws IllegalArgumentException if the bean deployment archive id is not found
  */
 public BeanManagerImpl getBeanManager(String beanArchiveId) {
   if (!started) {
     throw WeldMessages.MESSAGES.notStarted("WeldContainer");
   }
   BeanDeploymentArchive beanDeploymentArchive = beanDeploymentArchives.get(beanArchiveId);
   if (beanDeploymentArchive == null) {
     throw WeldMessages.MESSAGES.beanDeploymentNotFound(beanArchiveId);
   }
   return bootstrap.getManager(beanDeploymentArchive);
 }
 /**
  * Stops the container
  *
  * @throws IllegalStateException if the container is not running
  */
 public synchronized void stop() {
   if (!started) {
     throw WeldMessages.MESSAGES.notStarted("WeldContainer");
   }
   ClassLoader oldTccl = SecurityActions.getContextClassLoader();
   try {
     SecurityActions.setContextClassLoader(deployment.getModule().getClassLoader());
     bootstrap.shutdown();
   } finally {
     SecurityActions.setContextClassLoader(oldTccl);
     ModuleGroupSingletonProvider.removeClassLoader(deployment.getModule().getClassLoader());
   }
   started = false;
 }
 /**
  * Stops the container
  *
  * @throws IllegalStateException if the container is not running
  */
 public synchronized void stop(final StopContext context) {
   if (!started) {
     throw WeldLogger.ROOT_LOGGER.notStarted("WeldContainer");
   }
   WeldLogger.DEPLOYMENT_LOGGER.stoppingWeldService(deploymentName);
   ClassLoader oldTccl = WildFlySecurityManager.getCurrentContextClassLoaderPrivileged();
   try {
     WildFlySecurityManager.setCurrentContextClassLoaderPrivileged(
         deployment.getModule().getClassLoader());
     WeldProvider.containerShutDown(Container.instance(deploymentName));
     bootstrap.shutdown();
   } finally {
     WildFlySecurityManager.setCurrentContextClassLoaderPrivileged(oldTccl);
     ModuleGroupSingletonProvider.removeClassLoader(deployment.getModule().getClassLoader());
   }
   started = false;
 }
示例#6
0
  /**
   * Bootstraps a new Weld SE container with the current {@link #containerId}.
   *
   * <p>The container must be shut down properly when an application is stopped. Applications are
   * encouraged to use the try-with-resources statement or invoke {@link WeldContainer#shutdown()}
   * explicitly.
   *
   * <p>However, a shutdown hook is also registered during initialization so that all running
   * containers are shut down automatically when a program exits or VM is terminated. This means
   * that it's not necessary to implement the shutdown logic in a class where a main method is used
   * to start the container.
   *
   * @return the Weld container
   * @see #enableDiscovery()
   * @see WeldContainer#shutdown()
   */
  public WeldContainer initialize() {
    // If also building a synthetic bean archive the check for beans.xml is not necessary
    if (!isSyntheticBeanArchiveRequired()
        && resourceLoader.getResource(WeldDeployment.BEANS_XML) == null) {
      throw CommonLogger.LOG.missingBeansXml();
    }

    final WeldBootstrap bootstrap = new WeldBootstrap();
    final Deployment deployment = createDeployment(resourceLoader, bootstrap);

    final ExternalConfigurationBuilder configurationBuilder =
        new ExternalConfigurationBuilder()
            // weld-se uses CommonForkJoinPoolExecutorServices by default
            .add(EXECUTOR_THREAD_POOL_TYPE.get(), COMMON.toString())
            // weld-se uses relaxed construction by default
            .add(ConfigurationKey.RELAXED_CONSTRUCTION.get(), true);
    for (Entry<String, Object> property : properties.entrySet()) {
      configurationBuilder.add(property.getKey(), property.getValue());
    }
    deployment.getServices().add(ExternalConfiguration.class, configurationBuilder.build());

    // Set up the container
    bootstrap.startContainer(containerId, Environments.SE, deployment);
    // Start the container
    bootstrap.startInitialization();
    // Bean builders - set bean deployment finder
    if (!beanBuilders.isEmpty()) {
      BeanDeploymentFinder beanDeploymentFinder = bootstrap.getBeanDeploymentFinder();
      for (BeanBuilderImpl<?> beanBuilder : beanBuilders) {
        beanBuilder.setBeanDeploymentFinder(beanDeploymentFinder);
      }
    }
    bootstrap.deployBeans();
    bootstrap.validateBeans();
    bootstrap.endInitialization();

    final WeldManager manager =
        bootstrap.getManager(deployment.loadBeanDeploymentArchive(WeldContainer.class));
    final WeldContainer weldContainer = WeldContainer.initialize(containerId, manager, bootstrap);

    initializedContainers.put(containerId, weldContainer);
    return weldContainer;
  }
 /**
  * Gets the {@link BeanManager} linked to the root bean deployment archive. This BeanManager has
  * access to all beans in a deployment
  *
  * @throws IllegalStateException if the container is not running
  */
 public BeanManager getBeanManager() {
   if (!started) {
     throw WeldMessages.MESSAGES.notStarted("WeldContainer");
   }
   return bootstrap.getManager(rootBeanDeploymentArchive);
 }
 /**
  * Gets the {@link BeanManager} linked to the root bean deployment archive. This BeanManager has
  * access to all beans in a deployment
  *
  * @throws IllegalStateException if the container is not running
  */
 public BeanManagerImpl getBeanManager() {
   if (!started) {
     throw WeldLogger.ROOT_LOGGER.notStarted("WeldContainer");
   }
   return bootstrap.getManager(rootBeanDeploymentArchive);
 }