KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > id > IdentifierGeneratorFactory


1 //$Id: IdentifierGeneratorFactory.java,v 1.11 2005/05/27 03:53:59 oneovthafew Exp $
2
package org.hibernate.id;
3
4 import java.io.Serializable JavaDoc;
5 import java.sql.ResultSet JavaDoc;
6 import java.sql.SQLException JavaDoc;
7 import java.util.HashMap JavaDoc;
8 import java.util.Properties JavaDoc;
9
10 import org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12 import org.hibernate.HibernateException;
13 import org.hibernate.MappingException;
14 import org.hibernate.dialect.Dialect;
15 import org.hibernate.type.Type;
16 import org.hibernate.util.ReflectHelper;
17
18 /**
19  * Factory and helper methods for <tt>IdentifierGenerator</tt> framework.
20  *
21  * @author Gavin King
22  */

23 public final class IdentifierGeneratorFactory {
24     
25     private static final Log log = LogFactory.getLog(IdentifierGeneratorFactory.class);
26
27     /**
28      * Get the generated identifier when using identity columns
29      */

30     public static Serializable JavaDoc getGeneratedIdentity(ResultSet JavaDoc rs, Type type)
31     throws SQLException JavaDoc, HibernateException, IdentifierGenerationException {
32         if ( !rs.next() ) {
33             throw new HibernateException( "The database returned no natively generated identity value" );
34         }
35         final Serializable JavaDoc id = IdentifierGeneratorFactory.get( rs, type );
36
37         if ( log.isDebugEnabled() ) log.debug( "Natively generated identity: " + id );
38         return id;
39     }
40
41     // unhappy about this being public ... is there a better way?
42
public static Serializable JavaDoc get(ResultSet JavaDoc rs, Type type)
43     throws SQLException JavaDoc, IdentifierGenerationException {
44     
45         Class JavaDoc clazz = type.getReturnedClass();
46         if ( clazz==Long JavaDoc.class ) {
47             return new Long JavaDoc( rs.getLong(1) );
48         }
49         else if ( clazz==Integer JavaDoc.class ) {
50             return new Integer JavaDoc( rs.getInt(1) );
51         }
52         else if ( clazz==Short JavaDoc.class ) {
53             return new Short JavaDoc( rs.getShort(1) );
54         }
55         else if ( clazz==String JavaDoc.class ) {
56             return rs.getString(1);
57         }
58         else {
59             throw new IdentifierGenerationException("this id generator generates long, integer, short or string");
60         }
61         
62     }
63
64     private static final HashMap JavaDoc GENERATORS = new HashMap JavaDoc();
65
66     public static final Serializable JavaDoc SHORT_CIRCUIT_INDICATOR = new Serializable JavaDoc() {
67         public String JavaDoc toString() { return "SHORT_CIRCUIT_INDICATOR"; }
68     };
69     
70     public static final Serializable JavaDoc POST_INSERT_INDICATOR = new Serializable JavaDoc() {
71         public String JavaDoc toString() { return "POST_INSERT_INDICATOR"; }
72     };
73
74     static {
75         GENERATORS.put("uuid", UUIDHexGenerator.class);
76         GENERATORS.put("hilo", TableHiLoGenerator.class);
77         GENERATORS.put("assigned", Assigned.class);
78         GENERATORS.put("identity", IdentityGenerator.class);
79         GENERATORS.put("select", SelectGenerator.class);
80         GENERATORS.put("sequence", SequenceGenerator.class);
81         GENERATORS.put("seqhilo", SequenceHiLoGenerator.class);
82         GENERATORS.put("increment", IncrementGenerator.class);
83         GENERATORS.put("foreign", ForeignGenerator.class);
84         GENERATORS.put("guid", GUIDGenerator.class);
85         GENERATORS.put("uuid.hex", UUIDHexGenerator.class); //uuid.hex is deprecated
86
}
87
88     public static IdentifierGenerator create(String JavaDoc strategy, Type type, Properties JavaDoc params, Dialect dialect)
89     throws MappingException {
90         try {
91             Class JavaDoc clazz = getIdentifierGeneratorClass( strategy, dialect );
92             IdentifierGenerator idgen = (IdentifierGenerator) clazz.newInstance();
93             if (idgen instanceof Configurable) ( (Configurable) idgen).configure(type, params, dialect);
94             return idgen;
95         }
96         catch (Exception JavaDoc e) {
97             throw new MappingException("could not instantiate id generator", e);
98         }
99     }
100
101     public static Class JavaDoc getIdentifierGeneratorClass(String JavaDoc strategy, Dialect dialect) {
102         Class JavaDoc clazz = (Class JavaDoc) GENERATORS.get(strategy);
103         if ( "native".equals(strategy) ) clazz = dialect.getNativeIdentifierGeneratorClass();
104         try {
105             if (clazz==null) clazz = ReflectHelper.classForName(strategy);
106         }
107         catch (ClassNotFoundException JavaDoc e) {
108             throw new MappingException("could not interpret id generator strategy: " + strategy);
109         }
110         return clazz;
111     }
112
113     public static Number JavaDoc createNumber(long value, Class JavaDoc clazz) throws IdentifierGenerationException {
114         if ( clazz==Long JavaDoc.class ) {
115             return new Long JavaDoc(value);
116         }
117         else if ( clazz==Integer JavaDoc.class ) {
118             return new Integer JavaDoc( (int) value );
119         }
120         else if ( clazz==Short JavaDoc.class ) {
121             return new Short JavaDoc( (short) value );
122         }
123         else {
124             throw new IdentifierGenerationException("this id generator generates long, integer, short");
125         }
126     }
127
128     private IdentifierGeneratorFactory() {} //cannot be instantiated
129

130 }
131
Popular Tags