Search in sources :

Example 6 with EJB

use of javax.ejb.EJB in project tomee by apache.

the class TestClient method processSetterInjections.

protected final void processSetterInjections() {
    Object home = null;
    ClassFinder finder = null;
    List<Method> methodList = null;
    finder = new ClassFinder(getClassPath());
    methodList = finder.findAnnotatedMethods(EJB.class);
    for (final Iterator methods = methodList.iterator(); methods.hasNext(); ) {
        final Method method = (Method) methods.next();
        final EJB ejbAnnotation = method.getAnnotation(EJB.class);
        if ((ejbAnnotation.name() != null) && (ejbAnnotation.name() != "") && (ejbAnnotation.beanInterface() != null)) {
            try {
                home = initialContext.lookup(ejbAnnotation.name());
                // home = ejbAnnotation.beanInterface().cast(home;
                home = cast(home, ejbAnnotation.beanInterface());
                method.setAccessible(true);
                method.invoke(this, new Object[] { home });
            } catch (final Exception ex) {
                // TODO - MNour : Needs better exception handling
                ex.printStackTrace();
            }
        }
    }
}
Also used : ClassFinder(org.apache.xbean.finder.ClassFinder) Iterator(java.util.Iterator) Method(java.lang.reflect.Method) EJB(javax.ejb.EJB)

Example 7 with EJB

use of javax.ejb.EJB in project Payara by payara.

the class InjectionServicesImpl method validateEjbProducer.

/**
 * @param annotatedClass
 * @param annotatedField
 * @param injectionResources
 */
private void validateEjbProducer(Class annotatedClass, AnnotatedField annotatedField, List<InjectionCapable> injectionResources) {
    EJB ejbAnnotation = annotatedField.getAnnotation(EJB.class);
    if (ejbAnnotation != null) {
        String lookupName = getLookupName(annotatedClass, annotatedField, injectionResources);
        EjbDescriptor foundEjb = null;
        Collection<EjbDescriptor> ejbs = deployment.getDeployedEjbs();
        for (EjbDescriptor oneEjb : ejbs) {
            String jndiName = oneEjb.getJndiName();
            if (jndiName.isEmpty()) {
                jndiName = oneEjb.getName();
            }
            if (lookupName.contains(jndiName)) {
                foundEjb = oneEjb;
                break;
            }
        }
        if (foundEjb != null) {
            String className = foundEjb.getEjbImplClassName();
            try {
                Class clazz = Class.forName(className, false, annotatedClass.getClassLoader());
                validateResourceClass(annotatedField, clazz);
            } catch (ClassNotFoundException ignore) {
            }
        }
    }
}
Also used : EJB(javax.ejb.EJB)

Example 8 with EJB

use of javax.ejb.EJB in project Payara by payara.

the class InjectionServicesImpl method getLookupName.

/**
 * Returns the JNDI name to lookup
 * <p>
 * This calls getJndiName with the appropriate arguments for the annotation type
 * @param annotatedClass
 * @param annotatedField
 * @param injectionResources
 * @return
 */
private String getLookupName(Class annotatedClass, AnnotatedField annotatedField, List<InjectionCapable> injectionResources) {
    String lookupName = null;
    if (annotatedField.isAnnotationPresent(Resource.class)) {
        Resource resource = annotatedField.getAnnotation(Resource.class);
        lookupName = getJndiName(resource.lookup(), resource.mappedName(), resource.name());
    } else if (annotatedField.isAnnotationPresent(EJB.class)) {
        EJB ejb = annotatedField.getAnnotation(EJB.class);
        lookupName = getJndiName(ejb.lookup(), ejb.mappedName(), ejb.name());
    } else if (annotatedField.isAnnotationPresent(WebServiceRef.class)) {
        WebServiceRef webServiceRef = annotatedField.getAnnotation(WebServiceRef.class);
        lookupName = getJndiName(webServiceRef.lookup(), webServiceRef.mappedName(), webServiceRef.name());
    } else if (annotatedField.isAnnotationPresent(PersistenceUnit.class)) {
        PersistenceUnit persistenceUnit = annotatedField.getAnnotation(PersistenceUnit.class);
        lookupName = getJndiName(persistenceUnit.unitName(), null, persistenceUnit.name());
    } else if (annotatedField.isAnnotationPresent(PersistenceContext.class)) {
        PersistenceContext persistenceContext = annotatedField.getAnnotation(PersistenceContext.class);
        lookupName = getJndiName(persistenceContext.unitName(), null, persistenceContext.name());
    }
    if (lookupName == null || lookupName.trim().length() == 0) {
        lookupName = getComponentEnvName(annotatedClass, annotatedField.getJavaMember().getName(), injectionResources);
    }
    return lookupName;
}
Also used : PersistenceUnit(javax.persistence.PersistenceUnit) Resource(javax.annotation.Resource) PersistenceContext(javax.persistence.PersistenceContext) EJB(javax.ejb.EJB) WebServiceRef(javax.xml.ws.WebServiceRef)

Example 9 with EJB

use of javax.ejb.EJB in project tomcat70 by apache.

the class DefaultInstanceManager method populateAnnotationsCache.

/**
 * Make sure that the annotations cache has been populated for the provided
 * class.
 *
 * @param clazz         clazz to populate annotations for
 * @param injections    map of injections for this class from xml deployment
 *                      descriptor
 * @throws IllegalAccessException       if injection target is inaccessible
 * @throws javax.naming.NamingException if value cannot be looked up in jndi
 * @throws java.lang.reflect.InvocationTargetException
 *                                      if injection fails
 */
protected void populateAnnotationsCache(Class<?> clazz, Map<String, String> injections) throws IllegalAccessException, InvocationTargetException, NamingException {
    List<AnnotationCacheEntry> annotations = null;
    while (clazz != null) {
        AnnotationCacheEntry[] annotationsArray = annotationCache.get(clazz);
        if (annotationsArray == null) {
            if (annotations == null) {
                annotations = new ArrayList<AnnotationCacheEntry>();
            } else {
                annotations.clear();
            }
            if (context != null) {
                // Initialize fields annotations for resource injection if
                // JNDI is enabled
                Field[] fields = Introspection.getDeclaredFields(clazz);
                for (Field field : fields) {
                    Resource resourceAnnotation;
                    Annotation ejbAnnotation;
                    Annotation webServiceRefAnnotation;
                    Annotation persistenceContextAnnotation;
                    Annotation 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 (EJB_PRESENT && (ejbAnnotation = field.getAnnotation(EJB.class)) != null) {
                        annotations.add(new AnnotationCacheEntry(field.getName(), null, ((EJB) ejbAnnotation).name(), AnnotationCacheEntryType.FIELD));
                    } else if (WS_PRESENT && (webServiceRefAnnotation = field.getAnnotation(WebServiceRef.class)) != null) {
                        annotations.add(new AnnotationCacheEntry(field.getName(), null, ((WebServiceRef) webServiceRefAnnotation).name(), AnnotationCacheEntryType.FIELD));
                    } else if (JPA_PRESENT && (persistenceContextAnnotation = field.getAnnotation(PersistenceContext.class)) != null) {
                        annotations.add(new AnnotationCacheEntry(field.getName(), null, ((PersistenceContext) persistenceContextAnnotation).name(), AnnotationCacheEntryType.FIELD));
                    } else if (JPA_PRESENT && (persistenceUnitAnnotation = field.getAnnotation(PersistenceUnit.class)) != null) {
                        annotations.add(new AnnotationCacheEntry(field.getName(), null, ((PersistenceUnit) 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;
                    Annotation ejbAnnotation;
                    Annotation webServiceRefAnnotation;
                    Annotation persistenceContextAnnotation;
                    Annotation persistenceUnitAnnotation;
                    if ((resourceAnnotation = method.getAnnotation(Resource.class)) != null) {
                        annotations.add(new AnnotationCacheEntry(method.getName(), method.getParameterTypes(), resourceAnnotation.name(), AnnotationCacheEntryType.SETTER));
                    } else if (EJB_PRESENT && (ejbAnnotation = method.getAnnotation(EJB.class)) != null) {
                        annotations.add(new AnnotationCacheEntry(method.getName(), method.getParameterTypes(), ((EJB) ejbAnnotation).name(), AnnotationCacheEntryType.SETTER));
                    } else if (WS_PRESENT && (webServiceRefAnnotation = method.getAnnotation(WebServiceRef.class)) != null) {
                        annotations.add(new AnnotationCacheEntry(method.getName(), method.getParameterTypes(), ((WebServiceRef) webServiceRefAnnotation).name(), AnnotationCacheEntryType.SETTER));
                    } else if (JPA_PRESENT && (persistenceContextAnnotation = method.getAnnotation(PersistenceContext.class)) != null) {
                        annotations.add(new AnnotationCacheEntry(method.getName(), method.getParameterTypes(), ((PersistenceContext) persistenceContextAnnotation).name(), AnnotationCacheEntryType.SETTER));
                    } else if (JPA_PRESENT && (persistenceUnitAnnotation = method.getAnnotation(PersistenceUnit.class)) != null) {
                        annotations.add(new AnnotationCacheEntry(method.getName(), method.getParameterTypes(), ((PersistenceUnit) persistenceUnitAnnotation).name(), AnnotationCacheEntryType.SETTER));
                    }
                }
                postConstruct = findPostConstruct(postConstruct, postConstructFromXml, method);
                preDestroy = findPreDestroy(preDestroy, preDestroyFromXml, method);
            }
            if (postConstruct != null) {
                annotations.add(new AnnotationCacheEntry(postConstruct.getName(), postConstruct.getParameterTypes(), null, AnnotationCacheEntryType.POST_CONSTRUCT));
            } else if (postConstructFromXml != null) {
                throw new IllegalArgumentException("Post construct method " + postConstructFromXml + " for class " + clazz.getName() + " is declared in deployment descriptor but cannot be found.");
            }
            if (preDestroy != null) {
                annotations.add(new AnnotationCacheEntry(preDestroy.getName(), preDestroy.getParameterTypes(), null, AnnotationCacheEntryType.PRE_DESTROY));
            } else if (preDestroyFromXml != null) {
                throw new IllegalArgumentException("Pre destroy method " + preDestroyFromXml + " for class " + clazz.getName() + " is declared in deployment descriptor but cannot be found.");
            }
            if (annotations.isEmpty()) {
                // Use common object to save memory
                annotationsArray = ANNOTATIONS_EMPTY;
            } else {
                annotationsArray = annotations.toArray(new AnnotationCacheEntry[annotations.size()]);
            }
            synchronized (annotationCache) {
                annotationCache.put(clazz, annotationsArray);
            }
        }
        clazz = clazz.getSuperclass();
    }
}
Also used : Resource(javax.annotation.Resource) PersistenceContext(javax.persistence.PersistenceContext) Method(java.lang.reflect.Method) Annotation(java.lang.annotation.Annotation) Field(java.lang.reflect.Field) PersistenceUnit(javax.persistence.PersistenceUnit) EJB(javax.ejb.EJB) WebServiceRef(javax.xml.ws.WebServiceRef)

Aggregations

EJB (javax.ejb.EJB)9 Field (java.lang.reflect.Field)4 Method (java.lang.reflect.Method)4 Resource (javax.annotation.Resource)3 PersistenceContext (javax.persistence.PersistenceContext)3 PersistenceUnit (javax.persistence.PersistenceUnit)3 WebServiceRef (javax.xml.ws.WebServiceRef)3 Iterator (java.util.Iterator)2 ClassFinder (org.apache.xbean.finder.ClassFinder)2 Stateful (com.flowlogix.web.services.annotations.Stateful)1 Annotation (java.lang.annotation.Annotation)1 ArrayList (java.util.ArrayList)1 EJBs (javax.ejb.EJBs)1 SneakyThrows (lombok.SneakyThrows)1 PlasticField (org.apache.tapestry5.plastic.PlasticField)1 HandlerProcessingResult (org.glassfish.apf.HandlerProcessingResult)1 JNDIObjectLocator (org.omnifaces.util.JNDIObjectLocator)1