use of org.jboss.weld.bean.SessionBean in project core by weld.
the class JsonObjects method createFullBeanJson.
/**
* @param bean
* @param transientDependencies
* @param transientDependents
* @param probe
* @return the full bean representation
*/
static JsonObjectBuilder createFullBeanJson(Bean<?> bean, boolean transientDependencies, boolean transientDependents, BeanManagerImpl beanManager, Probe probe) {
JsonObjectBuilder beanBuilder = createBasicBeanJson(bean, probe);
// NAME
if (bean.getName() != null) {
beanBuilder.add(NAME, bean.getName());
}
// STEREOTYPES
if (bean.getStereotypes() != null && !bean.getStereotypes().isEmpty()) {
JsonArrayBuilder stereotypesBuilder = Json.arrayBuilder();
for (Class<?> stereotype : Components.getSortedProbeComponetCandidates(bean.getStereotypes())) {
stereotypesBuilder.add(Json.objectBuilder().add(CLASS, stereotype.getName()).add(PROBE_COMPONENT, Components.isProbeComponent(stereotype)));
}
beanBuilder.add(STEREOTYPES, stereotypesBuilder);
}
if (bean instanceof SessionBean) {
// SESSION BEAN
SessionBean<?> sessionBean = (SessionBean<?>) bean;
if (sessionBean.getEjbDescriptor().getEjbName() != null) {
beanBuilder.add(EJB_NAME, true);
}
beanBuilder.add(SESSION_BEAN_TYPE, Components.getSessionBeanType(sessionBean.getEjbDescriptor()).toString());
} else if (bean instanceof AbstractProducerBean) {
// PRODUCERS
AbstractProducerBean<?, ?, ?> producerBean = (AbstractProducerBean<?, ?, ?>) bean;
if (producerBean.getDeclaringBean() != null) {
beanBuilder.add(DECLARING_BEAN, createSimpleBeanJson(producerBean.getDeclaringBean(), probe));
}
if (producerBean.getProducer() instanceof ProducerMethodProducer) {
ProducerMethodProducer<?, ?> producer = (ProducerMethodProducer<?, ?>) producerBean.getProducer();
if (producer.getDisposalMethod() != null) {
beanBuilder.add(DISPOSAL_METHOD, annotatedMethodToString(producer.getDisposalMethod().getAnnotated(), bean.getBeanClass()));
}
beanBuilder.add(PRODUCER_METHOD, annotatedMethodToString((AnnotatedMethod<?>) producer.getAnnotated(), bean.getBeanClass()));
} else if (producerBean.getProducer() instanceof ProducerFieldProducer) {
ProducerFieldProducer<?, ?> producer = (ProducerFieldProducer<?, ?>) producerBean.getProducer();
if (producer.getDisposalMethod() != null) {
beanBuilder.add(DISPOSAL_METHOD, annotatedMethodToString(producer.getDisposalMethod().getAnnotated(), bean.getBeanClass()));
}
beanBuilder.add(PRODUCER_FIELD, annotatedFieldToString(producer.getAnnotated(), bean.getBeanClass()));
}
}
// DEPENDENCIES
JsonArrayBuilder dependencies = createDependencies(null, bean, probe, transientDependencies);
if (dependencies != null) {
beanBuilder.add(DEPENDENCIES, dependencies);
}
// DEPENDENTS
JsonArrayBuilder dependents = createDependents(null, bean, probe, transientDependents);
if (dependents != null) {
beanBuilder.add(DEPENDENTS, dependents);
}
// DECLARED OBSERVERS
JsonArrayBuilder declaredObservers = Json.arrayBuilder();
for (ObserverMethod<?> observerMethod : probe.getObservers()) {
if (observerMethod instanceof ObserverMethodImpl) {
ObserverMethodImpl<?, ?> observerMethodImpl = (ObserverMethodImpl<?, ?>) observerMethod;
if (bean.equals(observerMethodImpl.getDeclaringBean())) {
JsonObjectBuilder observerBuilder = createSimpleObserverJson(observerMethodImpl, probe);
observerBuilder.add(RECEPTION, observerMethodImpl.getReception().toString());
observerBuilder.add(TX_PHASE, observerMethodImpl.getTransactionPhase().toString());
observerBuilder.add(ANNOTATED_METHOD, annotatedMethodToString(observerMethodImpl.getMethod().getAnnotated(), bean.getBeanClass()));
declaredObservers.add(observerBuilder);
}
}
}
beanBuilder.add(DECLARED_OBSERVERS, declaredObservers);
// DECLARED PRODUCERS
beanBuilder.add(DECLARED_PRODUCERS, createDeclaredProducers(bean, probe));
// ENABLEMENT
BeanKind kind = BeanKind.from(bean);
if (BeanKind.INTERCEPTOR.equals(kind) || BeanKind.DECORATOR.equals(kind) || bean.isAlternative()) {
JsonObjectBuilder enablementBuilder = Json.objectBuilder();
AnnotationApiAbstraction annotationApi = beanManager.getServices().get(AnnotationApiAbstraction.class);
Object priority = bean.getBeanClass().getAnnotation(annotationApi.PRIORITY_ANNOTATION_CLASS);
if (priority != null) {
int priorityValue = annotationApi.getPriority(priority);
enablementBuilder.add(PRIORITY, priorityValue);
enablementBuilder.add(PRIORITY_RANGE, Components.PriorityRange.of(priorityValue).toString());
if (!probe.getLocalEnablementOfBean(bean.getBeanClass()).isEmpty()) {
enablementBuilder.add(WARNING, WARNING_CONFLICTING_ENABLEMENT);
JsonArrayBuilder conflictingBdas = Json.arrayBuilder();
for (String bdaId : probe.getLocalEnablementOfBean(bean.getBeanClass())) {
conflictingBdas.add(createSimpleBdaJson(bdaId));
}
enablementBuilder.add(CONFLICTS, conflictingBdas);
}
} else {
JsonArrayBuilder bdasBuilder = Json.arrayBuilder();
Collection<BeanManagerImpl> beanManagers = Container.instance(beanManager).beanDeploymentArchives().values();
for (BeanManagerImpl manager : beanManagers) {
ModuleEnablement enablement = manager.getEnabled();
if ((BeanKind.INTERCEPTOR.equals(kind) && enablement.isInterceptorEnabled(bean.getBeanClass())) || (BeanKind.DECORATOR.equals(kind) && enablement.isDecoratorEnabled(bean.getBeanClass())) || isSelectedAlternative(enablement, bean)) {
bdasBuilder.add(createSimpleBdaJson(manager.getId()));
}
}
enablementBuilder.add(BDAS, bdasBuilder);
}
beanBuilder.add(ENABLEMENT, enablementBuilder);
}
// INTERCEPTOR BINDINGS
if (BeanKind.INTERCEPTOR.equals(kind)) {
Interceptor<?> interceptor = (Interceptor<?>) bean;
JsonArrayBuilder bindings = Json.arrayBuilder(true);
for (Annotation binding : interceptor.getInterceptorBindings()) {
bindings.add(binding.toString());
}
beanBuilder.add(BINDINGS, bindings);
}
// DECORATOR
if (BeanKind.DECORATOR.equals(kind)) {
Decorator<?> decorator = (Decorator<?>) bean;
beanBuilder.add(DELEGATE_TYPE, Formats.formatType(decorator.getDelegateType(), false));
beanBuilder.add(DELEGATE_QUALIFIERS, createQualifiers(decorator.getDelegateQualifiers(), false));
JsonArrayBuilder decoratedTypes = Json.arrayBuilder(true);
for (Type type : decorator.getDecoratedTypes()) {
decoratedTypes.add(Formats.formatType(type, false));
}
beanBuilder.add(DECORATED_TYPES, decoratedTypes);
// ASSOCIATED TO BEANS
Set<Bean<?>> decoratedBeans = findDecoratedBeans(decorator, beanManager, probe);
if (!decoratedBeans.isEmpty()) {
JsonArrayBuilder decoratedBeansBuilder = Json.arrayBuilder();
for (Bean<?> decoratedBean : decoratedBeans) {
decoratedBeansBuilder.add(createSimpleBeanJson(decoratedBean, probe));
}
beanBuilder.add(ASSOCIATED_TO, decoratedBeansBuilder);
}
}
if (bean instanceof AbstractClassBean) {
AbstractClassBean<?> abstractClassBean = (AbstractClassBean<?>) bean;
InterceptionModel interceptionModel = abstractClassBean.getInterceptors();
// CLASS-LEVEL BINDINGS
if (interceptionModel != null) {
Set<Annotation> classInterceptorBindings = interceptionModel.getClassInterceptorBindings();
if (!classInterceptorBindings.isEmpty()) {
JsonArrayBuilder bindingsBuilder = Json.arrayBuilder();
for (Annotation binding : Components.getSortedProbeComponetAnnotationCandidates(classInterceptorBindings)) {
bindingsBuilder.add(Json.objectBuilder().add(VALUE, binding.toString()).add(PROBE_COMPONENT, Components.isProbeAnnotation(binding)));
}
beanBuilder.add(CLASS_INTERCEPTOR_BINDINGS, bindingsBuilder);
}
}
// ASSOCIATED DECORATORS
List<Decorator<?>> decorators = abstractClassBean.getDecorators();
if (!decorators.isEmpty()) {
JsonArrayBuilder decoratorsBuilder = Json.arrayBuilder();
for (Decorator<?> decorator : decorators) {
decoratorsBuilder.add(createSimpleBeanJson(decorator, probe));
}
beanBuilder.add(DECORATORS, decoratorsBuilder);
}
}
return beanBuilder;
}
use of org.jboss.weld.bean.SessionBean in project core by weld.
the class EnterpriseBeanInterceptionTest method testInterceptors.
@Test
public void testInterceptors() throws Exception {
SessionBean<Ball> ballSessionBean = (SessionBean<Ball>) beanManager.getBeans(Ball.class).iterator().next();
InterceptorBindings interceptorBindings = new InterceptorBindingsAdapter(beanManager.getInterceptorModelRegistry().get(ballSessionBean.getAnnotated()));
List<javax.enterprise.inject.spi.Interceptor> interceptors = new ArrayList<javax.enterprise.inject.spi.Interceptor>(interceptorBindings.getAllInterceptors());
Assert.assertEquals(3, interceptors.size());
List<Class<?>> expectedInterceptors = Arrays.<Class<?>>asList(Goalkeeper.class, Defender.class, Referee.class);
Assert.assertTrue(expectedInterceptors.contains(interceptors.get(0).getBeanClass()));
Assert.assertTrue(expectedInterceptors.contains(interceptors.get(1).getBeanClass()));
Assert.assertTrue(expectedInterceptors.contains(interceptors.get(2).getBeanClass()));
Assert.assertEquals(0, interceptorBindings.getMethodInterceptors(InterceptionType.AROUND_TIMEOUT, ballSessionBean.getBeanClass().getMethod("shoot")).size());
Assert.assertEquals(1, interceptorBindings.getMethodInterceptors(InterceptionType.AROUND_INVOKE, ballSessionBean.getBeanClass().getMethod("shoot")).size());
Assert.assertEquals(Goalkeeper.class, interceptorBindings.getMethodInterceptors(InterceptionType.AROUND_INVOKE, ballSessionBean.getBeanClass().getMethod("shoot")).get(0).getBeanClass());
Assert.assertEquals(0, interceptorBindings.getMethodInterceptors(InterceptionType.AROUND_TIMEOUT, ballSessionBean.getBeanClass().getMethod("pass")).size());
Assert.assertEquals(1, interceptorBindings.getMethodInterceptors(InterceptionType.AROUND_INVOKE, ballSessionBean.getBeanClass().getMethod("pass")).size());
Assert.assertEquals(Defender.class, interceptorBindings.getMethodInterceptors(InterceptionType.AROUND_INVOKE, ballSessionBean.getBeanClass().getMethod("pass")).get(0).getBeanClass());
Method finishGameMethod = ballSessionBean.getBeanClass().getMethod("finishGame", Timer.class);
Assert.assertEquals(0, interceptorBindings.getMethodInterceptors(InterceptionType.AROUND_INVOKE, finishGameMethod).size());
Assert.assertEquals(1, interceptorBindings.getMethodInterceptors(InterceptionType.AROUND_TIMEOUT, finishGameMethod).size());
Assert.assertEquals(Referee.class, interceptorBindings.getMethodInterceptors(InterceptionType.AROUND_TIMEOUT, finishGameMethod).get(0).getBeanClass());
}
use of org.jboss.weld.bean.SessionBean in project wildfly by wildfly.
the class WeldComponentService method start.
@Override
public synchronized void start(final StartContext context) throws StartException {
final ClassLoader cl = WildFlySecurityManager.getCurrentContextClassLoaderPrivileged();
try {
WildFlySecurityManager.setCurrentContextClassLoaderPrivileged(classLoader);
beanManager = weldContainerSupplier.get().getBeanManager(beanDeploymentArchiveId);
for (final Class<?> interceptor : interceptorClasses) {
AnnotatedType<?> type = beanManager.createAnnotatedType(interceptor);
@SuppressWarnings("rawtypes") InjectionTarget injectionTarget = beanManager.getInjectionTargetFactory(type).createInterceptorInjectionTarget();
interceptorInjections.put(interceptor, beanManager.fireProcessInjectionTarget(type, injectionTarget));
}
if (ejbName != null) {
EjbDescriptor<Object> descriptor = beanManager.getEjbDescriptor(ejbName);
// may happen if the Jakarta Enterprise Beans were vetoed
if (descriptor != null) {
bean = beanManager.getBean(descriptor);
}
}
if (bean instanceof SessionBean<?>) {
SessionBean<?> sessionBean = (SessionBean<?>) bean;
this.injectionTarget = sessionBean.getProducer();
return;
}
WeldInjectionTarget<?> injectionTarget = InjectionTargets.createInjectionTarget(componentClass, bean, beanManager, !isComponentWithView);
for (ComponentSupport support : ServiceLoader.load(ComponentSupport.class, WildFlySecurityManager.getClassLoaderPrivileged(WeldComponentService.class))) {
if (support.isProcessing(componentDescription)) {
this.injectionTarget = support.processInjectionTarget(injectionTarget, componentDescription, beanManager);
break;
}
}
if (this.injectionTarget == null) {
this.injectionTarget = injectionTarget;
}
beanManager.getServices().get(InjectionTargetService.class).validateProducer(injectionTarget);
} finally {
WildFlySecurityManager.setCurrentContextClassLoaderPrivileged(cl);
}
}
Aggregations