1 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 ; 24 import java.lang.reflect.InvocationTargetException ; 25 import java.sql.ResultSet ; 26 import java.util.Calendar ; 27 import java.util.HashMap ; 28 import java.util.Map ; 29 30 38 public final class RowMapperFactory { 39 40 private static final HashMap <Class , Class <? extends RowMapper>> _rowMappings 41 = new HashMap <Class , Class <? extends RowMapper>>(); 42 43 private static Class <? extends RowMapper> DEFAULT_OBJ_ROWMAPPING = RowToObjectMapper.class; 44 45 private static Class XMLOBJ_CLASS = null; 46 private static Class <? extends RowMapper> DEFAULT_XMLOBJ_ROWMAPPING = null; 47 private final static Class [] _params = {ResultSet .class, Class .class, Calendar .class}; 48 49 static { 50 51 _rowMappings.put(HashMap .class, RowToHashMapMapper.class); 52 _rowMappings.put(Map .class, RowToMapMapper.class); 53 54 try { 55 XMLOBJ_CLASS = Class.forName("org.apache.xmlbeans.XmlObject"); 56 DEFAULT_XMLOBJ_ROWMAPPING = RowToXmlObjectMapper.class; 57 } catch (ClassNotFoundException e) { 58 } 60 } 61 62 70 public static RowMapper getRowMapper(ResultSet rs, Class returnTypeClass, Calendar cal) { 71 72 Class <? extends RowMapper> rm = _rowMappings.get(returnTypeClass); 73 if (rm != null) { 74 return getMapper(rm, rs, returnTypeClass, cal); 75 } 76 77 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 96 public static void addRowMapping(Class returnTypeClass, Class <? extends RowMapper> rowMapperClass) { 97 _rowMappings.put(returnTypeClass, rowMapperClass); 98 } 99 100 107 public static Class <? extends RowMapper> replaceRowMapping(Class returnTypeClass, Class <? extends RowMapper> rowMapperClass) { 108 return _rowMappings.put(returnTypeClass, rowMapperClass); 109 } 110 111 118 public static Class <? extends RowMapper> removeRowMapping(Class returnTypeClass) { 119 return _rowMappings.remove(returnTypeClass); 120 } 121 122 127 public static Class <? extends RowMapper> setDefaultRowMapping(Class <? extends RowMapper> rowMapperClass) { 128 Class <? extends RowMapper> ret = DEFAULT_OBJ_ROWMAPPING; 129 DEFAULT_OBJ_ROWMAPPING = rowMapperClass; 130 return ret; 131 } 132 133 138 public static Class <? extends RowMapper> setDefaultXmlRowMapping(Class mapToClass, Class <? extends RowMapper> rowMapperClass) { 139 Class <? extends RowMapper> ret = DEFAULT_XMLOBJ_ROWMAPPING; 140 DEFAULT_XMLOBJ_ROWMAPPING = rowMapperClass; 141 XMLOBJ_CLASS = mapToClass; 142 return ret; 143 } 144 145 154 private static RowMapper getMapper(Class <? extends RowMapper> rowMapper, ResultSet rs, Class returnType, Calendar cal) 155 { 156 Constructor c = null; 157 try { 158 c = rowMapper.getDeclaredConstructor(_params); 159 return (RowMapper) c.newInstance(new Object []{rs, returnType, cal}); 160 } catch (NoSuchMethodException e) { 161 throw new ControlException("Failure creating new instance of RowMapper, " + e.toString(), e); 162 } catch (InstantiationException e) { 163 throw new ControlException("Failure creating new instance of RowMapper, " + e.toString(), e); 164 } catch (IllegalAccessException e) { 165 throw new ControlException("Failure creating new instance of RowMapper, " + e.toString(), e); 166 } catch (InvocationTargetException e) { 167 throw new ControlException("Failure creating new instance of RowMapper, " + e.getCause().toString(), e); 168 } 169 } 170 } 171 | Popular Tags |