public ResourceElement(Member member, PropertyDescriptor pd) { super(member, pd); AnnotatedElement ae = (AnnotatedElement) member; Resource resource = ae.getAnnotation(Resource.class); String resourceName = resource.name(); Class<?> resourceType = resource.type(); this.isDefaultName = !StringUtils.hasLength(resourceName); if (this.isDefaultName) { resourceName = this.member.getName(); if (this.member instanceof Method && resourceName.startsWith("set") && resourceName.length() > 3) { resourceName = Introspector.decapitalize(resourceName.substring(3)); } } else if (beanFactory instanceof ConfigurableBeanFactory) { resourceName = ((ConfigurableBeanFactory) beanFactory).resolveEmbeddedValue(resourceName); } if (resourceType != null && !Object.class.equals(resourceType)) { checkResourceType(resourceType); } else { // No resource type specified... check field/method. resourceType = getResourceType(); } this.name = resourceName; this.lookupType = resourceType; this.mappedName = resource.mappedName(); this.shareable = resource.shareable(); }
private static void inject0(Object target, Class<?> injectableType, Object injectableInstance) { Class<?> targetClass = target.getClass(); Method[] methods = targetClass.getMethods(); for (Method method : methods) { String methodName = method.getName(); Class<?>[] parameterTypes = method.getParameterTypes(); if (methodName.startsWith("set") && methodName.length() > "set".length() && parameterTypes.length == 1) { Resource annotation = method.getAnnotation(Resource.class); if (annotation != null) { Class<?> resourceType = annotation.type(); if (resourceType == Object.class) { resourceType = parameterTypes[0]; } if (resourceType == injectableType) { try { method.invoke(target, injectableInstance); } catch (IllegalArgumentException | InvocationTargetException | IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } } }
private ValueGenerator generateContext( String loc, Class<?> bindType, String fullJndiName, Resource resource) throws ConfigException { String name = resource.name(); String mappedName = resource.mappedName(); String lookupName = null; if (_lookupMethod != null) { try { lookupName = (String) _lookupMethod.invoke(resource); } catch (Exception e) { log.log(Level.FINER, e.toString(), e); } } if (!resource.type().equals(Object.class) && !resource.type().equals(void.class)) { bindType = resource.type(); } ValueGenerator gen = null; if (lookupName != null && !"".equals(lookupName)) gen = new JndiValueGenerator(loc, bindType, lookupName); else gen = lookupJndi(loc, bindType, name); if (gen != null) { if (fullJndiName != null) bindJndi(null, gen, fullJndiName); } else { gen = bindValueGenerator(loc, bindType, name, mappedName); bindJndi(name, gen, fullJndiName); } return gen; }
protected boolean injectNamedResource(Object serviceInstance, Field field, Resource resource) { if (resources == null) { return false; } Object value = resources.get(resource.name()); if (value == null) { value = resources.get(resource.mappedName()); } if (value != null) { injectResourceValue(serviceInstance, field, value); return true; } return false; }
public static Reference createFor(Resource resource, Field field) { final Class<?> type; if (!Object.class.equals(resource.type())) { type = resource.type(); } else { type = field.getType(); } final String name; if (resource.name().length() > 0) { name = resource.name(); } else { name = field.getDeclaringClass().getName() + "/" + field.getName(); } return new Reference(type, name, field); }
private void introspectClass(String location, Resource resource) { String name = resource.name(); Class<?> bindType = resource.type(); if ("".equals(name)) throw new ConfigException( L.l("{0}: @Resource name() attribute is required for @Resource on a class.", location)); if (Object.class.equals(bindType)) throw new ConfigException( L.l( "{0}: @Resource beanInterface() attribute is required for @Resource on a class.", location)); ValueGenerator gen = generateContext(location, bindType, null, resource); if (name != null && !"".equals(name)) { bindJndi(name, gen, name); } }
public static String getResourceName(InjectionPoint injectionPoint) { Resource resource = getResourceAnnotated(injectionPoint).getAnnotation(Resource.class); String mappedName = resource.mappedName(); if (!mappedName.equals("")) { return mappedName; } String name = resource.name(); if (!name.equals("")) { 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 WeldMessages.MESSAGES.injectionPointNotAJavabean((Method) injectionPoint.getMember()); } } else { throw WeldMessages.MESSAGES.cannotInject(injectionPoint); } String className = injectionPoint.getMember().getDeclaringClass().getName(); return RESOURCE_LOOKUP_PREFIX + "/" + className + "/" + propertyName; }