1 16 17 package org.apache.commons.beanutils; 18 19 import java.io.Serializable ; 20 import java.sql.ResultSet ; 21 import java.sql.ResultSetMetaData ; 22 import java.sql.SQLException ; 23 import java.util.ArrayList ; 24 import java.util.HashMap ; 25 import java.util.Map ; 26 27 34 35 abstract class JDBCDynaClass implements DynaClass, Serializable { 36 37 39 43 protected boolean lowerCase = true; 44 45 49 protected DynaProperty properties[] = null; 50 51 57 protected Map propertiesMap = new HashMap (); 58 59 61 67 public String getName() { 68 69 return (this.getClass().getName()); 70 71 } 72 73 82 public DynaProperty getDynaProperty(String name) { 83 84 if (name == null) { 85 throw new IllegalArgumentException ("No property name specified"); 86 } 87 return ((DynaProperty) propertiesMap.get(name)); 88 89 } 90 91 96 public DynaProperty[] getDynaProperties() { 97 98 return (properties); 99 100 } 101 102 113 public DynaBean newInstance() 114 throws IllegalAccessException , InstantiationException { 115 116 throw new UnsupportedOperationException ("newInstance() not supported"); 117 118 } 119 120 129 protected Class loadClass(String className) throws SQLException { 130 131 try { 132 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 133 if (cl == null) { 134 cl = this.getClass().getClassLoader(); 135 } 136 return (cl.loadClass(className)); 137 } catch (Exception e) { 138 throw new SQLException ( 139 "Cannot load column class '" + className + "': " + e); 140 } 141 142 } 143 144 152 protected DynaProperty createDynaProperty( 153 ResultSetMetaData metadata, 154 int i) 155 throws SQLException { 156 157 String name = null; 158 if (lowerCase) { 159 name = metadata.getColumnName(i).toLowerCase(); 160 } else { 161 name = metadata.getColumnName(i); 162 } 163 String className = null; 164 try { 165 className = metadata.getColumnClassName(i); 166 } catch (SQLException e) { 167 } 170 171 Class clazz = Object .class; 174 if (className != null) { 175 clazz = loadClass(className); 176 } 177 return new DynaProperty(name, clazz); 178 179 } 180 181 192 protected void introspect(ResultSet resultSet) throws SQLException { 193 194 ArrayList list = new ArrayList (); 196 ResultSetMetaData metadata = resultSet.getMetaData(); 197 int n = metadata.getColumnCount(); 198 for (int i = 1; i <= n; i++) { DynaProperty dynaProperty = createDynaProperty(metadata, i); 200 if (dynaProperty != null) { 201 list.add(dynaProperty); 202 } 203 } 204 205 properties = 207 (DynaProperty[]) list.toArray(new DynaProperty[list.size()]); 208 for (int i = 0; i < properties.length; i++) { 209 propertiesMap.put(properties[i].getName(), properties[i]); 210 } 211 212 } 213 214 } 215 | Popular Tags |