HHH-6091 Make IdentifierGeneratorFactory a service

Create a MutableIdentifierGeneratorFactory to host the register contract
Make MutableIdentifierGeneratorFactory a default StandardServiceInitiator
This commit is contained in:
Emmanuel Bernard 2011-09-06 19:07:22 +02:00
parent d71a15d1c1
commit 88a7edbdd7
6 changed files with 58 additions and 18 deletions

View File

@ -27,13 +27,12 @@ import java.io.Serializable;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
import javax.persistence.GenerationType;
import org.jboss.logging.Logger;
import org.hibernate.AssertionFailure;
import org.hibernate.MappingException;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.jdbc.spi.JdbcServices;
import org.hibernate.id.Assigned;
import org.hibernate.id.Configurable;
import org.hibernate.id.ForeignGenerator;
@ -41,7 +40,6 @@ import org.hibernate.id.GUIDGenerator;
import org.hibernate.id.IdentifierGenerator;
import org.hibernate.id.IdentityGenerator;
import org.hibernate.id.IncrementGenerator;
import org.hibernate.id.MultipleHiLoPerTableGenerator;
import org.hibernate.id.SelectGenerator;
import org.hibernate.id.SequenceGenerator;
import org.hibernate.id.SequenceHiLoGenerator;
@ -51,8 +49,11 @@ import org.hibernate.id.UUIDGenerator;
import org.hibernate.id.UUIDHexGenerator;
import org.hibernate.id.enhanced.SequenceStyleGenerator;
import org.hibernate.id.enhanced.TableGenerator;
import org.hibernate.id.factory.spi.MutableIdentifierGeneratorFactory;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.util.ReflectHelper;
import org.hibernate.service.spi.ServiceRegistryAwareService;
import org.hibernate.service.spi.ServiceRegistryImplementor;
import org.hibernate.type.Type;
/**
@ -60,7 +61,7 @@ import org.hibernate.type.Type;
*
* @author Steve Ebersole
*/
public class DefaultIdentifierGeneratorFactory implements IdentifierGeneratorFactory, Serializable {
public class DefaultIdentifierGeneratorFactory implements MutableIdentifierGeneratorFactory, Serializable, ServiceRegistryAwareService {
private static final CoreMessageLogger LOG = Logger.getMessageLogger(CoreMessageLogger.class,
DefaultIdentifierGeneratorFactory.class.getName());
@ -68,16 +69,6 @@ public class DefaultIdentifierGeneratorFactory implements IdentifierGeneratorFac
private transient Dialect dialect;
private ConcurrentHashMap<String, Class> generatorStrategyToClassNameMap = new ConcurrentHashMap<String, Class>();
/**
* Constructs a new DefaultIdentifierGeneratorFactory
*
* @param dialect The dialect.
*/
public DefaultIdentifierGeneratorFactory(Dialect dialect) {
this();
this.dialect = dialect;
}
/**
* Constructs a new DefaultIdentifierGeneratorFactory.
*/
@ -151,4 +142,9 @@ public class DefaultIdentifierGeneratorFactory implements IdentifierGeneratorFac
}
return generatorClass;
}
@Override
public void injectServices(ServiceRegistryImplementor serviceRegistry) {
this.dialect = serviceRegistry.getService( JdbcServices.class ).getDialect();
}
}

View File

@ -26,6 +26,8 @@ package org.hibernate.id.factory;
import java.util.Properties;
import org.hibernate.dialect.Dialect;
import org.hibernate.id.IdentifierGenerator;
import org.hibernate.service.Service;
import org.hibernate.service.spi.ServiceRegistryAwareService;
import org.hibernate.type.Type;
/**

View File

@ -0,0 +1,25 @@
package org.hibernate.id.factory.internal;
import java.util.Map;
import org.hibernate.id.factory.DefaultIdentifierGeneratorFactory;
import org.hibernate.id.factory.spi.MutableIdentifierGeneratorFactory;
import org.hibernate.service.spi.BasicServiceInitiator;
import org.hibernate.service.spi.ServiceRegistryImplementor;
/**
* @author Emmanuel Bernard <emmanuel@hibernate.org>
*/
public class MutableIdentifierGeneratorFactoryInitiator implements BasicServiceInitiator<MutableIdentifierGeneratorFactory> {
public static final MutableIdentifierGeneratorFactoryInitiator INSTANCE = new MutableIdentifierGeneratorFactoryInitiator();
@Override
public Class<MutableIdentifierGeneratorFactory> getServiceInitiated() {
return MutableIdentifierGeneratorFactory.class;
}
@Override
public MutableIdentifierGeneratorFactory initiateService(Map configurationValues, ServiceRegistryImplementor registry) {
return new DefaultIdentifierGeneratorFactory();
}
}

View File

@ -0,0 +1,13 @@
package org.hibernate.id.factory.spi;
import org.hibernate.id.factory.IdentifierGeneratorFactory;
import org.hibernate.service.Service;
/**
* Let people register strategies
*
* @author Emmanuel Bernard <emmanuel@hibernate.org>
*/
public interface MutableIdentifierGeneratorFactory extends IdentifierGeneratorFactory, Service {
public void register(String strategy, Class generatorClass);
}

View File

@ -46,6 +46,7 @@ import org.hibernate.engine.spi.NamedQueryDefinition;
import org.hibernate.engine.spi.NamedSQLQueryDefinition;
import org.hibernate.id.factory.DefaultIdentifierGeneratorFactory;
import org.hibernate.id.factory.IdentifierGeneratorFactory;
import org.hibernate.id.factory.spi.MutableIdentifierGeneratorFactory;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.util.Value;
import org.hibernate.metamodel.MetadataSourceProcessingOrder;
@ -96,7 +97,7 @@ public class MetadataImpl implements MetadataImplementor, Serializable {
private SessionFactoryBuilder sessionFactoryBuilder = new SessionFactoryBuilderImpl( this );
private final DefaultIdentifierGeneratorFactory identifierGeneratorFactory;
private final MutableIdentifierGeneratorFactory identifierGeneratorFactory;
private final Database database;
@ -120,10 +121,10 @@ public class MetadataImpl implements MetadataImplementor, Serializable {
private boolean globallyQuotedIdentifiers = false;
public MetadataImpl(MetadataSources metadataSources, Options options) {
Dialect dialect = metadataSources.getServiceRegistry().getService( JdbcServices.class ).getDialect();
this.serviceRegistry = metadataSources.getServiceRegistry();
this.serviceRegistry = metadataSources.getServiceRegistry();
this.options = options;
this.identifierGeneratorFactory = new DefaultIdentifierGeneratorFactory( dialect );
this.identifierGeneratorFactory = serviceRegistry.getService( MutableIdentifierGeneratorFactory.class );
//new DefaultIdentifierGeneratorFactory( dialect );
this.database = new Database( options );
this.mappingDefaults = new MappingDefaultsImpl();

View File

@ -30,6 +30,7 @@ import org.hibernate.cache.internal.RegionFactoryInitiator;
import org.hibernate.engine.jdbc.batch.internal.BatchBuilderInitiator;
import org.hibernate.engine.jdbc.internal.JdbcServicesInitiator;
import org.hibernate.engine.transaction.internal.TransactionFactoryInitiator;
import org.hibernate.id.factory.internal.MutableIdentifierGeneratorFactoryInitiator;
import org.hibernate.integrator.internal.IntegratorServiceInitiator;
import org.hibernate.persister.internal.PersisterClassResolverInitiator;
import org.hibernate.persister.internal.PersisterFactoryInitiator;
@ -70,6 +71,8 @@ public class StandardServiceInitiators {
serviceInitiators.add( BatchBuilderInitiator.INSTANCE );
serviceInitiators.add( JdbcServicesInitiator.INSTANCE );
serviceInitiators.add( MutableIdentifierGeneratorFactoryInitiator.INSTANCE);
serviceInitiators.add( JtaPlatformInitiator.INSTANCE );
serviceInitiators.add( TransactionFactoryInitiator.INSTANCE );