/** * Wraps all JAR-RS components by Jersey-specific injection target. * * @param pit process injection target. * @param <T> type of the processed injection target. */ public <T> void observeInjectionTarget(@Observes ProcessInjectionTarget<T> pit) { BasicInjectionTarget<T> it = (BasicInjectionTarget<T>) pit.getInjectionTarget(); JerseyInjectionTarget<T> jerseyInjectionTarget = new JerseyInjectionTarget<>(it, pit.getAnnotatedType().getJavaClass()); jerseyInjectionTargets.add(jerseyInjectionTarget); pit.setInjectionTarget(jerseyInjectionTarget); }
/** * Ignores the classes which are manually added using bindings (through {@link Application} class) and scanned by CDI. * The manual adding is privileged and the beans scanned using CDI are ignored. * <p> * TODO: The method counts with the condition that the all bindings are known before the CDI scanning has been started, * can be changed during the migration from CDI SE to JAVA EE environment. * * @param pat processed type. * @param <T> type of the scanned bean. */ public <T> void ignoreManuallyRegisteredComponents( @Observes @WithAnnotations({ Path.class, Provider.class }) ProcessAnnotatedType<T> pat) { for (Binding binding : bindings.getBindings()) { if (ClassBinding.class.isAssignableFrom(binding.getClass())) { ClassBinding<?> classBinding = (ClassBinding<?>) binding; if (pat.getAnnotatedType().getJavaClass() == classBinding.getService()) { pat.veto(); return; } } else if (InstanceBinding.class.isAssignableFrom(binding.getClass())) { InstanceBinding<?> instanceBinding = (InstanceBinding<?>) binding; if (pat.getAnnotatedType().getJavaClass() == instanceBinding.getService().getClass()) { pat.veto(); return; } } } }
@SuppressWarnings("unused") private void processAnnotatedType(@Observes final AnnotatedType<?> annotatedType = processAnnotatedType.getAnnotatedType();
private void recordAnnotatedGaugeSite(@Observes @WithAnnotations(Gauge.class) ProcessAnnotatedType<?> pat) { LOGGER.log(Level.FINE, () -> "### recordAnnoatedGaugeSite for class " + pat.getAnnotatedType().getJavaClass()); AnnotatedType<?> type = pat.getAnnotatedType(); LOGGER.log(Level.FINE, () -> "### Processing annotations for " + type.getJavaClass().getName()); // Register metrics based on annotations AnnotatedTypeConfigurator<?> configurator = pat.configureAnnotatedType(); Class<?> clazz = configurator.getAnnotated().getJavaClass(); // If abstract class, then handled by concrete subclasses if (Modifier.isAbstract(clazz.getModifiers())) { return; } // Process @Gauge methods keeping non-private declared on this class configurator.filterMethods(method -> method.getJavaMember().getDeclaringClass().equals(clazz) && !Modifier.isPrivate(method.getJavaMember().getModifiers()) && method.isAnnotationPresent(Gauge.class)) .forEach(method -> { Method javaMethod = method.getAnnotated().getJavaMember(); String explicitGaugeName = method.getAnnotated().getAnnotation(Gauge.class).name(); String gaugeName = String.format("%s.%s", clazz.getName(), explicitGaugeName != null && explicitGaugeName.length() > 0 ? explicitGaugeName : javaMethod.getName()); annotatedGaugeSites.put(gaugeName, method); LOGGER.log(Level.FINE, () -> String.format("### Recorded annotated gauge with name %s", gaugeName)); }); }
/** * Register a claim producer bean for each {@link Claim} injection. * * @param abd event from CDI container * @param bm bean manager */ public void registerClaimProducers(@Observes AfterBeanDiscovery abd, BeanManager bm) { // each injection point will have its own bean qualifiers.forEach(q -> abd.addBean(new ClaimProducer(q.qualifier, q.type, bm))); }
/** * Gather Application or resource classes to start. * * @param pit injection target * @param <T> any type */ @SuppressWarnings("unchecked") public <T> void gatherApplications(@Observes ProcessInjectionTarget<T> pit) { AnnotatedType<T> at = pit.getAnnotatedType(); if (!at.isAnnotationPresent(ApplicationScoped.class)) { return; } // class is annotated, let's make sure it is an application Class<?> theClass = at.getJavaClass(); if (Application.class.isAssignableFrom(theClass)) { this.applications.add((Class<? extends Application>) theClass); } else { // still may be a jax-rs resource (with no application attached) if (at.isAnnotationPresent(Path.class)) { this.resourceClasses.add(theClass); } } }
/** * Used to register the method validation interceptor bindings. * * @param processAnnotatedTypeEvent event fired for each annotated type * @param <T> the annotated type */ public <T> void processAnnotatedType(@Observes @WithAnnotations({ Constraint.class, Valid.class, ValidateOnExecution.class }) ProcessAnnotatedType<T> processAnnotatedTypeEvent) { Contracts.assertNotNull( processAnnotatedTypeEvent, "The ProcessAnnotatedType event cannot be null" ); // validation globally disabled if ( !isExecutableValidationEnabled ) { return; } AnnotatedType<T> type = processAnnotatedTypeEvent.getAnnotatedType(); Set<AnnotatedCallable<? super T>> constrainedCallables = determineConstrainedCallables( type ); if ( !constrainedCallables.isEmpty() ) { ValidationEnabledAnnotatedType<T> wrappedType = new ValidationEnabledAnnotatedType<>( type, constrainedCallables ); processAnnotatedTypeEvent.setAnnotatedType( wrappedType ); } }
private void registerMetrics(@Observes @WithAnnotations({Counted.class, Metered.class, Timed.class}) ProcessAnnotatedType<?> pat) { AnnotatedType<?> type = pat.getAnnotatedType(); Interceptor annot = type.getAnnotation(Interceptor.class); if (annot != null) { LOGGER.log(Level.FINE, () -> "### Processing annotations for " + pat.getAnnotatedType().getJavaClass().getName()); Class<?> clazz = configurator.getAnnotated().getJavaClass();
@SuppressWarnings({"unused", "unchecked", "rawtypes"}) private void afterDiscoveryObserver(@Observes final AfterBeanDiscovery abd) { if (customHk2TypesProvider != null) { hk2ProvidedTypes.addAll(customHk2TypesProvider.getHk2Types()); } for (final Type t : hk2ProvidedTypes) { abd.addBean(new Hk2Bean(t)); } }
/** * Registers metrics for all field and method producers defined by the application. * * @param adv After deployment validation event. * @param bm Bean manager. */ private void registerProducers(@Observes AfterDeploymentValidation adv, BeanManager bm) { LOGGER.log(Level.FINE, () -> "### registerProducers"); MetricRegistry registry = getMetricRegistry(); producers.entrySet().forEach(entry -> { Metric metric = entry.getValue().getAnnotation(Metric.class); if (metric != null) { String metricName = getMetricName(new AnnotatedElementWrapper(entry.getValue()), entry.getValue().getDeclaringType().getJavaClass(), MetricUtil.MatchingType.METHOD, metric.name(), metric.absolute()); registry.register(metricName, getReference(bm, entry.getValue().getBaseType(), entry.getKey())); } }); producers.clear(); }
<T> void processAnnotatedType(@Observes ProcessAnnotatedType<T> event, BeanManager beanManager) { FacesConfig facesConfig = event.getAnnotatedType().getAnnotation(FacesConfig.class); if (facesConfig != null && facesConfig.version() == FacesConfig.Version.JSF_2_3) { registerCdiProducers = true; } }
public void registerClient(@Observes @WithAnnotations({RegisterRestClient.class}) ProcessAnnotatedType<?> pat) { Class<?> typeDef = pat.getAnnotatedType().getJavaClass(); if(typeDef.isInterface()) { proxyTypes.add(typeDef); pat.veto(); } else { errors.add(new IllegalArgumentException("Rest client needs to be interface: " + typeDef)); } }
void afterBeanDiscovery(@Observes AfterBeanDiscovery afterBeanDiscovery, BeanManager beanManager) { for (Entry<Class<?>, Set<Annotation>> entry : getRepositoryTypes()) { Class<?> repositoryType = entry.getKey(); Set<Annotation> qualifiers = entry.getValue(); // Create the bean representing the repository. CdiRepositoryBean<?> repositoryBean = createRepositoryBean(repositoryType, qualifiers, beanManager); if (LOG.isInfoEnabled()) { LOG.info(String.format("Registering bean for %s with qualifiers %s.", repositoryType.getName(), qualifiers)); } // Register the bean to the container. registerBean(repositoryBean); afterBeanDiscovery.addBean(repositoryBean); } }
/** * Collects all FT methods in a set for later processing. * * @param event Event information. */ void registerFaultToleranceMethods(@Observes ProcessManagedBean<?> event) { AnnotatedType<?> type = event.getAnnotatedBeanClass(); for (AnnotatedMethod<?> method : type.getMethods()) { if (isFaultToleranceMethod(type.getJavaClass(), method.getJavaMember())) { getRegisteredMethods().add(new BeanMethod(type.getJavaClass(), method.getJavaMember())); } } }
/** * Set a default scope for each CDI bean which is a JAX-RS Provider. * * @param <T> type * @param event event * @param beanManager bean manager */ public <T> void observeProviders(@WithAnnotations({Provider.class}) @Observes ProcessAnnotatedType<T> event, BeanManager beanManager) { setBeanManager(beanManager); AnnotatedType<T> annotatedType = event.getAnnotatedType(); if(!annotatedType.getJavaClass().isInterface() && !isSessionBean(annotatedType) // This check is redundant for CDI 1.1 containers but required for CDI 1.0 && annotatedType.isAnnotationPresent(Provider.class)) { LogMessages.LOGGER.debug(Messages.MESSAGES.discoveredCDIBeanJaxRsProvider(annotatedType.getJavaClass().getCanonicalName())); event.setAnnotatedType(wrapAnnotatedType(annotatedType, applicationScopedLiteral)); this.providers.add(annotatedType.getJavaClass()); } }
void afterBeanDiscovery(@Observes AfterBeanDiscovery afterBeanDiscovery, BeanManager beanManager) { registerDependenciesIfNecessary(afterBeanDiscovery, beanManager); for (Entry<Class<?>, Set<Annotation>> entry : getRepositoryTypes()) { Class<?> repositoryType = entry.getKey(); Set<Annotation> qualifiers = entry.getValue(); // Create the bean representing the repository. CdiRepositoryBean<?> repositoryBean = createRepositoryBean(repositoryType, qualifiers, beanManager); if (LOG.isInfoEnabled()) { LOG.info(String.format("Registering bean for %s with qualifiers %s.", repositoryType.getName(), qualifiers)); } // Register the bean to the container. registerBean(repositoryBean); afterBeanDiscovery.addBean(repositoryBean); } }
@SuppressWarnings("unused") private void processInjectionTarget(@Observes final ProcessInjectionTarget event) { final InjectionTarget it = event.getInjectionTarget(); final Class<?> componentClass = event.getAnnotatedType().getJavaClass();
/** * Set a default scope for each CDI bean which is a JAX-RS Resource. * * @param <T> type * @param event event * @param beanManager bean manager */ public <T> void observeResources(@WithAnnotations({Path.class}) @Observes ProcessAnnotatedType<T> event, BeanManager beanManager) { setBeanManager(beanManager); AnnotatedType<T> annotatedType = event.getAnnotatedType(); if(!annotatedType.getJavaClass().isInterface() && !isSessionBean(annotatedType) // This check is redundant for CDI 1.1 containers but required for CDI 1.0 && GetRestful.isRootResource(annotatedType.getJavaClass()) && !annotatedType.isAnnotationPresent(Decorator.class)) { LogMessages.LOGGER.debug(Messages.MESSAGES.discoveredCDIBeanJaxRsResource(annotatedType.getJavaClass().getCanonicalName())); event.setAnnotatedType(wrapAnnotatedType(annotatedType, requestScopedLiteral)); this.resources.add(annotatedType.getJavaClass()); } }
/** * Implementation of a an observer which registers beans to the CDI container for the detected Spring Data * repositories. * <p> * The repository beans are associated to the EntityManagers using their qualifiers. * * @param beanManager The BeanManager instance. */ void afterBeanDiscovery(@Observes AfterBeanDiscovery afterBeanDiscovery, BeanManager beanManager) { for (Entry<Class<?>, Set<Annotation>> entry : getRepositoryTypes()) { Class<?> repositoryType = entry.getKey(); Set<Annotation> qualifiers = entry.getValue(); // Create the bean representing the repository. CdiRepositoryBean<?> repositoryBean = createRepositoryBean(repositoryType, qualifiers, beanManager); LOGGER.info("Registering bean for '{}' with qualifiers {}.", repositoryType.getName(), qualifiers); // Register the bean to the extension and the container. registerBean(repositoryBean); afterBeanDiscovery.addBean(repositoryBean); } }