diff --git a/src/main/java/org/gcube/vremanagement/executor/ExecutorImpl.java b/src/main/java/org/gcube/vremanagement/executor/ExecutorImpl.java index 2e1f8e0..b284914 100644 --- a/src/main/java/org/gcube/vremanagement/executor/ExecutorImpl.java +++ b/src/main/java/org/gcube/vremanagement/executor/ExecutorImpl.java @@ -83,6 +83,11 @@ public class ExecutorImpl implements Executor { */ private static JDBCPersistenceConnector jdbcPersistenceConnector; + /** + * Application Context + */ + public static ApplicationContext ctx; + /** * Publish the provided resource on all Service Scopes retrieved from * Context @@ -94,15 +99,13 @@ public class ExecutorImpl implements Executor { StringWriter stringWriter = new StringWriter(); Resources.marshal(resource, stringWriter); - ApplicationContext ctx = ContextProvider.get(); - - if(ctx == null){ + if(ExecutorImpl.ctx == null){ logger.error(String.format("%s null. Unable to Publish the Resource\n%s", ApplicationContext.class.getSimpleName(), stringWriter.toString())); return; } - Set scopeSet = ctx.configuration().startScopes(); + Set scopeSet = ExecutorImpl.ctx.configuration().startScopes(); List scopes = new ArrayList(scopeSet); ScopedPublisher scopedPublisher = RegistryPublisherFactory.scopedPublisher(); @@ -125,15 +128,7 @@ public class ExecutorImpl implements Executor { StringWriter stringWriter = new StringWriter(); Resources.marshal(resource, stringWriter); - ApplicationContext ctx = ContextProvider.get(); - - if(ctx == null){ - logger.error(String.format("%s null. Unable to UnPublish the Resource\n%s", - ApplicationContext.class.getSimpleName(), stringWriter.toString())); - return; - } - - Set scopeSet = ctx.configuration().startScopes(); + Set scopeSet = ExecutorImpl.ctx.configuration().startScopes(); List scopes = new ArrayList(scopeSet); ScopedPublisher scopedPublisher = RegistryPublisherFactory.scopedPublisher(); @@ -198,7 +193,34 @@ public class ExecutorImpl implements Executor { ExecutorImpl.serviceEndpoint = createServiceEndpoint(); ExecutorImpl.pluginInstances = new HashMap>>(); - initPersistence(); + try { + initContextDependendent(); + } catch (ExecutorException e) { + // This is normal + } catch (Exception e) { + throw e; + } + + } + + + private static void initContextDependendent() throws ExecutorException, Exception { + + if(ExecutorImpl.ctx == null){ + ExecutorImpl.ctx = ContextProvider.get(); + if(ExecutorImpl.ctx == null){ + String message = String.format("%s not ready", + ApplicationContext.class.getSimpleName()); + throw new ExecutorException(message); + } + } + + try { + initPersistence(); + } catch(Exception e){ + String message = "Unable to init persistence"; + throw new Exception(message); + } // TODO Before publishing the new Resource check if on IS there is // an old published ServiceEndpoint resource that where not unpublished @@ -223,7 +245,7 @@ public class ExecutorImpl implements Executor { * discovered plugin and plugin state. * @throws Exception if fails */ - protected static void initPersistence() throws Exception { + private static void initPersistence() throws Exception { try { jdbcPersistenceConnector = new JDBCPersistenceConnector(); } catch (Exception e) { @@ -286,6 +308,16 @@ public class ExecutorImpl implements Executor { @Override public String launch(LaunchParameter parameter) throws InputsNullException, PluginNotFoundException, LaunchException, ExecutorException { + if(ExecutorImpl.ctx == null){ + try { + initContextDependendent(); + } catch (ExecutorException e) { + throw e; + } catch (Exception e) { + throw new ExecutorException(e); + } + } + Map inputs = parameter.getInputs(); if(inputs==null){ throw new InputsNullException(); @@ -351,7 +383,17 @@ public class ExecutorImpl implements Executor { /**{@inheritDoc}*/ @Override - public PluginState getState(String executionIdentifier) throws PluginInstanceNotFoundException, ExecutorException { + public PluginState getState(String executionIdentifier) + throws PluginInstanceNotFoundException, ExecutorException { + if(ExecutorImpl.ctx == null){ + try { + initContextDependendent(); + } catch (ExecutorException e) { + throw e; + } catch (Exception e) { + throw new ExecutorException(e); + } + } try { return jdbcPersistenceConnector.getPluginInstanceState(UUID.fromString(executionIdentifier)); } catch (Exception e) { diff --git a/src/main/java/org/gcube/vremanagement/executor/persistence/JDBCPersistenceConnector.java b/src/main/java/org/gcube/vremanagement/executor/persistence/JDBCPersistenceConnector.java index 06215a5..927a63b 100644 --- a/src/main/java/org/gcube/vremanagement/executor/persistence/JDBCPersistenceConnector.java +++ b/src/main/java/org/gcube/vremanagement/executor/persistence/JDBCPersistenceConnector.java @@ -11,8 +11,7 @@ import java.sql.SQLException; import java.sql.Statement; import java.util.UUID; -import org.gcube.smartgears.ContextProvider; -import org.gcube.smartgears.context.application.ApplicationContext; +import org.gcube.vremanagement.executor.ExecutorImpl; import org.gcube.vremanagement.executor.plugin.PluginState; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -42,6 +41,8 @@ public class JDBCPersistenceConnector extends PersistenceConnector { public final static String PLUGIN_INSTANCE_EVOLUTION_TABLE_TIMESTAMP_FIELD = "timestamp"; public final static String PLUGIN_INSTANCE_EVOLUTION_TABLE_STATE_FIELD = "state"; + public final static String CATALINA_HOME = "CATALINA_HOME"; + /** * Default Constructor * @throws Exception if fails @@ -56,13 +57,13 @@ public class JDBCPersistenceConnector extends PersistenceConnector { throw e; } - // TODO Get which actually result null ApplicationContext ctx = ContextProvider.get(); - ApplicationContext ctx = ContextProvider.get(); - String dbFilePath = "/home/lucafrosini/workspace/runtime/tomcat/webapps/smart-executor-1.0.0-SNAPSHOT/WEB-INF/executor"; + String dbFilePath; try { - dbFilePath = ctx.application().getContextPath(); + dbFilePath = String.format("%s%s", ExecutorImpl.ctx.application().getContextPath(), dbName); } catch(Exception e){ + dbFilePath = String.format("./%s", dbName); } + String jdbcURL = String.format("%s%s", jdbcConnection, dbFilePath); logger.debug(String.format("DB URL : %s", jdbcURL)); diff --git a/src/test/java/org/gcube/vremanagement/executor/persistence/JDBCPersistenceConnectorTest.java b/src/test/java/org/gcube/vremanagement/executor/persistence/JDBCPersistenceConnectorTest.java index 096b2ef..5672e77 100644 --- a/src/test/java/org/gcube/vremanagement/executor/persistence/JDBCPersistenceConnectorTest.java +++ b/src/test/java/org/gcube/vremanagement/executor/persistence/JDBCPersistenceConnectorTest.java @@ -29,6 +29,7 @@ public class JDBCPersistenceConnectorTest { @Test public void getPluginInstanceStateTest() throws Exception { UUID uuid = UUID.randomUUID(); + JDBCPersistenceConnector jdbcPersistenceConnector = new JDBCPersistenceConnector(); JDBCPersistence jdbcPersistence = new JDBCPersistence(jdbcPersistenceConnector, HelloWorldPluginDeclaration.name, uuid);