use of org.hibernate.boot.registry.classloading.spi.ClassLoaderService 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);
}
use of org.hibernate.boot.registry.classloading.spi.ClassLoaderService 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());
}
}
use of org.hibernate.boot.registry.classloading.spi.ClassLoaderService in project hibernate-orm by hibernate.
the class MetadataBuildingProcess method handleTypes.
// private static JandexInitManager buildJandexInitializer(
// MetadataBuildingOptions options,
// ClassLoaderAccess classLoaderAccess) {
// final boolean autoIndexMembers = ConfigurationHelper.getBoolean(
// org.hibernate.cfg.AvailableSettings.ENABLE_AUTO_INDEX_MEMBER_TYPES,
// options.getServiceRegistry().getService( ConfigurationService.class ).getSettings(),
// false
// );
//
// return new JandexInitManager( options.getJandexView(), classLoaderAccess, autoIndexMembers );
// }
private static BasicTypeRegistry handleTypes(MetadataBuildingOptions options) {
final ClassLoaderService classLoaderService = options.getServiceRegistry().getService(ClassLoaderService.class);
// ultimately this needs to change a little bit to account for HHH-7792
final BasicTypeRegistry basicTypeRegistry = new BasicTypeRegistry();
final TypeContributions typeContributions = new TypeContributions() {
@Override
public void contributeType(org.hibernate.type.BasicType type) {
basicTypeRegistry.register(type);
}
@Override
public void contributeType(BasicType type, String... keys) {
basicTypeRegistry.register(type, keys);
}
@Override
public void contributeType(UserType type, String[] keys) {
basicTypeRegistry.register(type, keys);
}
@Override
public void contributeType(CompositeUserType type, String[] keys) {
basicTypeRegistry.register(type, keys);
}
};
// add Dialect contributed types
final Dialect dialect = options.getServiceRegistry().getService(JdbcServices.class).getDialect();
dialect.contributeTypes(typeContributions, options.getServiceRegistry());
// add TypeContributor contributed types.
for (TypeContributor contributor : classLoaderService.loadJavaServices(TypeContributor.class)) {
contributor.contribute(typeContributions, options.getServiceRegistry());
}
// add explicit application registered types
for (BasicTypeRegistration basicTypeRegistration : options.getBasicTypeRegistrations()) {
basicTypeRegistry.register(basicTypeRegistration.getBasicType(), basicTypeRegistration.getRegistrationKeys());
}
return basicTypeRegistry;
}
use of org.hibernate.boot.registry.classloading.spi.ClassLoaderService in project hibernate-orm by hibernate.
the class MetadataBuildingProcess method complete.
/**
* Second step of 2-phase for MetadataSources->Metadata process
*
* @param managedResources The token/memento from 1st phase
* @param options The building options
*
* @return Token/memento representing all known users resources (classes, packages, mapping files, etc).
*/
public static MetadataImplementor complete(final ManagedResources managedResources, final MetadataBuildingOptions options) {
final BasicTypeRegistry basicTypeRegistry = handleTypes(options);
final InFlightMetadataCollectorImpl metadataCollector = new InFlightMetadataCollectorImpl(options, new TypeResolver(basicTypeRegistry, new TypeFactory()));
for (AttributeConverterDefinition attributeConverterDefinition : managedResources.getAttributeConverterDefinitions()) {
metadataCollector.addAttributeConverter(attributeConverterDefinition);
}
final ClassLoaderService classLoaderService = options.getServiceRegistry().getService(ClassLoaderService.class);
final ClassLoaderAccess classLoaderAccess = new ClassLoaderAccessImpl(options.getTempClassLoader(), classLoaderService);
final MetadataBuildingContextRootImpl rootMetadataBuildingContext = new MetadataBuildingContextRootImpl(options, classLoaderAccess, metadataCollector);
final IndexView jandexView = options.getJandexView();
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Set up the processors and start binding
// NOTE : this becomes even more simplified afterQuery we move purely
// to unified model
final MetadataSourceProcessor processor = new MetadataSourceProcessor() {
private final HbmMetadataSourceProcessorImpl hbmProcessor = new HbmMetadataSourceProcessorImpl(managedResources, rootMetadataBuildingContext);
private final AnnotationMetadataSourceProcessorImpl annotationProcessor = new AnnotationMetadataSourceProcessorImpl(managedResources, rootMetadataBuildingContext, jandexView);
@Override
public void prepare() {
hbmProcessor.prepare();
annotationProcessor.prepare();
}
@Override
public void processTypeDefinitions() {
hbmProcessor.processTypeDefinitions();
annotationProcessor.processTypeDefinitions();
}
@Override
public void processQueryRenames() {
hbmProcessor.processQueryRenames();
annotationProcessor.processQueryRenames();
}
@Override
public void processNamedQueries() {
hbmProcessor.processNamedQueries();
annotationProcessor.processNamedQueries();
}
@Override
public void processAuxiliaryDatabaseObjectDefinitions() {
hbmProcessor.processAuxiliaryDatabaseObjectDefinitions();
annotationProcessor.processAuxiliaryDatabaseObjectDefinitions();
}
@Override
public void processIdentifierGenerators() {
hbmProcessor.processIdentifierGenerators();
annotationProcessor.processIdentifierGenerators();
}
@Override
public void processFilterDefinitions() {
hbmProcessor.processFilterDefinitions();
annotationProcessor.processFilterDefinitions();
}
@Override
public void processFetchProfiles() {
hbmProcessor.processFetchProfiles();
annotationProcessor.processFetchProfiles();
}
@Override
public void prepareForEntityHierarchyProcessing() {
for (MetadataSourceType metadataSourceType : options.getSourceProcessOrdering()) {
if (metadataSourceType == MetadataSourceType.HBM) {
hbmProcessor.prepareForEntityHierarchyProcessing();
}
if (metadataSourceType == MetadataSourceType.CLASS) {
annotationProcessor.prepareForEntityHierarchyProcessing();
}
}
}
@Override
public void processEntityHierarchies(Set<String> processedEntityNames) {
for (MetadataSourceType metadataSourceType : options.getSourceProcessOrdering()) {
if (metadataSourceType == MetadataSourceType.HBM) {
hbmProcessor.processEntityHierarchies(processedEntityNames);
}
if (metadataSourceType == MetadataSourceType.CLASS) {
annotationProcessor.processEntityHierarchies(processedEntityNames);
}
}
}
@Override
public void postProcessEntityHierarchies() {
for (MetadataSourceType metadataSourceType : options.getSourceProcessOrdering()) {
if (metadataSourceType == MetadataSourceType.HBM) {
hbmProcessor.postProcessEntityHierarchies();
}
if (metadataSourceType == MetadataSourceType.CLASS) {
annotationProcessor.postProcessEntityHierarchies();
}
}
}
@Override
public void processResultSetMappings() {
hbmProcessor.processResultSetMappings();
annotationProcessor.processResultSetMappings();
}
@Override
public void finishUp() {
hbmProcessor.finishUp();
annotationProcessor.finishUp();
}
};
processor.prepare();
processor.processTypeDefinitions();
processor.processQueryRenames();
processor.processAuxiliaryDatabaseObjectDefinitions();
processor.processIdentifierGenerators();
processor.processFilterDefinitions();
processor.processFetchProfiles();
final Set<String> processedEntityNames = new HashSet<String>();
processor.prepareForEntityHierarchyProcessing();
processor.processEntityHierarchies(processedEntityNames);
processor.postProcessEntityHierarchies();
processor.processResultSetMappings();
processor.processNamedQueries();
processor.finishUp();
for (MetadataContributor contributor : classLoaderService.loadJavaServices(MetadataContributor.class)) {
log.tracef("Calling MetadataContributor : %s", contributor);
contributor.contribute(metadataCollector, jandexView);
}
metadataCollector.processSecondPasses(rootMetadataBuildingContext);
Iterable<AdditionalJaxbMappingProducer> producers = classLoaderService.loadJavaServices(AdditionalJaxbMappingProducer.class);
if (producers != null) {
final EntityHierarchyBuilder hierarchyBuilder = new EntityHierarchyBuilder();
// final MappingBinder mappingBinder = new MappingBinder( true );
// We need to disable validation here. It seems Envers is not producing valid (according to schema) XML
final MappingBinder mappingBinder = new MappingBinder(classLoaderService, false);
for (AdditionalJaxbMappingProducer producer : producers) {
log.tracef("Calling AdditionalJaxbMappingProducer : %s", producer);
Collection<MappingDocument> additionalMappings = producer.produceAdditionalMappings(metadataCollector, jandexView, mappingBinder, rootMetadataBuildingContext);
for (MappingDocument mappingDocument : additionalMappings) {
hierarchyBuilder.indexMappingDocument(mappingDocument);
}
}
ModelBinder binder = ModelBinder.prepare(rootMetadataBuildingContext);
for (EntityHierarchySourceImpl entityHierarchySource : hierarchyBuilder.buildHierarchies()) {
binder.bindEntityHierarchy(entityHierarchySource);
}
}
return metadataCollector.buildMetadataInstance(rootMetadataBuildingContext);
}
use of org.hibernate.boot.registry.classloading.spi.ClassLoaderService in project hibernate-orm by hibernate.
the class BeanValidationIntegrator method integrate.
@Override
public void integrate(final Metadata metadata, final SessionFactoryImplementor sessionFactory, final SessionFactoryServiceRegistry serviceRegistry) {
final ConfigurationService cfgService = serviceRegistry.getService(ConfigurationService.class);
// IMPL NOTE : see the comments on ActivationContext.getValidationModes() as to why this is multi-valued...
final Set<ValidationMode> modes = ValidationMode.getModes(cfgService.getSettings().get(MODE_PROPERTY));
if (modes.size() > 1) {
LOG.multipleValidationModes(ValidationMode.loggable(modes));
}
if (modes.size() == 1 && modes.contains(ValidationMode.NONE)) {
// we have nothing to do; just return
return;
}
final ClassLoaderService classLoaderService = serviceRegistry.getService(ClassLoaderService.class);
// see if the Bean Validation API is available on the classpath
if (isBeanValidationApiAvailable(classLoaderService)) {
// and if so, call out to the TypeSafeActivator
try {
final Class typeSafeActivatorClass = loadTypeSafeActivatorClass(classLoaderService);
@SuppressWarnings("unchecked") final Method activateMethod = typeSafeActivatorClass.getMethod(ACTIVATE_METHOD_NAME, ActivationContext.class);
final ActivationContext activationContext = new ActivationContext() {
@Override
public Set<ValidationMode> getValidationModes() {
return modes;
}
@Override
public Metadata getMetadata() {
return metadata;
}
@Override
public SessionFactoryImplementor getSessionFactory() {
return sessionFactory;
}
@Override
public SessionFactoryServiceRegistry getServiceRegistry() {
return serviceRegistry;
}
};
try {
activateMethod.invoke(null, activationContext);
} catch (InvocationTargetException e) {
if (HibernateException.class.isInstance(e.getTargetException())) {
throw ((HibernateException) e.getTargetException());
}
throw new IntegrationException("Error activating Bean Validation integration", e.getTargetException());
} catch (Exception e) {
throw new IntegrationException("Error activating Bean Validation integration", e);
}
} catch (NoSuchMethodException e) {
throw new HibernateException("Unable to locate TypeSafeActivator#activate method", e);
}
} else {
// otherwise check the validation modes
// todo : in many ways this duplicates thew checks done on the TypeSafeActivator when a ValidatorFactory could not be obtained
validateMissingBeanValidationApi(modes);
}
}
Aggregations