HHH-9143 - Build baseline Jandex as part of scanning

This commit is contained in:
Steve Ebersole 2014-04-24 16:27:28 -05:00
parent 7e5b0c4aa5
commit 3cec19a274
4 changed files with 109 additions and 25 deletions

View File

@ -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
);
}

View File

@ -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;
}
}

View File

@ -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<MappingFileDescriptor> mappingFileSet = new HashSet<MappingFileDescriptor>();
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

View File

@ -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();
}