Modified: james/server/trunk/util/src/main/java/org/apache/james/util/sql/SqlResources.java URL: http://svn.apache.org/viewvc/james/server/trunk/util/src/main/java/org/apache/james/util/sql/SqlResources.java?rev=1088767&r1=1088766&r2=1088767&view=diff ============================================================================== --- james/server/trunk/util/src/main/java/org/apache/james/util/sql/SqlResources.java (original) +++ james/server/trunk/util/src/main/java/org/apache/james/util/sql/SqlResources.java Mon Apr 4 20:05:20 2011 @@ -17,7 +17,6 @@ * under the License. * ****************************************************************/ - package org.apache.james.util.sql; import org.w3c.dom.Attr; @@ -37,53 +36,49 @@ import java.util.Iterator; import java.util.Map; import java.util.regex.Pattern; - /** - * Provides a set of SQL String resources (eg SQL Strings) - * to use for a database connection. - * This class allows SQL strings to be customised to particular - * database products, by detecting product information from the - * jdbc DatabaseMetaData object. + * Provides a set of SQL String resources (eg SQL Strings) to use for a database + * connection.<br> + * This class allows SQL strings to be customised to particular database + * products, by detecting product information from the jdbc DatabaseMetaData + * object. * */ -public class SqlResources -{ - /** - * A map of statement types to SQL statements - */ +public class SqlResources { + /** A map of statement types to SQL statements */ private Map<String, String> m_sql = new HashMap<String, String>(); - /** - * A map of engine specific options - */ + /** A map of engine specific options */ private Map<String, String> m_dbOptions = new HashMap<String, String>(); - /** - * A set of all used String values - */ + /** A set of all used String values */ static private Map<String, String> stringTable = java.util.Collections.synchronizedMap(new HashMap<String, String>()); /** + * <p> * Configures a DbResources object to provide SQL statements from a file. + * </p> + * <p> + * SQL statements returned may be specific to the particular type and + * version of the connected database, as well as the database driver. + * </p> + * <p> + * Parameters encoded as $(parameter} in the input file are replace by + * values from the parameters Map, if the named parameter exists. Parameter + * values may also be specified in the resourceSection element. + * </p> * - * SQL statements returned may be specific to the particular type - * and version of the connected database, as well as the database driver. - * - * Parameters encoded as $(parameter} in the input file are - * replace by values from the parameters Map, if the named parameter exists. - * Parameter values may also be specified in the resourceSection element. - * - * @param sqlFile the input file containing the string definitions + * @param sqlFile + * the input file containing the string definitions * @param sqlDefsSection - * the xml element containing the strings to be used - * @param conn the Jdbc DatabaseMetaData, taken from a database connection - * @param configParameters a map of parameters (name-value string pairs) which are - * replaced where found in the input strings + * the xml element containing the strings to be used + * @param conn + * the Jdbc DatabaseMetaData, taken from a database connection + * @param configParameters + * a map of parameters (name-value string pairs) which are + * replaced where found in the input strings */ - public void init(File sqlFile, String sqlDefsSection, - Connection conn, Map<String, String> configParameters) - throws Exception - { + public void init(File sqlFile, String sqlDefsSection, Connection conn, Map<String, String> configParameters) throws Exception { // Parse the sqlFile as an XML document. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); @@ -91,28 +86,33 @@ public class SqlResources init(sqlDoc, sqlDefsSection, conn, configParameters); } - + /** - * Configures a DbResources object to provide SQL statements from an InputStream. - * - * SQL statements returned may be specific to the particular type - * and version of the connected database, as well as the database driver. + * <p> + * Configures a DbResources object to provide SQL statements from an + * InputStream. + * </p> + * <p> + * SQL statements returned may be specific to the particular type and + * version of the connected database, as well as the database driver. + * </p> + * <p> + * Parameters encoded as $(parameter} in the input file are replace by + * values from the parameters Map, if the named parameter exists. Parameter + * values may also be specified in the resourceSection element. + * </p> * - * Parameters encoded as $(parameter} in the input file are - * replace by values from the parameters Map, if the named parameter exists. - * Parameter values may also be specified in the resourceSection element. - * - * @param input the input stream containing the xml + * @param input + * the input stream containing the xml * @param sqlDefsSection - * the xml element containing the strings to be used - * @param conn the Jdbc DatabaseMetaData, taken from a database connection - * @param configParameters a map of parameters (name-value string pairs) which are - * replaced where found in the input strings + * the xml element containing the strings to be used + * @param conn + * the Jdbc DatabaseMetaData, taken from a database connection + * @param configParameters + * a map of parameters (name-value string pairs) which are + * replaced where found in the input strings */ - public void init(InputStream input, String sqlDefsSection, - Connection conn, Map<String, String> configParameters) - throws Exception - { + public void init(InputStream input, String sqlDefsSection, Connection conn, Map<String, String> configParameters) throws Exception { // Parse the InputStream as an XML document. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); @@ -120,7 +120,6 @@ public class SqlResources init(sqlDoc, sqlDefsSection, conn, configParameters); } - /** * Configures a SqlResources object from an xml document. @@ -131,65 +130,55 @@ public class SqlResources * @param configParameters * @throws SQLException */ - protected void init(Document sqlDoc, String sqlDefsSection, - Connection conn, Map<String, String> configParameters) throws SQLException { - // First process the database matcher, to determine the - // sql statements to use. - Element dbMatcherElement = - (Element)(sqlDoc.getElementsByTagName("dbMatchers").item(0)); + protected void init(Document sqlDoc, String sqlDefsSection, Connection conn, Map<String, String> configParameters) throws SQLException { + // First process the database matcher, to determine the + // sql statements to use. + Element dbMatcherElement = (Element) (sqlDoc.getElementsByTagName("dbMatchers").item(0)); String dbProduct = null; - if ( dbMatcherElement != null ) { + if (dbMatcherElement != null) { dbProduct = matchDbConnection(conn, dbMatcherElement); } // Now get the options valid for the database product used. - Element dbOptionsElement = - (Element)(sqlDoc.getElementsByTagName("dbOptions").item(0)); - if ( dbOptionsElement != null ) { - // First populate the map with default values + Element dbOptionsElement = (Element) (sqlDoc.getElementsByTagName("dbOptions").item(0)); + if (dbOptionsElement != null) { + // First populate the map with default values populateDbOptions("", dbOptionsElement, m_dbOptions); // Now update the map with specific product values - if ( dbProduct != null ) { + if (dbProduct != null) { populateDbOptions(dbProduct, dbOptionsElement, m_dbOptions); } } - // Now get the section defining sql for the repository required. NodeList sections = sqlDoc.getElementsByTagName("sqlDefs"); int sectionsCount = sections.getLength(); Element sectionElement = null; boolean found = false; - for (int i = 0; i < sectionsCount; i++ ) { - sectionElement = (Element)(sections.item(i)); + for (int i = 0; i < sectionsCount; i++) { + sectionElement = (Element) (sections.item(i)); String sectionName = sectionElement.getAttribute("name"); - if ( sectionName != null && sectionName.equals(sqlDefsSection) ) { + if (sectionName != null && sectionName.equals(sqlDefsSection)) { found = true; break; } } - if ( !found ) { - StringBuilder exceptionBuffer = - new StringBuilder(64) - .append("Error loading sql definition file. ") - .append("The element named \'") - .append(sqlDefsSection) - .append("\' does not exist."); + if (!found) { + StringBuilder exceptionBuffer = new StringBuilder(64).append("Error loading sql definition file. ").append("The element named \'").append(sqlDefsSection).append("\' does not exist."); throw new RuntimeException(exceptionBuffer.toString()); } // Get parameters defined within the file as defaults, // and use supplied parameters as overrides. - Map<String,String> parameters = new HashMap<String, String>(); + Map<String, String> parameters = new HashMap<String, String>(); // First read from the <params> element, if it exists. - Element parametersElement = - (Element)(sectionElement.getElementsByTagName("parameters").item(0)); - if ( parametersElement != null ) { + Element parametersElement = (Element) (sectionElement.getElementsByTagName("parameters").item(0)); + if (parametersElement != null) { NamedNodeMap params = parametersElement.getAttributes(); int paramCount = params.getLength(); - for (int i = 0; i < paramCount; i++ ) { - Attr param = (Attr)params.item(i); + for (int i = 0; i < paramCount; i++) { + Attr param = (Attr) params.item(i); String paramName = param.getName(); String paramValue = param.getValue(); parameters.put(paramName, paramValue); @@ -199,7 +188,7 @@ public class SqlResources parameters.putAll(configParameters); // 2 maps - one for storing default statements, - // the other for statements with a "db" attribute matching this + // the other for statements with a "db" attribute matching this // connection. Map<String, String> defaultSqlStatements = new HashMap<String, String>(); Map<String, String> dbProductSqlStatements = new HashMap<String, String>(); @@ -208,27 +197,26 @@ public class SqlResources // and adding to the appropriate map.. NodeList sqlDefs = sectionElement.getElementsByTagName("sql"); int sqlCount = sqlDefs.getLength(); - for ( int i = 0; i < sqlCount; i++ ) { - // See if this needs to be processed (is default or product specific) - Element sqlElement = (Element)(sqlDefs.item(i)); + for (int i = 0; i < sqlCount; i++) { + // See if this needs to be processed (is default or product + // specific) + Element sqlElement = (Element) (sqlDefs.item(i)); String sqlDb = sqlElement.getAttribute("db"); Map<String, String> sqlMap; - if ( sqlDb.equals("")) { + if (sqlDb.equals("")) { // default sqlMap = defaultSqlStatements; - } - else if (sqlDb.equals(dbProduct) ) { + } else if (sqlDb.equals(dbProduct)) { // Specific to this product sqlMap = dbProductSqlStatements; - } - else { + } else { // for a different product continue; } // Get the key and value for this SQL statement. String sqlKey = sqlElement.getAttribute("name"); - if ( sqlKey == null ) { + if (sqlKey == null) { // ignore statements without a "name" attribute. continue; } @@ -237,12 +225,13 @@ public class SqlResources // Do parameter replacements for this sql string. Iterator<String> paramNames = parameters.keySet().iterator(); StringBuilder replaceBuffer = new StringBuilder(64); - while ( paramNames.hasNext() ) { - String paramName = (String)paramNames.next(); - String paramValue = (String)parameters.get(paramName); + while (paramNames.hasNext()) { + String paramName = (String) paramNames.next(); + String paramValue = (String) parameters.get(paramName); replaceBuffer.append("${").append(paramName).append("}"); sqlString = substituteSubString(sqlString, replaceBuffer.toString(), paramValue); - if (paramNames.hasNext()) replaceBuffer.setLength(0); + if (paramNames.hasNext()) + replaceBuffer.setLength(0); } // See if we already have registered a string of this value @@ -261,38 +250,36 @@ public class SqlResources // Copy in default strings, then overwrite product-specific ones. m_sql.putAll(defaultSqlStatements); m_sql.putAll(dbProductSqlStatements); - } + } /** - * Compares the DatabaseProductName value for a jdbc Connection - * against a set of regular expressions defined in XML. + * Compares the DatabaseProductName value for a jdbc Connection against a + * set of regular expressions defined in XML.<br> * The first successful match defines the name of the database product - * connected to. This value is then used to choose the specific SQL + * connected to. This value is then used to choose the specific SQL * expressions to use. - * - * @param conn the JDBC connection being tested - * @param dbMatchersElement the XML element containing the database type information - * + * + * @param conn + * the JDBC connection being tested + * @param dbMatchersElement + * the XML element containing the database type information + * * @return the type of database to which James is connected - * + * */ - private String matchDbConnection(Connection conn, - Element dbMatchersElement) - throws SQLException - { + private String matchDbConnection(Connection conn, Element dbMatchersElement) throws SQLException { String dbProductName = conn.getMetaData().getDatabaseProductName(); - - NodeList dbMatchers = - dbMatchersElement.getElementsByTagName("dbMatcher"); - for ( int i = 0; i < dbMatchers.getLength(); i++ ) { + + NodeList dbMatchers = dbMatchersElement.getElementsByTagName("dbMatcher"); + for (int i = 0; i < dbMatchers.getLength(); i++) { // Get the values for this matcher element. - Element dbMatcher = (Element)dbMatchers.item(i); + Element dbMatcher = (Element) dbMatchers.item(i); String dbMatchName = dbMatcher.getAttribute("db"); Pattern dbProductPattern = Pattern.compile(dbMatcher.getAttribute("databaseProductName"), Pattern.CASE_INSENSITIVE); // If the connection databaseProcuctName matches the pattern, // use the match name from this matcher. - if ( dbProductPattern.matcher(dbProductName).find() ) { + if (dbProductPattern.matcher(dbProductName).find()) { return dbMatchName; } } @@ -302,19 +289,20 @@ public class SqlResources /** * Gets all the name/value pair db option couples related to the dbProduct, * and put them into the dbOptionsMap. - * - * @param dbProduct the db product used - * @param dbOptionsElement the XML element containing the options - * @param dbOptionsMap the <CODE>Map</CODE> to populate - * + * + * @param dbProduct + * the db product used + * @param dbOptionsElement + * the XML element containing the options + * @param dbOptionsMap + * the <code>Map</code> to populate + * */ - private void populateDbOptions(String dbProduct, Element dbOptionsElement, Map<String, String> dbOptionsMap) - { - NodeList dbOptions = - dbOptionsElement.getElementsByTagName("dbOption"); - for ( int i = 0; i < dbOptions.getLength(); i++ ) { + private void populateDbOptions(String dbProduct, Element dbOptionsElement, Map<String, String> dbOptionsMap) { + NodeList dbOptions = dbOptionsElement.getElementsByTagName("dbOption"); + for (int i = 0; i < dbOptions.getLength(); i++) { // Get the values for this option element. - Element dbOption = (Element)dbOptions.item(i); + Element dbOption = (Element) dbOptions.item(i); // Check is this element is pertinent to the dbProduct // Notice that a missing attribute returns "", good for defaults if (!dbProduct.equalsIgnoreCase(dbOption.getAttribute("db"))) { @@ -326,16 +314,18 @@ public class SqlResources } /** - * Replace substrings of one string with another string and return altered string. - * @param input input string - * @param find the string to replace - * @param replace the string to replace with + * Replace substrings of one string with another string and return altered + * string. + * + * @param input + * input string + * @param find + * the string to replace + * @param replace + * the string to replace with * @return the substituted string */ - private String substituteSubString( String input, - String find, - String replace ) - { + private String substituteSubString(String input, String find, String replace) { int find_length = find.length(); int replace_length = replace.length(); @@ -343,7 +333,7 @@ public class SqlResources int index = input.indexOf(find); int outputOffset = 0; - while ( index > -1 ) { + while (index > -1) { output.replace(index + outputOffset, index + outputOffset + find_length, replace); outputOffset = outputOffset + (replace_length - find_length); @@ -355,50 +345,49 @@ public class SqlResources } /** - * Returns a named SQL string for the specified connection, - * replacing parameters with the values set. + * Returns a named SQL string for the specified connection, replacing + * parameters with the values set. * - * @param name the name of the SQL resource required. + * @param name + * the name of the SQL resource required. * @return the requested resource */ - public String getSqlString(String name) - { - return (String)m_sql.get(name); + public String getSqlString(String name) { + return (String) m_sql.get(name); } /** - * Returns a named SQL string for the specified connection, - * replacing parameters with the values set. - * throws ConfigurationException if a required resource cannot be found. + * Returns a named SQL string for the specified connection, replacing + * parameters with the values set. + * + * @throws RuntimeException + * if a required resource cannot be found. * - * @param name the name of the SQL resource required. - * @param required true if the resource is required + * @param name + * the name of the SQL resource required. + * @param required + * true if the resource is required * @return the requested resource */ - public String getSqlString(String name, boolean required) - { + public String getSqlString(String name, boolean required) { String sql = getSqlString(name); if (sql == null && required) { - StringBuilder exceptionBuffer = - new StringBuilder(64) - .append("Required SQL resource: '") - .append(name) - .append("' was not found."); + StringBuilder exceptionBuffer = new StringBuilder(64).append("Required SQL resource: '").append(name).append("' was not found."); throw new RuntimeException(exceptionBuffer.toString()); } return sql; } - + /** * Returns the dbOption string value set for the specified dbOption name. * - * @param name the name of the dbOption required. + * @param name + * the name of the dbOption required. * @return the requested dbOption value */ - public String getDbOption(String name) - { - return (String)m_dbOptions.get(name); + public String getDbOption(String name) { + return (String) m_dbOptions.get(name); } }
--------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
