001: //$Id: IdentifierGeneratorFactory.java 9686 2006-03-27 16:47:06Z steve.ebersole@jboss.com $
002: package org.hibernate.id;
003:
004: import java.io.Serializable;
005: import java.sql.ResultSet;
006: import java.sql.SQLException;
007: import java.util.HashMap;
008: import java.util.Properties;
009:
010: import org.apache.commons.logging.Log;
011: import org.apache.commons.logging.LogFactory;
012: import org.hibernate.HibernateException;
013: import org.hibernate.MappingException;
014: import org.hibernate.dialect.Dialect;
015: import org.hibernate.type.Type;
016: import org.hibernate.util.ReflectHelper;
017:
018: /**
019: * Factory and helper methods for <tt>IdentifierGenerator</tt> framework.
020: *
021: * @author Gavin King
022: */
023: public final class IdentifierGeneratorFactory {
024:
025: private static final Log log = LogFactory
026: .getLog(IdentifierGeneratorFactory.class);
027:
028: /**
029: * Get the generated identifier when using identity columns
030: */
031: public static Serializable getGeneratedIdentity(ResultSet rs,
032: Type type) throws SQLException, HibernateException,
033: IdentifierGenerationException {
034: if (!rs.next()) {
035: throw new HibernateException(
036: "The database returned no natively generated identity value");
037: }
038: final Serializable id = IdentifierGeneratorFactory
039: .get(rs, type);
040:
041: if (log.isDebugEnabled())
042: log.debug("Natively generated identity: " + id);
043: return id;
044: }
045:
046: // unhappy about this being public ... is there a better way?
047: public static Serializable get(ResultSet rs, Type type)
048: throws SQLException, IdentifierGenerationException {
049:
050: Class clazz = type.getReturnedClass();
051: if (clazz == Long.class) {
052: return new Long(rs.getLong(1));
053: } else if (clazz == Integer.class) {
054: return new Integer(rs.getInt(1));
055: } else if (clazz == Short.class) {
056: return new Short(rs.getShort(1));
057: } else if (clazz == String.class) {
058: return rs.getString(1);
059: } else {
060: throw new IdentifierGenerationException(
061: "this id generator generates long, integer, short or string");
062: }
063:
064: }
065:
066: private static final HashMap GENERATORS = new HashMap();
067:
068: public static final Serializable SHORT_CIRCUIT_INDICATOR = new Serializable() {
069: public String toString() {
070: return "SHORT_CIRCUIT_INDICATOR";
071: }
072: };
073:
074: public static final Serializable POST_INSERT_INDICATOR = new Serializable() {
075: public String toString() {
076: return "POST_INSERT_INDICATOR";
077: }
078: };
079:
080: static {
081: GENERATORS.put("uuid", UUIDHexGenerator.class);
082: GENERATORS.put("hilo", TableHiLoGenerator.class);
083: GENERATORS.put("assigned", Assigned.class);
084: GENERATORS.put("identity", IdentityGenerator.class);
085: GENERATORS.put("select", SelectGenerator.class);
086: GENERATORS.put("sequence", SequenceGenerator.class);
087: GENERATORS.put("seqhilo", SequenceHiLoGenerator.class);
088: GENERATORS.put("increment", IncrementGenerator.class);
089: GENERATORS.put("foreign", ForeignGenerator.class);
090: GENERATORS.put("guid", GUIDGenerator.class);
091: GENERATORS.put("uuid.hex", UUIDHexGenerator.class); //uuid.hex is deprecated
092: GENERATORS.put("sequence-identity",
093: SequenceIdentityGenerator.class);
094: }
095:
096: public static IdentifierGenerator create(String strategy,
097: Type type, Properties params, Dialect dialect)
098: throws MappingException {
099: try {
100: Class clazz = getIdentifierGeneratorClass(strategy, dialect);
101: IdentifierGenerator idgen = (IdentifierGenerator) clazz
102: .newInstance();
103: if (idgen instanceof Configurable)
104: ((Configurable) idgen).configure(type, params, dialect);
105: return idgen;
106: } catch (Exception e) {
107: throw new MappingException(
108: "could not instantiate id generator", e);
109: }
110: }
111:
112: public static Class getIdentifierGeneratorClass(String strategy,
113: Dialect dialect) {
114: Class clazz = (Class) GENERATORS.get(strategy);
115: if ("native".equals(strategy))
116: clazz = dialect.getNativeIdentifierGeneratorClass();
117: try {
118: if (clazz == null)
119: clazz = ReflectHelper.classForName(strategy);
120: } catch (ClassNotFoundException e) {
121: throw new MappingException(
122: "could not interpret id generator strategy: "
123: + strategy);
124: }
125: return clazz;
126: }
127:
128: public static Number createNumber(long value, Class clazz)
129: throws IdentifierGenerationException {
130: if (clazz == Long.class) {
131: return new Long(value);
132: } else if (clazz == Integer.class) {
133: return new Integer((int) value);
134: } else if (clazz == Short.class) {
135: return new Short((short) value);
136: } else {
137: throw new IdentifierGenerationException(
138: "this id generator generates long, integer, short");
139: }
140: }
141:
142: private IdentifierGeneratorFactory() {
143: } //cannot be instantiated
144:
145: }
|