diff --git a/hibernate-core/src/main/java/org/hibernate/service/StandardServiceInitiators.java b/hibernate-core/src/main/java/org/hibernate/service/StandardServiceInitiators.java index caa32b0cee..bda7be6e66 100644 --- a/hibernate-core/src/main/java/org/hibernate/service/StandardServiceInitiators.java +++ b/hibernate-core/src/main/java/org/hibernate/service/StandardServiceInitiators.java @@ -35,6 +35,7 @@ import org.hibernate.id.factory.internal.MutableIdentifierGeneratorFactoryInitia import org.hibernate.persister.internal.PersisterClassResolverInitiator; import org.hibernate.persister.internal.PersisterFactoryInitiator; import org.hibernate.service.config.internal.ConfigurationServiceInitiator; +import org.hibernate.service.instrumentation.internal.InstrumentationServiceInitiator; import org.hibernate.service.internal.SessionFactoryServiceRegistryFactoryInitiator; import org.hibernate.service.jdbc.connections.internal.ConnectionProviderInitiator; import org.hibernate.service.jdbc.connections.internal.MultiTenantConnectionProviderInitiator; @@ -80,6 +81,8 @@ public class StandardServiceInitiators { serviceInitiators.add( RegionFactoryInitiator.INSTANCE ); + serviceInitiators.add( InstrumentationServiceInitiator.INSTANCE ); + return Collections.unmodifiableList( serviceInitiators ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/service/instrumentation/internal/CachingInstrumentationService.java b/hibernate-core/src/main/java/org/hibernate/service/instrumentation/internal/CachingInstrumentationService.java new file mode 100644 index 0000000000..b24ac8f752 --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/service/instrumentation/internal/CachingInstrumentationService.java @@ -0,0 +1,30 @@ +package org.hibernate.service.instrumentation.internal; + +import java.util.concurrent.ConcurrentHashMap; + +import org.hibernate.bytecode.instrumentation.internal.FieldInterceptionHelper; +import org.hibernate.service.instrumentation.spi.InstrumentationService; + +/** + * @author Emmanuel Bernard + */ +public class CachingInstrumentationService implements InstrumentationService { + + private final ConcurrentHashMap, Boolean> isInstrumentedCache = new ConcurrentHashMap, Boolean>( ); + + @Override + public boolean isInstrumented(Class entityType) { + Boolean isInstrumented = isInstrumentedCache.get( entityType ); + if ( isInstrumented == null ) { + isInstrumented = FieldInterceptionHelper.isInstrumented( entityType ); + isInstrumentedCache.put( entityType, isInstrumented ); + } + //noinspection ConstantConditions + return isInstrumented; + } + + @Override + public boolean isInstrumented(Object entity) { + return entity != null && isInstrumented( entity.getClass() ); + } +} diff --git a/hibernate-core/src/main/java/org/hibernate/service/instrumentation/internal/InstrumentationServiceInitiator.java b/hibernate-core/src/main/java/org/hibernate/service/instrumentation/internal/InstrumentationServiceInitiator.java new file mode 100644 index 0000000000..118b71eee9 --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/service/instrumentation/internal/InstrumentationServiceInitiator.java @@ -0,0 +1,27 @@ +package org.hibernate.service.instrumentation.internal; + +import java.util.Map; + +import org.hibernate.service.instrumentation.spi.InstrumentationService; +import org.hibernate.service.spi.BasicServiceInitiator; +import org.hibernate.service.spi.ServiceRegistryImplementor; + +/** + * Simple ServiceInitiator for InstrumentationService + * + * @author Emmanuel Bernard + */ +public class InstrumentationServiceInitiator implements BasicServiceInitiator { + public static final InstrumentationServiceInitiator INSTANCE = new InstrumentationServiceInitiator(); + + @Override + public InstrumentationService initiateService(Map configurationValues, ServiceRegistryImplementor registry) { + return new CachingInstrumentationService(); + } + + @Override + public Class getServiceInitiated() { + return InstrumentationService.class; + } + +} diff --git a/hibernate-core/src/main/java/org/hibernate/service/instrumentation/spi/InstrumentationService.java b/hibernate-core/src/main/java/org/hibernate/service/instrumentation/spi/InstrumentationService.java new file mode 100644 index 0000000000..cc50551670 --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/service/instrumentation/spi/InstrumentationService.java @@ -0,0 +1,20 @@ +package org.hibernate.service.instrumentation.spi; + +import org.hibernate.service.Service; + +/** + * Service caching instrumentation information + * + * @author Emmanuel Bernard + */ +public interface InstrumentationService extends Service { + /** + * Is entityType instrumented + */ + public boolean isInstrumented(Class entityType); + + /** + * Is entity instrumented + */ + public boolean isInstrumented(Object entity); +}