Package javax.annotation

Examples of javax.annotation.Resource


                if (context != null) {
                    // Initialize fields annotations for resource injection if
                    // JNDI is enabled
                    Field[] fields = Introspection.getDeclaredFields(clazz);
                    for (Field field : fields) {
                        Resource resourceAnnotation;
                        EJB ejbAnnotation;
                        WebServiceRef webServiceRefAnnotation;
                        PersistenceContext persistenceContextAnnotation;
                        PersistenceUnit persistenceUnitAnnotation;
                        if (injections != null && injections.containsKey(field.getName())) {
                            annotations.add(new AnnotationCacheEntry(
                                    field.getName(), null,
                                    injections.get(field.getName()),
                                    AnnotationCacheEntryType.FIELD));
                        } else if ((resourceAnnotation =
                                field.getAnnotation(Resource.class)) != null) {
                            annotations.add(new AnnotationCacheEntry(field.getName(), null,
                                    resourceAnnotation.name(), AnnotationCacheEntryType.FIELD));
                        } else if ((ejbAnnotation =
                                field.getAnnotation(EJB.class)) != null) {
                            annotations.add(new AnnotationCacheEntry(field.getName(), null,
                                    ejbAnnotation.name(), AnnotationCacheEntryType.FIELD));
                        } else if ((webServiceRefAnnotation =
                                field.getAnnotation(WebServiceRef.class)) != null) {
                            annotations.add(new AnnotationCacheEntry(field.getName(), null,
                                    webServiceRefAnnotation.name(),
                                    AnnotationCacheEntryType.FIELD));
                        } else if ((persistenceContextAnnotation =
                                field.getAnnotation(PersistenceContext.class)) != null) {
                            annotations.add(new AnnotationCacheEntry(field.getName(), null,
                                    persistenceContextAnnotation.name(),
                                    AnnotationCacheEntryType.FIELD));
                        } else if ((persistenceUnitAnnotation =
                                field.getAnnotation(PersistenceUnit.class)) != null) {
                            annotations.add(new AnnotationCacheEntry(field.getName(), null,
                                    persistenceUnitAnnotation.name(),
                                    AnnotationCacheEntryType.FIELD));
                        }
                    }
                }

                // Initialize methods annotations
                Method[] methods = Introspection.getDeclaredMethods(clazz);
                Method postConstruct = null;
                String postConstructFromXml = postConstructMethods.get(clazz.getName());
                Method preDestroy = null;
                String preDestroyFromXml = preDestroyMethods.get(clazz.getName());
                for (Method method : methods) {
                    if (context != null) {
                        // Resource injection only if JNDI is enabled
                        if (injections != null &&
                                Introspection.isValidSetter(method)) {
                            String fieldName = Introspection.getPropertyName(method);
                            if (injections.containsKey(fieldName)) {
                                annotations.add(new AnnotationCacheEntry(
                                        method.getName(),
                                        method.getParameterTypes(),
                                        injections.get(fieldName),
                                        AnnotationCacheEntryType.SETTER));
                                continue;
                            }
                        }
                        Resource resourceAnnotation;
                        EJB ejbAnnotation;
                        WebServiceRef webServiceRefAnnotation;
                        PersistenceContext persistenceContextAnnotation;
                        PersistenceUnit persistenceUnitAnnotation;
                        if ((resourceAnnotation =
                                method.getAnnotation(Resource.class)) != null) {
                            annotations.add(new AnnotationCacheEntry(
                                    method.getName(),
                                    method.getParameterTypes(),
                                    resourceAnnotation.name(),
                                    AnnotationCacheEntryType.SETTER));
                        } else if ((ejbAnnotation =
                                method.getAnnotation(EJB.class)) != null) {
                            annotations.add(new AnnotationCacheEntry(
                                    method.getName(),
View Full Code Here


            for (Field field : fields) {
                if (injections != null && injections.containsKey(field.getName())) {
                    lookupFieldResource(context, instance, field,
                            injections.get(field.getName()), clazz);
                } else if (field.isAnnotationPresent(Resource.class)) {
                    Resource annotation = field.getAnnotation(Resource.class);
                    lookupFieldResource(context, instance, field,
                            annotation.name(), clazz);
                } else if (field.isAnnotationPresent(EJB.class)) {
                    EJB annotation = field.getAnnotation(EJB.class);
                    lookupFieldResource(context, instance, field,
                            annotation.name(), clazz);
                } else if (field.isAnnotationPresent(WebServiceRef.class)) {
                    WebServiceRef annotation =
                            field.getAnnotation(WebServiceRef.class);
                    lookupFieldResource(context, instance, field,
                            annotation.name(), clazz);
                } else if (field.isAnnotationPresent(PersistenceContext.class)) {
                    PersistenceContext annotation =
                            field.getAnnotation(PersistenceContext.class);
                    lookupFieldResource(context, instance, field,
                            annotation.name(), clazz);
                } else if (field.isAnnotationPresent(PersistenceUnit.class)) {
                    PersistenceUnit annotation =
                            field.getAnnotation(PersistenceUnit.class);
                    lookupFieldResource(context, instance, field,
                            annotation.name(), clazz);
                }
            }
   
            // Initialize methods annotations
            Method[] methods = null;
            if (Globals.IS_SECURITY_ENABLED) {
                final Class<?> clazz2 = clazz;
                methods = AccessController.doPrivileged(
                        new PrivilegedAction<Method[]>(){
                    @Override
                    public Method[] run(){
                        return clazz2.getDeclaredMethods();
                    }
                });
            } else {
                methods = clazz.getDeclaredMethods();
            }
            for (Method method : methods) {
                String methodName = method.getName();
                if (injections != null && methodName.startsWith("set") && methodName.length() > 3) {
                    String fieldName = Character.toLowerCase(methodName.charAt(3)) + methodName.substring(4);
                    if (injections.containsKey(fieldName)) {
                        lookupMethodResource(context, instance, method,
                                injections.get(fieldName), clazz);
                        break;
                    }
                }
                if (method.isAnnotationPresent(Resource.class)) {
                    Resource annotation = method.getAnnotation(Resource.class);
                    lookupMethodResource(context, instance, method,
                            annotation.name(), clazz);
                } else if (method.isAnnotationPresent(EJB.class)) {
                    EJB annotation = method.getAnnotation(EJB.class);
                    lookupMethodResource(context, instance, method,
                            annotation.name(), clazz);
                } else if (method.isAnnotationPresent(WebServiceRef.class)) {
                    WebServiceRef annotation =
                            method.getAnnotation(WebServiceRef.class);
                    lookupMethodResource(context, instance, method,
                            annotation.name(), clazz);
                } else if (method.isAnnotationPresent(PersistenceContext.class)) {
                    PersistenceContext annotation =
                            method.getAnnotation(PersistenceContext.class);
                    lookupMethodResource(context, instance, method,
                            annotation.name(), clazz);
                } else if (method.isAnnotationPresent(PersistenceUnit.class)) {
                    PersistenceUnit annotation =
                            method.getAnnotation(PersistenceUnit.class);
                    lookupMethodResource(context, instance, method,
                            annotation.name(), clazz);
                }
            }
            clazz = clazz.getSuperclass();
        }
View Full Code Here

    private void processClassResourceAnnotations ()
    {
        for (int i=0; i<_classes.size();i++)
        {
            Class clazz = (Class)_classes.get(i);
            Resource resource = (Resource)clazz.getAnnotation(Resource.class);
            if (resource != null)
            {
               String name = resource.name();
               String mappedName = resource.mappedName();
               Resource.AuthenticationType auth = resource.authenticationType();
               Class type = resource.type();
               boolean shareable = resource.shareable();
              
               if (name==null || name.trim().equals(""))
                   throw new IllegalStateException ("Class level Resource annotations must contain a name (Common Annotations Spec Section 2.3)");
              
               try
View Full Code Here

    {
        //Get the method level Resource annotations       
        for (int i=0;i<_methods.size();i++)
        {
            Method m = (Method)_methods.get(i);
            Resource resource = (Resource)m.getAnnotation(Resource.class);
            if (resource != null)
            {
                //JavaEE Spec 5.2.3: Method cannot be static
                if (Modifier.isStatic(m.getModifiers()))
                    throw new IllegalStateException(m+" cannot be static");
               
               
                // Check it is a valid javabean
                if (!IntrospectionUtil.isJavaBeanCompliantSetter(m))
                    throw new IllegalStateException(m+" is not a java bean compliant setter method");
    
                //allow default name to be overridden
                String name = (resource.name()!=null && !resource.name().trim().equals("")? resource.name(): defaultResourceNameFromMethod(m));
                //get the mappedName if there is one
                String mappedName = (resource.mappedName()!=null && !resource.mappedName().trim().equals("")?resource.mappedName():null);      
                Class type = m.getParameterTypes()[0];
                //get other parts that can be specified in @Resource
                Resource.AuthenticationType auth = resource.authenticationType();
                boolean shareable = resource.shareable();

                //if @Resource specifies a type, check it is compatible with setter param
                if ((resource.type() != null)
                        &&
                        !resource.type().equals(Object.class)
                        &&
                        (!IntrospectionUtil.isTypeCompatible(type, resource.type(), false)))
                    throw new IllegalStateException("@Resource incompatible type="+resource.type()+ " with method param="+type+ " for "+m);
              
                //check if an injection has already been setup for this target by web.xml
                Injection webXmlInjection = webXmlInjections.getInjection(getTargetClass(), m);
               
                if (webXmlInjection == null)
View Full Code Here

    private void processFieldResourceAnnotations (InjectionCollection webXmlInjections)
    {
        for (int i=0;i<_fields.size();i++)
        {
            Field f = (Field)_fields.get(i);
            Resource resource = (Resource)f.getAnnotation(Resource.class);
            if (resource != null)
            {
                //JavaEE Spec 5.2.3: Field cannot be static
                if (Modifier.isStatic(f.getModifiers()))
                    throw new IllegalStateException(f+" cannot be static");
               
                //JavaEE Spec 5.2.3: Field cannot be final
                if (Modifier.isFinal(f.getModifiers()))
                    throw new IllegalStateException(f+" cannot be final");
               
                //work out default name
                String name = f.getDeclaringClass().getCanonicalName()+"/"+f.getName();
                //allow @Resource name= to override the field name
                name = (resource.name()!=null && !resource.name().trim().equals("")? resource.name(): name);
               
                //get the type of the Field
                Class type = f.getType();
                //if @Resource specifies a type, check it is compatible with field type
                if ((resource.type() != null)
                        &&
                        !resource.type().equals(Object.class)
                        &&
                        (!IntrospectionUtil.isTypeCompatible(type, resource.type(), false)))
                    throw new IllegalStateException("@Resource incompatible type="+resource.type()+ " with field type ="+f.getType());
               
                //get the mappedName if there is one
                String mappedName = (resource.mappedName()!=null && !resource.mappedName().trim().equals("")?resource.mappedName():null);
                //get other parts that can be specified in @Resource
                Resource.AuthenticationType auth = resource.authenticationType();
                boolean shareable = resource.shareable();
           
                //check if an injection has already been setup for this target by web.xml
                Injection webXmlInjection = webXmlInjections.getInjection(getTargetClass(), f);
                if (webXmlInjection == null)
                {
View Full Code Here

        Class<?> type = null;

        do {
            try {
                Field field = clazz.getDeclaredField(fieldName);
                Resource resource = field.getAnnotation(Resource.class);
                if (resource != null && resource.type()!=Object.class) {
                    type = chooseType(fieldName, field.getType(), resource.type());
                } else {
                    type = field.getType();
                }

            } catch (NoSuchFieldException e) {
                //look at superclass
            }
            for (Method method: clazz.getDeclaredMethods()) {
                if (method.getReturnType() == void.class && method.getParameterTypes().length == 1) {
                    String methodName = method.getName();
                    if (methodName.startsWith("set")) {
                        String setName = Introspector.decapitalize(methodName.substring(3));
                        if (fieldName.equals(setName)) {
                            Resource resource = method.getAnnotation(Resource.class);
                            if (resource != null && resource.type()!=Object.class) {
                                type = chooseType(fieldName, method.getParameterTypes()[0], resource.type());
                            } else {
                                type = method.getParameterTypes()[0];
                            }
                        }
                    }
View Full Code Here

        List<Method> methodswithResource = classFinder.findAnnotatedMethods(Resource.class);
        List<Field> fieldswithResource = classFinder.findAnnotatedFields(Resource.class);

        // Class-level annotation
        for (Class cls : classeswithResource) {
            Resource resource = (Resource) cls.getAnnotation(Resource.class);
            if (resource != null) {
                resourceProcessor.processResource(annotatedApp, resource, cls, null, null);
            }
        }

        // Method-level annotation
        for (Method method : methodswithResource) {
            Resource resource = (Resource) method.getAnnotation(Resource.class);
            if (resource != null) {
                resourceProcessor.processResource(annotatedApp, resource, null, method, null);
            }
        }

        // Field-level annotation
        for (Field field : fieldswithResource) {
            Resource resource = (Resource) field.getAnnotation(Resource.class);
            if (resource != null) {
                resourceProcessor.processResource(annotatedApp, resource, null, null, field);
            }
        }
View Full Code Here

    }

    public static void addWebServiceContextInjections(Holder holder, ClassFinder finder) {
        List<Field> fields = finder.findAnnotatedFields(Resource.class);
        for (Field field : fields) {
            Resource resource = field.getAnnotation(Resource.class);
            Class type = getInjectionType(resource.type(), null, field);
            if (WebServiceContext.class == type) {
                holder.addInjection(field.getDeclaringClass().getName(),
                                    new Injection(field.getDeclaringClass().getName(), getInjectionName(null, field), BINDING_WEBSERVICE_CONTEXR_JNDI_NAME, ReferenceType.RESOURCE_ENV));
            }
        }
        List<Method> methods = finder.findAnnotatedMethods(Resource.class);
        for (Method method : methods) {
            Resource resource = method.getAnnotation(Resource.class);
            Class type = getInjectionType(resource.type(), method, null);
            if (WebServiceContext.class == type) {
                holder.addInjection(method.getDeclaringClass().getName(),
                                    new Injection(method.getDeclaringClass().getName(), getInjectionName(method, null), BINDING_WEBSERVICE_CONTEXR_JNDI_NAME, ReferenceType.RESOURCE_ENV));
            }
        }
View Full Code Here

      for (Resource ref : resources.value())
      {
         handleClassAnnotation(ref, container, clazz);
      }
      }
      Resource res = container.getAnnotation(Resource.class, clazz);
      if (res != null) handleClassAnnotation(res, container, clazz);
   }
View Full Code Here

      }
   }

   public void handleMethodAnnotations(Method method, InjectionContainer container, Map<AccessibleObject, Injector> injectors)
   {
      Resource ref = container.getAnnotation(Resource.class, method);
      if (ref == null) return;

      log.trace("method " + method + " has @Resource");
     
      handlePropertyAnnotation(ref, new MethodBeanProperty(method), container, injectors);
View Full Code Here

TOP

Related Classes of javax.annotation.Resource

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.