diff --git a/hibernate-entitymanager/src/main/java/org/hibernate/jpa/boot/internal/EntityManagerFactoryBuilderImpl.java b/hibernate-entitymanager/src/main/java/org/hibernate/jpa/boot/internal/EntityManagerFactoryBuilderImpl.java index 3049b99762..d46c9df48f 100644 --- a/hibernate-entitymanager/src/main/java/org/hibernate/jpa/boot/internal/EntityManagerFactoryBuilderImpl.java +++ b/hibernate-entitymanager/src/main/java/org/hibernate/jpa/boot/internal/EntityManagerFactoryBuilderImpl.java @@ -99,6 +99,7 @@ import org.hibernate.service.spi.ServiceRegistryImplementor; import org.jboss.jandex.Index; import org.jboss.jandex.IndexView; +import org.jboss.jandex.Indexer; import org.jboss.logging.Logger; import static org.hibernate.cfg.AvailableSettings.JACC_CONTEXT_ID; @@ -689,10 +690,22 @@ public class EntityManagerFactoryBuilderImpl implements EntityManagerFactoryBuil @SuppressWarnings("unchecked") private ScanResult scanDeployment(BootstrapServiceRegistry bootstrapServiceRegistry) { - final Scanner scanner = locateOrBuildScanner( bootstrapServiceRegistry ); - final ScanOptions scanOptions = determineScanOptions(); + IndexView jandexIndex = (IndexView) configurationValues.remove( JANDEX_INDEX ); + Indexer indexer = null; + if ( jandexIndex != null ) { + indexer = new Indexer(); + } - return scanner.scan( persistenceUnit, scanOptions ); + final Scanner scanner = locateOrBuildScanner( bootstrapServiceRegistry ); + final ScanOptions scanOptions = determineScanOptions( indexer ); + + ScanResult scanResult = scanner.scan( persistenceUnit, scanOptions ); + + if ( indexer != null ) { + jandexIndex = indexer.complete(); + configurationValues.put( JANDEX_INDEX, jandexIndex ); + } + return scanResult; } @SuppressWarnings("unchecked") @@ -733,10 +746,11 @@ public class EntityManagerFactoryBuilderImpl implements EntityManagerFactoryBuil } } - private ScanOptions determineScanOptions() { + private ScanOptions determineScanOptions(Indexer indexer) { return new StandardScanOptions( (String) configurationValues.get( AvailableSettings.AUTODETECTION ), - persistenceUnit.isExcludeUnlistedClasses() + persistenceUnit.isExcludeUnlistedClasses(), + indexer ); } diff --git a/hibernate-entitymanager/src/main/java/org/hibernate/jpa/boot/scan/internal/StandardScanOptions.java b/hibernate-entitymanager/src/main/java/org/hibernate/jpa/boot/scan/internal/StandardScanOptions.java index e2dcf9e154..8b919a4bbe 100644 --- a/hibernate-entitymanager/src/main/java/org/hibernate/jpa/boot/scan/internal/StandardScanOptions.java +++ b/hibernate-entitymanager/src/main/java/org/hibernate/jpa/boot/scan/internal/StandardScanOptions.java @@ -25,6 +25,8 @@ package org.hibernate.jpa.boot.scan.internal; import org.hibernate.jpa.boot.scan.spi.ScanOptions; +import org.jboss.jandex.Indexer; + /** * @author Steve Ebersole */ @@ -32,22 +34,32 @@ public class StandardScanOptions implements ScanOptions { private final boolean detectClassesInRoot; private final boolean detectClassesInNonRoot; private final boolean detectHibernateMappingFiles; + private final Indexer jandexIndexer; public StandardScanOptions() { this( "hbm,class", false ); } + public StandardScanOptions( + String explicitDetectionSetting, + boolean persistenceUnitExcludeUnlistedClassesValue) { + this( explicitDetectionSetting, persistenceUnitExcludeUnlistedClassesValue, null ); + } - public StandardScanOptions(String explicitDetectionSetting, boolean persistenceUnitExcludeUnlistedClassesValue) { + public StandardScanOptions( + String explicitDetectionSetting, + boolean persistenceUnitExcludeUnlistedClassesValue, + Indexer jandexIndexer) { if ( explicitDetectionSetting == null ) { - detectHibernateMappingFiles = true; - detectClassesInRoot = ! persistenceUnitExcludeUnlistedClassesValue; - detectClassesInNonRoot = true; + this.detectHibernateMappingFiles = true; + this.detectClassesInRoot = ! persistenceUnitExcludeUnlistedClassesValue; + this.detectClassesInNonRoot = true; } else { - detectHibernateMappingFiles = explicitDetectionSetting.contains( "hbm" ); - detectClassesInRoot = explicitDetectionSetting.contains( "class" ); - detectClassesInNonRoot = detectClassesInRoot; + this.detectHibernateMappingFiles = explicitDetectionSetting.contains( "hbm" ); + this.detectClassesInRoot = explicitDetectionSetting.contains( "class" ); + this.detectClassesInNonRoot = this.detectClassesInRoot; } + this.jandexIndexer = jandexIndexer; } @Override @@ -64,4 +76,9 @@ public class StandardScanOptions implements ScanOptions { public boolean canDetectHibernateMappingFiles() { return detectHibernateMappingFiles; } + + @Override + public Indexer getJandexIndexer() { + return jandexIndexer; + } } diff --git a/hibernate-entitymanager/src/main/java/org/hibernate/jpa/boot/scan/spi/AbstractScannerImpl.java b/hibernate-entitymanager/src/main/java/org/hibernate/jpa/boot/scan/spi/AbstractScannerImpl.java index 985d453658..b05aaecd48 100644 --- a/hibernate-entitymanager/src/main/java/org/hibernate/jpa/boot/scan/spi/AbstractScannerImpl.java +++ b/hibernate-entitymanager/src/main/java/org/hibernate/jpa/boot/scan/spi/AbstractScannerImpl.java @@ -23,6 +23,8 @@ */ package org.hibernate.jpa.boot.scan.spi; +import java.io.IOException; +import java.io.InputStream; import java.net.URL; import java.util.Collections; import java.util.HashMap; @@ -30,6 +32,8 @@ import java.util.HashSet; import java.util.Map; import java.util.Set; +import javax.persistence.PersistenceException; + import org.hibernate.jpa.boot.archive.spi.ArchiveContext; import org.hibernate.jpa.boot.archive.spi.ArchiveDescriptor; import org.hibernate.jpa.boot.archive.spi.ArchiveDescriptorFactory; @@ -110,6 +114,8 @@ public abstract class AbstractScannerImpl implements Scanner { PackageInfoArchiveEntryHandler.Callback, ClassFileArchiveEntryHandler.Callback, NonClassFileArchiveEntryHandler.Callback { + private final ScanOptions scanOptions; + private final ClassFileArchiveEntryHandler classFileHandler; private final PackageInfoArchiveEntryHandler packageInfoHandler; private final NonClassFileArchiveEntryHandler fileHandler; @@ -119,6 +125,8 @@ public abstract class AbstractScannerImpl implements Scanner { private final Set mappingFileSet = new HashSet(); public ResultCollector(ScanOptions scanOptions) { + this.scanOptions = scanOptions; + this.classFileHandler = new ClassFileArchiveEntryHandler( scanOptions, this ); this.packageInfoHandler = new PackageInfoArchiveEntryHandler( scanOptions, this ); this.fileHandler = new NonClassFileArchiveEntryHandler( scanOptions, this ); @@ -141,34 +149,75 @@ public abstract class AbstractScannerImpl implements Scanner { @Override public void locatedPackage(PackageDescriptor packageDescriptor) { + final PackageDescriptor keeper; + if ( PackageDescriptorImpl.class.isInstance( packageDescriptor ) ) { - packageDescriptorSet.add( packageDescriptor ); + keeper = packageDescriptor; } else { // to make sure we have proper equals/hashcode - packageDescriptorSet.add( - new PackageDescriptorImpl( - packageDescriptor.getName(), - packageDescriptor.getStreamAccess() - ) + keeper = new PackageDescriptorImpl( + packageDescriptor.getName(), + packageDescriptor.getStreamAccess() ); } + + if ( scanOptions.getJandexIndexer() != null ) { + InputStream stream = keeper.getStreamAccess().accessInputStream(); + try { + scanOptions.getJandexIndexer().index( stream ); + } + catch (IOException e) { + throw new PersistenceException( "Could not add package-info to Jandex Indexer", e ); + } + finally { + try { + stream.close(); + } + catch (IOException ignore) { + } + } + } + + packageDescriptorSet.add( keeper ); } @Override public void locatedClass(ClassDescriptor classDescriptor) { + // to make sure we have proper equals/hashcode + final ClassDescriptor keeper; + if ( ClassDescriptorImpl.class.isInstance( classDescriptor ) ) { - classDescriptorSet.add( classDescriptor ); + keeper = classDescriptor; } else { - // to make sure we have proper equals/hashcode - classDescriptorSet.add( - new ClassDescriptorImpl( - classDescriptor.getName(), - classDescriptor.getStreamAccess() - ) + keeper = new ClassDescriptorImpl( + classDescriptor.getName(), + classDescriptor.getStreamAccess() ); } + + if ( scanOptions.getJandexIndexer() != null ) { + InputStream stream = keeper.getStreamAccess().accessInputStream(); + try { + scanOptions.getJandexIndexer().index( stream ); + } + catch (IOException e) { + throw new PersistenceException( + "Could not add class [" + keeper.getName() + "] to Jandex Indexer", + e + ); + } + finally { + try { + stream.close(); + } + catch (IOException ignore) { + } + } + } + + classDescriptorSet.add( keeper ); } @Override diff --git a/hibernate-entitymanager/src/main/java/org/hibernate/jpa/boot/scan/spi/ScanOptions.java b/hibernate-entitymanager/src/main/java/org/hibernate/jpa/boot/scan/spi/ScanOptions.java index a5ec564a8d..a2af3c5e71 100644 --- a/hibernate-entitymanager/src/main/java/org/hibernate/jpa/boot/scan/spi/ScanOptions.java +++ b/hibernate-entitymanager/src/main/java/org/hibernate/jpa/boot/scan/spi/ScanOptions.java @@ -23,6 +23,8 @@ */ package org.hibernate.jpa.boot.scan.spi; +import org.jboss.jandex.Indexer; + /** * @author Steve Ebersole */ @@ -31,4 +33,6 @@ public interface ScanOptions { public boolean canDetectUnlistedClassesInNonRoot(); public boolean canDetectHibernateMappingFiles(); + + public Indexer getJandexIndexer(); }