KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.access.types;
21
22 import java.lang.reflect.Method JavaDoc;
23 import java.sql.CallableStatement JavaDoc;
24 import java.sql.ResultSet JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import org.apache.cayenne.CayenneRuntimeException;
30 import org.apache.cayenne.dba.TypesMapping;
31
32 /**
33  * An ExtendedType that can work with any Java class, providing JDBC-to-Java mapping
34  * exactly per JDBC specification.
35  *
36  * @author Andrus Adamchik
37  */

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

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