Author: acheype Date: 2013-02-15 08:33:46 +0100 (Fri, 15 Feb 2013) New Revision: 114 Url: http://forge.codelutin.com/projects/cantharella/repository/revisions/114 Log: replace the placeholder xml configuration with a java based one. No more dataContext.xml and serviceContext.xml ! Removed: trunk/cantharella.service/src/main/resources/serviceContext.xml trunk/cantharella.web/src/main/resources/webContext.xml Modified: trunk/cantharella.data/src/main/java/nc/ird/cantharella/data/config/DataContext.java trunk/cantharella.data/src/main/resources/dataContext.xml trunk/cantharella.service/src/main/java/nc/ird/cantharella/service/config/ServiceContext.java trunk/cantharella.web/src/main/java/nc/ird/cantharella/web/config/WebContext.java Modified: trunk/cantharella.data/src/main/java/nc/ird/cantharella/data/config/DataContext.java =================================================================== --- trunk/cantharella.data/src/main/java/nc/ird/cantharella/data/config/DataContext.java 2013-02-15 06:40:39 UTC (rev 113) +++ trunk/cantharella.data/src/main/java/nc/ird/cantharella/data/config/DataContext.java 2013-02-15 07:33:46 UTC (rev 114) @@ -61,97 +61,120 @@ import com.mchange.v2.c3p0.ComboPooledDataSource; import com.mchange.v2.c3p0.impl.DefaultConnectionTester; +import java.util.logging.Level; +import javax.annotation.Resource; +import nc.ird.module.utils.CantharellaConfig; +import org.nuiton.util.ArgumentsParserException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; /** * Spring context for the data layer * <p> * DB_* properties can be placed in the properties file, if they vary depending on the environment. * </p> + * * @author Mickael Tricot * @author Adrien Cheype */ // Import the spring context file @ImportResource(value = "classpath:/dataContext.xml") // Scans for @Repository, @Service and @Component -@ComponentScan(basePackages = { "nc.ird.cantharella.data.dao", "nc.ird.cantharella.data.validation.utils" }) +@ComponentScan(basePackages = {"nc.ird.cantharella.data.dao", "nc.ird.cantharella.data.validation.utils"}) // Enable @Transactional support -> not work with <aop:aspectj-autoproxy /> in xml context file @EnableTransactionManagement // This is a configuration class @Configuration public abstract class DataContext { - /** Logger */ + /** + * Logger + */ private static final Logger LOG = LoggerFactory.getLogger(DataContext.class); - - /** Country codes */ + /** + * Country codes + */ public static final List<String> COUNTRY_CODES = Collections.unmodifiableList(Arrays.asList(Locale .getISOCountries())); - - /** DB connection: acquire retry attemps */ + /** + * DB connection: acquire retry attemps + */ private static final int DB_CONNECTION_ACQUIRE_RETRY_ATTEMPS = 30; - - /** DB connexion: acquire retry delay */ + /** + * DB connexion: acquire retry delay + */ private static final int DB_CONNECTION_ACQUIRE_RETRY_DELAY = 1000; - - /** DB pool: acquire increment */ + /** + * DB pool: acquire increment + */ private static final int DB_POOL_ACQUIRE_INCREMENT = 3; - - /** DB pool: max size */ + /** + * DB pool: max size + */ private static final int DB_POOL_MAX_SIZE = 15; - - /** DB pool: min size */ + /** + * DB pool: min size + */ private static final int DB_POOL_MIN_SIZE = 3; - - /** Encoding */ + /** + * Encoding + */ public static final String ENCODING = Charset.forName("UTF-8").name(); - - /** Availables locales for the application */ + /** + * Availables locales for the application + */ public static final List<Locale> LOCALES; - - /** Precision is the total number of digits */ + /** + * Precision is the total number of digits + */ public static final int DECIMAL_PRECISION = 9; - /** * Scale is the number of digits to the right of the decimal point in a number. For example, the number 123.45 has a * precision of 5 and a scale of 2 - **/ + * + */ public static final int DECIMAL_SCALE = 4; - /** * According to DECIMAL_PRECISION and DECIMAL_SCALE, number max allowed for decimals. Attention to ensure that is * equal to 10^(DataContext.DECIMAL_PRECISION - DataContext.DECIMAL_SCALE) -1 */ public static final int DECIMAL_MAX = 99999; - - /** Référentiels */ + /** + * Référentiels + */ public static final Map<Integer, String> REFERENTIELS; - - /** DB debug */ + /** + * DB debug + */ @Value("${db.debug}") protected boolean dbDebugProperty; - - /** DB password */ + /** + * DB password + */ @Value("${db.password}") protected String dbPasswordProperty; - - /** DB URL */ + /** + * DB URL + */ @Value("${db.url}") protected String dbUrlProperty; - - /** DB user */ + /** + * DB user + */ @Value("${db.user}") protected String dbUserProperty; - - /** DB user */ + /** + * DB user + */ @Value("${db.hbm2ddl:validate}") protected String hbm2ddl; - + static { // The first Locale in the list is the default one List<Locale> locales = new ArrayList<Locale>(); locales.add(Locale.FRENCH); locales.add(Locale.ENGLISH); - + LOCALES = Collections.unmodifiableList(locales); Locale.setDefault(LOCALES.get(0)); @@ -234,7 +257,7 @@ public SessionFactory sessionFactory() { LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean(); sessionFactoryBean.setDataSource(dataSource()); - sessionFactoryBean.setPackagesToScan(new String[] { "nc.ird.cantharella.data.model" }); + sessionFactoryBean.setPackagesToScan(new String[]{"nc.ird.cantharella.data.model"}); Properties hibernateProperties = new Properties(); // Hibernate: basic hibernateProperties.setProperty(Environment.DIALECT, PostgreSQL82Dialect.class.getName()); @@ -318,4 +341,21 @@ return new ModelValidatorImpl(validatorFactory(), dataMessageSource()); } + /** + * Set the data layer properties for the cantharella configuration + * @return The placeholder configurer which get the data layer properties + */ + @Bean + public static PropertySourcesPlaceholderConfigurer properties() { + PropertySourcesPlaceholderConfigurer pspc = + new PropertySourcesPlaceholderConfigurer(); + try { + pspc.setProperties(CantharellaConfig.getProperties()); + } catch (ArgumentsParserException e) { + LOG.error(e.getMessage(), e); + throw new UnexpectedException(e); + } + pspc.setIgnoreUnresolvablePlaceholders(true); + return pspc; + } } Modified: trunk/cantharella.data/src/main/resources/dataContext.xml =================================================================== --- trunk/cantharella.data/src/main/resources/dataContext.xml 2013-02-15 06:40:39 UTC (rev 113) +++ trunk/cantharella.data/src/main/resources/dataContext.xml 2013-02-15 07:33:46 UTC (rev 114) @@ -37,15 +37,6 @@ http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> - <bean id="propertiesConfig" - class="nc.ird.module.utils.CantharellaConfig" factory-method="getProperties"/> - - <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> - <property name="properties" ref="propertiesConfig" /> - <!-- set on except on the last file to allow multiple files to load --> - <property name="ignoreUnresolvablePlaceholders" value="true"/> - </bean> - <!-- Enable @Transactional support --> <tx:annotation-driven/> Modified: trunk/cantharella.service/src/main/java/nc/ird/cantharella/service/config/ServiceContext.java =================================================================== --- trunk/cantharella.service/src/main/java/nc/ird/cantharella/service/config/ServiceContext.java 2013-02-15 06:40:39 UTC (rev 113) +++ trunk/cantharella.service/src/main/java/nc/ird/cantharella/service/config/ServiceContext.java 2013-02-15 07:33:46 UTC (rev 114) @@ -25,11 +25,16 @@ import java.util.Properties; import nc.ird.cantharella.data.config.DataContext; +import nc.ird.cantharella.data.exceptions.UnexpectedException; import nc.ird.cantharella.data.model.Utilisateur; import nc.ird.cantharella.data.model.Utilisateur.TypeDroit; import nc.ird.cantharella.service.utils.normalizers.PersonneNormalizer; import nc.ird.cantharella.service.utils.normalizers.utils.Normalizer; +import nc.ird.module.utils.CantharellaConfig; import nc.ird.module.utils.PasswordTools; +import org.nuiton.util.ArgumentsParserException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; @@ -38,6 +43,7 @@ import org.springframework.context.annotation.Import; import org.springframework.context.annotation.ImportResource; import org.springframework.context.support.MessageSourceAccessor; +import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; import org.springframework.context.support.ReloadableResourceBundleMessageSource; import org.springframework.mail.MailSender; import org.springframework.mail.SimpleMailMessage; @@ -50,8 +56,6 @@ */ // Load the data-layer configuration @Import(DataContext.class) -// Import the spring context file -@ImportResource(value = "classpath:/serviceContext.xml") // Scans for @Repository, @Service and @Component @ComponentScan(basePackages = { "nc.ird.cantharella.service.services", "nc.ird.cantharella.service.utils" }) // Enable @Transactional support -> not work with <aop:aspectj-autoproxy /> in xml context file @@ -60,6 +64,11 @@ @Configuration public abstract class ServiceContext { + /** + * Logger + */ + private static final Logger LOG = LoggerFactory.getLogger(ServiceContext.class); + /** Mail SMTP: port */ private static final int MAIL_SMTP_PORT = 25; @@ -166,4 +175,22 @@ serviceMessageSource.setCacheSeconds(-1); return new MessageSourceAccessor(serviceMessageSource); } + + /** + * Set the service layer properties for the cantharella configuration + * @return The placeholder configurer which get the service layer properties + */ + @Bean + public static PropertySourcesPlaceholderConfigurer properties() { + PropertySourcesPlaceholderConfigurer pspc = + new PropertySourcesPlaceholderConfigurer(); + try { + pspc.setProperties(CantharellaConfig.getProperties()); + } catch (ArgumentsParserException e) { + LOG.error(e.getMessage(), e); + throw new UnexpectedException(e); + } + pspc.setIgnoreUnresolvablePlaceholders(true); + return pspc; + } } Deleted: trunk/cantharella.service/src/main/resources/serviceContext.xml =================================================================== --- trunk/cantharella.service/src/main/resources/serviceContext.xml 2013-02-15 06:40:39 UTC (rev 113) +++ trunk/cantharella.service/src/main/resources/serviceContext.xml 2013-02-15 07:33:46 UTC (rev 114) @@ -1,49 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<!-- - #%L - Cantharella :: Service - $Id:$ - $HeadURL:$ - %% - Copyright (C) 2009 - 2012 IRD (Institut de Recherche pour le Developpement) and by respective authors (see below) - %% - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see <http://www.gnu.org/licenses/>. - #L% - --> - -<beans xmlns="http://www.springframework.org/schema/beans" - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xmlns:aop="http://www.springframework.org/schema/aop" - xmlns:tx="http://www.springframework.org/schema/tx" - xmlns:context="http://www.springframework.org/schema/context" - xmlns:p="http://www.springframework.org/schema/p" - xsi:schemaLocation="http://www.springframework.org/schema/beans - http://www.springframework.org/schema/beans/spring-beans-3.0.xsd - http://www.springframework.org/schema/tx - http://www.springframework.org/schema/tx/spring-tx-3.0.xsd - http://www.springframework.org/schema/aop - http://www.springframework.org/schema/aop/spring-aop-3.0.xsd - http://www.springframework.org/schema/context - http://www.springframework.org/schema/context/spring-context-2.5.xsd"> - - <bean id="propertiesConfig" - class="nc.ird.module.utils.CantharellaConfig" factory-method="getProperties"/> - - <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> - <property name="properties" ref="propertiesConfig" /> - <!-- set on except on the last file to allow multiple files to load --> - <property name="ignoreUnresolvablePlaceholders" value="true"/> - </bean> - -</beans> \ No newline at end of file Modified: trunk/cantharella.web/src/main/java/nc/ird/cantharella/web/config/WebContext.java =================================================================== --- trunk/cantharella.web/src/main/java/nc/ird/cantharella/web/config/WebContext.java 2013-02-15 06:40:39 UTC (rev 113) +++ trunk/cantharella.web/src/main/java/nc/ird/cantharella/web/config/WebContext.java 2013-02-15 07:33:46 UTC (rev 114) @@ -37,10 +37,12 @@ import javax.validation.ValidatorFactory; import nc.ird.cantharella.data.config.DataContext; +import nc.ird.cantharella.data.exceptions.UnexpectedException; import nc.ird.cantharella.data.validation.utils.ModelValidator; import nc.ird.cantharella.data.validation.utils.ModelValidatorImpl; import nc.ird.cantharella.service.config.ServiceContext; import nc.ird.cantharella.web.utils.resources.WebMessages; +import nc.ird.module.utils.CantharellaConfig; import nc.ird.module.utils.Pair; import nc.ird.module.utils.StringTools; @@ -48,12 +50,16 @@ import org.apache.wicket.protocol.http.WebApplication; import org.apache.wicket.util.lang.Bytes; import org.apache.wicket.util.time.Duration; +import org.nuiton.util.ArgumentsParserException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.context.annotation.ImportResource; import org.springframework.context.support.MessageSourceAccessor; +import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; import org.springframework.context.support.ReloadableResourceBundleMessageSource; import org.springframework.util.Log4jConfigurer; @@ -65,13 +71,16 @@ */ // Load the service-layer configuration @Import(ServiceContext.class) -// Import the spring context file -@ImportResource(value = "classpath:/webContext.xml") // This is a configuration class @Configuration public abstract class WebContext { /** + * Logger + */ + private static final Logger LOG = LoggerFactory.getLogger(WebContext.class); + + /** * Cookie key for authentification */ public static final String AUTH_COOKIE_KEY = "LoggedIn"; @@ -224,9 +233,27 @@ } /** + * Set the web layer properties for the cantharella configuration + * @return The placeholder configurer which get the web layer properties + */ + @Bean + public static PropertySourcesPlaceholderConfigurer properties() { + PropertySourcesPlaceholderConfigurer pspc = + new PropertySourcesPlaceholderConfigurer(); + try { + pspc.setProperties(CantharellaConfig.getProperties()); + } catch (ArgumentsParserException e) { + LOG.error(e.getMessage(), e); + throw new UnexpectedException(e); + } + pspc.setIgnoreUnresolvablePlaceholders(true); + return pspc; + } + + /** * Init the log4j configuration filepath * - * @throws FileNotFoundException + * @throws FileNotFoundException If the config file is not found */ @PostConstruct public void initLog4j() throws FileNotFoundException { Deleted: trunk/cantharella.web/src/main/resources/webContext.xml =================================================================== --- trunk/cantharella.web/src/main/resources/webContext.xml 2013-02-15 06:40:39 UTC (rev 113) +++ trunk/cantharella.web/src/main/resources/webContext.xml 2013-02-15 07:33:46 UTC (rev 114) @@ -1,49 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<!-- - #%L - Cantharella :: Web - $Id:$ - $HeadURL:$ - %% - Copyright (C) 2009 - 2012 IRD (Institut de Recherche pour le Developpement) and by respective authors (see below) - %% - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see <http://www.gnu.org/licenses/>. - #L% - --> - -<beans xmlns="http://www.springframework.org/schema/beans" - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xmlns:aop="http://www.springframework.org/schema/aop" - xmlns:tx="http://www.springframework.org/schema/tx" - xmlns:context="http://www.springframework.org/schema/context" - xmlns:p="http://www.springframework.org/schema/p" - xsi:schemaLocation="http://www.springframework.org/schema/beans - http://www.springframework.org/schema/beans/spring-beans-3.0.xsd - http://www.springframework.org/schema/tx - http://www.springframework.org/schema/tx/spring-tx-3.0.xsd - http://www.springframework.org/schema/aop - http://www.springframework.org/schema/aop/spring-aop-3.0.xsd - http://www.springframework.org/schema/context - http://www.springframework.org/schema/context/spring-context-2.5.xsd"> - - <bean id="propertiesConfig" - class="nc.ird.module.utils.CantharellaConfig" factory-method="getProperties"/> - - <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> - <property name="properties" ref="propertiesConfig" /> - <!-- set on except on the last file to allow multiple files to load --> - <property name="ignoreUnresolvablePlaceholders" value="true"/> - </bean> - -</beans> \ No newline at end of file