This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository coselmar. See http://git.codelutin.com/coselmar.git commit a29f12f56c49019ebe1a046a6067fe67d27fbfea Author: Yannick Martel <martel@©odelutin.com> Date: Mon Jan 12 15:58:06 2015 +0100 close lucene index writter when close application --- .../services/CoselmarApplicationContext.java | 3 +++ .../CoselmarServicesApplicationContext.java | 27 ++++++++++++++++++++-- .../services/DefaultCoselmarServicesContext.java | 7 +++--- .../coselmar/services/indexation/LuceneUtils.java | 24 +++++++++++++++---- .../services/AbstractCoselmarWebServiceTest.java | 5 ++-- .../services/FakeCoselmarApplicationContext.java | 21 ++++++++++++++++- .../services/FakeCoselmarServicesContext.java | 5 +++- 7 files changed, 79 insertions(+), 13 deletions(-) diff --git a/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/CoselmarApplicationContext.java b/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/CoselmarApplicationContext.java index 6b59193..d812a69 100644 --- a/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/CoselmarApplicationContext.java +++ b/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/CoselmarApplicationContext.java @@ -31,6 +31,7 @@ import fr.ifremer.coselmar.persistence.CoselmarPersistenceContext; import fr.ifremer.coselmar.persistence.CoselmarTopiaApplicationContext; import fr.ifremer.coselmar.persistence.CoselmarTopiaPersistenceContext; import fr.ifremer.coselmar.services.config.CoselmarServicesConfig; +import fr.ifremer.coselmar.services.indexation.LuceneUtils; /** * @author ymartel <martel@codelutin.com> @@ -43,6 +44,8 @@ public interface CoselmarApplicationContext extends Closeable { CoselmarTopiaPersistenceContext newPersistenceContext(); + LuceneUtils getLuceneUtils(); + CoselmarServicesContext newServiceContext(CoselmarPersistenceContext persistenceContext, Locale locale); void init(); diff --git a/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/CoselmarServicesApplicationContext.java b/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/CoselmarServicesApplicationContext.java index 3ebc006..5d51d8a 100644 --- a/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/CoselmarServicesApplicationContext.java +++ b/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/CoselmarServicesApplicationContext.java @@ -36,6 +36,7 @@ import fr.ifremer.coselmar.persistence.CoselmarPersistenceContext; import fr.ifremer.coselmar.persistence.CoselmarTopiaApplicationContext; import fr.ifremer.coselmar.persistence.CoselmarTopiaPersistenceContext; import fr.ifremer.coselmar.services.config.CoselmarServicesConfig; +import fr.ifremer.coselmar.services.indexation.LuceneUtils; import fr.ifremer.coselmar.services.v1.InitialisationService; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -69,7 +70,9 @@ public class CoselmarServicesApplicationContext implements CoselmarApplicationCo CoselmarTopiaApplicationContext coselmarTopiaApplicationContext = new CoselmarTopiaApplicationContext(topiaProperties); - applicationContext = new CoselmarServicesApplicationContext(applicationConfig, coselmarTopiaApplicationContext); + LuceneUtils luceneUtils = new LuceneUtils(applicationConfig); + + applicationContext = new CoselmarServicesApplicationContext(applicationConfig, coselmarTopiaApplicationContext, luceneUtils); applicationContext.init(); } @@ -99,13 +102,18 @@ public class CoselmarServicesApplicationContext implements CoselmarApplicationCo protected CoselmarServicesConfig applicationConfig; - protected CoselmarServicesApplicationContext(CoselmarServicesConfig applicationConfig, CoselmarTopiaApplicationContext topiaApplicationContext) { + protected LuceneUtils luceneUtils; + + protected CoselmarServicesApplicationContext(CoselmarServicesConfig applicationConfig, + CoselmarTopiaApplicationContext topiaApplicationContext, + LuceneUtils luceneUtils) { Preconditions.checkNotNull(applicationConfig, "Configuration can not be null!"); Preconditions.checkNotNull(topiaApplicationContext, "topiaApplicationContext can not be null!"); this.applicationConfig = applicationConfig; this.topiaApplicationContext = topiaApplicationContext; + this.luceneUtils = luceneUtils; this.started = new AtomicBoolean(false); this.closed = new AtomicBoolean(false); } @@ -128,10 +136,16 @@ public class CoselmarServicesApplicationContext implements CoselmarApplicationCo } @Override + public LuceneUtils getLuceneUtils() { + return this.luceneUtils; + } + + @Override public CoselmarServicesContext newServiceContext(CoselmarPersistenceContext persistenceContext, Locale locale) { DefaultCoselmarServicesContext newServiceContext = new DefaultCoselmarServicesContext(); newServiceContext.setCoselmarServicesConfig(applicationConfig); + newServiceContext.setLuceneUtils(luceneUtils); newServiceContext.setTopiaApplicationContext(topiaApplicationContext); newServiceContext.setPersistenceContext(persistenceContext); newServiceContext.setLocale(locale); @@ -165,6 +179,14 @@ public class CoselmarServicesApplicationContext implements CoselmarApplicationCo topiaApplicationContext.close(); } + if (luceneUtils != null ) { + + if (log.isInfoEnabled()) { + log.info("Close Lucene Writer"); + } + luceneUtils.closeWriter(); + } + closed.set(true); started.set(false); } @@ -182,6 +204,7 @@ public class CoselmarServicesApplicationContext implements CoselmarApplicationCo Preconditions.checkState(applicationConfig != null, "No configuration initialized!"); Preconditions.checkState(topiaApplicationContext != null, "No topiaApplicationContext initialized!"); + Preconditions.checkState(luceneUtils != null, "No LuceneUtils initialized!"); if (applicationConfig.isLogConfigurationProvided()) { diff --git a/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/DefaultCoselmarServicesContext.java b/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/DefaultCoselmarServicesContext.java index 5f81b74..95a41ae 100644 --- a/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/DefaultCoselmarServicesContext.java +++ b/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/DefaultCoselmarServicesContext.java @@ -71,6 +71,10 @@ public class DefaultCoselmarServicesContext implements CoselmarServicesContext { this.persistenceContext = persistenceContext; } + public void setLuceneUtils(LuceneUtils luceneUtils) { + this.luceneUtils = luceneUtils; + } + public void setLocale(Locale locale) { this.locale = locale; } @@ -98,9 +102,6 @@ public class DefaultCoselmarServicesContext implements CoselmarServicesContext { @Override public LuceneUtils getLuceneUtils() { - if (this.luceneUtils == null) { - this.luceneUtils = new LuceneUtils(this.servicesConfig); - } return this.luceneUtils; } diff --git a/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/indexation/LuceneUtils.java b/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/indexation/LuceneUtils.java index c1ab995..27fd883 100644 --- a/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/indexation/LuceneUtils.java +++ b/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/indexation/LuceneUtils.java @@ -28,6 +28,8 @@ import java.io.File; import java.io.IOException; import fr.ifremer.coselmar.services.config.CoselmarServicesConfig; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.core.SimpleAnalyzer; import org.apache.lucene.index.IndexWriter; @@ -41,9 +43,11 @@ import org.apache.lucene.util.Version; */ public class LuceneUtils { - public static Analyzer analyzer; - public static final IndexWriterConfig indexationConfig = new IndexWriterConfig(Version.LUCENE_4_10_2, getAnalyzer()); - public static IndexWriter indexWriter; + private static final Log log = LogFactory.getLog(LuceneUtils.class); + + public Analyzer analyzer; + public final IndexWriterConfig indexationConfig = new IndexWriterConfig(Version.LUCENE_4_10_2, getAnalyzer()); + public IndexWriter indexWriter; protected CoselmarServicesConfig servicesConfig; @@ -51,7 +55,7 @@ public class LuceneUtils { this.servicesConfig = servicesConfig; } - protected static Analyzer getAnalyzer() { + protected Analyzer getAnalyzer() { if (analyzer == null) { // analyzer = new StandardAnalyzer(); //Use simple analyzer to index all words and be able to search with "close word" classified in StandardAnalyzer @@ -72,6 +76,18 @@ public class LuceneUtils { return indexWriter; } + public void closeWriter() { + if (indexWriter != null) { + try { + indexWriter.close(); + } catch (IOException | IllegalStateException e) { + if (log.isErrorEnabled()) { + log.error("Unable to close lucene index writer", e); + } + } + } + } + public void clearIndex() throws IOException { getIndexWriter().deleteAll(); getIndexWriter().commit(); diff --git a/coselmar-rest/src/test/java/fr/ifremer/coselmar/services/AbstractCoselmarWebServiceTest.java b/coselmar-rest/src/test/java/fr/ifremer/coselmar/services/AbstractCoselmarWebServiceTest.java index ea91a5c..3c1e540 100644 --- a/coselmar-rest/src/test/java/fr/ifremer/coselmar/services/AbstractCoselmarWebServiceTest.java +++ b/coselmar-rest/src/test/java/fr/ifremer/coselmar/services/AbstractCoselmarWebServiceTest.java @@ -58,7 +58,7 @@ public class AbstractCoselmarWebServiceTest extends WebMotionTest { CoselmarServicesApplicationContext applicationContext = new CoselmarServicesApplicationContext(application.getApplicationConfig(), - application.getTopiaApplicationContext()) { + application.getTopiaApplicationContext(), application.getLuceneUtils()) { @Override public CoselmarServicesContext newServiceContext(CoselmarPersistenceContext persistenceContext, Locale locale) { @@ -68,7 +68,8 @@ public class AbstractCoselmarWebServiceTest extends WebMotionTest { Locale.FRANCE, application.getApplicationConfig(), application.getTopiaApplicationContext(), - application.newPersistenceContext()); + application.newPersistenceContext(), + application.getLuceneUtils()); return servicesContext; } diff --git a/coselmar-rest/src/test/java/fr/ifremer/coselmar/services/FakeCoselmarApplicationContext.java b/coselmar-rest/src/test/java/fr/ifremer/coselmar/services/FakeCoselmarApplicationContext.java index c7c3129..b619a03 100644 --- a/coselmar-rest/src/test/java/fr/ifremer/coselmar/services/FakeCoselmarApplicationContext.java +++ b/coselmar-rest/src/test/java/fr/ifremer/coselmar/services/FakeCoselmarApplicationContext.java @@ -39,6 +39,7 @@ import fr.ifremer.coselmar.persistence.CoselmarTopiaApplicationContext; import fr.ifremer.coselmar.persistence.CoselmarTopiaPersistenceContext; import fr.ifremer.coselmar.services.config.CoselmarServicesConfig; import fr.ifremer.coselmar.services.config.CoselmarServicesConfigOption; +import fr.ifremer.coselmar.services.indexation.LuceneUtils; import fr.ifremer.coselmar.services.v1.InitialisationService; import org.apache.commons.logging.Log; import org.junit.rules.TestWatcher; @@ -66,6 +67,8 @@ public class FakeCoselmarApplicationContext extends TestWatcher implements Cosel protected CoselmarServicesConfig configuration; + protected LuceneUtils luceneUtils; + protected String methodName; protected int currentPortNumber; @@ -121,6 +124,8 @@ public class FakeCoselmarApplicationContext extends TestWatcher implements Cosel Map<String, String> topiaProperties = configuration.getTopiaProperties(); applicationContext = new CoselmarTopiaApplicationContext(topiaProperties); + luceneUtils = new LuceneUtils(configuration); + {//Init some users CoselmarTopiaPersistenceContext persistenceContext = newPersistenceContext(); CoselmarServicesContext serviceContext = newServiceContext(persistenceContext, Locale.FRANCE); @@ -156,6 +161,14 @@ public class FakeCoselmarApplicationContext extends TestWatcher implements Cosel applicationContext.close(); } + + if (luceneUtils != null ) { + + if (log.isInfoEnabled()) { + log.info("Close Lucene Reader"); + } + luceneUtils.closeWriter(); + } } @Override @@ -169,6 +182,11 @@ public class FakeCoselmarApplicationContext extends TestWatcher implements Cosel } @Override + public LuceneUtils getLuceneUtils() { + return luceneUtils; + } + + @Override public CoselmarTopiaPersistenceContext newPersistenceContext() { CoselmarTopiaPersistenceContext persistenceContext; @@ -193,7 +211,8 @@ public class FakeCoselmarApplicationContext extends TestWatcher implements Cosel Locale.FRANCE, getApplicationConfig(), getTopiaApplicationContext(), - newPersistenceContext()); + newPersistenceContext(), + getLuceneUtils()); return serviceContext; } diff --git a/coselmar-rest/src/test/java/fr/ifremer/coselmar/services/FakeCoselmarServicesContext.java b/coselmar-rest/src/test/java/fr/ifremer/coselmar/services/FakeCoselmarServicesContext.java index 7dfd312..bc28811 100644 --- a/coselmar-rest/src/test/java/fr/ifremer/coselmar/services/FakeCoselmarServicesContext.java +++ b/coselmar-rest/src/test/java/fr/ifremer/coselmar/services/FakeCoselmarServicesContext.java @@ -31,6 +31,7 @@ import com.google.common.base.Preconditions; import fr.ifremer.coselmar.persistence.CoselmarTopiaApplicationContext; import fr.ifremer.coselmar.persistence.CoselmarTopiaPersistenceContext; import fr.ifremer.coselmar.services.config.CoselmarServicesConfig; +import fr.ifremer.coselmar.services.indexation.LuceneUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -49,12 +50,14 @@ public class FakeCoselmarServicesContext extends DefaultCoselmarServicesContext Locale locale, CoselmarServicesConfig serviceConfig, CoselmarTopiaApplicationContext applicationcontext, - CoselmarTopiaPersistenceContext persistenceContext) { + CoselmarTopiaPersistenceContext persistenceContext, + LuceneUtils luceneUtils) { FakeCoselmarServicesContext serviceContext = new FakeCoselmarServicesContext(); serviceContext.setPersistenceContext(persistenceContext); serviceContext.setCoselmarServicesConfig(serviceConfig); serviceContext.setTopiaApplicationContext(applicationcontext); + serviceContext.setLuceneUtils(luceneUtils); serviceContext.setLocale(locale); serviceContext.setDate(now); return serviceContext; -- To stop receiving notification emails like this one, please contact codelutin.com SCM administrator <admin+scm@codelutin.com>.