use of javax.annotation.Resource in project wildfly by wildfly.
the class ResourceInjectionUtilities method getResourceName.
public static String getResourceName(InjectionPoint injectionPoint, PropertyReplacer propertyReplacer) {
Resource resource = getResourceAnnotated(injectionPoint).getAnnotation(Resource.class);
String mappedName = resource.mappedName();
if (!mappedName.equals("")) {
return propertyReplacer == null ? mappedName : propertyReplacer.replaceProperties(mappedName);
}
String name = resource.name();
if (!name.equals("")) {
name = propertyReplacer == null ? name : propertyReplacer.replaceProperties(name);
// see if this is a prefixed name
// and if so just return it
int firstSlash = name.indexOf("/");
int colon = name.indexOf(":");
if (colon != -1 && (firstSlash == -1 || colon < firstSlash)) {
return name;
}
return RESOURCE_LOOKUP_PREFIX + "/" + name;
}
String propertyName;
if (injectionPoint.getMember() instanceof Field) {
propertyName = injectionPoint.getMember().getName();
} else if (injectionPoint.getMember() instanceof Method) {
propertyName = getPropertyName((Method) injectionPoint.getMember());
if (propertyName == null) {
throw WeldLogger.ROOT_LOGGER.injectionPointNotAJavabean((Method) injectionPoint.getMember());
}
} else {
throw WeldLogger.ROOT_LOGGER.cannotInject(injectionPoint);
}
String className = injectionPoint.getMember().getDeclaringClass().getName();
return RESOURCE_LOOKUP_PREFIX + "/" + className + "/" + propertyName;
}
use of javax.annotation.Resource 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.annotation.Resource in project tomee by apache.
the class ResourceManagerImpl method setBus.
@Resource
public final void setBus(Bus b) {
if (bus != b) {
bus = b;
firstCalled = false;
super.addResourceResolver(new ObjectTypeResolver(bus));
if (null != bus) {
bus.setExtension(this, ResourceManager.class);
}
}
}
use of javax.annotation.Resource in project tomee by apache.
the class ResourceInjector method visitMethod.
public final void visitMethod(final Method method, final Annotation annotation) {
assert annotation instanceof Resource : annotation;
Resource res = (Resource) annotation;
String resourceName = getResourceName(res, method);
Class<?> clz = getResourceType(res, method);
Object resource = resolveResource(resourceName, clz);
if (resource == null && "".equals(res.name())) {
resource = resolveResource(null, clz);
}
if (resource != null) {
invokeSetter(method, resource);
} else {
LOG.log(Level.FINE, "RESOURCE_RESOLVE_FAILED", new Object[] { resourceName, clz });
}
}
use of javax.annotation.Resource in project tomee by apache.
the class LegacyAnnotationProcessor method processAnnotations.
/**
* Inject resources in specified instance.
* @param instance
* @throws java.lang.IllegalAccessException
* @throws java.lang.reflect.InvocationTargetException
* @throws javax.naming.NamingException
*/
public void processAnnotations(final Object instance) throws IllegalAccessException, InvocationTargetException, NamingException {
if (context == null) {
// No resource injection
return;
}
Class<?> clazz = instance.getClass();
while (clazz != null) {
// Initialize fields annotations
final Field[] fields = clazz.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
if (fields[i].isAnnotationPresent(Resource.class)) {
final Resource annotation = fields[i].getAnnotation(Resource.class);
lookupFieldResource(context, instance, fields[i], annotation.name(), clazz);
}
if (fields[i].isAnnotationPresent(EJB.class)) {
final EJB annotation = fields[i].getAnnotation(EJB.class);
lookupFieldResource(context, instance, fields[i], annotation.name(), clazz);
}
if (fields[i].isAnnotationPresent(WebServiceRef.class)) {
final WebServiceRef annotation = fields[i].getAnnotation(WebServiceRef.class);
lookupFieldResource(context, instance, fields[i], annotation.name(), clazz);
}
if (fields[i].isAnnotationPresent(PersistenceContext.class)) {
final PersistenceContext annotation = fields[i].getAnnotation(PersistenceContext.class);
lookupFieldResource(context, instance, fields[i], annotation.name(), clazz);
}
if (fields[i].isAnnotationPresent(PersistenceUnit.class)) {
final PersistenceUnit annotation = fields[i].getAnnotation(PersistenceUnit.class);
lookupFieldResource(context, instance, fields[i], annotation.name(), clazz);
}
}
// Initialize methods annotations
final Method[] methods = clazz.getDeclaredMethods();
for (int i = 0; i < methods.length; i++) {
if (methods[i].isAnnotationPresent(Resource.class)) {
final Resource annotation = methods[i].getAnnotation(Resource.class);
lookupMethodResource(context, instance, methods[i], annotation.name(), clazz);
}
if (methods[i].isAnnotationPresent(EJB.class)) {
final EJB annotation = methods[i].getAnnotation(EJB.class);
lookupMethodResource(context, instance, methods[i], annotation.name(), clazz);
}
if (methods[i].isAnnotationPresent(WebServiceRef.class)) {
final WebServiceRef annotation = methods[i].getAnnotation(WebServiceRef.class);
lookupMethodResource(context, instance, methods[i], annotation.name(), clazz);
}
if (methods[i].isAnnotationPresent(PersistenceContext.class)) {
final PersistenceContext annotation = methods[i].getAnnotation(PersistenceContext.class);
lookupMethodResource(context, instance, methods[i], annotation.name(), clazz);
}
if (methods[i].isAnnotationPresent(PersistenceUnit.class)) {
final PersistenceUnit annotation = methods[i].getAnnotation(PersistenceUnit.class);
lookupMethodResource(context, instance, methods[i], annotation.name(), clazz);
}
}
clazz = clazz.getSuperclass();
}
}
Aggregations