Java Code Examples for org.hibernate.boot.registry.classloading.spi.ClassLoaderService#classForName()

The following examples show how to use org.hibernate.boot.registry.classloading.spi.ClassLoaderService#classForName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SimpleValue.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
public void setTypeName(String typeName) {
	if ( typeName != null && typeName.startsWith( AttributeConverterTypeAdapter.NAME_PREFIX ) ) {
		final String converterClassName = typeName.substring( AttributeConverterTypeAdapter.NAME_PREFIX.length() );
		final ClassLoaderService cls = getMetadata()
				.getMetadataBuildingOptions()
				.getServiceRegistry()
				.getService( ClassLoaderService.class );
		try {
			final Class<? extends AttributeConverter> converterClass = cls.classForName( converterClassName );
			this.attributeConverterDescriptor = new ClassBasedConverterDescriptor(
					converterClass,
					false,
					( (InFlightMetadataCollector) getMetadata() ).getClassmateContext()
			);
			return;
		}
		catch (Exception e) {
			log.logBadHbmAttributeConverterType( typeName, e.getMessage() );
		}
	}

	this.typeName = typeName;
}
 
Example 2
Source File: DriverManagerConnectionProviderImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private Driver loadDriverIfPossible(String driverClassName) {
	if ( driverClassName == null ) {
		log.debug( "No driver class specified" );
		return null;
	}

	if ( serviceRegistry != null ) {
		final ClassLoaderService classLoaderService = serviceRegistry.getService( ClassLoaderService.class );
		final Class<Driver> driverClass = classLoaderService.classForName( driverClassName );
		try {
			return driverClass.newInstance();
		}
		catch ( Exception e ) {
			throw new ServiceException( "Specified JDBC Driver " + driverClassName + " could not be loaded", e );
		}
	}

	try {
		return (Driver) Class.forName( driverClassName ).newInstance();
	}
	catch ( Exception e1 ) {
		throw new ServiceException( "Specified JDBC Driver " + driverClassName + " could not be loaded", e1 );
	}
}
 
Example 3
Source File: DefaultIdentifierGeneratorFactory.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Class getIdentifierGeneratorClass(String strategy) {
	if ( "hilo".equals( strategy ) ) {
		throw new UnsupportedOperationException( "Support for 'hilo' generator has been removed" );
	}
	String resolvedStrategy = "native".equals( strategy ) ?
			getDialect().getNativeIdentifierGeneratorStrategy() : strategy;

	Class generatorClass = generatorStrategyToClassNameMap.get( resolvedStrategy );
	try {
		if ( generatorClass == null ) {
			final ClassLoaderService cls = serviceRegistry.getService( ClassLoaderService.class );
			generatorClass = cls.classForName( resolvedStrategy );
		}
	}
	catch ( ClassLoadingException e ) {
		throw new MappingException( String.format( "Could not interpret id generator strategy [%s]", strategy ) );
	}
	return generatorClass;
}
 
Example 4
Source File: TypeDefinitionBinder.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Handling for a {@code <typedef/>} declaration
 *
 * @param context Access to information relative to the mapping document containing this binding
 * @param typeDefinitionBinding The {@code <typedef/>} binding
 */
public static void processTypeDefinition(
		HbmLocalMetadataBuildingContext context,
		JaxbHbmTypeDefinitionType typeDefinitionBinding) {
	final ClassLoaderService cls = context.getBuildingOptions().getServiceRegistry().getService( ClassLoaderService.class );

	final TypeDefinition definition = new TypeDefinition(
			typeDefinitionBinding.getName(),
			cls.classForName( typeDefinitionBinding.getClazz() ),
			null,
			ConfigParameterHelper.extractConfigParameters( typeDefinitionBinding )
	);

	log.debugf(
			"Processed type-definition : %s -> %s",
			definition.getName(),
			definition.getTypeImplementorClass().getName()
	);

	context.getMetadataCollector().addTypeDefinition( definition );
}
 
Example 5
Source File: BootstrapContextImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private ClassLoaderDelegate generateHcannClassLoaderDelegate() {
	//	class loading here needs to be drastically different for 7.0
	//		but luckily 7.0 will do away with HCANN use and be easier to
	//		implement this.
	//
	// todo (6.0) : *if possible* make similar change in 6.0
	// 		possibly using the JPA temp class loader or create our own "throw awy" ClassLoader;
	//		the trouble there is that we eventually need to load the Class into the real
	//		ClassLoader prior to use

	final ClassLoaderService classLoaderService = getServiceRegistry().getService( ClassLoaderService.class );

	return new ClassLoaderDelegate() {
		@Override
		public <T> Class<T> classForName(String className) throws ClassLoadingException {
			try {
				return classLoaderService.classForName( className );
			}
			catch (org.hibernate.boot.registry.classloading.spi.ClassLoadingException e) {
				return StandardClassLoaderDelegateImpl.INSTANCE.classForName( className );
			}
		}
	};
}
 
Example 6
Source File: BeanValidationIntegrator.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private boolean isBeanValidationApiAvailable(ClassLoaderService classLoaderService) {
	try {
		classLoaderService.classForName( BV_CHECK_CLASS );
		return true;
	}
	catch (Exception e) {
		return false;
	}
}
 
Example 7
Source File: BeanValidationIntegrator.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private Class loadTypeSafeActivatorClass(ClassLoaderService classLoaderService) {
	try {
		return classLoaderService.classForName( ACTIVATOR_CLASS_NAME );
	}
	catch (Exception e) {
		throw new HibernateException( "Unable to load TypeSafeActivator class", e );
	}
}
 
Example 8
Source File: Component.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public Class getComponentClass() throws MappingException {
	final ClassLoaderService classLoaderService = getMetadata()
			.getMetadataBuildingOptions()
			.getServiceRegistry()
			.getService( ClassLoaderService.class );
	try {
		return classLoaderService.classForName( componentClassName );
	}
	catch (ClassLoadingException e) {
		throw new MappingException("component class not found: " + componentClassName, e);
	}
}
 
Example 9
Source File: ManagedBeanRegistryInitiator.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private BeanContainer interpretExplicitBeanContainer(
		Object explicitSetting,
		ClassLoaderService classLoaderService, ServiceRegistryImplementor serviceRegistry) {
	if ( explicitSetting == null ) {
		return null;
	}

	if ( explicitSetting instanceof BeanContainer ) {
		return (BeanContainer) explicitSetting;
	}

	// otherwise we ultimately need to resolve this to a class
	final Class containerClass;
	if ( explicitSetting instanceof Class ) {
		containerClass = (Class) explicitSetting;
	}
	else {
		final String name = explicitSetting.toString();
		// try the StrategySelector service
		final Class selected = serviceRegistry.getService( StrategySelector.class )
				.selectStrategyImplementor( BeanContainer.class, name );
		if ( selected != null ) {
			containerClass = selected;
		}
		else {
			containerClass = classLoaderService.classForName( name );
		}
	}

	try {
		return (BeanContainer) containerClass.newInstance();
	}
	catch (Exception e) {
		throw new InstantiationException( "Unable to instantiate specified BeanContainer : " + containerClass.getName(), containerClass, e );
	}
}
 
Example 10
Source File: ReflectHelper.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Attempt to resolve the specified property type through reflection.
 *
 * @param className The name of the class owning the property.
 * @param name The name of the property.
 * @param classLoaderService ClassLoader services
 *
 * @return The type of the property.
 *
 * @throws MappingException Indicates we were unable to locate the property.
 */
public static Class reflectedPropertyClass(
		String className,
		String name,
		ClassLoaderService classLoaderService) throws MappingException {
	try {
		Class clazz = classLoaderService.classForName( className );
		return getter( clazz, name ).getReturnType();
	}
	catch ( ClassLoadingException e ) {
		throw new MappingException( "class " + className + " not found while looking for property: " + name, e );
	}
}
 
Example 11
Source File: ManagedBeanRegistryInitiator.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public static Class cdiBeanManagerClass(ClassLoaderService classLoaderService) throws ClassLoadingException {
	return classLoaderService.classForName( "javax.enterprise.inject.spi.BeanManager" );
}
 
Example 12
Source File: MultiTenantConnectionProviderInitiator.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
@SuppressWarnings( {"unchecked"})
public MultiTenantConnectionProvider initiateService(Map configurationValues, ServiceRegistryImplementor registry) {
	final MultiTenancyStrategy strategy = MultiTenancyStrategy.determineMultiTenancyStrategy(  configurationValues );
	if ( !strategy.requiresMultiTenantConnectionProvider() ) {
		// nothing to do, but given the separate hierarchies have to handle this here.
		return null;
	}

	final Object configValue = configurationValues.get( AvailableSettings.MULTI_TENANT_CONNECTION_PROVIDER );
	if ( configValue == null ) {
		// if they also specified the data source *name*, then lets assume they want
		// DataSourceBasedMultiTenantConnectionProviderImpl
		final Object dataSourceConfigValue = configurationValues.get( AvailableSettings.DATASOURCE );
		if ( dataSourceConfigValue != null && String.class.isInstance( dataSourceConfigValue ) ) {
			return new DataSourceBasedMultiTenantConnectionProviderImpl();
		}

		return null;
	}

	if ( MultiTenantConnectionProvider.class.isInstance( configValue ) ) {
		return (MultiTenantConnectionProvider) configValue;
	}
	else {
		final Class<MultiTenantConnectionProvider> implClass;
		if ( Class.class.isInstance( configValue ) ) {
			implClass = (Class) configValue;
		}
		else {
			final String className = configValue.toString();
			final ClassLoaderService classLoaderService = registry.getService( ClassLoaderService.class );
			try {
				implClass = classLoaderService.classForName( className );
			}
			catch (ClassLoadingException cle) {
				log.warn( "Unable to locate specified class [" + className + "]", cle );
				throw new ServiceException( "Unable to locate specified multi-tenant connection provider [" + className + "]" );
			}
		}

		try {
			return implClass.newInstance();
		}
		catch (Exception e) {
			log.warn( "Unable to instantiate specified class [" + implClass.getName() + "]", e );
			throw new ServiceException( "Unable to instantiate specified multi-tenant connection provider [" + implClass.getName() + "]" );
		}
	}
}
 
Example 13
Source File: TypeResolver.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Uses heuristics to deduce the proper {@link Type} given a string naming the type or Java class.
 * <p/>
 * The search goes as follows:<ol>
 * 	<li>search for a basic type with 'typeName' as a registration key</li>
 * 	<li>
 * 		look for 'typeName' as a class name and<ol>
 *			<li>if it names a {@link Type} implementor, return an instance</li>
 *			<li>if it names a {@link CompositeUserType} or a {@link UserType}, return an instance of class wrapped intot the appropriate {@link Type} adapter</li>
 * 			<li>if it implements {@link org.hibernate.classic.Lifecycle}, return the corresponding entity type</li>
 * 			<li>if it implements {@link Serializable}, return the corresponding serializable type</li>
 * 		</ol>
 * 	</li>
 * </ol>
 *
 * @param typeName The name (see heuristic algorithm above).
 * @param parameters Any parameters for the type.  Only applied if built!
 *
 * @return The deduced type; may be null.
 *
 * @throws MappingException Indicates a problem attempting to resolve 'typeName' as a {@link Class}
 */
public Type heuristicType(String typeName, Properties parameters) throws MappingException {
	Type type = basic( typeName );
	if ( type != null ) {
		return type;
	}

	try {
		final ClassLoaderService classLoaderService = typeConfiguration.getServiceRegistry().getService( ClassLoaderService.class );
		Class typeClass = classLoaderService.classForName( typeName );
		if ( typeClass != null ) {
			return typeFactory.byClass( typeClass, parameters );
		}
	}
	catch ( ClassLoadingException ignore ) {
	}

	return null;
}