1 61 62 package net.mlw.vlh.adapter.jdbc.dynabean.fix; 63 64 import java.io.Serializable ; 65 import java.sql.ResultSet ; 66 import java.sql.ResultSetMetaData ; 67 import java.sql.SQLException ; 68 import java.util.ArrayList ; 69 import java.util.HashMap ; 70 import java.util.List ; 71 import java.util.Map ; 72 73 import org.apache.commons.beanutils.DynaBean; 74 import org.apache.commons.beanutils.DynaClass; 75 import org.apache.commons.beanutils.DynaProperty; 76 77 84 85 abstract class JDBCDynaClass implements DynaClass, Serializable { 86 87 89 93 protected boolean lowerCase = true; 94 95 99 protected boolean useName = true; 100 101 102 106 protected DynaProperty properties[] = null; 107 108 114 protected Map propertiesMap = new HashMap (); 115 116 118 124 public String getName() { 125 126 return (this.getClass().getName()); 127 128 } 129 130 139 public DynaProperty getDynaProperty(String name) { 140 141 if (name == null) { 142 throw new IllegalArgumentException ("No property name specified"); 143 } 144 return ((DynaProperty) propertiesMap.get(name)); 145 146 } 147 148 153 public DynaProperty[] getDynaProperties() { 154 155 return (properties); 156 157 } 158 159 170 public DynaBean newInstance() 171 throws IllegalAccessException , InstantiationException { 172 173 throw new UnsupportedOperationException ("newInstance() not supported"); 174 175 } 176 177 186 protected Class loadClass(String className) throws SQLException { 187 188 try { 189 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 190 if (cl == null) { 191 cl = this.getClass().getClassLoader(); 192 } 193 return (cl.loadClass(className)); 194 } catch (Exception e) { 195 throw new SQLException ( 196 "Cannot load column class '" + className + "': " + e); 197 } 198 199 } 200 201 209 protected DynaProperty createDynaProperty( 210 ResultSetMetaData metadata, 211 int i) 212 throws SQLException { 213 214 String name = ( useName ) ? metadata.getColumnName(i) : metadata.getColumnLabel(i); 215 216 if (lowerCase) { 217 name = name.toLowerCase(); 218 } 219 220 String className = null; 221 try { 222 className = metadata.getColumnClassName(i); 223 } catch (SQLException e) { 224 } 227 228 Class clazz = Object .class; 231 if (className != null) { 232 clazz = loadClass(className); 233 } 234 return new DynaProperty(name, clazz); 235 236 } 237 238 249 protected void introspect(ResultSet resultSet) throws SQLException { 250 251 List list = new ArrayList (); 253 ResultSetMetaData metadata = resultSet.getMetaData(); 254 int n = metadata.getColumnCount(); 255 for (int i = 1; i <= n; i++) { DynaProperty dynaProperty = createDynaProperty(metadata, i); 257 if (dynaProperty != null) { 258 list.add(dynaProperty); 259 } 260 } 261 262 properties = 264 (DynaProperty[]) list.toArray(new DynaProperty[list.size()]); 265 for (int i = 0; i < properties.length; i++) { 266 propertiesMap.put(properties[i].getName(), properties[i]); 267 } 268 269 } 270 271 } 272 | Popular Tags |