All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
org.hibernate.boot.registry.selector.internal.StrategySelectorImpl Maven / Gradle / Ivy
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or .
*/
package org.hibernate.boot.registry.selector.internal;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
import org.hibernate.boot.registry.classloading.spi.ClassLoadingException;
import org.hibernate.boot.registry.selector.spi.StrategyCreator;
import org.hibernate.boot.registry.selector.spi.StrategySelectionException;
import org.hibernate.boot.registry.selector.spi.StrategySelector;
import org.jboss.logging.Logger;
/**
* Standard implementation of the StrategySelector contract.
*
* @author Steve Ebersole
*/
public class StrategySelectorImpl implements StrategySelector {
private static final Logger log = Logger.getLogger( StrategySelectorImpl.class );
public static StrategyCreator STANDARD_STRATEGY_CREATOR = strategyClass -> {
try {
return strategyClass.newInstance();
}
catch (Exception e) {
throw new StrategySelectionException(
String.format( "Could not instantiate named strategy class [%s]", strategyClass.getName() ),
e
);
}
};
private final Map> namedStrategyImplementorByStrategyMap = new ConcurrentHashMap<>();
private final ClassLoaderService classLoaderService;
/**
* Constructs a StrategySelectorImpl using the given class loader service.
*
* @param classLoaderService The class loader service usable by this StrategySelectorImpl instance.
*/
public StrategySelectorImpl(ClassLoaderService classLoaderService) {
this.classLoaderService = classLoaderService;
}
@Override
public void registerStrategyImplementor(Class strategy, String name, Class implementation) {
Map namedStrategyImplementorMap = namedStrategyImplementorByStrategyMap.get( strategy );
if ( namedStrategyImplementorMap == null ) {
namedStrategyImplementorMap = new ConcurrentHashMap<>();
namedStrategyImplementorByStrategyMap.put( strategy, namedStrategyImplementorMap );
}
final Class old = namedStrategyImplementorMap.put( name, implementation );
if ( old == null ) {
log.trace(
String.format(
"Registering named strategy selector [%s] : [%s] -> [%s]",
strategy.getName(),
name,
implementation.getName()
)
);
}
else {
log.debug(
String.format(
"Registering named strategy selector [%s] : [%s] -> [%s] (replacing [%s])",
strategy.getName(),
name,
implementation.getName(),
old.getName()
)
);
}
}
@Override
public void unRegisterStrategyImplementor(Class strategy, Class implementation) {
final Map namedStrategyImplementorMap = namedStrategyImplementorByStrategyMap.get( strategy );
if ( namedStrategyImplementorMap == null ) {
log.debug( "Named strategy map did not exist on call to un-register" );
return;
}
final Iterator itr = namedStrategyImplementorMap.values().iterator();
while ( itr.hasNext() ) {
final Class registered = (Class) itr.next();
if ( registered.equals( implementation ) ) {
itr.remove();
}
}
// try tp clean up afterQuery ourselves...
if ( namedStrategyImplementorMap.isEmpty() ) {
namedStrategyImplementorByStrategyMap.remove( strategy );
}
}
@Override
@SuppressWarnings("unchecked")
public Class selectStrategyImplementor(Class strategy, String name) {
final Map namedStrategyImplementorMap = namedStrategyImplementorByStrategyMap.get( strategy );
if ( namedStrategyImplementorMap != null ) {
final Class registered = namedStrategyImplementorMap.get( name );
if ( registered != null ) {
return (Class) registered;
}
}
try {
return classLoaderService.classForName( name );
}
catch (ClassLoadingException e) {
throw new StrategySelectionException(
"Unable to resolve name [" + name + "] as strategy [" + strategy.getName() + "]"
);
}
}
@Override
public T resolveStrategy(Class strategy, Object strategyReference) {
return resolveDefaultableStrategy( strategy, strategyReference, (T) null );
}
@Override
@SuppressWarnings("unchecked")
public T resolveDefaultableStrategy(Class strategy, Object strategyReference, final T defaultValue) {
return resolveDefaultableStrategy(
strategy,
strategyReference,
(Callable) () -> defaultValue
);
}
@Override
@SuppressWarnings("unchecked")
public T resolveDefaultableStrategy(
Class strategy,
Object strategyReference,
Callable defaultResolver) {
return (T) resolveStrategy( strategy, strategyReference, defaultResolver, STANDARD_STRATEGY_CREATOR );
}
@Override
public T resolveStrategy(
Class strategy,
Object strategyReference,
T defaultValue,
StrategyCreator creator) {
return resolveStrategy(
strategy,
strategyReference,
(Callable) () -> defaultValue,
creator
);
}
@SuppressWarnings("unchecked")
@Override
public T resolveStrategy(
Class strategy,
Object strategyReference,
Callable defaultResolver,
StrategyCreator creator) {
if ( strategyReference == null ) {
try {
return defaultResolver.call();
}
catch (Exception e) {
throw new StrategySelectionException( "Default-resolver threw exception", e );
}
}
if ( strategy.isInstance( strategyReference ) ) {
return strategy.cast( strategyReference );
}
final Class implementationClass;
if ( Class.class.isInstance( strategyReference ) ) {
implementationClass = (Class) strategyReference;
}
else {
implementationClass = selectStrategyImplementor( strategy, strategyReference.toString() );
}
try {
return creator.create( implementationClass );
}
catch (Exception e) {
throw new StrategySelectionException(
String.format( "Could not instantiate named strategy class [%s]", implementationClass.getName() ),
e
);
}
}
}