X-Original-To: pgsql-jdbc-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id CDAD932AF9E for ; Tue, 12 Oct 2004 05:55:51 +0100 (BST) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 86338-03 for ; Tue, 12 Oct 2004 04:55:49 +0000 (GMT) Received: from yass.opencloud.co.nz (yass.opencloud.co.nz [203.79.85.162]) by svr1.postgresql.org (Postfix) with ESMTP id ACF9432AE0E for ; Tue, 12 Oct 2004 05:55:46 +0100 (BST) Received: from flood.opencloud.co.nz ([192.168.0.31]) by yass.opencloud.co.nz with esmtp (Exim 3.35 #1 (Debian)) id 1CHEh9-0001gh-00; Tue, 12 Oct 2004 17:55:31 +1300 Message-ID: <416B63BF.4090304@opencloud.com> Date: Tue, 12 Oct 2004 17:55:27 +1300 From: Oliver Jowett User-Agent: Mozilla Thunderbird 0.7.3 (X11/20040803) X-Accept-Language: en-us, en MIME-Version: 1.0 To: Markus Schaber Cc: PostGreSQL JDBC Subject: Re: Avoiding explicit addDataType calls for PostGIS References: <20041006181653.4fc83924@kingfisher.intern.logi-track.com> <41645F7A.2040106@opencloud.com> <20041007104603.78af41f3@kingfisher.intern.logi-track.com> In-Reply-To: <20041007104603.78af41f3@kingfisher.intern.logi-track.com> Content-Type: multipart/mixed; boundary="------------050909000309050706090808" X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200410/101 X-Sequence-Number: 11206 This is a multi-part message in MIME format. --------------050909000309050706090808 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Markus Schaber wrote: > Hi, Oliver, > > On Thu, 07 Oct 2004 10:11:22 +1300 > Oliver Jowett wrote: > >>If your objective is to produce a driver that automatically supports >>the extra datatypes, how about a provider-properties-file approach? > > That is a nice idea that allows to control default configuration without > recompiling, and without explicit code in the app. Here is a first cut at implementing this. The driver looks for resources named "postgresql.properties" in its classloader, loads them as property files in the order the classloader returns them, and uses the result as default properties. These defaults are overridden in turn by user-provided properties and properties extracted from the connection URL. Properties of the form datatype.= cause the given class to be loaded and registered via addDataType. The default set of datatypes (interval, money, line, etc) are registered in this way by a default postgresql.properties included in the driver jar. I haven't looked at preventing user properties from causing classloading yet.. don't know if it is worth it. Also I have not investigated the exact order of resources that ClassLoader.getResources() returns; it might need some tuning. -O --------------050909000309050706090808 Content-Type: text/plain; name="pgjdbc-properties-from-classpath.txt" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="pgjdbc-properties-from-classpath.txt" Index: org/postgresql/Driver.java.in =================================================================== RCS file: /usr/local/cvsroot/pgjdbc/pgjdbc/org/postgresql/Driver.java.in,v retrieving revision 1.48 diff -u -c -r1.48 Driver.java.in *** org/postgresql/Driver.java.in 10 Oct 2004 15:39:30 -0000 1.48 --- org/postgresql/Driver.java.in 12 Oct 2004 04:35:56 -0000 *************** *** 15,20 **** --- 15,21 ---- import java.io.*; import java.sql.*; import java.util.*; + import java.net.URL; import org.postgresql.util.PSQLException; import org.postgresql.util.PSQLState; *************** *** 66,71 **** --- 67,95 ---- } } + // + // Helper to retrieve default properties from classloader resource + // properties files. + // + private Properties defaultProperties; + synchronized Properties getDefaultProperties() throws IOException { + if (defaultProperties != null) + return defaultProperties; + + Properties merged = new Properties(); + + Enumeration urls = getClass().getClassLoader().getResources("postgresql.properties"); + while (urls.hasMoreElements()) { + URL url = (URL)urls.nextElement(); + InputStream is = url.openStream(); + merged.load(is); + is.close(); + } + + defaultProperties = merged; + return defaultProperties; + } + /* * Try to make a database connection to the given URL. The driver * should return "null" if it realizes it is the wrong kind of *************** *** 125,132 **** */ public java.sql.Connection connect(String url, Properties info) throws SQLException { ! Properties props; ! if ((props = parseURL(url, info)) == null) { if (Driver.logDebug) Driver.debug("Error in url" + url); --- 149,172 ---- */ public java.sql.Connection connect(String url, Properties info) throws SQLException { ! // get defaults ! Properties defaults; ! try { ! defaults = getDefaultProperties(); ! } catch (IOException ioe) { ! throw new PSQLException(GT.tr("Error loading default settings from postgresql.properties"), ! PSQLState.UNEXPECTED_ERROR, ioe); ! } ! ! // override defaults with provided properties ! Properties props = new Properties(defaults); ! for (Enumeration e = info.propertyNames(); e.hasMoreElements(); ) { ! String propName = (String)e.nextElement(); ! props.put(propName, info.getProperty(propName)); ! } ! ! // parse URL and add more properties ! if ((props = parseURL(url, props)) == null) { if (Driver.logDebug) Driver.debug("Error in url" + url); Index: org/postgresql/jdbc2/AbstractJdbc2Connection.java =================================================================== RCS file: /usr/local/cvsroot/pgjdbc/pgjdbc/org/postgresql/jdbc2/AbstractJdbc2Connection.java,v retrieving revision 1.18 diff -u -c -r1.18 AbstractJdbc2Connection.java *** org/postgresql/jdbc2/AbstractJdbc2Connection.java 10 Oct 2004 15:39:40 -0000 1.18 --- org/postgresql/jdbc2/AbstractJdbc2Connection.java 12 Oct 2004 04:35:57 -0000 *************** *** 93,103 **** prepareThreshold = 0; try { prepareThreshold = Integer.parseInt(info.getProperty("prepareThreshold", "0")); } catch (Exception e) {} ! ! if (prepareThreshold < 0) ! prepareThreshold = 0; ! //Print out the driver version number if (Driver.logInfo) Driver.info(Driver.getVersion()); --- 93,102 ---- prepareThreshold = 0; try { prepareThreshold = Integer.parseInt(info.getProperty("prepareThreshold", "0")); + if (prepareThreshold < 0) + prepareThreshold = 0; } catch (Exception e) {} ! //Print out the driver version number if (Driver.logInfo) Driver.info(Driver.getVersion()); *************** *** 118,124 **** rollbackQuery = getQueryExecutor().createSimpleQuery("ROLLBACK"); // Initialize object handling ! initObjectTypes(); } /* --- 117,123 ---- rollbackQuery = getQueryExecutor().createSimpleQuery("ROLLBACK"); // Initialize object handling ! initObjectTypes(info); } /* *************** *** 417,444 **** // This holds the available types, a String to Class mapping. private final HashMap objectTypes = new HashMap(); - // This array contains the types that are supported as standard. - // - // The first entry is the types name on the database, the second - // the full class name of the handling class. - // - private static final Object[][] defaultObjectTypes = { - { "box", org.postgresql.geometric.PGbox.class }, - { "circle", org.postgresql.geometric.PGcircle.class }, - { "line", org.postgresql.geometric.PGline.class }, - { "lseg", org.postgresql.geometric.PGlseg.class }, - { "path", org.postgresql.geometric.PGpath.class }, - { "point", org.postgresql.geometric.PGpoint.class }, - { "polygon", org.postgresql.geometric.PGpolygon.class }, - { "money", org.postgresql.util.PGmoney.class }, - { "interval", org.postgresql.util.PGInterval.class } - }; - // This initialises the objectTypes hashtable ! private void initObjectTypes() throws SQLException { ! for (int i = 0; i < defaultObjectTypes.length; ++i) ! addDataType((String) defaultObjectTypes[i][0], (Class) defaultObjectTypes[i][1]); } /** --- 416,441 ---- // This holds the available types, a String to Class mapping. private final HashMap objectTypes = new HashMap(); // This initialises the objectTypes hashtable ! private void initObjectTypes(Properties info) throws SQLException { ! for (Enumeration e = info.propertyNames(); e.hasMoreElements(); ) { ! String propertyName = (String)e.nextElement(); ! if (propertyName.startsWith("datatype.")) { ! String typeName = propertyName.substring(9); ! String className = info.getProperty(propertyName); ! Class klass; ! ! try { ! klass = Class.forName(className); ! } catch (ClassNotFoundException cnfe) { ! throw new PSQLException(GT.tr("Unable to load the class {0} responsible for the datatype {1}", new Object[] { className, typeName }), ! PSQLState.SYSTEM_ERROR, cnfe); ! } ! ! addDataType(typeName, klass); ! } ! } } /** Index: build.xml =================================================================== RCS file: /usr/local/cvsroot/pgjdbc/pgjdbc/build.xml,v retrieving revision 1.52 diff -u -c -r1.52 build.xml *** build.xml 10 Oct 2004 15:39:29 -0000 1.52 --- build.xml 12 Oct 2004 04:35:57 -0000 *************** *** 105,110 **** --- 105,111 ---- + *** /dev/null Mon Feb 2 17:15:26 2004 --- postgresql.properties Tue Oct 12 17:08:52 2004 *************** *** 0 **** --- 1,16 ---- + # + # This property file is included in the driver jar and controls the default + # driver settings. These values may be overridden or added to by placing + # additional property files (also named 'postgresql.properties') in the + # driver's classpath. + # + prepareThreshold=5 + datatype.box=org.postgresql.geometric.PGbox + datatype.circle=org.postgresql.geometric.PGcircle + datatype.line=org.postgresql.geometric.PGline + datatype.lseg=org.postgresql.geometric.PGlseg + datatype.path=org.postgresql.geometric.PGpath + datatype.point=org.postgresql.geometric.PGpoint + datatype.polygon=org.postgresql.geometric.PGpolygon + datatype.money=org.postgresql.util.PGmoney + datatype.interval=org.postgresql.util.PGInterval --------------050909000309050706090808--