Search in sources :

Example 1 with PersistenceContext

use of javax.persistence.PersistenceContext in project tomee by apache.

the class PersistenceContextAnnFactoryTest method test.

public void test() throws Exception {
    final Field useAsm = PersistenceContextAnnFactory.class.getDeclaredField("useAsm");
    useAsm.setAccessible(true);
    useAsm.set(null, true);
    final PersistenceContextAnnFactory factory = new PersistenceContextAnnFactory();
    factory.addAnnotations(Foo.class);
    for (final PersistenceContext annotation : Foo.class.getAnnotation(PersistenceContexts.class).value()) {
        assertEq(annotation, factory.create(annotation, null));
    }
    PersistenceContext annotation = Foo.class.getAnnotation(PersistenceContext.class);
    assertEq(annotation, factory.create(annotation, null));
    for (final Field field : Foo.class.getFields()) {
        annotation = field.getAnnotation(PersistenceContext.class);
        assertEq(annotation, factory.create(annotation, new AnnotationDeployer.FieldMember(field)));
    }
    for (final Method method : Foo.class.getMethods()) {
        annotation = method.getAnnotation(PersistenceContext.class);
        if (annotation != null) {
            assertEq(annotation, factory.create(annotation, new AnnotationDeployer.MethodMember(method)));
        }
    }
}
Also used : Field(java.lang.reflect.Field) PersistenceContexts(javax.persistence.PersistenceContexts) PersistenceContext(javax.persistence.PersistenceContext) Method(java.lang.reflect.Method)

Example 2 with PersistenceContext

use of javax.persistence.PersistenceContext in project tomee by apache.

the class LegacyAnnotationProcessor method processAnnotations.

/**
     * Inject resources in specified instance.
     */
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();
    }
}
Also used : Field(java.lang.reflect.Field) PersistenceUnit(javax.persistence.PersistenceUnit) Resource(javax.annotation.Resource) PersistenceContext(javax.persistence.PersistenceContext) Method(java.lang.reflect.Method) EJB(javax.ejb.EJB) WebServiceRef(javax.xml.ws.WebServiceRef)

Example 3 with PersistenceContext

use of javax.persistence.PersistenceContext in project tomcat 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<>();
            } 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;
                    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(), method.getParameterTypes(), ejbAnnotation.name(), AnnotationCacheEntryType.SETTER));
                    } else if ((webServiceRefAnnotation = method.getAnnotation(WebServiceRef.class)) != null) {
                        annotations.add(new AnnotationCacheEntry(method.getName(), method.getParameterTypes(), webServiceRefAnnotation.name(), AnnotationCacheEntryType.SETTER));
                    } else if ((persistenceContextAnnotation = method.getAnnotation(PersistenceContext.class)) != null) {
                        annotations.add(new AnnotationCacheEntry(method.getName(), method.getParameterTypes(), persistenceContextAnnotation.name(), AnnotationCacheEntryType.SETTER));
                    } else if ((persistenceUnitAnnotation = method.getAnnotation(PersistenceUnit.class)) != null) {
                        annotations.add(new AnnotationCacheEntry(method.getName(), method.getParameterTypes(), 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) Field(java.lang.reflect.Field) PersistenceUnit(javax.persistence.PersistenceUnit) EJB(javax.ejb.EJB) WebServiceRef(javax.xml.ws.WebServiceRef)

Example 4 with PersistenceContext

use of javax.persistence.PersistenceContext in project wildfly by wildfly.

the class WeldJpaInjectionServices method registerPersistenceContextInjectionPoint.

@Override
public ResourceReferenceFactory<EntityManager> registerPersistenceContextInjectionPoint(final InjectionPoint injectionPoint) {
    //TODO: cache this stuff
    final PersistenceContext context = getResourceAnnotated(injectionPoint).getAnnotation(PersistenceContext.class);
    if (context == null) {
        throw WeldLogger.ROOT_LOGGER.annotationNotFound(PersistenceContext.class, injectionPoint.getMember());
    }
    final String scopedPuName = getScopedPUName(deploymentUnit, context.unitName(), injectionPoint.getMember());
    final ServiceName persistenceUnitServiceName = PersistenceUnitServiceImpl.getPUServiceName(scopedPuName);
    final ServiceController<?> serviceController = deploymentUnit.getServiceRegistry().getRequiredService(persistenceUnitServiceName);
    //now we have the service controller, as this method is only called at runtime the service should
    //always be up
    final PersistenceUnitServiceImpl persistenceUnitService = (PersistenceUnitServiceImpl) serviceController.getValue();
    return new EntityManagerResourceReferenceFactory(scopedPuName, persistenceUnitService.getEntityManagerFactory(), context, deploymentUnit.getAttachment(JpaAttachments.TRANSACTION_SYNCHRONIZATION_REGISTRY), deploymentUnit.getAttachment(JpaAttachments.TRANSACTION_MANAGER));
}
Also used : PersistenceUnitServiceImpl(org.jboss.as.jpa.service.PersistenceUnitServiceImpl) ServiceName(org.jboss.msc.service.ServiceName) PersistenceContext(javax.persistence.PersistenceContext)

Example 5 with PersistenceContext

use of javax.persistence.PersistenceContext in project aries by apache.

the class PersistenceContextHandler method handleFieldAnnotation.

@Override
public void handleFieldAnnotation(Class<?> clazz, List<Field> fields, ContextEnricher contextEnricher, BeanEnricher beanEnricher) {
    final String nsJpa1 = Namespaces.getNamespaceByPattern(contextEnricher.getBlueprintConfiguration().getNamespaces(), Namespaces.PATTERN_NS_JPA1);
    if (nsJpa1 != null) {
        for (final Field field : fields) {
            final String name = field.getName();
            final PersistenceContext persistenceContext = field.getAnnotation(PersistenceContext.class);
            beanEnricher.addBeanContentWriter("javax.persistence.field.context/" + name, new XmlWriter() {

                @Override
                public void write(XMLStreamWriter writer) throws XMLStreamException {
                    writer.writeEmptyElement("context");
                    writer.writeDefaultNamespace(nsJpa1);
                    writer.writeAttribute("unitname", persistenceContext.unitName());
                    writer.writeAttribute("property", name);
                }
            });
        }
    }
    final String nsJpa2 = Namespaces.getNamespaceByPattern(contextEnricher.getBlueprintConfiguration().getNamespaces(), Namespaces.PATTERN_NS_JPA2);
    if (nsJpa2 != null) {
        contextEnricher.addBlueprintContentWriter("javax.persistence.enableJpa2", new XmlWriter() {

            @Override
            public void write(XMLStreamWriter writer) throws XMLStreamException {
                writer.writeEmptyElement("enable");
                writer.writeDefaultNamespace(nsJpa2);
            }
        });
    }
}
Also used : Field(java.lang.reflect.Field) XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) PersistenceContext(javax.persistence.PersistenceContext) XmlWriter(org.apache.aries.blueprint.plugin.spi.XmlWriter)

Aggregations

PersistenceContext (javax.persistence.PersistenceContext)5 Field (java.lang.reflect.Field)4 Method (java.lang.reflect.Method)3 Resource (javax.annotation.Resource)2 EJB (javax.ejb.EJB)2 PersistenceUnit (javax.persistence.PersistenceUnit)2 WebServiceRef (javax.xml.ws.WebServiceRef)2 PersistenceContexts (javax.persistence.PersistenceContexts)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)1 XmlWriter (org.apache.aries.blueprint.plugin.spi.XmlWriter)1 PersistenceUnitServiceImpl (org.jboss.as.jpa.service.PersistenceUnitServiceImpl)1 ServiceName (org.jboss.msc.service.ServiceName)1