r312 - trunk/wikitty-jdbc-impl/src/main/java/org/nuiton/wikitty/jdbc
Author: echatellier Date: 2010-09-15 13:32:41 +0200 (Wed, 15 Sep 2010) New Revision: 312 Url: http://nuiton.org/repositories/revision/wikitty/312 Log: Fix NPE when trying to load non existing wikity config properties file Modified: trunk/wikitty-jdbc-impl/src/main/java/org/nuiton/wikitty/jdbc/WikittyJDBCUtil.java Modified: trunk/wikitty-jdbc-impl/src/main/java/org/nuiton/wikitty/jdbc/WikittyJDBCUtil.java =================================================================== --- trunk/wikitty-jdbc-impl/src/main/java/org/nuiton/wikitty/jdbc/WikittyJDBCUtil.java 2010-09-15 10:14:23 UTC (rev 311) +++ trunk/wikitty-jdbc-impl/src/main/java/org/nuiton/wikitty/jdbc/WikittyJDBCUtil.java 2010-09-15 11:32:41 UTC (rev 312) @@ -39,6 +39,7 @@ import org.apache.commons.beanutils.BeanUtils; import org.apache.commons.dbcp.managed.BasicManagedDataSource; +import org.apache.commons.io.IOUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.nuiton.wikitty.WikittyException; @@ -157,13 +158,17 @@ /** - * Loads the properties in the jdbc.properties file. + * Loads the properties in the {@code wikitty-jdbc-config.properties} file. * + * @param properties custom properties to override default configuration * @return the properties for the connection and the queries */ public static synchronized Properties loadProperties(Properties properties) { Properties queryConfig = new Properties(); Properties databaseConfig = new Properties(queryConfig); + + InputStream streamQuery = null; + InputStream streamConfig = null; try { // FIXME poussin 20100112 perhaps used nuitonutil.ApplicationConfig @@ -173,10 +178,13 @@ ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); url = contextClassLoader.getResource("wikitty-jdbc-query.properties"); } - log.info("Reading resource from: " + url); - InputStream stream = url.openStream(); - queryConfig.load(stream); - stream.close(); + + if (log.isInfoEnabled()) { + log.info("Reading resource from: " + url); + } + // url can't be null + streamQuery = url.openStream(); + queryConfig.load(streamQuery); // config url = ClassLoader.getSystemResource("wikitty-jdbc-config.properties"); @@ -184,19 +192,32 @@ ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); url = contextClassLoader.getResource("wikitty-jdbc-config.properties"); } - log.info("Reading resource from: " + url); - stream = url.openStream(); - databaseConfig.load(stream); - stream.close(); + + if (url == null) { + if (log.isInfoEnabled()) { + log.info("No wikitty-jdbc-config.properties file found in classpath (skip default configuration loading)"); + } + } + else { + if (log.isInfoEnabled()) { + log.info("Reading resource from: " + url); + } + streamConfig = url.openStream(); + databaseConfig.load(streamConfig); + } // extra config if (properties != null) { databaseConfig.putAll(properties); } - } catch(IOException eee) { + } catch (IOException eee) { throw new WikittyException("Unable to load property file", eee); } + finally { + IOUtils.closeQuietly(streamQuery); + IOUtils.closeQuietly(streamConfig); + } return databaseConfig; }
participants (1)
-
echatellier@users.nuiton.org