From: <fg...@us...> - 2007-02-25 20:40:28
|
Revision: 306 http://svn.sourceforge.net/openutils/?rev=306&view=rev Author: fgiust Date: 2007-02-25 12:40:29 -0800 (Sun, 25 Feb 2007) Log Message: ----------- initial import of db configurer Added Paths: ----------- trunk/openutils-deployment/src/main/java/it/openutils/deployment/spring/DatabaseEnvironmentPropertyConfigurer.java Added: trunk/openutils-deployment/src/main/java/it/openutils/deployment/spring/DatabaseEnvironmentPropertyConfigurer.java =================================================================== --- trunk/openutils-deployment/src/main/java/it/openutils/deployment/spring/DatabaseEnvironmentPropertyConfigurer.java (rev 0) +++ trunk/openutils-deployment/src/main/java/it/openutils/deployment/spring/DatabaseEnvironmentPropertyConfigurer.java 2007-02-25 20:40:29 UTC (rev 306) @@ -0,0 +1,168 @@ +package it.openutils.deployment.spring; + +import java.io.IOException; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Properties; + +import javax.sql.DataSource; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.BeanInitializationException; +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; +import org.springframework.context.ApplicationEvent; +import org.springframework.context.ApplicationListener; +import org.springframework.dao.DataAccessException; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.RowCallbackHandler; + + +/** + * @author fgiust + * @version $Id$ + */ +public class DatabaseEnvironmentPropertyConfigurer extends EnvironmentPropertyConfigurer + implements + ApplicationContextAware, + ApplicationListener +{ + + private static Logger log = LoggerFactory.getLogger(DatabaseEnvironmentPropertyConfigurer.class); + + private String sqlQuery; + + private String dataSourceName; + + private DataSource dataSource; + + private Properties props; + + private ApplicationContext applicationContext; + + public void setSqlQuery(String sqlQuery) + { + this.sqlQuery = sqlQuery; + } + + public void setDataSourceName(String dataSourceName) + { + this.dataSourceName = dataSourceName; + } + + /** + * {@inheritDoc} + */ + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException + { + this.applicationContext = applicationContext; + } + + /** + * Get Spring Context proprieties. + * @return A properties object containing all spring properties. + * @throws IOException Any IOException. + */ + public Properties getProperties() throws IOException + { + props = mergeProperties(); + refresh(); + return props; + } + + /** + * used to reload configuration code from db + */ + public void refresh() + { + manuallyLoadDatasource(); + + /** + * inner utility class to create properties from rows extracted by the query + * @author diego + * @version $Id$ + */ + class RowHandler implements RowCallbackHandler + { + + /** + * {@inheritDoc} + */ + public void processRow(final ResultSet rs) throws SQLException + { + String parmName = rs.getString(1); + String parmValue = rs.getString(2); + + log.debug("Configuring property {}={}", parmName, parmValue); + props.put(parmName, parmValue); + + } + } + + JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); + try + { + jdbcTemplate.query(sqlQuery, new RowHandler()); + } + catch (DataAccessException e) + { + log.error(e.getMessage()); + } + } + + /** + * {@inheritDoc} + */ + public void onApplicationEvent(ApplicationEvent event) + { + manuallyLoadDatasource(); + } + + /** + * + */ + private void manuallyLoadDatasource() + { + if (dataSource == null) + { + dataSource = (DataSource) applicationContext.getBean(dataSourceName); + try + { + props = mergeProperties(); + } + catch (IOException e) + { + log.debug("Exception while loading environment properties from file."); + } + } + } + + /** + * {@inheritDoc} + */ + @Override + public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException + { + + super.postProcessBeanFactory(beanFactory); + + try + { + Properties mergedProps = getProperties(); + + // Convert the merged properties, if necessary. + convertProperties(mergedProps); + + // Let the subclass process the properties. + processProperties(beanFactory, mergedProps); + } + catch (IOException ex) + { + throw new BeanInitializationException("Could not load properties", ex); + } + + } +} Property changes on: trunk/openutils-deployment/src/main/java/it/openutils/deployment/spring/DatabaseEnvironmentPropertyConfigurer.java ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:keywords + Author Date Id Revision Name: svn:eol-style + native This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fc...@us...> - 2008-02-14 16:02:40
|
Revision: 616 http://openutils.svn.sourceforge.net/openutils/?rev=616&view=rev Author: fcarone Date: 2008-02-14 08:02:44 -0800 (Thu, 14 Feb 2008) Log Message: ----------- missing override added Modified Paths: -------------- trunk/openutils-deployment/src/main/java/it/openutils/deployment/spring/DatabaseEnvironmentPropertyConfigurer.java Modified: trunk/openutils-deployment/src/main/java/it/openutils/deployment/spring/DatabaseEnvironmentPropertyConfigurer.java =================================================================== --- trunk/openutils-deployment/src/main/java/it/openutils/deployment/spring/DatabaseEnvironmentPropertyConfigurer.java 2008-02-12 16:57:54 UTC (rev 615) +++ trunk/openutils-deployment/src/main/java/it/openutils/deployment/spring/DatabaseEnvironmentPropertyConfigurer.java 2008-02-14 16:02:44 UTC (rev 616) @@ -69,6 +69,7 @@ /** * {@inheritDoc} */ + @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |