From 27c09eec6747444b03149b59c11b48ac1c866372 Mon Sep 17 00:00:00 2001 From: Luca Foppiano Date: Wed, 9 Jun 2021 09:46:30 +0900 Subject: [PATCH] migrated to typed engineFactory --- .../core/factory/GrobidPoolingFactory.java | 45 +++++++------------ 1 file changed, 16 insertions(+), 29 deletions(-) diff --git a/grobid-core/src/main/java/org/grobid/core/factory/GrobidPoolingFactory.java b/grobid-core/src/main/java/org/grobid/core/factory/GrobidPoolingFactory.java index 17ea91e925..0deb01b902 100755 --- a/grobid-core/src/main/java/org/grobid/core/factory/GrobidPoolingFactory.java +++ b/grobid-core/src/main/java/org/grobid/core/factory/GrobidPoolingFactory.java @@ -11,12 +11,12 @@ import org.slf4j.LoggerFactory; public class GrobidPoolingFactory extends AbstractEngineFactory implements - PoolableObjectFactory { + PoolableObjectFactory { /** * A pool which contains objects of type Engine for the conversion. */ - private static volatile GenericObjectPool grobidEnginePool = null; + private static volatile GenericObjectPool grobidEnginePool = null; private static volatile Boolean grobidEnginePoolControl = false; private static final Logger LOGGER = LoggerFactory .getLogger(GrobidPoolingFactory.class); @@ -37,13 +37,13 @@ protected GrobidPoolingFactory() { * * @return GenericObjectPool */ - protected static GenericObjectPool newPoolInstance() { + protected static GenericObjectPool newPoolInstance() { if (grobidEnginePool == null) { // initialize grobidEnginePool LOGGER.debug("synchronized newPoolInstance"); synchronized (grobidEnginePoolControl) { if (grobidEnginePool == null) { - grobidEnginePool = new GenericObjectPool(GrobidPoolingFactory.newInstance()); + grobidEnginePool = new GenericObjectPool<>(GrobidPoolingFactory.newInstance()); //grobidEnginePool.setFactory(GrobidPoolingFactory.newInstance()); grobidEnginePool .setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_BLOCK); @@ -72,7 +72,7 @@ public static synchronized Engine getEngineFromPool(boolean preloadModels) { } Engine engine = null; try { - engine = (Engine) grobidEnginePool.borrowObject(); + engine = grobidEnginePool.borrowObject(); } catch (NoSuchElementException nseExp) { throw new NoSuchElementException(); } catch (Exception exp) { @@ -92,7 +92,7 @@ public static void returnEngine(Engine engine) { try { //engine.close(); if (grobidEnginePool == null) - System.out.println("grobidEnginePool is null !"); + LOGGER.error("grobidEnginePool is null !"); grobidEnginePool.returnObject(engine); } catch (Exception exp) { throw new GrobidException( @@ -109,41 +109,28 @@ public static void returnEngine(Engine engine) { protected static GrobidPoolingFactory newInstance() { return new GrobidPoolingFactory(); } - - /** - * {@inheritDoc} - */ + @Override - public void activateObject(Object arg0) throws Exception { + public void activateObject(Engine arg0) throws Exception { } - /** - * {@inheritDoc} - */ @Override - public void destroyObject(Object arg0) throws Exception { + public void destroyObject(Engine engine) throws Exception { + engine.close(); } - /** - * {@inheritDoc} - */ + @Override - public Object makeObject() throws Exception { + public Engine makeObject() throws Exception { return (createEngine(this.preload)); } - - /** - * {@inheritDoc} - */ + @Override - public void passivateObject(Object arg0) throws Exception { + public void passivateObject(Engine arg0) throws Exception { } - - /** - * {@inheritDoc} - */ + @Override - public boolean validateObject(Object arg0) { + public boolean validateObject(Engine arg0) { return false; }