Skip to content

Loading…

HHH-7552 javadocs improvement and using this for identifier generator st... #424

Closed
wants to merge 1 commit into from

2 participants

@stliu
Hibernate member

...rategy selection.

@brmeyer
Hibernate member

@stliu , what's the state of this PR? Should @sebersole review?

@stliu
Hibernate member

I'd love to get someone take a look but it's fine to merge it I think

@stliu stliu closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 13, 2012
  1. @stliu
This page is out of date. Refresh to see the latest.
View
26 hibernate-core/src/main/java/org/hibernate/boot/registry/selector/Availability.java
@@ -24,10 +24,30 @@
package org.hibernate.boot.registry.selector;
/**
+ * Availability of strategy selector
+ *
* @author Steve Ebersole
*/
-public interface Availability {
- public Class getStrategyRole();
+public interface Availability<T> {
+ /**
+ * The strategy role class or interface.
+ *
+ * @return The class of strategy role, can't be {@code null}.
+ */
+ public Class<T> getStrategyRole();
+
+ /**
+ * Alternative name(s) instead of the implementation's FQN used for look up implementation of target strategy.
+ * This is the place to define some short / simple names for the implementation.
+ *
+ * @return Names used for look up the strategy implementation, or {@code null} then only FQN will be used.
+ */
public Iterable<String> getSelectorNames();
- public Class getStrategyImplementation();
+
+ /**
+ * The {@code getStrategyRole()}'s implementation class.
+ *
+ * @return The implementation class of target strategy role, can't be {@code null}.
+ */
+ public Class<? extends T> getStrategyImplementation();
}
View
2 hibernate-core/src/main/java/org/hibernate/boot/registry/selector/AvailabilityAnnouncer.java
@@ -25,7 +25,7 @@
/**
* Responsible for announcing the availability of strategy selector(s). Can be registered directly with the
- * {@link org.hibernate.boot.registry.BootstrapServiceRegistry} or located via discovery
+ * {@link org.hibernate.boot.registry.BootstrapServiceRegistryBuilder} or located via Java service loader discovery.
*
* todo : better name?
*
View
13 .../src/main/java/org/hibernate/boot/registry/selector/internal/StrategySelectorBuilder.java
@@ -153,13 +153,22 @@ public StrategySelector buildSelector(ClassLoaderServiceImpl classLoaderService)
@SuppressWarnings("unchecked")
private void applyFromAvailability(StrategySelectorImpl strategySelector, Availability availability) {
- for ( String name : availability.getSelectorNames() ) {
+ if ( availability.getSelectorNames() == null || !availability.getSelectorNames().iterator().hasNext() ) {
strategySelector.registerStrategyImplementor(
availability.getStrategyRole(),
- name,
+ availability.getStrategyImplementation().getName(),
availability.getStrategyImplementation()
);
}
+ else {
+ for ( String name : availability.getSelectorNames() ) {
+ strategySelector.registerStrategyImplementor(
+ availability.getStrategyRole(),
+ name,
+ availability.getStrategyImplementation()
+ );
+ }
+ }
}
private void addDialects(StrategySelectorImpl strategySelector) {
View
39 ...ore/src/main/java/org/hibernate/boot/registry/selector/internal/StrategySelectorImpl.java
@@ -59,24 +59,20 @@ public StrategySelectorImpl(ClassLoaderService classLoaderService) {
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()
- )
+ log.tracef(
+ "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()
- )
+ log.debugf(
+ "Registering named strategy selector [%s] : [%s] -> [%s] (replacing [%s])",
+ strategy.getName(),
+ name,
+ implementation.getName(),
+ old.getName()
);
}
}
@@ -89,9 +85,9 @@ public StrategySelectorImpl(ClassLoaderService classLoaderService) {
return;
}
- final Iterator itr = namedStrategyImplementorMap.values().iterator();
+ final Iterator<Class> itr = namedStrategyImplementorMap.values().iterator();
while ( itr.hasNext() ) {
- final Class registered = (Class) itr.next();
+ final Class registered = itr.next();
if ( registered.equals( implementation ) ) {
itr.remove();
}
@@ -115,7 +111,14 @@ public StrategySelectorImpl(ClassLoaderService classLoaderService) {
}
try {
- return classLoaderService.classForName( name );
+ Class impl = classLoaderService.classForName( name );
+ if ( strategy.isAssignableFrom( impl ) ) {
+ return (Class<? extends T>)impl;
+ } else {
+ throw new StrategySelectionException(
+ "Unable to resolve name [" + name + "] as strategy [" + strategy.getName() + "]"
+ );
+ }
}
catch (ClassLoadingException e) {
throw new StrategySelectionException(
View
3 hibernate-core/src/main/java/org/hibernate/dialect/Cache71Dialect.java
@@ -50,6 +50,7 @@
import org.hibernate.exception.spi.SQLExceptionConversionDelegate;
import org.hibernate.exception.spi.TemplatedViolatedConstraintNameExtracter;
import org.hibernate.exception.spi.ViolatedConstraintNameExtracter;
+import org.hibernate.id.IdentifierGenerator;
import org.hibernate.id.IdentityGenerator;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.persister.entity.Lockable;
@@ -494,7 +495,7 @@ public boolean supportsIdentityColumns() {
return true;
}
- public Class getNativeIdentifierGeneratorClass() {
+ public Class<? extends IdentifierGenerator> getNativeIdentifierGeneratorClass() {
return IdentityGenerator.class;
}
View
3 hibernate-core/src/main/java/org/hibernate/dialect/Dialect.java
@@ -65,6 +65,7 @@
import org.hibernate.exception.spi.SQLExceptionConversionDelegate;
import org.hibernate.exception.spi.SQLExceptionConverter;
import org.hibernate.exception.spi.ViolatedConstraintNameExtracter;
+import org.hibernate.id.IdentifierGenerator;
import org.hibernate.id.IdentityGenerator;
import org.hibernate.id.SequenceGenerator;
import org.hibernate.id.TableHiLoGenerator;
@@ -652,7 +653,7 @@ protected void registerKeyword(String word) {
*
* @return The native generator class.
*/
- public Class getNativeIdentifierGeneratorClass() {
+ public Class<? extends IdentifierGenerator> getNativeIdentifierGeneratorClass() {
if ( supportsIdentityColumns() ) {
return IdentityGenerator.class;
}
View
3 hibernate-core/src/main/java/org/hibernate/dialect/PostgreSQL81Dialect.java
@@ -41,6 +41,7 @@
import org.hibernate.exception.spi.SQLExceptionConversionDelegate;
import org.hibernate.exception.spi.TemplatedViolatedConstraintNameExtracter;
import org.hibernate.exception.spi.ViolatedConstraintNameExtracter;
+import org.hibernate.id.IdentifierGenerator;
import org.hibernate.id.SequenceGenerator;
import org.hibernate.internal.util.JdbcExceptionHelper;
import org.hibernate.type.StandardBasicTypes;
@@ -272,7 +273,7 @@ public boolean supportsCaseInsensitiveLike() {
return true;
}
- public Class getNativeIdentifierGeneratorClass() {
+ public Class<? extends IdentifierGenerator> getNativeIdentifierGeneratorClass() {
return SequenceGenerator.class;
}
View
5 hibernate-core/src/main/java/org/hibernate/id/factory/IdentifierGeneratorFactory.java
@@ -39,7 +39,9 @@
* Get the dialect.
*
* @return the dialect
+ * @deprecated Get the dialect from service registry instead.
*/
+ @Deprecated
public Dialect getDialect();
/**
@@ -49,6 +51,7 @@
* @deprecated The intention is that Dialect should be required to be specified up-front and it would then get
* ctor injected.
*/
+ @Deprecated
public void setDialect(Dialect dialect);
/**
@@ -68,5 +71,5 @@
* @param strategy The strategy
* @return The generator class.
*/
- public Class getIdentifierGeneratorClass(String strategy);
+ public Class<? extends IdentifierGenerator> getIdentifierGeneratorClass(String strategy);
}
View
89 ...re/src/main/java/org/hibernate/id/factory/internal/DefaultIdentifierGeneratorFactory.java
@@ -30,6 +30,7 @@
import org.jboss.logging.Logger;
import org.hibernate.MappingException;
+import org.hibernate.boot.registry.selector.spi.StrategySelector;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.jdbc.spi.JdbcServices;
import org.hibernate.id.Assigned;
@@ -66,35 +67,10 @@
DefaultIdentifierGeneratorFactory.class.getName());
private transient Dialect dialect;
- private ConcurrentHashMap<String, Class> generatorStrategyToClassNameMap = new ConcurrentHashMap<String, Class>();
-
- /**
- * Constructs a new DefaultIdentifierGeneratorFactory.
- */
- public DefaultIdentifierGeneratorFactory() {
- register( "uuid2", UUIDGenerator.class );
- register( "guid", GUIDGenerator.class ); // can be done with UUIDGenerator + strategy
- register( "uuid", UUIDHexGenerator.class ); // "deprecated" for new use
- register( "uuid.hex", UUIDHexGenerator.class ); // uuid.hex is deprecated
- register( "hilo", TableHiLoGenerator.class );
- register( "assigned", Assigned.class );
- register( "identity", IdentityGenerator.class );
- register( "select", SelectGenerator.class );
- register( "sequence", SequenceGenerator.class );
- register( "seqhilo", SequenceHiLoGenerator.class );
- register( "increment", IncrementGenerator.class );
- register( "foreign", ForeignGenerator.class );
- register( "sequence-identity", SequenceIdentityGenerator.class );
- register( "enhanced-sequence", SequenceStyleGenerator.class );
- register( "enhanced-table", TableGenerator.class );
- }
-
- public void register(String strategy, Class generatorClass) {
- LOG.debugf( "Registering IdentifierGenerator strategy [%s] -> [%s]", strategy, generatorClass.getName() );
- final Class previous = generatorStrategyToClassNameMap.put( strategy, generatorClass );
- if ( previous != null ) {
- LOG.debugf( " - overriding [%s]", previous.getName() );
- }
+ private transient StrategySelector strategySelector;
+ @Override
+ public void register(String strategy, Class<? extends IdentifierGenerator> generatorClass) {
+ strategySelector.registerStrategyImplementor( IdentifierGenerator.class, strategy, generatorClass );
}
@Override
@@ -111,39 +87,56 @@ public void setDialect(Dialect dialect) {
@Override
public IdentifierGenerator createIdentifierGenerator(String strategy, Type type, Properties config) {
try {
- Class clazz = getIdentifierGeneratorClass( strategy );
- IdentifierGenerator identifierGenerator = ( IdentifierGenerator ) clazz.newInstance();
+ IdentifierGenerator identifierGenerator = strategySelector.resolveStrategy(
+ IdentifierGenerator.class,
+ strategy
+ );
if ( identifierGenerator instanceof Configurable ) {
- ( ( Configurable ) identifierGenerator ).configure( type, config, dialect );
+ ( (Configurable) identifierGenerator ).configure( type, config, dialect );
}
return identifierGenerator;
}
catch ( Exception e ) {
final String entityName = config.getProperty( IdentifierGenerator.ENTITY_NAME );
- throw new MappingException( String.format( "Could not instantiate id generator [entity-name=%s]", entityName ), e );
+ throw new MappingException(
+ String.format(
+ "Could not instantiate id generator [entity-name=%s]",
+ entityName
+ ), e
+ );
}
}
@Override
- public Class getIdentifierGeneratorClass(String strategy) {
- if ( "native".equals( strategy ) ) {
- return dialect.getNativeIdentifierGeneratorClass();
- }
-
- Class generatorClass = generatorStrategyToClassNameMap.get( strategy );
- try {
- if ( generatorClass == null ) {
- generatorClass = ReflectHelper.classForName( strategy );
- }
- }
- catch ( ClassNotFoundException e ) {
- throw new MappingException( String.format( "Could not interpret id generator strategy [%s]", strategy ) );
- }
- return generatorClass;
+ public Class<? extends IdentifierGenerator> getIdentifierGeneratorClass(String strategy) {
+ return strategySelector.selectStrategyImplementor( IdentifierGenerator.class, strategy );
}
@Override
public void injectServices(ServiceRegistryImplementor serviceRegistry) {
this.dialect = serviceRegistry.getService( JdbcServices.class ).getDialect();
+ this.strategySelector = serviceRegistry.getService( StrategySelector.class );
+ registerDefaultStrategies();
+ }
+
+ private void registerDefaultStrategies() {
+ register( "native", dialect.getNativeIdentifierGeneratorClass() );
+ register( "uuid2", UUIDGenerator.class );
+ register( "guid", GUIDGenerator.class ); // can be done with UUIDGenerator + strategy
+ register( "uuid", UUIDHexGenerator.class ); // "deprecated" for new use
+ register( "uuid.hex", UUIDHexGenerator.class ); // uuid.hex is deprecated
+ register( "hilo", TableHiLoGenerator.class );
+ register( "assigned", Assigned.class );
+ register( "identity", IdentityGenerator.class );
+ register( "select", SelectGenerator.class );
+ register( "sequence", SequenceGenerator.class );
+ register( "seqhilo", SequenceHiLoGenerator.class );
+ register( "increment", IncrementGenerator.class );
+ register( "foreign", ForeignGenerator.class );
+ register( "sequence-identity", SequenceIdentityGenerator.class );
+ register( "enhanced-sequence", SequenceStyleGenerator.class );
+ register( "enhanced-table", TableGenerator.class );
}
+
+
}
View
3 ...te-core/src/main/java/org/hibernate/id/factory/spi/MutableIdentifierGeneratorFactory.java
@@ -1,5 +1,6 @@
package org.hibernate.id.factory.spi;
+import org.hibernate.id.IdentifierGenerator;
import org.hibernate.id.factory.IdentifierGeneratorFactory;
import org.hibernate.service.Service;
@@ -9,5 +10,5 @@
* @author Emmanuel Bernard <emmanuel@hibernate.org>
*/
public interface MutableIdentifierGeneratorFactory extends IdentifierGeneratorFactory, Service {
- public void register(String strategy, Class generatorClass);
+ public void register(String strategy, Class<? extends IdentifierGenerator> generatorClass);
}
View
1 hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/AvailabilityAnnouncerImpl.java
@@ -37,6 +37,7 @@
*
* @author Steve Ebersole
*/
+@SuppressWarnings( {"UnusedDeclaration"})
public class AvailabilityAnnouncerImpl implements AvailabilityAnnouncer {
@Override
@SuppressWarnings("unchecked")
View
1 ...te-infinispan/src/main/java/org/hibernate/cache/infinispan/AvailabilityAnnouncerImpl.java
@@ -37,6 +37,7 @@
*
* @author Steve Ebersole
*/
+@SuppressWarnings( {"UnusedDeclaration"})
public class AvailabilityAnnouncerImpl implements AvailabilityAnnouncer {
@Override
public Iterable<Availability> getAvailabilities() {
Something went wrong with that request. Please try again.