protected Collection<ContextHolder<? extends Context>> createContexts(ServiceRegistry services) { List<ContextHolder<? extends Context>> contexts = new ArrayList<ContextHolder<? extends Context>>(); BeanIdentifierIndex beanIdentifierIndex = services.get(BeanIdentifierIndex.class); /* * Register a full set of bound and unbound contexts. Although we may not use all of * these (e.g. if we are running in a servlet environment) they may be * useful for an application. */ contexts.add( new ContextHolder<ApplicationContext>( new ApplicationContextImpl(contextId), ApplicationContext.class, UnboundLiteral.INSTANCE)); contexts.add( new ContextHolder<SingletonContext>( new SingletonContextImpl(contextId), SingletonContext.class, UnboundLiteral.INSTANCE)); contexts.add( new ContextHolder<BoundSessionContext>( new BoundSessionContextImpl(contextId, beanIdentifierIndex), BoundSessionContext.class, BoundLiteral.INSTANCE)); contexts.add( new ContextHolder<BoundConversationContext>( new BoundConversationContextImpl(contextId, services), BoundConversationContext.class, BoundLiteral.INSTANCE)); contexts.add( new ContextHolder<BoundRequestContext>( new BoundRequestContextImpl(contextId), BoundRequestContext.class, BoundLiteral.INSTANCE)); contexts.add( new ContextHolder<RequestContext>( new RequestContextImpl(contextId), RequestContext.class, UnboundLiteral.INSTANCE)); contexts.add( new ContextHolder<DependentContext>( new DependentContextImpl(services.get(ContextualStore.class)), DependentContext.class, UnboundLiteral.INSTANCE)); services.get(WeldModules.class).postContextRegistration(contextId, services, contexts); /* * Register the contexts with the bean manager and add the beans to the * deployment manager so that they are easily accessible (contexts are app * scoped) */ for (ContextHolder<? extends Context> context : contexts) { deploymentManager.addContext(context.getContext()); deploymentManager.addBean(ContextBean.of(context, deploymentManager)); } return contexts; }
// needs to be resolved once extension beans are deployed private void installFastProcessAnnotatedTypeResolver(ServiceRegistry services) { ClassFileServices classFileServices = services.get(ClassFileServices.class); if (classFileServices != null) { final GlobalObserverNotifierService observers = services.get(GlobalObserverNotifierService.class); try { final FastProcessAnnotatedTypeResolver resolver = new FastProcessAnnotatedTypeResolver(observers.getAllObserverMethods()); services.add(FastProcessAnnotatedTypeResolver.class, resolver); } catch (UnsupportedObserverMethodException e) { BootstrapLogger.LOG.notUsingFastResolver(e.getObserver()); return; } } }
private void setupInitialServices() { if (initialServices.contains(TypeStore.class)) { return; } // instantiate initial services which we need for this phase TypeStore store = new TypeStore(); SharedObjectCache cache = new SharedObjectCache(); ReflectionCache reflectionCache = ReflectionCacheFactory.newInstance(store); ClassTransformer classTransformer = new ClassTransformer(store, cache, reflectionCache, contextId); initialServices.add(TypeStore.class, store); initialServices.add(SharedObjectCache.class, cache); initialServices.add(ReflectionCache.class, reflectionCache); initialServices.add(ClassTransformer.class, classTransformer); }
protected static void verifyServices( ServiceRegistry services, Set<Class<? extends Service>> requiredServices, Object target) { for (Class<? extends Service> serviceType : requiredServices) { if (!services.contains(serviceType)) { throw BootstrapLogger.LOG.unspecifiedRequiredService(serviceType.getName(), target); } } }
public BeanDeployment( BeanDeploymentArchive beanDeploymentArchive, BeanManagerImpl deploymentManager, ServiceRegistry deploymentServices, Collection<ContextHolder<? extends Context>> contexts) { this.beanDeploymentArchive = beanDeploymentArchive; EjbDescriptors ejbDescriptors = new EjbDescriptors(); beanDeploymentArchive.getServices().add(EjbDescriptors.class, ejbDescriptors); if (beanDeploymentArchive.getServices().get(ResourceLoader.class) == null) { beanDeploymentArchive.getServices().add(ResourceLoader.class, DefaultResourceLoader.INSTANCE); } ServiceRegistry services = new SimpleServiceRegistry(); services.addAll(deploymentServices.entrySet()); services.addAll(beanDeploymentArchive.getServices().entrySet()); services.add( EJBApiAbstraction.class, new EJBApiAbstraction(beanDeploymentArchive.getServices().get(ResourceLoader.class))); services.add( JsfApiAbstraction.class, new JsfApiAbstraction(beanDeploymentArchive.getServices().get(ResourceLoader.class))); services.add( PersistenceApiAbstraction.class, new PersistenceApiAbstraction( beanDeploymentArchive.getServices().get(ResourceLoader.class))); services.add( WSApiAbstraction.class, new WSApiAbstraction(beanDeploymentArchive.getServices().get(ResourceLoader.class))); this.beanManager = BeanManagerImpl.newManager( deploymentManager, beanDeploymentArchive.getId(), services, Enabled.of( beanDeploymentArchive.getBeansXml(), beanDeploymentArchive.getServices().get(ResourceLoader.class))); log.debug( ENABLED_ALTERNATIVES, this.beanManager, beanManager.getEnabled().getAlternativeClasses(), beanManager.getEnabled().getAlternativeStereotypes()); log.debug(ENABLED_DECORATORS, this.beanManager, beanManager.getEnabled().getDecorators()); log.debug(ENABLED_INTERCEPTORS, this.beanManager, beanManager.getEnabled().getInterceptors()); if (beanManager.getServices().contains(EjbServices.class)) { // Must populate EJB cache first, as we need it to detect whether a // bean is an EJB! ejbDescriptors.addAll(beanDeploymentArchive.getEjbs()); } beanDeployer = new BeanDeployer(beanManager, ejbDescriptors); // Must at the Manager bean straight away, as it can be injected during startup! beanManager.addBean(new BeanManagerBean(beanManager)); this.contexts = contexts; }
private void addImplementationServices(ServiceRegistry services) { final WeldModules modules = new WeldModules(); services.add(WeldModules.class, modules); final WeldConfiguration configuration = services.get(WeldConfiguration.class); services.add(SlimAnnotatedTypeStore.class, new SlimAnnotatedTypeStoreImpl()); if (services.get(ClassTransformer.class) == null) { throw new IllegalStateException(ClassTransformer.class.getSimpleName() + " not installed."); } services.add( MemberTransformer.class, new MemberTransformer(services.get(ClassTransformer.class))); services.add( MetaAnnotationStore.class, new MetaAnnotationStore(services.get(ClassTransformer.class))); BeanIdentifierIndex beanIdentifierIndex = null; if (configuration.getBooleanProperty(ConfigurationKey.BEAN_IDENTIFIER_INDEX_OPTIMIZATION)) { beanIdentifierIndex = new BeanIdentifierIndex(); services.add(BeanIdentifierIndex.class, beanIdentifierIndex); } services.add(ContextualStore.class, new ContextualStoreImpl(contextId, beanIdentifierIndex)); services.add(CurrentInjectionPoint.class, new CurrentInjectionPoint()); services.add(CurrentEventMetadata.class, new CurrentEventMetadata()); services.add( SpecializationAndEnablementRegistry.class, new SpecializationAndEnablementRegistry()); services.add(MissingDependenciesRegistry.class, new MissingDependenciesRegistry()); /* * Setup ExecutorServices */ ExecutorServices executor = services.get(ExecutorServices.class); if (executor == null) { executor = ExecutorServicesFactory.create(DefaultResourceLoader.INSTANCE, configuration); if (executor != null) { services.add(ExecutorServices.class, executor); } } services.add( RequiredAnnotationDiscovery.class, new RequiredAnnotationDiscovery(services.get(ReflectionCache.class))); services.add(GlobalEnablementBuilder.class, new GlobalEnablementBuilder()); if (!services.contains(HttpContextActivationFilter.class)) { services.add( HttpContextActivationFilter.class, AcceptingHttpContextActivationFilter.INSTANCE); } services.add(ProtectionDomainCache.class, new ProtectionDomainCache()); services.add(ProxyInstantiator.class, ProxyInstantiator.Factory.create(configuration)); services.add(ObserverNotifierFactory.class, DefaultObserverNotifierFactory.INSTANCE); services.add(ResourceInjectionFactory.class, new ResourceInjectionFactory()); modules.postServiceRegistration(contextId, services); /* * Setup Validator */ if (configuration.getBooleanProperty(ConfigurationKey.CONCURRENT_DEPLOYMENT) && services.contains(ExecutorServices.class)) { services.add( Validator.class, new ConcurrentValidator(modules.getPluggableValidators(), executor)); } else { services.add(Validator.class, new Validator(modules.getPluggableValidators())); } GlobalObserverNotifierService observerNotificationService = new GlobalObserverNotifierService(services, contextId); services.add(GlobalObserverNotifierService.class, observerNotificationService); /* * Preloader for container lifecycle events */ ContainerLifecycleEventPreloader preloader = null; int preloaderThreadPoolSize = configuration.getIntegerProperty(ConfigurationKey.PRELOADER_THREAD_POOL_SIZE); if (preloaderThreadPoolSize > 0 && Permissions.hasPermission(Permissions.MODIFY_THREAD_GROUP)) { preloader = new ContainerLifecycleEventPreloader( preloaderThreadPoolSize, observerNotificationService.getGlobalLenientObserverNotifier()); } services.add( ContainerLifecycleEvents.class, new ContainerLifecycleEvents(preloader, services.get(RequiredAnnotationDiscovery.class))); if (isEEModulesAwareEnvironment()) { services.add(BeanDeploymentModules.class, new BeanDeploymentModules(contextId, services)); } }
public WeldRuntime startContainer( String contextId, Environment environment, Deployment deployment) { if (deployment == null) { throw BootstrapLogger.LOG.deploymentRequired(); } checkApiVersion(); final ServiceRegistry registry = deployment.getServices(); // initiate part of registry in order to allow access to WeldConfiguration new AdditionalServiceLoader(deployment).loadAdditionalServices(registry); // Resource Loader has to be loaded prior to WeldConfiguration if (!registry.contains(ResourceLoader.class)) { registry.add(ResourceLoader.class, DefaultResourceLoader.INSTANCE); } WeldConfiguration configuration = new WeldConfiguration(registry, deployment); registry.add(WeldConfiguration.class, configuration); String finalContextId = BeanDeployments.getFinalId( contextId, registry.get(WeldConfiguration.class).getStringProperty(ROLLING_UPGRADES_ID_DELIMITER)); this.contextId = finalContextId; this.deployment = deployment; this.environment = environment; if (this.extensions == null) { setExtensions(deployment.getExtensions()); } // Add extension to register built-in components this.extensions.add(MetadataImpl.from(new WeldExtension())); // Add Weld extensions String vetoTypeRegex = configuration.getStringProperty( ConfigurationKey.VETO_TYPES_WITHOUT_BEAN_DEFINING_ANNOTATION); if (!vetoTypeRegex.isEmpty()) { this.extensions.add(MetadataImpl.from(new WeldVetoExtension(vetoTypeRegex))); } // Finish the rest of registry init, setupInitialServices() requires already changed // finalContextId setupInitialServices(); registry.addAll(initialServices.entrySet()); if (!registry.contains(ProxyServices.class)) { registry.add(ProxyServices.class, new SimpleProxyServices()); } if (!registry.contains(SecurityServices.class)) { registry.add(SecurityServices.class, NoopSecurityServices.INSTANCE); } addImplementationServices(registry); verifyServices(registry, environment.getRequiredDeploymentServices(), contextId); if (!registry.contains(TransactionServices.class)) { BootstrapLogger.LOG.jtaUnavailable(); } this.deploymentManager = BeanManagerImpl.newRootManager(finalContextId, "deployment", registry); Container.initialize( finalContextId, deploymentManager, ServiceRegistries.unmodifiableServiceRegistry(deployment.getServices())); getContainer().setState(ContainerState.STARTING); this.contexts = createContexts(registry); this.bdaMapping = new BeanDeploymentArchiveMapping(); this.deploymentVisitor = new DeploymentVisitor(deploymentManager, environment, deployment, contexts, bdaMapping); if (deployment instanceof CDI11Deployment) { registry.add( BeanManagerLookupService.class, new BeanManagerLookupService( (CDI11Deployment) deployment, bdaMapping.getBdaToBeanManagerMap())); } else { BootstrapLogger.LOG.legacyDeploymentMetadataProvided(); } // Read the deployment structure, bdaMapping will be the physical structure // as caused by the presence of beans.xml deploymentVisitor.visit(); return new WeldRuntime(finalContextId, deploymentManager, bdaMapping.getBdaToBeanManagerMap()); }