KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > controls > system > jdbc > RowMapperFactory


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

18
19 package org.apache.beehive.controls.system.jdbc;
20
21 import org.apache.beehive.controls.api.ControlException;
22
23 import java.lang.reflect.Constructor JavaDoc;
24 import java.lang.reflect.InvocationTargetException JavaDoc;
25 import java.sql.ResultSet JavaDoc;
26 import java.util.Calendar JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Map JavaDoc;
29
30 /**
31  * Factory for creating row mappers.
32  * <p/>
33  * Row mapper types supported by this factory include: HashMap, Map, Object, XmlObject. The factory determines the
34  * proper row mapper to use by checking its List of RowMappers against the type of mapping requested. When performing
35  * the lookup, the factory attempts to find the most specific type match. If a match can't be found the most general
36  * type of RowMapper is returned, RowToObjectMapper.
37  */

38 public final class RowMapperFactory {
39
40     private static final HashMap JavaDoc<Class JavaDoc, Class JavaDoc<? extends RowMapper>> _rowMappings
41             = new HashMap JavaDoc<Class JavaDoc, Class JavaDoc<? extends RowMapper>>();
42
43     private static Class JavaDoc<? extends RowMapper> DEFAULT_OBJ_ROWMAPPING = RowToObjectMapper.class;
44
45     private static Class JavaDoc XMLOBJ_CLASS = null;
46     private static Class JavaDoc<? extends RowMapper> DEFAULT_XMLOBJ_ROWMAPPING = null;
47     private final static Class JavaDoc[] _params = {ResultSet JavaDoc.class, Class JavaDoc.class, Calendar JavaDoc.class};
48
49     static {
50
51         _rowMappings.put(HashMap JavaDoc.class, RowToHashMapMapper.class);
52         _rowMappings.put(Map JavaDoc.class, RowToMapMapper.class);
53
54         try {
55             XMLOBJ_CLASS = Class.forName("org.apache.xmlbeans.XmlObject");
56             DEFAULT_XMLOBJ_ROWMAPPING = RowToXmlObjectMapper.class;
57         } catch (ClassNotFoundException JavaDoc e) {
58             // NOOP if apache xml beans not present
59
}
60     }
61
62     /**
63      * Get a RowMapper instance which knows how to map a ResultSet row to the given return type.
64      *
65      * @param rs The ResultSet to map.
66      * @param returnTypeClass The class to map a ResultSet row to.
67      * @param cal Calendar instance for mapping date/time values.
68      * @return A RowMapper instance.
69      */

70     public static RowMapper getRowMapper(ResultSet JavaDoc rs, Class JavaDoc returnTypeClass, Calendar JavaDoc cal) {
71
72         Class JavaDoc<? extends RowMapper> rm = _rowMappings.get(returnTypeClass);
73         if (rm != null) {
74             return getMapper(rm, rs, returnTypeClass, cal);
75         }
76
77         //
78
// if we made it to here, check if the default XMLObject Mapper can be used,
79
// otherwise use the default object mapper
80
//
81
if (XMLOBJ_CLASS != null && XMLOBJ_CLASS.isAssignableFrom(returnTypeClass)) {
82             return getMapper(DEFAULT_XMLOBJ_ROWMAPPING, rs, returnTypeClass, cal);
83         } else {
84             return getMapper(DEFAULT_OBJ_ROWMAPPING, rs, returnTypeClass, cal);
85         }
86     }
87
88     /**
89      * Add a new row mapper to the list of available row mappers. The getRowMapper method traverses the
90      * list of mappers from beginning to end, checking to see if a mapper can handle the specified
91      * returnTypeClass. There is a default mapper which is used if a match cannot be found in the list.
92      *
93      * @param returnTypeClass Class which this mapper maps a row to.
94      * @param rowMapperClass The row mapper class.
95      */

96     public static void addRowMapping(Class JavaDoc returnTypeClass, Class JavaDoc<? extends RowMapper> rowMapperClass) {
97         _rowMappings.put(returnTypeClass, rowMapperClass);
98     }
99
100     /**
101      * Replace a row mapping.
102      *
103      * @param returnTypeClass Class which this mapper maps a row to.
104      * @param rowMapperClass The row mapper class.
105      * @return if the mapper was replaced, false mapper for returnTypeClass was not found, no action taken.
106      */

107     public static Class JavaDoc<? extends RowMapper> replaceRowMapping(Class JavaDoc returnTypeClass, Class JavaDoc<? extends RowMapper> rowMapperClass) {
108         return _rowMappings.put(returnTypeClass, rowMapperClass);
109     }
110
111     /**
112      * remove the row mapping for the specified class type.
113      *
114      * @param returnTypeClass
115      * @return the RowMapper class which was removed, null if returnTypeClass did not match any of the registered
116      * row mappers.
117      */

118     public static Class JavaDoc<? extends RowMapper> removeRowMapping(Class JavaDoc returnTypeClass) {
119         return _rowMappings.remove(returnTypeClass);
120     }
121
122     /**
123      * Sets the rowmapper for Object.class
124      *
125      * @param rowMapperClass
126      */

127     public static Class JavaDoc <? extends RowMapper> setDefaultRowMapping(Class JavaDoc<? extends RowMapper> rowMapperClass) {
128         Class JavaDoc<? extends RowMapper> ret = DEFAULT_OBJ_ROWMAPPING;
129         DEFAULT_OBJ_ROWMAPPING = rowMapperClass;
130         return ret;
131     }
132
133     /**
134      * Sets the rowmapper for XmlObject.class
135      *
136      * @param rowMapperClass
137      */

138     public static Class JavaDoc<? extends RowMapper> setDefaultXmlRowMapping(Class JavaDoc mapToClass, Class JavaDoc<? extends RowMapper> rowMapperClass) {
139         Class JavaDoc<? extends RowMapper> ret = DEFAULT_XMLOBJ_ROWMAPPING;
140         DEFAULT_XMLOBJ_ROWMAPPING = rowMapperClass;
141         XMLOBJ_CLASS = mapToClass;
142         return ret;
143     }
144
145     /**
146      * Create an instance of the RowMapper class.
147      *
148      * @param rowMapper
149      * @param rs ResultSet we are mapping from.
150      * @param returnType Class to map rows to.
151      * @param cal Calendar instance for date/time values.
152      * @return A RowMapper instance.
153      */

154     private static RowMapper getMapper(Class JavaDoc<? extends RowMapper> rowMapper, ResultSet JavaDoc rs, Class JavaDoc returnType, Calendar JavaDoc cal)
155     {
156         Constructor JavaDoc c = null;
157         try {
158             c = rowMapper.getDeclaredConstructor(_params);
159             return (RowMapper) c.newInstance(new Object JavaDoc[]{rs, returnType, cal});
160         } catch (NoSuchMethodException JavaDoc e) {
161             throw new ControlException("Failure creating new instance of RowMapper, " + e.toString(), e);
162         } catch (InstantiationException JavaDoc e) {
163             throw new ControlException("Failure creating new instance of RowMapper, " + e.toString(), e);
164         } catch (IllegalAccessException JavaDoc e) {
165             throw new ControlException("Failure creating new instance of RowMapper, " + e.toString(), e);
166         } catch (InvocationTargetException JavaDoc e) {
167             throw new ControlException("Failure creating new instance of RowMapper, " + e.getCause().toString(), e);
168         }
169     }
170 }
171
Popular Tags