You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by js...@apache.org on 2002/09/13 13:51:19 UTC
cvs commit: jakarta-commons/beanutils/src/java/org/apache/commons/beanutils ResultSetDynaClass.java
jstrachan 2002/09/13 04:51:19
Modified: beanutils/src/java/org/apache/commons/beanutils
ResultSetDynaClass.java
Log:
Minor patch to refactor the implementation code a touch.
Firstly there's a patch so that this class works on HsqlDb which does not support ResultSetMetaData.getColumnClassName();
Also new Factory and Strategy methods have been added to make it easier to extend this class to, use different DynaProperty implementations and to use different class loading strategies.
Finally the class loading of column types will use the ClassLoader used to load this library, rather than the system class loader.
Revision Changes Path
1.5 +50 -13 jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/ResultSetDynaClass.java
Index: ResultSetDynaClass.java
===================================================================
RCS file: /home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/ResultSetDynaClass.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- ResultSetDynaClass.java 14 Aug 2002 20:29:14 -0000 1.4
+++ ResultSetDynaClass.java 13 Sep 2002 11:51:18 -0000 1.5
@@ -301,16 +301,10 @@
ResultSetMetaData metadata = resultSet.getMetaData();
int n = metadata.getColumnCount();
for (int i = 1; i <= n; i++) { // JDBC is one-relative!
- String name = metadata.getColumnName(i).toLowerCase();
- String className = metadata.getColumnClassName(i);
- Class clazz = null;
- try {
- clazz = Class.forName(className);
- } catch (Exception e) {
- throw new SQLException("Cannot load column class '" +
- className + "': " + e);
+ DynaProperty dynaProperty = createDynaProperty(metadata, i);
+ if (dynaProperty != null) {
+ list.add(dynaProperty);
}
- list.add(new DynaProperty(name, clazz));
}
// Convert this list into the internal data structures we need
@@ -322,5 +316,48 @@
}
+ /**
+ * <p>Factory method to create a new DynaProperty for the given index
+ * into the result set metadata</p>
+ *
+ * @param metadata is the result set metadata
+ * @param i is the column index in the metadata
+ * @return the newly created DynaProperty instance
+ */
+ protected DynaProperty createDynaProperty(ResultSetMetaData metadata, int i) throws SQLException {
+ String name = metadata.getColumnName(i).toLowerCase();
+
+ String className = null;
+ try {
+ className = metadata.getColumnClassName(i);
+ }
+ catch (SQLException e) {
+ // this is a patch for HsqlDb to ignore exceptions thrown by its metadata implementation
+ }
+
+ // lets default to Object type if no class name could be retrieved from the metadata
+ Class clazz = Object.class;
+ if (className != null) {
+ clazz = loadClass(className);
+ }
+ return new DynaProperty(name, clazz);
+ }
+
+ /**
+ * <p>Loads the class of the given name which by default uses the class loader used
+ * to load this library.
+ * Dervations of this class could implement alternative class loading policies such as
+ * using custom ClassLoader or using the Threads's context class loader etc.
+ * </p>
+ */
+ protected Class loadClass(String className) throws SQLException {
+ try {
+ return getClass().getClassLoader().loadClass(className);
+ }
+ catch (Exception e) {
+ throw new SQLException("Cannot load column class '" +
+ className + "': " + e);
+ }
+ }
}
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>