Search in sources :

Example 1 with PersistenceUnit

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

the class WeldJpaInjectionServices method registerPersistenceUnitInjectionPoint.

@Override
public ResourceReferenceFactory<EntityManagerFactory> registerPersistenceUnitInjectionPoint(final InjectionPoint injectionPoint) {
    //TODO: cache this stuff
    final PersistenceUnit context = getResourceAnnotated(injectionPoint).getAnnotation(PersistenceUnit.class);
    if (context == null) {
        throw WeldLogger.ROOT_LOGGER.annotationNotFound(PersistenceUnit.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 ImmediateResourceReferenceFactory<EntityManagerFactory>(persistenceUnitService.getEntityManagerFactory());
}
Also used : PersistenceUnitServiceImpl(org.jboss.as.jpa.service.PersistenceUnitServiceImpl) PersistenceUnit(javax.persistence.PersistenceUnit) ServiceName(org.jboss.msc.service.ServiceName) ImmediateResourceReferenceFactory(org.jboss.as.weld.util.ImmediateResourceReferenceFactory)

Example 2 with PersistenceUnit

use of javax.persistence.PersistenceUnit 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 PersistenceUnit

use of javax.persistence.PersistenceUnit 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 PersistenceUnit

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

the class PersistenceUnitHandler method handleFieldAnnotation.

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

                @Override
                public void write(XMLStreamWriter writer) throws XMLStreamException {
                    writer.writeEmptyElement("unit");
                    writer.writeDefaultNamespace(nsJpa1);
                    writer.writeAttribute("unitname", persistenceUnit.unitName());
                    writer.writeAttribute("property", name);
                }
            });
        }
    }
    final String nsJpa2 = getNamespaceByPattern(contextEnricher.getBlueprintConfiguration().getNamespaces(), 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) PersistenceUnit(javax.persistence.PersistenceUnit) XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) XmlWriter(org.apache.aries.blueprint.plugin.spi.XmlWriter)

Example 5 with PersistenceUnit

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

the class XmlOverridesTest method test.

public void test() throws Exception {
    final ConfigurationFactory config = new ConfigurationFactory();
    final Assembler assembler = new Assembler();
    assembler.createTransactionManager(config.configureService(TransactionServiceInfo.class));
    assembler.createSecurityService(config.configureService(SecurityServiceInfo.class));
    final EjbJar ejbJar = new EjbJar();
    final StatefulBean bean = ejbJar.addEnterpriseBean(new StatefulBean(AnnotatedBean.class));
    bean.getEjbLocalRef().add(new EjbLocalRef(name("annotatedLocal"), "BarBean"));
    bean.getEnvEntry().add(new EnvEntry(name("striing"), "java.lang.Integer", "2"));
    bean.getEnvEntry().add(new EnvEntry(name("doouble"), "java.lang.String", "two"));
    bean.getEnvEntry().add(new EnvEntry(name("loong"), "java.lang.String", "three"));
    bean.getEnvEntry().add(new EnvEntry(name("flooat"), "java.lang.String", "four"));
    bean.getEnvEntry().add(new EnvEntry(name("inteeger"), "java.lang.String", "five"));
    bean.getEnvEntry().add(new EnvEntry(name("shoort"), "java.lang.String", "six"));
    bean.getEnvEntry().add(new EnvEntry(name("booolean"), "java.lang.String", "seven"));
    bean.getEnvEntry().add(new EnvEntry(name("byyte"), "java.lang.String", "eight"));
    bean.getEnvEntry().add(new EnvEntry(name("chaaracter"), "java.lang.String", "nine"));
    final EnvEntry lookupEntry = new EnvEntry(name("lookup"), "java.lang.String", null);
    lookupEntry.setLookupName("java:app/AppName");
    bean.getEnvEntry().add(lookupEntry);
    bean.getResourceRef().add(new ResourceRef(name("daataSource"), DataSource.class.getName(), ResAuth.CONTAINER, ResSharingScope.SHAREABLE));
    bean.getPersistenceUnitRef().add(new PersistenceUnitRef(name("emf"), "yellow"));
    bean.getPersistenceContextRef().add(new PersistenceContextRef(name("em"), "yellow", PersistenceContextType.TRANSACTION, new ArrayList(Arrays.asList(new Property("zzzz", "AAAA")))));
    final org.apache.openejb.jee.jpa.unit.PersistenceUnit persistenceUnit = new org.apache.openejb.jee.jpa.unit.PersistenceUnit("yellow");
    final AppModule app = new AppModule(this.getClass().getClassLoader(), "app");
    app.getEjbModules().add(new EjbModule(ejbJar));
    app.addPersistenceModule(new PersistenceModule("root", new Persistence(persistenceUnit)));
    final AppInfo appInfo = config.configureApplication(app);
    final EjbJarInfo ejbJarInfo = appInfo.ejbJars.get(0);
    final EnterpriseBeanInfo beanInfo = ejbJarInfo.enterpriseBeans.get(0);
    final JndiEncInfo enc = beanInfo.jndiEnc;
    assertEquals("Enc.ejbLocalReferences.size()", 1, enc.ejbLocalReferences.size());
    assertEquals("Enc.ejbLocalReferences.get(0).link", "BarBean", enc.ejbLocalReferences.get(0).link);
    assertEquals("Enc.ejbReferences.size()", 0, enc.ejbReferences.size());
    // 10 + ComponentName
    assertEquals("Enc.envEntries.size()", 11, enc.envEntries.size());
    final Map<String, EnvEntryInfo> entries = map(enc.envEntries);
    assertEnvEntry(entries, name("striing"), "java.lang.Integer", "2");
    assertEnvEntry(entries, name("doouble"), "java.lang.String", "two");
    assertEnvEntry(entries, name("loong"), "java.lang.String", "three");
    assertEnvEntry(entries, name("flooat"), "java.lang.String", "four");
    assertEnvEntry(entries, name("inteeger"), "java.lang.String", "five");
    assertEnvEntry(entries, name("shoort"), "java.lang.String", "six");
    assertEnvEntry(entries, name("booolean"), "java.lang.String", "seven");
    assertEnvEntry(entries, name("byyte"), "java.lang.String", "eight");
    assertEnvEntry(entries, name("chaaracter"), "java.lang.String", "nine");
    assertEnvEntryLookup(entries, name("lookup"), "java.lang.String", "java:app/AppName");
    assertEquals("Enc.persistenceContextRefs.size()", 1, enc.persistenceContextRefs.size());
    final PersistenceContextReferenceInfo context = enc.persistenceContextRefs.get(0);
    assertEquals("Context.extended", false, context.extended);
    assertEquals("Context.persistenceUnitName", "yellow", context.persistenceUnitName);
    assertEquals("Context.properties.size()", 1, context.properties.size());
    assertEquals("Context.properties.getProperty(\"zzzz\")", "AAAA", context.properties.getProperty("zzzz"));
    assertEquals("Enc.persistenceUnitRefs.size()", 1, enc.persistenceUnitRefs.size());
    final PersistenceUnitReferenceInfo unit = enc.persistenceUnitRefs.get(0);
    assertEquals("Unit.persistenceUnitName", "yellow", unit.persistenceUnitName);
    assertEquals("Enc.resourceRefs.size()", 1, enc.resourceRefs.size());
    final ResourceReferenceInfo resource = enc.resourceRefs.get(0);
    assertEquals("Resource.referenceAuth", "CONTAINER", resource.referenceAuth);
}
Also used : PersistenceContextRef(org.apache.openejb.jee.PersistenceContextRef) ArrayList(java.util.ArrayList) PersistenceUnitReferenceInfo(org.apache.openejb.assembler.classic.PersistenceUnitReferenceInfo) EnterpriseBeanInfo(org.apache.openejb.assembler.classic.EnterpriseBeanInfo) ResourceReferenceInfo(org.apache.openejb.assembler.classic.ResourceReferenceInfo) PersistenceUnit(javax.persistence.PersistenceUnit) SecurityServiceInfo(org.apache.openejb.assembler.classic.SecurityServiceInfo) Property(org.apache.openejb.jee.Property) PersistenceUnitRef(org.apache.openejb.jee.PersistenceUnitRef) EjbJar(org.apache.openejb.jee.EjbJar) EnvEntry(org.apache.openejb.jee.EnvEntry) EjbLocalRef(org.apache.openejb.jee.EjbLocalRef) PersistenceContextReferenceInfo(org.apache.openejb.assembler.classic.PersistenceContextReferenceInfo) JndiEncInfo(org.apache.openejb.assembler.classic.JndiEncInfo) StatefulBean(org.apache.openejb.jee.StatefulBean) EnvEntryInfo(org.apache.openejb.assembler.classic.EnvEntryInfo) AppInfo(org.apache.openejb.assembler.classic.AppInfo) Persistence(org.apache.openejb.jee.jpa.unit.Persistence) TransactionServiceInfo(org.apache.openejb.assembler.classic.TransactionServiceInfo) ResourceRef(org.apache.openejb.jee.ResourceRef) Assembler(org.apache.openejb.assembler.classic.Assembler) EjbJarInfo(org.apache.openejb.assembler.classic.EjbJarInfo)

Aggregations

PersistenceUnit (javax.persistence.PersistenceUnit)5 Field (java.lang.reflect.Field)3 Method (java.lang.reflect.Method)2 Resource (javax.annotation.Resource)2 EJB (javax.ejb.EJB)2 PersistenceContext (javax.persistence.PersistenceContext)2 WebServiceRef (javax.xml.ws.WebServiceRef)2 ArrayList (java.util.ArrayList)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)1 XmlWriter (org.apache.aries.blueprint.plugin.spi.XmlWriter)1 AppInfo (org.apache.openejb.assembler.classic.AppInfo)1 Assembler (org.apache.openejb.assembler.classic.Assembler)1 EjbJarInfo (org.apache.openejb.assembler.classic.EjbJarInfo)1 EnterpriseBeanInfo (org.apache.openejb.assembler.classic.EnterpriseBeanInfo)1 EnvEntryInfo (org.apache.openejb.assembler.classic.EnvEntryInfo)1 JndiEncInfo (org.apache.openejb.assembler.classic.JndiEncInfo)1 PersistenceContextReferenceInfo (org.apache.openejb.assembler.classic.PersistenceContextReferenceInfo)1 PersistenceUnitReferenceInfo (org.apache.openejb.assembler.classic.PersistenceUnitReferenceInfo)1 ResourceReferenceInfo (org.apache.openejb.assembler.classic.ResourceReferenceInfo)1