use of org.apache.webbeans.spi.api.ResourceReference 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);
}
}
}
}
}
}
Aggregations