KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > access > types > DefaultType


1 /* ====================================================================
2  *
3  * The ObjectStyle Group Software License, version 1.1
4  * ObjectStyle Group - http://objectstyle.org/
5  *
6  * Copyright (c) 2002-2005, Andrei (Andrus) Adamchik and individual authors
7  * of the software. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if any,
22  * must include the following acknowlegement:
23  * "This product includes software developed by independent contributors
24  * and hosted on ObjectStyle Group web site (http://objectstyle.org/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The names "ObjectStyle Group" and "Cayenne" must not be used to endorse
29  * or promote products derived from this software without prior written
30  * permission. For written permission, email
31  * "andrus at objectstyle dot org".
32  *
33  * 5. Products derived from this software may not be called "ObjectStyle"
34  * or "Cayenne", nor may "ObjectStyle" or "Cayenne" appear in their
35  * names without prior written permission.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals and hosted on ObjectStyle Group web site. For more
53  * information on the ObjectStyle Group, please see
54  * <http://objectstyle.org/>.
55  */

56 package org.objectstyle.cayenne.access.types;
57
58 import java.lang.reflect.Method JavaDoc;
59 import java.sql.CallableStatement JavaDoc;
60 import java.sql.ResultSet JavaDoc;
61 import java.util.HashMap JavaDoc;
62 import java.util.Iterator JavaDoc;
63 import java.util.Map JavaDoc;
64
65 import org.objectstyle.cayenne.CayenneRuntimeException;
66 import org.objectstyle.cayenne.dba.TypesMapping;
67
68 /**
69  * Default implementation of ExtendedType that matches Java and JDBC types exactly per
70  * JDBC specification.
71  *
72  * @author Andrei Adamchik
73  */

74 public class DefaultType extends AbstractType {
75
76     private static final Map JavaDoc readMethods = new HashMap JavaDoc();
77     private static final Map JavaDoc procReadMethods = new HashMap JavaDoc();
78     private static Method JavaDoc readObjectMethod;
79     private static Method JavaDoc procReadObjectMethod;
80
81     static {
82         try {
83             Class JavaDoc rsClass = ResultSet JavaDoc.class;
84             Class JavaDoc[] paramTypes = new Class JavaDoc[] {
85                 Integer.TYPE
86             };
87             readMethods.put(TypesMapping.JAVA_LONG, rsClass.getMethod("getLong",
88                     paramTypes));
89             readMethods.put(TypesMapping.JAVA_BIGDECIMAL, rsClass
90                     .getMethod("getBigDecimal", paramTypes));
91             readMethods.put(TypesMapping.JAVA_BOOLEAN, rsClass.getMethod("getBoolean",
92                     paramTypes));
93             readMethods.put(TypesMapping.JAVA_BYTE, rsClass.getMethod("getByte",
94                     paramTypes));
95             readMethods.put(TypesMapping.JAVA_BYTES, rsClass.getMethod("getBytes",
96                     paramTypes));
97             readMethods.put(TypesMapping.JAVA_SQLDATE, rsClass.getMethod("getDate",
98                     paramTypes));
99             readMethods.put(TypesMapping.JAVA_DOUBLE, rsClass.getMethod("getDouble",
100                     paramTypes));
101             readMethods.put(TypesMapping.JAVA_FLOAT, rsClass.getMethod("getFloat",
102                     paramTypes));
103             readMethods.put(TypesMapping.JAVA_INTEGER, rsClass.getMethod("getInt",
104                     paramTypes));
105             readMethods.put(TypesMapping.JAVA_SHORT, rsClass.getMethod("getShort",
106                     paramTypes));
107             readMethods.put(TypesMapping.JAVA_STRING, rsClass.getMethod("getString",
108                     paramTypes));
109             readMethods.put(TypesMapping.JAVA_TIME, rsClass.getMethod("getTime",
110                     paramTypes));
111             readMethods.put(TypesMapping.JAVA_TIMESTAMP, rsClass
112                     .getMethod("getTimestamp", paramTypes));
113
114             readObjectMethod = rsClass.getMethod("getObject", paramTypes);
115
116             // init procedure read methods
117
Class JavaDoc csClass = CallableStatement JavaDoc.class;
118             procReadMethods.put(TypesMapping.JAVA_LONG, csClass.getMethod("getLong",
119                     paramTypes));
120             procReadMethods.put(TypesMapping.JAVA_BIGDECIMAL, csClass
121                     .getMethod("getBigDecimal", paramTypes));
122             procReadMethods.put(TypesMapping.JAVA_BOOLEAN, csClass
123                     .getMethod("getBoolean", paramTypes));
124             procReadMethods.put(TypesMapping.JAVA_BYTE, csClass.getMethod("getByte",
125                     paramTypes));
126             procReadMethods.put(TypesMapping.JAVA_BYTES, csClass.getMethod("getBytes",
127                     paramTypes));
128             procReadMethods.put(TypesMapping.JAVA_SQLDATE, csClass.getMethod("getDate",
129                     paramTypes));
130             procReadMethods.put(TypesMapping.JAVA_DOUBLE, csClass.getMethod("getDouble",
131                     paramTypes));
132             procReadMethods.put(TypesMapping.JAVA_FLOAT, csClass.getMethod("getFloat",
133                     paramTypes));
134             procReadMethods.put(TypesMapping.JAVA_INTEGER, csClass.getMethod("getInt",
135                     paramTypes));
136             procReadMethods.put(TypesMapping.JAVA_SHORT, csClass.getMethod("getShort",
137                     paramTypes));
138             procReadMethods.put(TypesMapping.JAVA_STRING, csClass.getMethod("getString",
139                     paramTypes));
140             procReadMethods.put(TypesMapping.JAVA_TIME, csClass.getMethod("getTime",
141                     paramTypes));
142             procReadMethods.put(TypesMapping.JAVA_TIMESTAMP, csClass
143                     .getMethod("getTimestamp", paramTypes));
144
145             procReadObjectMethod = csClass.getMethod("getObject", paramTypes);
146         }
147         catch (Exception JavaDoc ex) {
148             throw new CayenneRuntimeException("Error initializing read methods.", ex);
149         }
150     }
151
152     /** Returns an Iterator of supported default Java classes (as Strings) */
153     public static Iterator JavaDoc defaultTypes() {
154         return readMethods.keySet().iterator();
155     }
156
157     protected String JavaDoc className;
158     protected Method JavaDoc readMethod;
159     protected Method JavaDoc procReadMethod;
160
161     /**
162      * CreatesDefaultType to read objects from ResultSet using "getObject" method.
163      */

164     public DefaultType() {
165         this.className = Object JavaDoc.class.getName();
166         this.readMethod = readObjectMethod;
167         this.procReadMethod = procReadObjectMethod;
168     }
169
170     public DefaultType(String JavaDoc className) {
171         this.className = className;
172         this.readMethod = (Method JavaDoc) readMethods.get(className);
173
174         if (readMethod == null) {
175             throw new CayenneRuntimeException("Unsupported default class: "
176                     + className
177                     + ". If you want a non-standard class to map to JDBC type,"
178                     + " you will need to implement ExtendedType interface yourself.");
179         }
180
181         this.procReadMethod = (Method JavaDoc) procReadMethods.get(className);
182         if (procReadMethod == null) {
183             throw new CayenneRuntimeException("Unsupported default class: "
184                     + className
185                     + ". If you want a non-standard class to map to JDBC type,"
186                     + " you will need to implement ExtendedType interface yourself.");
187         }
188     }
189
190     public String JavaDoc getClassName() {
191         return className;
192     }
193
194     public Object JavaDoc materializeObject(ResultSet JavaDoc rs, int index, int type) throws Exception JavaDoc {
195         Object JavaDoc val = readMethod.invoke(rs, new Object JavaDoc[] {
196             new Integer JavaDoc(index)
197         });
198         return (rs.wasNull()) ? null : val;
199     }
200
201     public Object JavaDoc materializeObject(CallableStatement JavaDoc st, int index, int type)
202             throws Exception JavaDoc {
203         Object JavaDoc val = procReadMethod.invoke(st, new Object JavaDoc[] {
204             new Integer JavaDoc(index)
205         });
206         return (st.wasNull()) ? null : val;
207     }
208 }
209
Popular Tags