Skip to content

Commit

Permalink
HSEARCH-1724 Simplify access to ClassLoaderService
Browse files Browse the repository at this point in the history
  • Loading branch information
Sanne committed Apr 27, 2016
1 parent e8f4645 commit fd4acb5
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public final class BridgeFactory {
private final Set<BridgeProvider> regularProviders = new HashSet<>();

public BridgeFactory(ServiceManager serviceManager) {
ClassLoaderService classLoaderService = serviceManager.requestService( ClassLoaderService.class );
ClassLoaderService classLoaderService = serviceManager.getClassLoaderService();

for ( IndexManagerTypeSpecificBridgeProvider provider : classLoaderService.loadJavaServices( IndexManagerTypeSpecificBridgeProvider.class ) ) {
backendSpecificProviders.put( provider.getIndexManagerType(), provider );
Expand All @@ -73,13 +73,8 @@ public BridgeFactory(ServiceManager serviceManager) {
annotationBasedProviders.add( new JavaTimeBridgeProvider() );
}

try {
for ( BridgeProvider provider : classLoaderService.loadJavaServices( BridgeProvider.class ) ) {
regularProviders.add( provider );
}
}
finally {
serviceManager.releaseService( ClassLoaderService.class );
for ( BridgeProvider provider : classLoaderService.loadJavaServices( BridgeProvider.class ) ) {
regularProviders.add( provider );
}
regularProviders.add( new EnumBridgeProvider() );
regularProviders.add( new BasicJDKTypesBridgeProvider( serviceManager ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.hibernate.search.cfg.spi.IndexManagerFactory;
import org.hibernate.search.engine.service.classloading.spi.ClassLoaderService;
import org.hibernate.search.engine.service.spi.ServiceManager;
import org.hibernate.search.engine.service.spi.ServiceReference;
import org.hibernate.search.engine.service.spi.Startable;
import org.hibernate.search.exception.SearchException;
import org.hibernate.search.indexes.impl.NRTIndexManager;
Expand Down Expand Up @@ -102,14 +101,13 @@ protected IndexManager fromAlias(String alias) {
}
// TODO HSEARCH-2115 Remove once generic alias resolver contribution scheme is implemented
else if ( "elasticsearch".equals( alias ) ) {
try ( ServiceReference<ClassLoaderService> classLoaderService = serviceManager.requestReference( ClassLoaderService.class ) ) {
Class<?> imType = classLoaderService.get().classForName( ES_INDEX_MANAGER );
try {
return (IndexManager) imType.newInstance();
}
catch (InstantiationException | IllegalAccessException e) {
throw new SearchException( "Could not instantiate Elasticsearch index manager", e );
}
ClassLoaderService classLoaderService = serviceManager.getClassLoaderService();
Class<?> imType = classLoaderService.classForName( ES_INDEX_MANAGER );
try {
return (IndexManager) imType.newInstance();
}
catch (InstantiationException | IllegalAccessException e) {
throw new SearchException( "Could not instantiate Elasticsearch index manager", e );
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,6 @@ public boolean isIdProvidedImplicit() {

@Override
public ClassLoaderService getClassLoaderService() {
return searchFactoryState.getServiceManager().requestService( ClassLoaderService.class );
return searchFactoryState.getServiceManager().getClassLoaderService();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class StandardServiceManager implements ServiceManager {
private final ConcurrentHashMap<Class<?>, ServiceWrapper<?>> cachedServices = new ConcurrentHashMap<Class<?>, ServiceWrapper<?>>();
private final Map<Class<? extends Service>, Object> providedServices;
private final Map<Class<? extends Service>, String> defaultServices;
private final ClassLoaderService classloaderService;

private volatile boolean allServicesReleased = false;

Expand All @@ -56,6 +57,7 @@ public StandardServiceManager(SearchConfiguration searchConfiguration,
this.properties = searchConfiguration.getProperties();
this.providedServices = createProvidedServices( searchConfiguration );
this.defaultServices = defaultServices;
this.classloaderService = searchConfiguration.getClassLoaderService();
}

@Override
Expand Down Expand Up @@ -265,4 +267,10 @@ private void stateExpectedFailure() {
private enum ServiceStatus {
RUNNING, STOPPED, STARTING, STOPPING
}

@Override
public ClassLoaderService getClassLoaderService() {
return classloaderService;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*/
package org.hibernate.search.engine.service.spi;

import org.hibernate.search.engine.service.classloading.spi.ClassLoaderService;

/**
* The {@code ServiceManager} is used to manage services in and runtime discovery of service implementations in the scope
* of a single {@code SearchFactory}.
Expand Down Expand Up @@ -71,4 +73,12 @@ public interface ServiceManager {
* {@code IllegalStateException} will be thrown in this case.
*/
void releaseAllServices();

/**
* Provides direct access to the {@link ClassLoaderService}.
* This service lookup is treated as a special case both for convenience and performance reasons.
* @return the {@link ClassLoaderService}
*/
ClassLoaderService getClassLoaderService();

}
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ private static Constructor<?> getSingleMapConstructor(Class<?> classToLoad, Stri

public static Class<?> classForName(String classNameToLoad, String componentDescription, ServiceManager serviceManager) {
Class<?> clazz;
ClassLoaderService classLoaderService = serviceManager.requestService( ClassLoaderService.class );
ClassLoaderService classLoaderService = serviceManager.getClassLoaderService();
try {
clazz = classLoaderService.classForName( classNameToLoad );
}
Expand All @@ -279,9 +279,6 @@ public static Class<?> classForName(String classNameToLoad, String componentDesc
" implementation class: " + classNameToLoad, e
);
}
finally {
serviceManager.releaseService( ClassLoaderService.class );
}
return clazz;
}

Expand Down Expand Up @@ -315,13 +312,7 @@ public static <T> Class<? extends T> classForName(Class<T> targetSuperType,
* @throws ClassLoadingException From {@link Class#forName(String, boolean, ClassLoader)}.
*/
public static Class classForName(String classNameToLoad, ServiceManager serviceManager) {
ClassLoaderService classLoaderService = serviceManager.requestService( ClassLoaderService.class );
try {
return classLoaderService.classForName( classNameToLoad );
}

finally {
serviceManager.releaseService( ClassLoaderService.class );
}
ClassLoaderService classLoaderService = serviceManager.getClassLoaderService();
return classLoaderService.classForName( classNameToLoad );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,8 @@ public HibernateSearchResourceLoader(ServiceManager serviceManager) {

@Override
public InputStream openResource(String resource) throws IOException {
ClassLoaderService classLoaderService = serviceManager.requestService( ClassLoaderService.class );
InputStream inputStream;
try {
inputStream = classLoaderService.locateResourceStream( resource );
}
finally {
serviceManager.releaseService( ClassLoaderService.class );
}
ClassLoaderService classLoaderService = serviceManager.getClassLoaderService();
InputStream inputStream = classLoaderService.locateResourceStream( resource );

if ( inputStream == null ) {
throw log.unableToLoadResource( resource );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,16 +284,10 @@ public void testMultiThreadedAddClasses() throws Exception {
}

private static Class<?> getClassByNumber(int i, ServiceManager serviceManager) throws ClassNotFoundException {
ClassLoaderService classLoaderService = serviceManager.requestService( ClassLoaderService.class );
Class<?> clazz;
try {
clazz = classLoaderService.classForName(
ClassLoaderService classLoaderService = serviceManager.getClassLoaderService();
Class<?> clazz = classLoaderService.classForName(
Generated.A0.class.getName().replace( "A0", "A" + i )
);
}
finally {
serviceManager.releaseService( ClassLoaderService.class );
}
return clazz;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,13 @@ public void purge(String entity) {

private Class<?> getEntityClass(String entity) {
Class<?> clazz;
ClassLoaderService classLoaderService = serviceManager.requestService( ClassLoaderService.class );
ClassLoaderService classLoaderService = serviceManager.getClassLoaderService();
try {
clazz = classLoaderService.classForName( entity );
}
catch (ClassLoadingException e) {
throw new IllegalArgumentException( entity + " not a indexed entity" );
}
finally {
serviceManager.releaseService( ClassLoaderService.class );
}
return clazz;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,12 @@ private Loader getCriteriaLoader() {
if ( entityType == null ) {
ServiceManager serviceManager = extendedIntegrator.getServiceManager();
try {
ClassLoaderService classLoaderService = serviceManager.requestService( ClassLoaderService.class );
ClassLoaderService classLoaderService = serviceManager.getClassLoaderService();
entityType = classLoaderService.classForName( targetEntity );
}
catch (ClassLoadingException e) {
throw new SearchException( "Unable to load entity class from criteria: " + targetEntity, e );
}
finally {
serviceManager.releaseService( ClassLoaderService.class );
}
}
else {
if ( !entityType.getName().equals( targetEntity ) ) {
Expand Down

0 comments on commit fd4acb5

Please sign in to comment.