use of javax.enterprise.inject.spi.DefinitionException in project core by weld.
the class DummyExtension method replaceObserverMethod.
void replaceObserverMethod(@Observes ProcessObserverMethod<Number, ?> event) {
if (!checkExperimentalObserver(event.getObserverMethod().getObservedQualifiers())) {
return;
}
// first, verify getBeanClass() check
ObserverMethod<Number> wrongObserver = new ForwardingObserverMethod<Number>(event.getObserverMethod()) {
@Override
public Class<?> getBeanClass() {
return Number.class;
}
};
try {
event.setObserverMethod(wrongObserver);
throw new RuntimeException("Expected exception not thrown");
} catch (DefinitionException expected) {
}
ObserverMethod<Number> replacement = new ForwardingObserverMethod<Number>(event.getObserverMethod()) {
@Override
public Type getObservedType() {
return Integer.class;
}
@Override
public Set<Annotation> getObservedQualifiers() {
Set<Annotation> qualifiers = new HashSet<>(delegate().getObservedQualifiers());
qualifiers.add(new NamedLiteral("experimental"));
return qualifiers;
}
};
event.setObserverMethod(replacement);
}
use of javax.enterprise.inject.spi.DefinitionException in project tomee by apache.
the class Assembler method validateCdiResourceProducers.
private void validateCdiResourceProducers(final AppContext appContext, final AppInfo info) {
if (appContext.getWebBeansContext() == null) {
return;
}
// validate @Produces @Resource/@PersistenceX/@EJB once all is bound to JNDI - best case - or with our model
if (appContext.isStandaloneModule() && !appContext.getProperties().containsKey("openejb.cdi.skip-resource-validation")) {
final Map<String, Object> bindings = appContext.getWebContexts().isEmpty() ? appContext.getBindings() : appContext.getWebContexts().iterator().next().getBindings();
if (bindings != null && appContext.getWebBeansContext() != null && appContext.getWebBeansContext().getBeanManagerImpl().isInUse()) {
for (final Bean<?> bean : appContext.getWebBeansContext().getBeanManagerImpl().getBeans()) {
if (ResourceBean.class.isInstance(bean)) {
final ResourceReference reference = ResourceBean.class.cast(bean).getReference();
String jndi = reference.getJndiName().replace("java:", "");
if (reference.getJndiName().startsWith("java:/")) {
jndi = jndi.substring(1);
}
Object lookup = bindings.get(jndi);
if (lookup == null && reference.getAnnotation(EJB.class) != null) {
final CdiPlugin plugin = CdiPlugin.class.cast(appContext.getWebBeansContext().getPluginLoader().getEjbPlugin());
if (!plugin.isSessionBean(reference.getResourceType())) {
// local beans are here and access is O(1) instead of O(n)
boolean ok = false;
for (final BeanContext bc : appContext.getBeanContexts()) {
if (bc.getBusinessLocalInterfaces().contains(reference.getResourceType()) || bc.getBusinessRemoteInterfaces().contains(reference.getResourceType())) {
ok = true;
break;
}
}
if (!ok) {
throw new DefinitionException("EJB " + reference.getJndiName() + " in " + reference.getOwnerClass() + " can't be cast to " + reference.getResourceType());
}
}
}
if (Reference.class.isInstance(lookup)) {
try {
lookup = Reference.class.cast(lookup).getContent();
} catch (final Exception e) {
// surely too early, let's try some known locations
if (JndiUrlReference.class.isInstance(lookup)) {
checkBuiltInResourceTypes(reference, JndiUrlReference.class.cast(lookup).getJndiName());
}
continue;
}
} else if (lookup == null) {
// TODO: better validation with lookups in tomee, should be in TWAB surely but would split current code
final Resource r = Resource.class.cast(reference.getAnnotation(Resource.class));
if (r != null) {
if (!r.lookup().isEmpty()) {
checkBuiltInResourceTypes(reference, r.lookup());
} else if (!r.name().isEmpty()) {
final String name = "comp/env/" + r.name();
boolean done = false;
for (final WebAppInfo w : info.webApps) {
for (final EnvEntryInfo e : w.jndiEnc.envEntries) {
if (name.equals(e.referenceName)) {
if (e.type != null && !reference.getResourceType().getName().equals(e.type)) {
throw new DefinitionException("Env Entry " + reference.getJndiName() + " in " + reference.getOwnerClass() + " can't be cast to " + reference.getResourceType());
}
done = true;
break;
}
}
if (done) {
break;
}
}
}
}
}
if (lookup != null && !reference.getResourceType().isInstance(lookup)) {
throw new DefinitionException("Resource " + reference.getJndiName() + " in " + reference.getOwnerClass() + " can't be cast, instance is " + lookup);
}
}
}
}
}
}
use of javax.enterprise.inject.spi.DefinitionException in project tomee by apache.
the class CdiPlugin method defineSessionBean.
@Override
public <T> Bean<T> defineSessionBean(final Class<T> clazz, final BeanAttributes<T> attributes, final AnnotatedType<T> annotatedType) {
final BeanContext bc = findBeanContext(webBeansContext, clazz);
final Class<?> superClass = bc.getManagedClass().getSuperclass();
if (annotatedType.isAnnotationPresent(Specializes.class)) {
if (superClass != Object.class && !isSessionBean(superClass)) {
throw new DefinitionException("You can only specialize another EJB: " + clazz);
}
final BeanContext parentBc = findBeanContext(webBeansContext, superClass);
final List<Class> businessLocalInterfaces = new ArrayList<>(parentBc.getBusinessLocalInterfaces());
for (final Class<?> api : bc.getBusinessLocalInterfaces()) {
businessLocalInterfaces.removeAll(GenericsUtil.getTypeClosure(api));
}
if (!businessLocalInterfaces.isEmpty()) {
throw new DefinitionException("You can only specialize another EJB with at least the same API: " + clazz);
}
}
final CdiEjbBean<T> bean = new OpenEJBBeanBuilder<>(bc, webBeansContext, annotatedType, attributes).createBean(clazz, !annotatedType.isAnnotationPresent(Vetoed.class));
bc.set(CdiEjbBean.class, bean);
bc.set(CurrentCreationalContext.class, new CurrentCreationalContext());
validateDisposeMethods(bean);
validateScope(bean);
final Set<ObserverMethod<?>> observerMethods;
if (bean.isEnabled()) {
observerMethods = new ObserverMethodsBuilder<>(webBeansContext, bean.getAnnotatedType()).defineObserverMethods(bean, true);
} else {
observerMethods = new HashSet<>();
}
final WebBeansUtil webBeansUtil = webBeansContext.getWebBeansUtil();
final Set<ProducerFieldBean<?>> producerFields = new ProducerFieldBeansBuilder(bean.getWebBeansContext(), bean.getAnnotatedType()).defineProducerFields(bean);
final Set<ProducerMethodBean<?>> producerMethods = new ProducerMethodBeansBuilder(bean.getWebBeansContext(), bean.getAnnotatedType()).defineProducerMethods(bean, producerFields);
final Map<ProducerMethodBean<?>, AnnotatedMethod<?>> annotatedMethods = new HashMap<>();
for (final ProducerMethodBean<?> producerMethod : producerMethods) {
final AnnotatedMethod<?> method = webBeansContext.getAnnotatedElementFactory().newAnnotatedMethod(producerMethod.getCreatorMethod(), annotatedType);
webBeansUtil.inspectDeploymentErrorStack("There are errors that are added by ProcessProducer event observers for " + "ProducerMethods. Look at logs for further details");
annotatedMethods.put(producerMethod, method);
}
final Map<ProducerFieldBean<?>, AnnotatedField<?>> annotatedFields = new HashMap<>();
for (final ProducerFieldBean<?> producerField : producerFields) {
if (!Modifier.isStatic(producerField.getCreatorField().getModifiers())) {
throw new DefinitionException("In an EJB all producer fields should be static");
}
webBeansUtil.inspectDeploymentErrorStack("There are errors that are added by ProcessProducer event observers for" + " ProducerFields. Look at logs for further details");
annotatedFields.put(producerField, webBeansContext.getAnnotatedElementFactory().newAnnotatedField(producerField.getCreatorField(), webBeansContext.getAnnotatedElementFactory().newAnnotatedType(producerField.getBeanClass())));
}
final Map<ObserverMethod<?>, AnnotatedMethod<?>> observerMethodsMap = new HashMap<>();
for (final ObserverMethod<?> observerMethod : observerMethods) {
final ObserverMethodImpl<?> impl = (ObserverMethodImpl<?>) observerMethod;
final AnnotatedMethod<?> method = impl.getObserverMethod();
observerMethodsMap.put(observerMethod, method);
}
validateProduceMethods(bean, producerMethods);
validateObserverMethods(bean, observerMethodsMap);
final BeanManagerImpl beanManager = webBeansContext.getBeanManagerImpl();
// Fires ProcessManagedBean
final GProcessSessionBean event = new GProcessSessionBean(Bean.class.cast(bean), annotatedType, bc.getEjbName(), bean.getEjbType());
beanManager.fireEvent(event, true);
event.setStarted();
webBeansUtil.inspectDeploymentErrorStack("There are errors that are added by ProcessSessionBean event observers for managed beans. Look at logs for further details");
// Fires ProcessProducerMethod
webBeansUtil.fireProcessProducerMethodBeanEvent(annotatedMethods, annotatedType);
webBeansUtil.inspectDeploymentErrorStack("There are errors that are added by ProcessProducerMethod event observers for producer method beans. Look at logs for further details");
// Fires ProcessProducerField
webBeansUtil.fireProcessProducerFieldBeanEvent(annotatedFields);
webBeansUtil.inspectDeploymentErrorStack("There are errors that are added by ProcessProducerField event observers for producer field beans. Look at logs for further details");
if (!webBeansUtil.isAnnotatedTypeDecoratorOrInterceptor(annotatedType)) {
for (final ProducerMethodBean<?> producerMethod : producerMethods) {
beanManager.addBean(producerMethod);
}
for (final ProducerFieldBean<?> producerField : producerFields) {
beanManager.addBean(producerField);
}
}
beanManager.addBean(bean);
return bean;
}
Aggregations