Search in sources :

Example 1 with ScanEnvironment

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

the class ScanningCoordinator method applyScanResultsToManagedResources.

public void applyScanResultsToManagedResources(ManagedResourcesImpl managedResources, ScanResult scanResult, MetadataBuildingOptions options, XmlMappingBinderAccess xmlMappingBinderAccess) {
    final ScanEnvironment scanEnvironment = options.getScanEnvironment();
    final ServiceRegistry serviceRegistry = options.getServiceRegistry();
    final ClassLoaderService classLoaderService = serviceRegistry.getService(ClassLoaderService.class);
    // mapping files ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    final Set<String> nonLocatedMappingFileNames = new HashSet<String>();
    final List<String> explicitMappingFileNames = scanEnvironment.getExplicitlyListedMappingFiles();
    if (explicitMappingFileNames != null) {
        nonLocatedMappingFileNames.addAll(explicitMappingFileNames);
    }
    for (MappingFileDescriptor mappingFileDescriptor : scanResult.getLocatedMappingFiles()) {
        managedResources.addXmlBinding(xmlMappingBinderAccess.bind(mappingFileDescriptor.getStreamAccess()));
        nonLocatedMappingFileNames.remove(mappingFileDescriptor.getName());
    }
    for (String name : nonLocatedMappingFileNames) {
        final URL url = classLoaderService.locateResource(name);
        if (url == null) {
            throw new MappingException("Unable to resolve explicitly named mapping-file : " + name, new Origin(SourceType.RESOURCE, name));
        }
        final UrlInputStreamAccess inputStreamAccess = new UrlInputStreamAccess(url);
        managedResources.addXmlBinding(xmlMappingBinderAccess.bind(inputStreamAccess));
    }
    // classes and packages ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    final List<String> unresolvedListedClassNames = scanEnvironment.getExplicitlyListedClassNames() == null ? new ArrayList<String>() : new ArrayList<String>(scanEnvironment.getExplicitlyListedClassNames());
    for (ClassDescriptor classDescriptor : scanResult.getLocatedClasses()) {
        if (classDescriptor.getCategorization() == ClassDescriptor.Categorization.CONVERTER) {
            // converter classes are safe to load because we never enhance them,
            // and notice we use the ClassLoaderService specifically, not the temp ClassLoader (if any)
            managedResources.addAttributeConverterDefinition(AttributeConverterDefinition.from(classLoaderService.<AttributeConverter>classForName(classDescriptor.getName())));
        } else if (classDescriptor.getCategorization() == ClassDescriptor.Categorization.MODEL) {
            managedResources.addAnnotatedClassName(classDescriptor.getName());
        }
        unresolvedListedClassNames.remove(classDescriptor.getName());
    }
    // IMPL NOTE : "explicitlyListedClassNames" can contain class or package names...
    for (PackageDescriptor packageDescriptor : scanResult.getLocatedPackages()) {
        managedResources.addAnnotatedPackageName(packageDescriptor.getName());
        unresolvedListedClassNames.remove(packageDescriptor.getName());
    }
    for (String unresolvedListedClassName : unresolvedListedClassNames) {
        // because the explicit list can contain either class names or package names
        // we need to check for both here...
        // First, try it as a class name
        final String classFileName = unresolvedListedClassName.replace('.', '/') + ".class";
        final URL classFileUrl = classLoaderService.locateResource(classFileName);
        if (classFileUrl != null) {
            managedResources.addAnnotatedClassName(unresolvedListedClassName);
            continue;
        }
        // Then, try it as a package name
        final String packageInfoFileName = unresolvedListedClassName.replace('.', '/') + "/package-info.class";
        final URL packageInfoFileUrl = classLoaderService.locateResource(packageInfoFileName);
        if (packageInfoFileUrl != null) {
            managedResources.addAnnotatedPackageName(unresolvedListedClassName);
            continue;
        }
        log.debugf("Unable to resolve class [%s] named in persistence unit [%s]", unresolvedListedClassName, scanEnvironment.getRootUrl());
    }
}
Also used : Origin(org.hibernate.boot.jaxb.Origin) MappingFileDescriptor(org.hibernate.boot.archive.scan.spi.MappingFileDescriptor) ClassDescriptor(org.hibernate.boot.archive.scan.spi.ClassDescriptor) UrlInputStreamAccess(org.hibernate.boot.archive.internal.UrlInputStreamAccess) PackageDescriptor(org.hibernate.boot.archive.scan.spi.PackageDescriptor) URL(java.net.URL) MappingException(org.hibernate.boot.MappingException) AttributeConverter(javax.persistence.AttributeConverter) ScanEnvironment(org.hibernate.boot.archive.scan.spi.ScanEnvironment) ServiceRegistry(org.hibernate.service.ServiceRegistry) ClassLoaderService(org.hibernate.boot.registry.classloading.spi.ClassLoaderService) HashSet(java.util.HashSet)

Example 2 with ScanEnvironment

use of org.hibernate.boot.archive.scan.spi.ScanEnvironment 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 3 with ScanEnvironment

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

the class JarVisitorTest method testNestedJarProtocol.

@Test
public void testNestedJarProtocol() throws Exception {
    File defaultPar = buildDefaultPar();
    File nestedEar = buildNestedEar(defaultPar);
    File nestedEarDir = buildNestedEarDir(defaultPar);
    addPackageToClasspath(nestedEar);
    String jarFileName = nestedEar.toURL().toExternalForm() + "!/defaultpar.par";
    URL rootUrl = new URL(jarFileName);
    JarProtocolArchiveDescriptor archiveDescriptor = new JarProtocolArchiveDescriptor(StandardArchiveDescriptorFactory.INSTANCE, rootUrl, "");
    ScanEnvironment environment = new ScanEnvironmentImpl(rootUrl);
    ScanResultCollector collector = new ScanResultCollector(environment, new StandardScanOptions(), StandardScanParameters.INSTANCE);
    archiveDescriptor.visitArchive(new AbstractScannerImpl.ArchiveContextImpl(true, collector));
    validateResults(collector.toScanResult(), org.hibernate.jpa.test.pack.defaultpar.ApplicationServer.class, Version.class);
    jarFileName = nestedEarDir.toURL().toExternalForm() + "!/defaultpar.par";
    rootUrl = new URL(jarFileName);
    archiveDescriptor = new JarProtocolArchiveDescriptor(StandardArchiveDescriptorFactory.INSTANCE, rootUrl, "");
    environment = new ScanEnvironmentImpl(rootUrl);
    collector = new ScanResultCollector(environment, new StandardScanOptions(), StandardScanParameters.INSTANCE);
    archiveDescriptor.visitArchive(new AbstractScannerImpl.ArchiveContextImpl(true, collector));
    validateResults(collector.toScanResult(), org.hibernate.jpa.test.pack.defaultpar.ApplicationServer.class, Version.class);
}
Also used : JarProtocolArchiveDescriptor(org.hibernate.boot.archive.internal.JarProtocolArchiveDescriptor) AbstractScannerImpl(org.hibernate.boot.archive.scan.spi.AbstractScannerImpl) URL(java.net.URL) ScanResultCollector(org.hibernate.boot.archive.scan.internal.ScanResultCollector) ScanEnvironment(org.hibernate.boot.archive.scan.spi.ScanEnvironment) File(java.io.File) StandardScanOptions(org.hibernate.boot.archive.scan.internal.StandardScanOptions) Test(org.junit.Test)

Example 4 with ScanEnvironment

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

the class JarVisitorTest method testJarProtocol.

@Test
public void testJarProtocol() throws Exception {
    File war = buildWar();
    addPackageToClasspath(war);
    String jarFileName = war.toURL().toExternalForm() + "!/WEB-INF/classes";
    URL rootUrl = new URL(jarFileName);
    JarProtocolArchiveDescriptor archiveDescriptor = new JarProtocolArchiveDescriptor(StandardArchiveDescriptorFactory.INSTANCE, rootUrl, "");
    final ScanEnvironment environment = new ScanEnvironmentImpl(rootUrl);
    final ScanResultCollector collector = new ScanResultCollector(environment, new StandardScanOptions(), StandardScanParameters.INSTANCE);
    archiveDescriptor.visitArchive(new AbstractScannerImpl.ArchiveContextImpl(true, collector));
    validateResults(collector.toScanResult(), org.hibernate.jpa.test.pack.war.ApplicationServer.class, org.hibernate.jpa.test.pack.war.Version.class);
}
Also used : JarProtocolArchiveDescriptor(org.hibernate.boot.archive.internal.JarProtocolArchiveDescriptor) AbstractScannerImpl(org.hibernate.boot.archive.scan.spi.AbstractScannerImpl) URL(java.net.URL) ScanResultCollector(org.hibernate.boot.archive.scan.internal.ScanResultCollector) ScanEnvironment(org.hibernate.boot.archive.scan.spi.ScanEnvironment) File(java.io.File) StandardScanOptions(org.hibernate.boot.archive.scan.internal.StandardScanOptions) Test(org.junit.Test)

Aggregations

ScanEnvironment (org.hibernate.boot.archive.scan.spi.ScanEnvironment)4 File (java.io.File)3 URL (java.net.URL)3 StandardScanOptions (org.hibernate.boot.archive.scan.internal.StandardScanOptions)3 Test (org.junit.Test)3 JarProtocolArchiveDescriptor (org.hibernate.boot.archive.internal.JarProtocolArchiveDescriptor)2 ScanResultCollector (org.hibernate.boot.archive.scan.internal.ScanResultCollector)2 AbstractScannerImpl (org.hibernate.boot.archive.scan.spi.AbstractScannerImpl)2 MappingFileDescriptor (org.hibernate.boot.archive.scan.spi.MappingFileDescriptor)2 InputStream (java.io.InputStream)1 HashSet (java.util.HashSet)1 AttributeConverter (javax.persistence.AttributeConverter)1 MappingException (org.hibernate.boot.MappingException)1 UrlInputStreamAccess (org.hibernate.boot.archive.internal.UrlInputStreamAccess)1 StandardScanner (org.hibernate.boot.archive.scan.internal.StandardScanner)1 ClassDescriptor (org.hibernate.boot.archive.scan.spi.ClassDescriptor)1 PackageDescriptor (org.hibernate.boot.archive.scan.spi.PackageDescriptor)1 ScanOptions (org.hibernate.boot.archive.scan.spi.ScanOptions)1 ScanResult (org.hibernate.boot.archive.scan.spi.ScanResult)1 Scanner (org.hibernate.boot.archive.scan.spi.Scanner)1