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 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;
  }
Esempio n. 3
0
 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 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;
 }