Author: dcosse Date: 2014-09-26 17:58:02 +0200 (Fri, 26 Sep 2014) New Revision: 3924 Url: http://forge.chorem.org/projects/lima/repository/revisions/3924 Log: refs #1115 refactoring sur l'initialisation des services Modified: trunk/lima-business/src/main/java/org/chorem/lima/business/LimaConfigurationHelper.java trunk/lima-business/src/main/java/org/chorem/lima/business/LimaInterceptor.java trunk/lima-business/src/main/java/org/chorem/lima/business/LimaServiceConfig.java trunk/lima-business/src/test/java/org/chorem/lima/business/FinancialPeriodServiceImplTest.java trunk/lima-business/src/test/java/org/chorem/lima/business/LimaTestsConfig.java trunk/lima-swing/src/main/java/org/chorem/lima/LimaSwingApplicationContext.java Modified: trunk/lima-business/src/main/java/org/chorem/lima/business/LimaConfigurationHelper.java =================================================================== --- trunk/lima-business/src/main/java/org/chorem/lima/business/LimaConfigurationHelper.java 2014-09-26 09:10:52 UTC (rev 3923) +++ trunk/lima-business/src/main/java/org/chorem/lima/business/LimaConfigurationHelper.java 2014-09-26 15:58:02 UTC (rev 3924) @@ -1,14 +1,8 @@ package org.chorem.lima.business; import com.google.common.base.Function; -import com.google.common.collect.Maps; -import org.chorem.lima.entity.LimaCallaoEntityEnum; import org.chorem.lima.entity.LimaCallaoTopiaApplicationContext; -import org.nuiton.topia.flyway.TopiaFlywayService; -import org.nuiton.topia.flyway.TopiaFlywayServiceImpl; -import org.nuiton.topia.persistence.TopiaConfigurationConstants; -import java.util.Map; import java.util.Properties; /** @@ -26,22 +20,4 @@ }; } - public static Properties getRootContextProperties(LimaServiceConfig serviceConfig) { - Properties result = serviceConfig.getFlatOptions(); - // add persistence classes from generated code - result.setProperty(TopiaConfigurationConstants.CONFIG_PERSISTENCE_CLASSES, LimaCallaoEntityEnum.getImplementationClassesAsString()); - - Map<String, String> toAddIfNotPresent = Maps.newLinkedHashMap(); - toAddIfNotPresent.put("topia.service.migration", TopiaFlywayServiceImpl.class.getName()); - toAddIfNotPresent.put("topia.service.migration." + TopiaFlywayService.USE_MODEL_VERSION, "true"); - toAddIfNotPresent.put(TopiaConfigurationConstants.CONFIG_PERSISTENCE_INIT_SCHEMA, "true"); - - for (Map.Entry<String, String> entry : toAddIfNotPresent.entrySet()) { - if (!result.containsKey(entry.getKey())) { - result.setProperty(entry.getKey(), entry.getValue()); - } - } - - return result; - } } Modified: trunk/lima-business/src/main/java/org/chorem/lima/business/LimaInterceptor.java =================================================================== --- trunk/lima-business/src/main/java/org/chorem/lima/business/LimaInterceptor.java 2014-09-26 09:10:52 UTC (rev 3923) +++ trunk/lima-business/src/main/java/org/chorem/lima/business/LimaInterceptor.java 2014-09-26 15:58:02 UTC (rev 3924) @@ -38,7 +38,6 @@ import javax.interceptor.InvocationContext; import javax.transaction.Transaction; import javax.transaction.TransactionManager; -import java.util.Properties; /** * Interceptor for topia context transaction. @@ -81,11 +80,10 @@ context.getTarget().getClass() + "#" + context.getMethod().getName()); } - LimaServiceConfig config = LimaServiceConfig.getInstance(); + LimaCallaoTopiaApplicationContext rootContext = TopiaApplicationContextCache.getContext( + LimaServiceConfig.getRootContextProperties(), + LimaConfigurationHelper.getCreateTopiaContextFunction()); - Properties contextProperties = LimaConfigurationHelper.getRootContextProperties(config); - LimaCallaoTopiaApplicationContext rootContext = TopiaApplicationContextCache.getContext(contextProperties, LimaConfigurationHelper.getCreateTopiaContextFunction()); - LimaCallaoTopiaPersistenceContext tx = rootContext.newPersistenceContext(); DAO_HELPER.set(tx); Modified: trunk/lima-business/src/main/java/org/chorem/lima/business/LimaServiceConfig.java =================================================================== --- trunk/lima-business/src/main/java/org/chorem/lima/business/LimaServiceConfig.java 2014-09-26 09:10:52 UTC (rev 3923) +++ trunk/lima-business/src/main/java/org/chorem/lima/business/LimaServiceConfig.java 2014-09-26 15:58:02 UTC (rev 3924) @@ -25,16 +25,22 @@ package org.chorem.lima.business; +import com.google.common.collect.Maps; import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.chorem.lima.LimaTechnicalException; import org.chorem.lima.business.accountingrules.FranceAccountingRules; +import org.chorem.lima.entity.LimaCallaoEntityEnum; import org.nuiton.config.ApplicationConfig; import org.nuiton.config.ArgumentsParserException; import org.nuiton.config.ConfigOptionDef; +import org.nuiton.topia.flyway.TopiaFlywayService; +import org.nuiton.topia.flyway.TopiaFlywayServiceImpl; +import org.nuiton.topia.persistence.TopiaConfigurationConstants; import java.io.File; +import java.util.Map; import java.util.Properties; import static org.nuiton.i18n.I18n.n; @@ -63,6 +69,8 @@ protected static volatile LimaServiceConfig instance; + protected static Properties rootContextProperties; + public LimaServiceConfig(String configFileName) { try { ApplicationConfig defaultConfig = new ApplicationConfig(LIMA_DEFAULT_CONF_FILENAME); @@ -80,14 +88,44 @@ } config = defaultConfig; } - instance = this; } catch (ArgumentsParserException ex) { throw new LimaTechnicalException("Can't read configuration", ex); } } + public static Properties getRootContextProperties() { + if (rootContextProperties == null) { + Properties result = instance.getFlatOptions(); + // add persistence classes from generated code + result.setProperty(TopiaConfigurationConstants.CONFIG_PERSISTENCE_CLASSES, LimaCallaoEntityEnum.getImplementationClassesAsString()); + + Map<String, String> toAddIfNotPresent = Maps.newLinkedHashMap(); + toAddIfNotPresent.put("topia.service.migration", TopiaFlywayServiceImpl.class.getName()); + toAddIfNotPresent.put("topia.service.migration." + TopiaFlywayService.USE_MODEL_VERSION, "true"); + toAddIfNotPresent.put(TopiaConfigurationConstants.CONFIG_PERSISTENCE_INIT_SCHEMA, "true"); + + for (Map.Entry<String, String> entry : toAddIfNotPresent.entrySet()) { + if (!result.containsKey(entry.getKey())) { + result.setProperty(entry.getKey(), entry.getValue()); + } + } + rootContextProperties = result; + } + return rootContextProperties; + } + + public static LimaServiceConfig getInstance(String configFileName) { + if (instance == null) { + instance= new LimaServiceConfig(configFileName); + } + return instance; + } + public static LimaServiceConfig getInstance() { + if (instance == null) { + instance= new LimaServiceConfig(null); + } return instance; } Modified: trunk/lima-business/src/test/java/org/chorem/lima/business/FinancialPeriodServiceImplTest.java =================================================================== --- trunk/lima-business/src/test/java/org/chorem/lima/business/FinancialPeriodServiceImplTest.java 2014-09-26 09:10:52 UTC (rev 3923) +++ trunk/lima-business/src/test/java/org/chorem/lima/business/FinancialPeriodServiceImplTest.java 2014-09-26 15:58:02 UTC (rev 3924) @@ -29,7 +29,6 @@ import org.chorem.lima.entity.ClosedPeriodicEntryBookTopiaDao; import org.chorem.lima.entity.LimaCallaoTopiaPersistenceContext; import org.junit.Assert; -import org.junit.Before; import org.junit.Test; import java.util.List; @@ -48,11 +47,6 @@ */ public class FinancialPeriodServiceImplTest extends AbstractLimaTest { - @Before - public void initTest() throws Exception { - //initTestDatabase(); - } - /** * Test de la fermeture d'une periode comptable pour un journal donné. * Modified: trunk/lima-business/src/test/java/org/chorem/lima/business/LimaTestsConfig.java =================================================================== --- trunk/lima-business/src/test/java/org/chorem/lima/business/LimaTestsConfig.java 2014-09-26 09:10:52 UTC (rev 3923) +++ trunk/lima-business/src/test/java/org/chorem/lima/business/LimaTestsConfig.java 2014-09-26 15:58:02 UTC (rev 3924) @@ -22,7 +22,12 @@ * #L% */ +import com.google.common.collect.Maps; +import org.chorem.lima.entity.LimaCallaoEntityEnum; import org.nuiton.config.ApplicationConfig; +import org.nuiton.topia.flyway.TopiaFlywayService; +import org.nuiton.topia.flyway.TopiaFlywayServiceImpl; +import org.nuiton.topia.persistence.TopiaConfigurationConstants; import java.util.Map; import java.util.Properties; @@ -41,6 +46,26 @@ } config = new ApplicationConfig(standardLimaConfig); instance = this; + setRootContextProperties(instance); } + public void setRootContextProperties(LimaServiceConfig instance) { + Properties result = instance.getFlatOptions(); + // add persistence classes from generated code + result.setProperty(TopiaConfigurationConstants.CONFIG_PERSISTENCE_CLASSES, LimaCallaoEntityEnum.getImplementationClassesAsString()); + + Map<String, String> toAddIfNotPresent = Maps.newLinkedHashMap(); + toAddIfNotPresent.put("topia.service.migration", TopiaFlywayServiceImpl.class.getName()); + toAddIfNotPresent.put("topia.service.migration." + TopiaFlywayService.USE_MODEL_VERSION, "true"); + toAddIfNotPresent.put(TopiaConfigurationConstants.CONFIG_PERSISTENCE_INIT_SCHEMA, "true"); + + for (Map.Entry<String, String> entry : toAddIfNotPresent.entrySet()) { + if (!result.containsKey(entry.getKey())) { + result.setProperty(entry.getKey(), entry.getValue()); + } + } + rootContextProperties = result; + + } + } Modified: trunk/lima-swing/src/main/java/org/chorem/lima/LimaSwingApplicationContext.java =================================================================== --- trunk/lima-swing/src/main/java/org/chorem/lima/LimaSwingApplicationContext.java 2014-09-26 09:10:52 UTC (rev 3923) +++ trunk/lima-swing/src/main/java/org/chorem/lima/LimaSwingApplicationContext.java 2014-09-26 15:58:02 UTC (rev 3924) @@ -90,8 +90,7 @@ } protected static void initApplicationContext() { - // TODO DCossé 25/09/14 revoir cette partie - new LimaServiceConfig(null); + LimaServiceConfig.getInstance(); } /**