Search in sources :

Example 1 with Scanner

use of org.hibernate.boot.archive.scan.spi.Scanner in project hibernate-orm by hibernate.

the class ScanningCoordinator method coordinateScan.

public void coordinateScan(ManagedResourcesImpl managedResources, MetadataBuildingOptions options, XmlMappingBinderAccess xmlMappingBinderAccess) {
    if (options.getScanEnvironment() == null) {
        return;
    }
    final ClassLoaderService classLoaderService = options.getServiceRegistry().getService(ClassLoaderService.class);
    final ClassLoaderAccess classLoaderAccess = new ClassLoaderAccessImpl(options.getTempClassLoader(), classLoaderService);
    // NOTE : the idea with JandexInitializer/JandexInitManager was to allow adding classes
    // to the index as we discovered them via scanning and .  Currently
    final Scanner scanner = buildScanner(options, classLoaderAccess);
    final ScanResult scanResult = scanner.scan(options.getScanEnvironment(), options.getScanOptions(), StandardScanParameters.INSTANCE);
    applyScanResultsToManagedResources(managedResources, scanResult, options, xmlMappingBinderAccess);
}
Also used : ClassLoaderAccess(org.hibernate.boot.spi.ClassLoaderAccess) StandardScanner(org.hibernate.boot.archive.scan.internal.StandardScanner) Scanner(org.hibernate.boot.archive.scan.spi.Scanner) ScanResult(org.hibernate.boot.archive.scan.spi.ScanResult) ClassLoaderAccessImpl(org.hibernate.boot.internal.ClassLoaderAccessImpl) ClassLoaderService(org.hibernate.boot.registry.classloading.spi.ClassLoaderService)

Example 2 with Scanner

use of org.hibernate.boot.archive.scan.spi.Scanner in project hibernate-orm by hibernate.

the class ScanningCoordinator method buildScanner.

@SuppressWarnings("unchecked")
private static Scanner buildScanner(MetadataBuildingOptions options, ClassLoaderAccess classLoaderAccess) {
    final Object scannerSetting = options.getScanner();
    final ArchiveDescriptorFactory archiveDescriptorFactory = options.getArchiveDescriptorFactory();
    if (scannerSetting == null) {
        // No custom Scanner specified, use the StandardScanner
        if (archiveDescriptorFactory == null) {
            return new StandardScanner();
        } else {
            return new StandardScanner(archiveDescriptorFactory);
        }
    } else {
        if (Scanner.class.isInstance(scannerSetting)) {
            if (archiveDescriptorFactory != null) {
                throw new IllegalStateException("A Scanner instance and an ArchiveDescriptorFactory were both specified; please " + "specify one or the other, or if you need to supply both, Scanner class to use " + "(assuming it has a constructor accepting a ArchiveDescriptorFactory).  " + "Alternatively, just pass the ArchiveDescriptorFactory during your own " + "Scanner constructor assuming it is statically known.");
            }
            return (Scanner) scannerSetting;
        }
        final Class<? extends Scanner> scannerImplClass;
        if (Class.class.isInstance(scannerSetting)) {
            scannerImplClass = (Class<? extends Scanner>) scannerSetting;
        } else {
            scannerImplClass = classLoaderAccess.classForName(scannerSetting.toString());
        }
        if (archiveDescriptorFactory != null) {
            // find the single-arg constructor - its an error if none exists
            try {
                final Constructor<? extends Scanner> constructor = scannerImplClass.getConstructor(SINGLE_ARG);
                try {
                    return constructor.newInstance(archiveDescriptorFactory);
                } catch (Exception e) {
                    throw new IllegalStateException("Error trying to instantiate custom specified Scanner [" + scannerImplClass.getName() + "]", e);
                }
            } catch (NoSuchMethodException e) {
                throw new IllegalArgumentException("Configuration named a custom Scanner and a custom ArchiveDescriptorFactory, but " + "Scanner impl did not define a constructor accepting ArchiveDescriptorFactory");
            }
        } else {
            // find the single-arg constructor - its an error if none exists
            try {
                final Constructor<? extends Scanner> constructor = scannerImplClass.getConstructor(SINGLE_ARG);
                try {
                    return constructor.newInstance(StandardArchiveDescriptorFactory.INSTANCE);
                } catch (Exception e) {
                    throw new IllegalStateException("Error trying to instantiate custom specified Scanner [" + scannerImplClass.getName() + "]", e);
                }
            } catch (NoSuchMethodException e) {
                try {
                    final Constructor<? extends Scanner> constructor = scannerImplClass.getConstructor();
                    try {
                        return constructor.newInstance();
                    } catch (Exception e2) {
                        throw new IllegalStateException("Error trying to instantiate custom specified Scanner [" + scannerImplClass.getName() + "]", e2);
                    }
                } catch (NoSuchMethodException ignore) {
                    throw new IllegalArgumentException("Configuration named a custom Scanner, but we were unable to locate " + "an appropriate constructor");
                }
            }
        }
    }
}
Also used : StandardScanner(org.hibernate.boot.archive.scan.internal.StandardScanner) Scanner(org.hibernate.boot.archive.scan.spi.Scanner) StandardScanner(org.hibernate.boot.archive.scan.internal.StandardScanner) Constructor(java.lang.reflect.Constructor) ArchiveDescriptorFactory(org.hibernate.boot.archive.spi.ArchiveDescriptorFactory) StandardArchiveDescriptorFactory(org.hibernate.boot.archive.internal.StandardArchiveDescriptorFactory) MappingException(org.hibernate.boot.MappingException)

Example 3 with Scanner

use of org.hibernate.boot.archive.scan.spi.Scanner in project hibernate-orm by hibernate.

the class ScannerTest method testNativeScanner.

@Test
public void testNativeScanner() throws Exception {
    File defaultPar = buildDefaultPar();
    addPackageToClasspath(defaultPar);
    PersistenceUnitDescriptor descriptor = new ParsedPersistenceXmlDescriptor(defaultPar.toURL());
    ScanEnvironment env = new StandardJpaScanEnvironmentImpl(descriptor);
    ScanOptions options = new StandardScanOptions("hbm,class", descriptor.isExcludeUnlistedClasses());
    Scanner scanner = new StandardScanner();
    ScanResult scanResult = scanner.scan(env, options, StandardScanParameters.INSTANCE);
    assertEquals(3, scanResult.getLocatedClasses().size());
    assertClassesContained(scanResult, ApplicationServer.class);
    assertClassesContained(scanResult, Version.class);
    assertEquals(2, scanResult.getLocatedMappingFiles().size());
    for (MappingFileDescriptor mappingFileDescriptor : scanResult.getLocatedMappingFiles()) {
        assertNotNull(mappingFileDescriptor.getName());
        assertNotNull(mappingFileDescriptor.getStreamAccess());
        InputStream stream = mappingFileDescriptor.getStreamAccess().accessInputStream();
        assertNotNull(stream);
        stream.close();
    }
}
Also used : ParsedPersistenceXmlDescriptor(org.hibernate.jpa.boot.internal.ParsedPersistenceXmlDescriptor) StandardScanner(org.hibernate.boot.archive.scan.internal.StandardScanner) Scanner(org.hibernate.boot.archive.scan.spi.Scanner) MappingFileDescriptor(org.hibernate.boot.archive.scan.spi.MappingFileDescriptor) ScanResult(org.hibernate.boot.archive.scan.spi.ScanResult) StandardJpaScanEnvironmentImpl(org.hibernate.jpa.boot.internal.StandardJpaScanEnvironmentImpl) StandardScanner(org.hibernate.boot.archive.scan.internal.StandardScanner) InputStream(java.io.InputStream) ScanOptions(org.hibernate.boot.archive.scan.spi.ScanOptions) StandardScanOptions(org.hibernate.boot.archive.scan.internal.StandardScanOptions) ScanEnvironment(org.hibernate.boot.archive.scan.spi.ScanEnvironment) File(java.io.File) StandardScanOptions(org.hibernate.boot.archive.scan.internal.StandardScanOptions) PersistenceUnitDescriptor(org.hibernate.jpa.boot.spi.PersistenceUnitDescriptor) Test(org.junit.Test)

Example 4 with Scanner

use of org.hibernate.boot.archive.scan.spi.Scanner in project hibernate-orm by hibernate.

the class ScanningCoordinator method coordinateScan.

public void coordinateScan(ManagedResourcesImpl managedResources, BootstrapContext bootstrapContext, XmlMappingBinderAccess xmlMappingBinderAccess) {
    if (bootstrapContext.getScanEnvironment() == null) {
        return;
    }
    final ClassLoaderService classLoaderService = bootstrapContext.getServiceRegistry().getService(ClassLoaderService.class);
    final ClassLoaderAccess classLoaderAccess = new ClassLoaderAccessImpl(bootstrapContext.getJpaTempClassLoader(), classLoaderService);
    // NOTE : the idea with JandexInitializer/JandexInitManager was to allow adding classes
    // to the index as we discovered them via scanning and .  Currently
    final Scanner scanner = buildScanner(bootstrapContext, classLoaderAccess);
    final ScanResult scanResult = scanner.scan(bootstrapContext.getScanEnvironment(), bootstrapContext.getScanOptions(), StandardScanParameters.INSTANCE);
    applyScanResultsToManagedResources(managedResources, scanResult, bootstrapContext, xmlMappingBinderAccess);
}
Also used : ClassLoaderAccess(org.hibernate.boot.spi.ClassLoaderAccess) StandardScanner(org.hibernate.boot.archive.scan.internal.StandardScanner) Scanner(org.hibernate.boot.archive.scan.spi.Scanner) ScanResult(org.hibernate.boot.archive.scan.spi.ScanResult) ClassLoaderAccessImpl(org.hibernate.boot.internal.ClassLoaderAccessImpl) ClassLoaderService(org.hibernate.boot.registry.classloading.spi.ClassLoaderService)

Example 5 with Scanner

use of org.hibernate.boot.archive.scan.spi.Scanner in project hibernate-orm by hibernate.

the class ScanningCoordinator method buildScanner.

@SuppressWarnings("unchecked")
private static Scanner buildScanner(BootstrapContext bootstrapContext, ClassLoaderAccess classLoaderAccess) {
    final Object scannerSetting = bootstrapContext.getScanner();
    final ArchiveDescriptorFactory archiveDescriptorFactory = bootstrapContext.getArchiveDescriptorFactory();
    if (scannerSetting == null) {
        // No custom Scanner specified, use the StandardScanner
        if (archiveDescriptorFactory == null) {
            return new StandardScanner();
        } else {
            return new StandardScanner(archiveDescriptorFactory);
        }
    } else {
        if (Scanner.class.isInstance(scannerSetting)) {
            if (archiveDescriptorFactory != null) {
                throw new IllegalStateException("A Scanner instance and an ArchiveDescriptorFactory were both specified; please " + "specify one or the other, or if you need to supply both, Scanner class to use " + "(assuming it has a constructor accepting a ArchiveDescriptorFactory).  " + "Alternatively, just pass the ArchiveDescriptorFactory during your own " + "Scanner constructor assuming it is statically known.");
            }
            return (Scanner) scannerSetting;
        }
        final Class<? extends Scanner> scannerImplClass;
        if (Class.class.isInstance(scannerSetting)) {
            scannerImplClass = (Class<? extends Scanner>) scannerSetting;
        } else {
            scannerImplClass = classLoaderAccess.classForName(scannerSetting.toString());
        }
        if (archiveDescriptorFactory != null) {
            // find the single-arg constructor - its an error if none exists
            try {
                final Constructor<? extends Scanner> constructor = scannerImplClass.getConstructor(SINGLE_ARG);
                try {
                    return constructor.newInstance(archiveDescriptorFactory);
                } catch (Exception e) {
                    throw new IllegalStateException("Error trying to instantiate custom specified Scanner [" + scannerImplClass.getName() + "]", e);
                }
            } catch (NoSuchMethodException e) {
                throw new IllegalArgumentException("Configuration named a custom Scanner and a custom ArchiveDescriptorFactory, but " + "Scanner impl did not define a constructor accepting ArchiveDescriptorFactory");
            }
        } else {
            // find the single-arg constructor - its an error if none exists
            try {
                final Constructor<? extends Scanner> constructor = scannerImplClass.getConstructor(SINGLE_ARG);
                try {
                    return constructor.newInstance(StandardArchiveDescriptorFactory.INSTANCE);
                } catch (Exception e) {
                    throw new IllegalStateException("Error trying to instantiate custom specified Scanner [" + scannerImplClass.getName() + "]", e);
                }
            } catch (NoSuchMethodException e) {
                try {
                    final Constructor<? extends Scanner> constructor = scannerImplClass.getConstructor();
                    try {
                        return constructor.newInstance();
                    } catch (Exception e2) {
                        throw new IllegalStateException("Error trying to instantiate custom specified Scanner [" + scannerImplClass.getName() + "]", e2);
                    }
                } catch (NoSuchMethodException ignore) {
                    throw new IllegalArgumentException("Configuration named a custom Scanner, but we were unable to locate " + "an appropriate constructor");
                }
            }
        }
    }
}
Also used : StandardScanner(org.hibernate.boot.archive.scan.internal.StandardScanner) Scanner(org.hibernate.boot.archive.scan.spi.Scanner) StandardScanner(org.hibernate.boot.archive.scan.internal.StandardScanner) Constructor(java.lang.reflect.Constructor) ArchiveDescriptorFactory(org.hibernate.boot.archive.spi.ArchiveDescriptorFactory) StandardArchiveDescriptorFactory(org.hibernate.boot.archive.internal.StandardArchiveDescriptorFactory) MappingException(org.hibernate.boot.MappingException)

Aggregations

StandardScanner (org.hibernate.boot.archive.scan.internal.StandardScanner)5 Scanner (org.hibernate.boot.archive.scan.spi.Scanner)5 ScanResult (org.hibernate.boot.archive.scan.spi.ScanResult)3 Constructor (java.lang.reflect.Constructor)2 MappingException (org.hibernate.boot.MappingException)2 StandardArchiveDescriptorFactory (org.hibernate.boot.archive.internal.StandardArchiveDescriptorFactory)2 ArchiveDescriptorFactory (org.hibernate.boot.archive.spi.ArchiveDescriptorFactory)2 ClassLoaderAccessImpl (org.hibernate.boot.internal.ClassLoaderAccessImpl)2 ClassLoaderService (org.hibernate.boot.registry.classloading.spi.ClassLoaderService)2 ClassLoaderAccess (org.hibernate.boot.spi.ClassLoaderAccess)2 File (java.io.File)1 InputStream (java.io.InputStream)1 StandardScanOptions (org.hibernate.boot.archive.scan.internal.StandardScanOptions)1 MappingFileDescriptor (org.hibernate.boot.archive.scan.spi.MappingFileDescriptor)1 ScanEnvironment (org.hibernate.boot.archive.scan.spi.ScanEnvironment)1 ScanOptions (org.hibernate.boot.archive.scan.spi.ScanOptions)1 ParsedPersistenceXmlDescriptor (org.hibernate.jpa.boot.internal.ParsedPersistenceXmlDescriptor)1 StandardJpaScanEnvironmentImpl (org.hibernate.jpa.boot.internal.StandardJpaScanEnvironmentImpl)1 PersistenceUnitDescriptor (org.hibernate.jpa.boot.spi.PersistenceUnitDescriptor)1 Test (org.junit.Test)1