Save This Page
Home » Hibernate-3.3.2.GA » org.hibernate » id » factory » [javadoc | source]
    1   /*
    2    * Hibernate, Relational Persistence for Idiomatic Java
    3    *
    4    * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
    5    * indicated by the @author tags or express copyright attribution
    6    * statements applied by the authors.  All third-party contributions are
    7    * distributed under license by Red Hat Middleware LLC.
    8    *
    9    * This copyrighted material is made available to anyone wishing to use, modify,
   10    * copy, or redistribute it subject to the terms and conditions of the GNU
   11    * Lesser General Public License, as published by the Free Software Foundation.
   12    *
   13    * This program is distributed in the hope that it will be useful,
   14    * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
   15    * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
   16    * for more details.
   17    *
   18    * You should have received a copy of the GNU Lesser General Public License
   19    * along with this distribution; if not, write to:
   20    * Free Software Foundation, Inc.
   21    * 51 Franklin Street, Fifth Floor
   22    * Boston, MA  02110-1301  USA
   23    *
   24    */
   25   package org.hibernate.id.factory;
   26   
   27   import java.util.Properties;
   28   
   29   import org.slf4j.Logger;
   30   import org.slf4j.LoggerFactory;
   31   
   32   import org.hibernate.id.IdentifierGenerator;
   33   import org.hibernate.id.UUIDHexGenerator;
   34   import org.hibernate.id.TableHiLoGenerator;
   35   import org.hibernate.id.Assigned;
   36   import org.hibernate.id.IdentityGenerator;
   37   import org.hibernate.id.SelectGenerator;
   38   import org.hibernate.id.SequenceGenerator;
   39   import org.hibernate.id.SequenceHiLoGenerator;
   40   import org.hibernate.id.IncrementGenerator;
   41   import org.hibernate.id.ForeignGenerator;
   42   import org.hibernate.id.GUIDGenerator;
   43   import org.hibernate.id.SequenceIdentityGenerator;
   44   import org.hibernate.id.Configurable;
   45   import org.hibernate.id.enhanced.SequenceStyleGenerator;
   46   import org.hibernate.id.enhanced.TableGenerator;
   47   import org.hibernate.type.Type;
   48   import org.hibernate.util.FastHashMap;
   49   import org.hibernate.util.ReflectHelper;
   50   import org.hibernate.dialect.Dialect;
   51   import org.hibernate.MappingException;
   52   
   53   /**
   54    * Basic <tt>templated</tt> support for {@link IdentifierGeneratorFactory} implementations.
   55    *
   56    * @author Steve Ebersole
   57    */
   58   public class DefaultIdentifierGeneratorFactory implements IdentifierGeneratorFactory {
   59   	private static final Logger log = LoggerFactory.getLogger( DefaultIdentifierGeneratorFactory.class );
   60   
   61   	private Dialect dialect;
   62   	private FastHashMap generatorStrategyToClassNameMap = new FastHashMap();
   63   
   64   	/**
   65   	 * Constructs a new DefaultIdentifierGeneratorFactory.
   66   	 */
   67   	public DefaultIdentifierGeneratorFactory() {
   68   		register( "uuid", UUIDHexGenerator.class );
   69   		register( "hilo", TableHiLoGenerator.class );
   70   		register( "assigned", Assigned.class );
   71   		register( "identity", IdentityGenerator.class );
   72   		register( "select", SelectGenerator.class );
   73   		register( "sequence", SequenceGenerator.class );
   74   		register( "seqhilo", SequenceHiLoGenerator.class );
   75   		register( "increment", IncrementGenerator.class );
   76   		register( "foreign", ForeignGenerator.class );
   77   		register( "guid", GUIDGenerator.class );
   78   		register( "uuid.hex", UUIDHexGenerator.class ); 	// uuid.hex is deprecated
   79   		register( "sequence-identity", SequenceIdentityGenerator.class );
   80   		register( "enhanced-sequence", SequenceStyleGenerator.class );
   81   		register( "enhanced-table", TableGenerator.class );
   82   	}
   83   
   84   	/**
   85   	 * {@inheritDoc}
   86   	 */
   87   	public void setDialect(Dialect dialect) {
   88   		log.debug( "Setting dialect [" + dialect + "]" );
   89   		this.dialect = dialect;
   90   	}
   91   
   92   	public void register(String strategy, Class generatorClass) {
   93   		String msg = "Registering IdentifierGenerator strategy [" + strategy + "] -> [" + generatorClass + "]";
   94   		Object old = generatorStrategyToClassNameMap.put( strategy, generatorClass );
   95   		if ( old != null ) {
   96   			msg += ", overriding [" + old + "]";
   97   		}
   98   		log.debug( msg );
   99   	}
  100   
  101   	/**
  102   	 * {@inheritDoc}
  103   	 */
  104   	public IdentifierGenerator createIdentifierGenerator(String strategy, Type type, Properties config) {
  105   		try {
  106   			Class clazz = getIdentifierGeneratorClass( strategy );
  107   			IdentifierGenerator idgen = ( IdentifierGenerator ) clazz.newInstance();
  108   			if ( idgen instanceof Configurable ) {
  109   				( ( Configurable ) idgen ).configure( type, config, dialect );
  110   			}
  111   			return idgen;
  112   		}
  113   		catch ( Exception e ) {
  114   			String msg = "Could not instantiate id generator [entity-name="
  115   					+ config.get( IdentifierGenerator.ENTITY_NAME ) + "]";
  116   			throw new MappingException( msg, e );
  117   		}
  118   	}
  119   
  120   	/**
  121   	 * {@inheritDoc}
  122   	 */
  123   	public Class getIdentifierGeneratorClass(String strategy) {
  124   		if ( "native".equals( strategy ) ) {
  125   			return dialect.getNativeIdentifierGeneratorClass();
  126   		}
  127   
  128   		Class generatorClass = ( Class ) generatorStrategyToClassNameMap.get( strategy );
  129   		try {
  130   			if ( generatorClass == null ) {
  131   				generatorClass = ReflectHelper.classForName( strategy );
  132   			}
  133   		}
  134   		catch ( ClassNotFoundException e ) {
  135   			throw new MappingException( "Could not interpret id generator strategy [" + strategy + "]" );
  136   		}
  137   		return generatorClass;
  138   	}
  139   }

Save This Page
Home » Hibernate-3.3.2.GA » org.hibernate » id » factory » [javadoc | source]