Search in sources :

Example 1 with PersistenceUnitMetadata

use of org.jipijapa.plugin.spi.PersistenceUnitMetadata in project wildfly by wildfly.

the class HibernateAnnotationScanner method getClassesInJar.

@Override
public Set<Class<?>> getClassesInJar(URL jarToScan, Set<Class<? extends Annotation>> annotationsToLookFor) {
    if (jarToScan == null) {
        throw JPA_LOGGER.nullVar("jarToScan");
    }
    JPA_LOGGER.tracef("getClassesInJar url=%s annotations=%s", jarToScan.getPath(), annotationsToLookFor);
    PersistenceUnitMetadata pu = PERSISTENCE_UNIT_METADATA_TLS.get();
    if (pu == null) {
        throw JPA_LOGGER.missingPersistenceUnitMetadata();
    }
    if (pu.getAnnotationIndex() != null) {
        Index index = getJarFileIndex(jarToScan, pu);
        if (index == null) {
            JPA_LOGGER.tracef("No classes to scan for annotations in jar '%s' (jars with classes '%s')", jarToScan, pu.getAnnotationIndex().keySet());
            return new HashSet<Class<?>>();
        }
        if (annotationsToLookFor == null) {
            throw JPA_LOGGER.nullVar("annotationsToLookFor");
        }
        if (annotationsToLookFor.size() == 0) {
            throw JPA_LOGGER.emptyParameter("annotationsToLookFor");
        }
        Set<Class<?>> result = new HashSet<Class<?>>();
        for (Class<? extends Annotation> annClass : annotationsToLookFor) {
            DotName annotation = DotName.createSimple(annClass.getName());
            List<AnnotationInstance> classesWithAnnotation = index.getAnnotations(annotation);
            Set<Class<?>> classesForAnnotation = new HashSet<Class<?>>();
            for (AnnotationInstance annotationInstance : classesWithAnnotation) {
                // may generate bytecode with annotations placed on methods (see AS7-2559)
                if (annotationInstance.target() instanceof ClassInfo) {
                    String className = annotationInstance.target().toString();
                    try {
                        JPA_LOGGER.tracef("getClassesInJar found class %s with annotation %s", className, annClass.getName());
                        Class<?> clazz = pu.cacheTempClassLoader().loadClass(className);
                        result.add(clazz);
                        classesForAnnotation.add(clazz);
                    } catch (ClassNotFoundException e) {
                        JPA_LOGGER.cannotLoadEntityClass(e, className);
                    } catch (NoClassDefFoundError e) {
                        JPA_LOGGER.cannotLoadEntityClass(e, className);
                    }
                }
            }
            cacheClasses(pu, jarToScan, annClass, classesForAnnotation);
        }
        return result;
    } else {
        return getCachedClasses(pu, jarToScan, annotationsToLookFor);
    }
}
Also used : Index(org.jboss.jandex.Index) DotName(org.jboss.jandex.DotName) PersistenceUnitMetadata(org.jipijapa.plugin.spi.PersistenceUnitMetadata) AnnotationInstance(org.jboss.jandex.AnnotationInstance) HashSet(java.util.HashSet) ClassInfo(org.jboss.jandex.ClassInfo)

Example 2 with PersistenceUnitMetadata

use of org.jipijapa.plugin.spi.PersistenceUnitMetadata in project wildfly by wildfly.

the class HibernateAnnotationScanner method getPackagesInJar.

@Override
public Set<Package> getPackagesInJar(URL jarToScan, Set<Class<? extends Annotation>> annotationsToLookFor) {
    if (jarToScan == null) {
        throw JPA_LOGGER.nullVar("jarToScan");
    }
    JPA_LOGGER.tracef("getPackagesInJar url=%s annotations=%s", jarToScan.getPath(), annotationsToLookFor);
    Set<Class<?>> resultClasses = new HashSet<Class<?>>();
    PersistenceUnitMetadata pu = PERSISTENCE_UNIT_METADATA_TLS.get();
    if (pu == null) {
        throw JPA_LOGGER.missingPersistenceUnitMetadata();
    }
    if (annotationsToLookFor.size() > 0) {
        // Hibernate doesn't pass any annotations currently
        resultClasses = getClassesInJar(jarToScan, annotationsToLookFor);
    } else {
        if (pu.getAnnotationIndex() != null) {
            Index index = getJarFileIndex(jarToScan, pu);
            if (index == null) {
                JPA_LOGGER.tracef("No classes to scan for annotations in jar '%s' (jars with classes '%s')", jarToScan, pu.getAnnotationIndex().keySet());
                return new HashSet<Package>();
            }
            Collection<ClassInfo> allClasses = index.getKnownClasses();
            for (ClassInfo classInfo : allClasses) {
                String className = classInfo.name().toString();
                try {
                    resultClasses.add(pu.cacheTempClassLoader().loadClass(className));
                } catch (ClassNotFoundException e) {
                    JPA_LOGGER.cannotLoadEntityClass(e, className);
                } catch (NoClassDefFoundError e) {
                    JPA_LOGGER.cannotLoadEntityClass(e, className);
                }
            }
        }
    }
    if (pu.getAnnotationIndex() != null || annotationsToLookFor.size() > 0) {
        Map<String, Package> uniquePackages = new HashMap<String, Package>();
        for (Class<?> classWithAnnotation : resultClasses) {
            Package classPackage = classWithAnnotation.getPackage();
            if (classPackage != null) {
                JPA_LOGGER.tracef("getPackagesInJar found package %s", classPackage);
                uniquePackages.put(classPackage.getName(), classPackage);
            }
        }
        Set<Package> packages = new HashSet<Package>(uniquePackages.values());
        cachePackages(pu, jarToScan, packages);
        return new HashSet<Package>(packages);
    } else {
        return getCachedPackages(pu, jarToScan);
    }
}
Also used : HashMap(java.util.HashMap) Index(org.jboss.jandex.Index) PersistenceUnitMetadata(org.jipijapa.plugin.spi.PersistenceUnitMetadata) HashSet(java.util.HashSet) ClassInfo(org.jboss.jandex.ClassInfo)

Example 3 with PersistenceUnitMetadata

use of org.jipijapa.plugin.spi.PersistenceUnitMetadata in project wildfly by wildfly.

the class PersistenceRefProcessor method getPersistenceUnitBindingSource.

private InjectionSource getPersistenceUnitBindingSource(final DeploymentUnit deploymentUnit, final String unitName) throws DeploymentUnitProcessingException {
    final String searchName;
    if (isEmpty(unitName)) {
        searchName = null;
    } else {
        searchName = unitName;
    }
    final PersistenceUnitMetadata pu = PersistenceUnitSearch.resolvePersistenceUnitSupplier(deploymentUnit, searchName);
    if (null == pu) {
        throw new DeploymentUnitProcessingException(JpaLogger.ROOT_LOGGER.persistenceUnitNotFound(searchName, deploymentUnit));
    }
    String scopedPuName = pu.getScopedPersistenceUnitName();
    ServiceName puServiceName = getPuServiceName(scopedPuName);
    return new PersistenceUnitInjectionSource(puServiceName, deploymentUnit.getServiceRegistry(), EntityManagerFactory.class.getName(), pu);
}
Also used : DeploymentUnitProcessingException(org.jboss.as.server.deployment.DeploymentUnitProcessingException) ServiceName(org.jboss.msc.service.ServiceName) EntityManagerFactory(javax.persistence.EntityManagerFactory) PersistenceUnitInjectionSource(org.jboss.as.jpa.injectors.PersistenceUnitInjectionSource) PersistenceUnitMetadata(org.jipijapa.plugin.spi.PersistenceUnitMetadata)

Example 4 with PersistenceUnitMetadata

use of org.jipijapa.plugin.spi.PersistenceUnitMetadata in project wildfly by wildfly.

the class PersistenceUnitServiceHandler method setAnnotationIndexes.

/**
 * Setup the annotation index map
 *
 * @param puHolder
 * @param deploymentUnit
 */
private static void setAnnotationIndexes(final PersistenceUnitMetadataHolder puHolder, DeploymentUnit deploymentUnit) {
    final Map<URL, Index> annotationIndexes = new HashMap<>();
    do {
        for (ResourceRoot root : DeploymentUtils.allResourceRoots(deploymentUnit)) {
            final Index index = root.getAttachment(Attachments.ANNOTATION_INDEX);
            if (index != null) {
                try {
                    ROOT_LOGGER.tracef("adding '%s' to annotation index map", root.getRoot().toURL());
                    annotationIndexes.put(root.getRoot().toURL(), index);
                } catch (MalformedURLException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        // get annotation indexes for top level also
        deploymentUnit = deploymentUnit.getParent();
    } while (deploymentUnit != null);
    for (PersistenceUnitMetadata pu : puHolder.getPersistenceUnits()) {
        // hold onto the annotation index for Persistence Provider use during deployment
        pu.setAnnotationIndex(annotationIndexes);
    }
}
Also used : ResourceRoot(org.jboss.as.server.deployment.module.ResourceRoot) MalformedURLException(java.net.MalformedURLException) HashMap(java.util.HashMap) PersistenceUnitMetadata(org.jipijapa.plugin.spi.PersistenceUnitMetadata) Index(org.jboss.jandex.Index) URL(java.net.URL)

Example 5 with PersistenceUnitMetadata

use of org.jipijapa.plugin.spi.PersistenceUnitMetadata in project wildfly by wildfly.

the class JPAAnnotationProcessor method getBindingSource.

private InjectionSource getBindingSource(final DeploymentUnit deploymentUnit, final AnnotationInstance annotation, String injectionTypeName, final EEModuleClassDescription classDescription) throws DeploymentUnitProcessingException {
    PersistenceUnitMetadata pu = getPersistenceUnit(deploymentUnit, annotation, classDescription);
    if (pu == null) {
        return null;
    }
    String scopedPuName = pu.getScopedPersistenceUnitName();
    ServiceName puServiceName = getPuServiceName(scopedPuName);
    if (isPersistenceContext(annotation)) {
        if (pu.getTransactionType() == PersistenceUnitTransactionType.RESOURCE_LOCAL) {
            classDescription.setInvalid(JpaLogger.ROOT_LOGGER.cannotInjectResourceLocalEntityManager());
            return null;
        }
        AnnotationValue pcType = annotation.value("type");
        PersistenceContextType type = (pcType == null || PersistenceContextType.TRANSACTION.name().equals(pcType.asString())) ? PersistenceContextType.TRANSACTION : PersistenceContextType.EXTENDED;
        AnnotationValue stType = annotation.value("synchronization");
        SynchronizationType synchronizationType = (stType == null || SynchronizationType.SYNCHRONIZED.name().equals(stType.asString())) ? SynchronizationType.SYNCHRONIZED : SynchronizationType.UNSYNCHRONIZED;
        Map<String, String> properties;
        AnnotationValue value = annotation.value("properties");
        AnnotationInstance[] props = value != null ? value.asNestedArray() : null;
        if (props != null) {
            properties = new HashMap<>();
            for (int source = 0; source < props.length; source++) {
                properties.put(props[source].value("name").asString(), props[source].value("value").asString());
            }
        } else {
            properties = null;
        }
        // get deployment settings from top level du (jboss-all.xml is only parsed at the top level).
        final JPADeploymentSettings jpaDeploymentSettings = DeploymentUtils.getTopDeploymentUnit(deploymentUnit).getAttachment(JpaAttachments.DEPLOYMENT_SETTINGS_KEY);
        return new PersistenceContextInjectionSource(type, synchronizationType, properties, puServiceName, deploymentUnit.getServiceRegistry(), scopedPuName, injectionTypeName, pu, jpaDeploymentSettings);
    } else {
        return new PersistenceUnitInjectionSource(puServiceName, deploymentUnit.getServiceRegistry(), injectionTypeName, pu);
    }
}
Also used : PersistenceContextType(javax.persistence.PersistenceContextType) SynchronizationType(javax.persistence.SynchronizationType) JPADeploymentSettings(org.jboss.as.jpa.config.JPADeploymentSettings) ServiceName(org.jboss.msc.service.ServiceName) PersistenceContextInjectionSource(org.jboss.as.jpa.injectors.PersistenceContextInjectionSource) PersistenceUnitInjectionSource(org.jboss.as.jpa.injectors.PersistenceUnitInjectionSource) PersistenceUnitMetadata(org.jipijapa.plugin.spi.PersistenceUnitMetadata) AnnotationValue(org.jboss.jandex.AnnotationValue) AnnotationInstance(org.jboss.jandex.AnnotationInstance)

Aggregations

PersistenceUnitMetadata (org.jipijapa.plugin.spi.PersistenceUnitMetadata)27 PersistenceUnitMetadataHolder (org.jboss.as.jpa.config.PersistenceUnitMetadataHolder)11 ResourceRoot (org.jboss.as.server.deployment.module.ResourceRoot)6 ServiceName (org.jboss.msc.service.ServiceName)6 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 PersistenceUnitsInApplication (org.jboss.as.jpa.config.PersistenceUnitsInApplication)3 DeploymentUnit (org.jboss.as.server.deployment.DeploymentUnit)3 AnnotationInstance (org.jboss.jandex.AnnotationInstance)3 Index (org.jboss.jandex.Index)3 StringReader (java.io.StringReader)2 URL (java.net.URL)2 Properties (java.util.Properties)2 PersistenceProvider (javax.persistence.spi.PersistenceProvider)2 XMLStreamReader (javax.xml.stream.XMLStreamReader)2 ComponentDescription (org.jboss.as.ee.component.ComponentDescription)2 EEModuleDescription (org.jboss.as.ee.component.EEModuleDescription)2 JPADeploymentSettings (org.jboss.as.jpa.config.JPADeploymentSettings)2 PersistenceProviderDeploymentHolder (org.jboss.as.jpa.config.PersistenceProviderDeploymentHolder)2