public void validateSpecialization(BeanManagerImpl manager) { SpecializationAndEnablementRegistry registry = manager.getServices().get(SpecializationAndEnablementRegistry.class); for (Entry<AbstractBean<?, ?>, Long> entry : registry.getBeansSpecializedInAnyDeploymentAsMap().entrySet()) { if (entry.getValue() > 1) { throw ValidatorLogger.LOG.beanSpecializedTooManyTimes(entry.getKey()); } } }
public static boolean isObserverMethodEnabled(ObserverMethod<?> method, BeanManagerImpl manager) { if (method instanceof ObserverMethodImpl<?, ?>) { Bean<?> declaringBean = Reflections.<ObserverMethodImpl<?, ?>> cast(method).getDeclaringBean(); return manager.getServices().get(SpecializationAndEnablementRegistry.class).isCandidateForLifecycleEvent(declaringBean); } return true; }
public boolean isCandidateForLifecycleEvent(Bean<?> bean) { if (bean instanceof AbstractProducerBean<?, ?, ?>) { AbstractProducerBean<?, ?, ?> producer = cast(bean); if (!isCandidateForLifecycleEvent(producer.getDeclaringBean())) { return false; } } return isEnabledInAnyBeanDeployment(bean) && !isSpecializedInAnyBeanDeployment(bean); }
protected void processBeanAttributes(Iterable<? extends AbstractBean<?, ?>> beans) { if (!containerLifecycleEvents.isProcessBeanAttributesObserved()) { return; } if (!beans.iterator().hasNext()) { return; // exit recursion } Collection<AbstractBean<?, ?>> vetoedBeans = new HashSet<AbstractBean<?, ?>>(); Collection<AbstractBean<?, ?>> previouslySpecializedBeans = new HashSet<AbstractBean<?, ?>>(); for (AbstractBean<?, ?> bean : beans) { // fire ProcessBeanAttributes for class beans boolean vetoed = fireProcessBeanAttributes(bean); if (vetoed) { vetoedBeans.add(bean); } } // remove vetoed class beans for (AbstractBean<?, ?> bean : vetoedBeans) { if (bean.isSpecializing()) { previouslySpecializedBeans.addAll(specializationAndEnablementRegistry.resolveSpecializedBeans(bean)); specializationAndEnablementRegistry.vetoSpecializingBean(bean); } getEnvironment().vetoBean(bean); } // if a specializing bean was vetoed, let's process the specializing bean now processBeans(previouslySpecializedBeans); }
ResourceLoader resourceLoader = registry.get(ResourceLoader.class); if (resourceLoader == null) { resourceLoader = DefaultResourceLoader.INSTANCE; registry.add(ResourceLoader.class, resourceLoader); services.addAll(deploymentServices.entrySet()); services.addAll(registry.entrySet()); services.add(InterceptorsApiAbstraction.class, new InterceptorsApiAbstraction(resourceLoader)); services.add(AnnotationApiAbstraction.class, new AnnotationApiAbstraction(resourceLoader)); this.beanManager = BeanManagerImpl.newManager(deploymentManager, BeanDeployments.getFinalId(beanDeploymentArchive.getId(), services.get(WeldConfiguration.class).getStringProperty(ROLLING_UPGRADES_ID_DELIMITER)), services); services.add(InjectionTargetService.class, new InjectionTargetService(beanManager)); services.add(InterceptionFactoryDataCache.class, new InterceptionFactoryDataCache(beanManager)); services.addIfAbsent(EjbSupport.class, EjbSupport.NOOP_IMPLEMENTATION); if (services.get(WeldConfiguration.class).getBooleanProperty(CONCURRENT_DEPLOYMENT) && services.contains(ExecutorServices.class)) { beanDeployer = new ConcurrentBeanDeployer(beanManager, deploymentServices); } else { beanDeployer = new BeanDeployer(beanManager, deploymentServices); beanManager.getServices().get(SpecializationAndEnablementRegistry.class).registerEnvironment(beanManager, beanDeployer.getEnvironment(), additionalBeanArchive); beanManager.addBean(new BeanManagerBean(beanManager)); beanManager.addBean(new BeanManagerImplBean(beanManager));
private void addImplementationServices(ServiceRegistry services) { 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."); if (configuration.getBooleanProperty(ConfigurationKey.BEAN_IDENTIFIER_INDEX_OPTIMIZATION)) { beanIdentifierIndex = new BeanIdentifierIndex(); services.add(BeanIdentifierIndex.class, beanIdentifierIndex); services.add(CurrentInvocationInjectionPoint.class, new CurrentInvocationInjectionPoint()); services.add(CurrentEventMetadata.class, new CurrentEventMetadata()); services.add(SpecializationAndEnablementRegistry.class, new SpecializationAndEnablementRegistry()); services.add(MissingDependenciesRegistry.class, new MissingDependenciesRegistry()); if (configuration.getBooleanProperty(ConfigurationKey.CONCURRENT_DEPLOYMENT) && services.contains(ExecutorServices.class)) { services.add(Validator.class, new ConcurrentValidator(executor)); } else { 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))); services.add(GlobalEnablementBuilder.class, new GlobalEnablementBuilder());
private void addBean(Bean<?> bean, List<Bean<?>> beanList, List<Bean<?>> transitiveBeans) { if (beanSet.add(bean)) { if (bean.isAlternative() && (!registry.isEnabledInAnyBeanDeployment(bean) && !(bean instanceof Prioritized)) && !isConfiguratorBeanWithPriority(bean)) { BootstrapLogger.LOG.foundDisabledAlternative(bean); } else if (registry.isSpecializedInAnyBeanDeployment(bean)) { BootstrapLogger.LOG.foundSpecializedBean(bean); } else if (bean instanceof AbstractProducerBean<?, ?, ?> && registry.isSpecializedInAnyBeanDeployment(((AbstractProducerBean<?, ?, ?>) bean).getDeclaringBean())) { BootstrapLogger.LOG.foundProducerOfSpecializedBean(bean); specializedBeans.add(bean); } else { BootstrapLogger.LOG.foundBean(bean); beanList.add(bean); if (bean instanceof SessionBean) { SessionBean<?> enterpriseBean = (SessionBean<?>) bean; enterpriseBeans.put(enterpriseBean.getEjbDescriptor(), enterpriseBean); } if (bean instanceof PassivationCapable) { getServices().get(ContextualStore.class).putIfAbsent(bean); } registerBeanNamespace(bean); // New beans (except for SessionBeans) and most built in beans aren't resolvable transitively if (bean instanceof ExtensionBean || bean instanceof SessionBean || (!(bean instanceof NewBean) && !(bean instanceof AbstractBuiltInBean<?>))) { transitiveBeans.add(bean); } } } }
protected Set<? extends AbstractBean<?, ?>> getSpecializedBeans() { return getBeanManager().getServices().get(SpecializationAndEnablementRegistry.class).resolveSpecializedBeans(this); }
protected <T, S> boolean fireProcessBeanAttributes(AbstractBean<T, S> bean) { if (!specializationAndEnablementRegistry.isCandidateForLifecycleEvent(bean)) { return false; } ProcessBeanAttributesImpl<T> event = containerLifecycleEvents.fireProcessBeanAttributes(getManager(), bean, bean.getAnnotated(), bean.getType()); if (event == null) { return false; } if (event.isVeto()) { return true; } if (event.isDirty()) { bean.setAttributes(ExternalBeanAttributesFactory.<T>of(event.getBeanAttributesInternal(), manager)); bean.checkSpecialization(); } return false; } }
@Override public Set<? extends AbstractBean<?, ?>> apply(Bean<?> specializingBean) { Set<? extends AbstractBean<?, ?>> result = null; if (specializingBean instanceof AbstractClassBean<?>) { result = apply((AbstractClassBean<?>) specializingBean); } if (specializingBean instanceof ProducerMethod<?, ?>) { result = apply((ProducerMethod<?, ?>) specializingBean); } if (result != null) { if (isEnabledInAnyBeanDeployment(specializingBean)) { for (AbstractBean<?, ?> specializedBean : result) { AtomicLong value = specializedBeansMap.get(specializedBean); if (value == null) { value = new AtomicLong(0); AtomicLong previous = specializedBeansMap.putIfAbsent(specializedBean, value); if (previous != null) { value = previous; } } value.incrementAndGet(); } } return result; } throw new IllegalArgumentException("Unsupported bean type " + specializingBean); }
ResourceLoader resourceLoader = registry.get(ResourceLoader.class); if (resourceLoader == null) { resourceLoader = DefaultResourceLoader.INSTANCE; registry.add(ResourceLoader.class, resourceLoader); services.addAll(deploymentServices.entrySet()); services.addAll(registry.entrySet()); services.add(InterceptorsApiAbstraction.class, new InterceptorsApiAbstraction(resourceLoader)); services.add(AnnotationApiAbstraction.class, new AnnotationApiAbstraction(resourceLoader)); this.beanManager = BeanManagerImpl.newManager(deploymentManager, BeanDeployments.getFinalId(beanDeploymentArchive.getId(), services.get(WeldConfiguration.class).getStringProperty(ROLLING_UPGRADES_ID_DELIMITER)), services); services.add(InjectionTargetService.class, new InjectionTargetService(beanManager)); services.add(InterceptionFactoryDataCache.class, new InterceptionFactoryDataCache(beanManager)); services.addIfAbsent(EjbSupport.class, EjbSupport.NOOP_IMPLEMENTATION); if (services.get(WeldConfiguration.class).getBooleanProperty(CONCURRENT_DEPLOYMENT) && services.contains(ExecutorServices.class)) { beanDeployer = new ConcurrentBeanDeployer(beanManager, deploymentServices); } else { beanDeployer = new BeanDeployer(beanManager, deploymentServices); beanManager.getServices().get(SpecializationAndEnablementRegistry.class).registerEnvironment(beanManager, beanDeployer.getEnvironment(), additionalBeanArchive); beanManager.addBean(new BeanManagerBean(beanManager)); beanManager.addBean(new BeanManagerImplBean(beanManager));
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."); if (configuration.getBooleanProperty(ConfigurationKey.BEAN_IDENTIFIER_INDEX_OPTIMIZATION)) { beanIdentifierIndex = new BeanIdentifierIndex(); services.add(BeanIdentifierIndex.class, 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()); if (configuration.getBooleanProperty(ConfigurationKey.CONCURRENT_DEPLOYMENT) && services.contains(ExecutorServices.class)) { validator = new ConcurrentValidator(modules.getPluggableValidators(), executor, UnusedBeans.isEnabled(configuration) ? new ConcurrentHashMap<>() : 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 (environment.isEEModulesAware()) { services.add(BeanDeploymentModules.class, new BeanDeploymentModules(contextId, services));
private void addBean(Bean<?> bean, List<Bean<?>> beanList, List<Bean<?>> transitiveBeans) { if (beanSet.add(bean)) { if (bean.isAlternative() && (!registry.isEnabledInAnyBeanDeployment(bean) && !(bean instanceof Prioritized)) && !isConfiguratorBeanWithPriority(bean)) { BootstrapLogger.LOG.foundDisabledAlternative(bean); } else if (registry.isSpecializedInAnyBeanDeployment(bean)) { BootstrapLogger.LOG.foundSpecializedBean(bean); } else if (bean instanceof AbstractProducerBean<?, ?, ?> && registry.isSpecializedInAnyBeanDeployment(((AbstractProducerBean<?, ?, ?>) bean).getDeclaringBean())) { BootstrapLogger.LOG.foundProducerOfSpecializedBean(bean); specializedBeans.add(bean); } else { BootstrapLogger.LOG.foundBean(bean); beanList.add(bean); if (bean instanceof SessionBean) { SessionBean<?> enterpriseBean = (SessionBean<?>) bean; enterpriseBeans.put(enterpriseBean.getEjbDescriptor(), enterpriseBean); } if (bean instanceof PassivationCapable) { getServices().get(ContextualStore.class).putIfAbsent(bean); } registerBeanNamespace(bean); // New beans (except for SessionBeans) and most built in beans aren't resolvable transitively if (bean instanceof ExtensionBean || bean instanceof SessionBean || (!(bean instanceof NewBean) && !(bean instanceof AbstractBuiltInBean<?>))) { transitiveBeans.add(bean); } } } }
protected Set<? extends AbstractBean<?, ?>> getSpecializedBeans() { return getBeanManager().getServices().get(SpecializationAndEnablementRegistry.class).resolveSpecializedBeans(this); }
protected void processBeanAttributes(Iterable<? extends AbstractBean<?, ?>> beans) { if (!containerLifecycleEvents.isProcessBeanAttributesObserved()) { return; } if (!beans.iterator().hasNext()) { return; // exit recursion } Collection<AbstractBean<?, ?>> vetoedBeans = new HashSet<AbstractBean<?, ?>>(); Collection<AbstractBean<?, ?>> previouslySpecializedBeans = new HashSet<AbstractBean<?, ?>>(); for (AbstractBean<?, ?> bean : beans) { // fire ProcessBeanAttributes for class beans boolean vetoed = fireProcessBeanAttributes(bean); if (vetoed) { vetoedBeans.add(bean); } } // remove vetoed class beans for (AbstractBean<?, ?> bean : vetoedBeans) { if (bean.isSpecializing()) { previouslySpecializedBeans.addAll(specializationAndEnablementRegistry.resolveSpecializedBeans(bean)); specializationAndEnablementRegistry.vetoSpecializingBean(bean); } getEnvironment().vetoBean(bean); } // if a specializing bean was vetoed, let's process the specializing bean now processBeans(previouslySpecializedBeans); }
protected <T, S> boolean fireProcessBeanAttributes(AbstractBean<T, S> bean) { if (!specializationAndEnablementRegistry.isCandidateForLifecycleEvent(bean)) { return false; } ProcessBeanAttributesImpl<T> event = containerLifecycleEvents.fireProcessBeanAttributes(getManager(), bean, bean.getAnnotated(), bean.getType()); if (event == null) { return false; } if (event.isVeto()) { return true; } if (event.isDirty()) { bean.setAttributes(ExternalBeanAttributesFactory.<T>of(event.getBeanAttributesInternal(), manager)); bean.checkSpecialization(); } if (event.isIgnoreFinalMethods()) { bean.setIgnoreFinalMethods(); } return false; } }
@Override public Set<? extends AbstractBean<?, ?>> apply(Bean<?> specializingBean) { Set<? extends AbstractBean<?, ?>> result = null; if (specializingBean instanceof AbstractClassBean<?>) { result = apply((AbstractClassBean<?>) specializingBean); } if (specializingBean instanceof ProducerMethod<?, ?>) { result = apply((ProducerMethod<?, ?>) specializingBean); } if (result != null) { if (isEnabledInAnyBeanDeployment(specializingBean)) { for (AbstractBean<?, ?> specializedBean : result) { specializedBeansMap.computeIfAbsent(specializedBean, (key) -> new LongAdder()).increment(); } } return result; } throw new IllegalArgumentException("Unsupported bean type " + specializingBean); }
ResourceLoader resourceLoader = registry.get(ResourceLoader.class); if (resourceLoader == null) { resourceLoader = DefaultResourceLoader.INSTANCE; registry.add(ResourceLoader.class, resourceLoader); services.addAll(deploymentServices.entrySet()); services.addAll(registry.entrySet()); services.add(InterceptorsApiAbstraction.class, new InterceptorsApiAbstraction(resourceLoader)); services.add(AnnotationApiAbstraction.class, new AnnotationApiAbstraction(resourceLoader)); this.beanManager = BeanManagerImpl.newManager(deploymentManager, BeanDeployments.getFinalId(beanDeploymentArchive.getId(), services.get(WeldConfiguration.class).getStringProperty(ROLLING_UPGRADES_ID_DELIMITER)), services); services.add(InjectionTargetService.class, new InjectionTargetService(beanManager)); services.add(InterceptionFactoryDataCache.class, new InterceptionFactoryDataCache(beanManager)); services.addIfAbsent(EjbSupport.class, EjbSupport.NOOP_IMPLEMENTATION); if (services.get(WeldConfiguration.class).getBooleanProperty(CONCURRENT_DEPLOYMENT) && services.contains(ExecutorServices.class)) { beanDeployer = new ConcurrentBeanDeployer(beanManager, deploymentServices); } else { beanDeployer = new BeanDeployer(beanManager, deploymentServices); beanManager.getServices().get(SpecializationAndEnablementRegistry.class).registerEnvironment(beanManager, beanDeployer.getEnvironment(), additionalBeanArchive); beanManager.addBean(new BeanManagerBean(beanManager)); beanManager.addBean(new BeanManagerImplBean(beanManager));
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."); if (configuration.getBooleanProperty(ConfigurationKey.BEAN_IDENTIFIER_INDEX_OPTIMIZATION)) { beanIdentifierIndex = new BeanIdentifierIndex(); services.add(BeanIdentifierIndex.class, 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()); if (configuration.getBooleanProperty(ConfigurationKey.CONCURRENT_DEPLOYMENT) && services.contains(ExecutorServices.class)) { validator = new ConcurrentValidator(modules.getPluggableValidators(), executor, UnusedBeans.isEnabled(configuration) ? new ConcurrentHashMap<>() : 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 (environment.isEEModulesAware()) { services.add(BeanDeploymentModules.class, new BeanDeploymentModules(contextId, services));
private void addBean(Bean<?> bean, List<Bean<?>> beanList, List<Bean<?>> transitiveBeans) { if (beanSet.add(bean)) { if (bean.isAlternative() && (!registry.isEnabledInAnyBeanDeployment(bean) && !(bean instanceof Prioritized)) && !isConfiguratorBeanWithPriority(bean)) { BootstrapLogger.LOG.foundDisabledAlternative(bean); } else if (registry.isSpecializedInAnyBeanDeployment(bean)) { BootstrapLogger.LOG.foundSpecializedBean(bean); } else if (bean instanceof AbstractProducerBean<?, ?, ?> && registry.isSpecializedInAnyBeanDeployment(((AbstractProducerBean<?, ?, ?>) bean).getDeclaringBean())) { BootstrapLogger.LOG.foundProducerOfSpecializedBean(bean); specializedBeans.add(bean); } else { BootstrapLogger.LOG.foundBean(bean); beanList.add(bean); if (bean instanceof SessionBean) { SessionBean<?> enterpriseBean = (SessionBean<?>) bean; enterpriseBeans.put(enterpriseBean.getEjbDescriptor(), enterpriseBean); } if (bean instanceof PassivationCapable) { getServices().get(ContextualStore.class).putIfAbsent(bean); } registerBeanNamespace(bean); // New beans (except for SessionBeans) and most built in beans aren't resolvable transitively if (bean instanceof ExtensionBean || bean instanceof SessionBean || (!(bean instanceof NewBean) && !(bean instanceof AbstractBuiltInBean<?>))) { transitiveBeans.add(bean); } } } }