refs #2112: Port SmartExecutor to be compliant with new Authorization

https://support.d4science.org/issues/2112

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/vre-management/smart-executor@122953 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2016-02-09 14:20:26 +00:00
parent 1ff064b7e6
commit 74806f6c2d
12 changed files with 65 additions and 68 deletions

View File

@ -25,7 +25,6 @@
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">

View File

@ -16,7 +16,6 @@ import org.gcube.common.resources.gcore.ServiceEndpoint.Property;
import org.gcube.common.resources.gcore.ServiceEndpoint.Runtime;
import org.gcube.common.resources.gcore.common.Platform;
import org.gcube.common.resources.gcore.utils.Group;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.informationsystem.publisher.RegistryPublisher;
import org.gcube.informationsystem.publisher.RegistryPublisherFactory;
import org.gcube.informationsystem.publisher.exception.RegistryNotFoundException;
@ -47,6 +46,18 @@ public class SmartExecutorInitializator implements ApplicationManager {
public static final long JOIN_TIMEOUT = 1000;
public static String getScope(){
String token = SecurityTokenProvider.instance.get();
AuthorizationEntry authorizationEntry;
try {
authorizationEntry = Constants.authorizationService().get(token);
} catch (Exception e) {
throw new RuntimeException(e);
}
String scope = authorizationEntry.getContext();
return scope;
}
/**
* Publish the provided resource on all Service Scopes retrieved from
* Context
@ -61,8 +72,7 @@ public class SmartExecutorInitializator implements ApplicationManager {
RegistryPublisher registryPublisher = RegistryPublisherFactory.create();
try {
String scope = ScopeProvider.instance.get();
logger.debug("Trying to publish to {}:\n{}", scope, stringWriter);
logger.debug("Trying to publish to {}:\n{}", getScope(), stringWriter);
registryPublisher.create(resource);
} catch (Exception e) {
logger.error("The resource was not published", e);
@ -83,7 +93,7 @@ public class SmartExecutorInitializator implements ApplicationManager {
RegistryPublisher registryPublisher = RegistryPublisherFactory.create();
String id = resource.id();
logger.debug("Trying to remove {} with ID {} from {}", resource.getClass().getSimpleName(), id, ScopeProvider.instance.get());
logger.debug("Trying to remove {} with ID {} from {}", resource.getClass().getSimpleName(), id, getScope());
registryPublisher.remove(resource);
@ -211,16 +221,14 @@ public class SmartExecutorInitializator implements ApplicationManager {
DiscoveryClient<ServiceEndpoint> client = ICFactory.clientFor(ServiceEndpoint.class);
List<ServiceEndpoint> serviceEndpoints = client.submit(query);
String scope = ScopeProvider.instance.get();
for (ServiceEndpoint serviceEndpoint : serviceEndpoints) {
try {
logger.debug("Trying to unpublish the old ServiceEndpoint with ID {} from scope {}",
serviceEndpoint.id(), scope);
serviceEndpoint.id(), getScope());
unPublishScopedResource(serviceEndpoint);
} catch(Exception e){
logger.debug("Exception tryng to unpublish the old ServiceEndpoint with ID {} from scope {}",
serviceEndpoint.id(), scope, e);
serviceEndpoint.id(), getScope(), e);
}
}
}catch(Exception e){
@ -292,19 +300,13 @@ public class SmartExecutorInitializator implements ApplicationManager {
*/
@Override
public void onShutdown(){
String token = SecurityTokenProvider.instance.get();
AuthorizationEntry authorizationEntry;
try {
authorizationEntry = Constants.authorizationService().get(token);
} catch (Exception e) {
throw new RuntimeException(e);
}
String scope = authorizationEntry.getContext();
logger.trace(
"\n-------------------------------------------------------\n"
+ "Smart Executor is Stopping on scope {}\n"
+ "-------------------------------------------------------", scope);
+ "-------------------------------------------------------",
getScope());
SmartExecutorScheduler.getInstance().stopAll();
@ -314,13 +316,15 @@ public class SmartExecutorInitializator implements ApplicationManager {
SmartExecutorPersistenceFactory.getPersistenceConnector().close();
} catch (Exception e) {
logger.error("Unable to correctly close {} for scope {}",
SmartExecutorPersistenceConnector.class.getSimpleName(), scope, e);
SmartExecutorPersistenceConnector.class.getSimpleName(),
getScope(), e);
}
logger.trace(
"\n-------------------------------------------------------\n"
+ "Smart Executor Stopped Successfully on scope {}\n"
+ "-------------------------------------------------------", scope);
+ "-------------------------------------------------------",
getScope());
}
}

View File

@ -10,10 +10,6 @@ import org.gcube.vremanagement.executor.api.types.LaunchParameter;
import org.gcube.vremanagement.executor.exception.SchedulePersistenceException;
/**
* Every implementation MUST take in account to store/query the records
* on the current scope which is not passed as argument but MUST be retrieved
* using {#org.gcube.common.scope.api.ScopeProvider} facilities
* i.e. ScopeProvider.instance.get()
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
*/
public interface ScheduledTaskConfiguration {

View File

@ -7,7 +7,7 @@ import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.vremanagement.executor.SmartExecutorInitializator;
import org.gcube.vremanagement.executor.api.types.LaunchParameter;
import org.gcube.vremanagement.executor.api.types.Scheduling;
import org.gcube.vremanagement.executor.exception.ScopeNotMatchException;
@ -48,36 +48,36 @@ public class JSONLaunchParameter extends LaunchParameter {
public JSONLaunchParameter(String pluginName, Map<String, String> pluginCapabilities, Map<String, Object> inputs) {
super(pluginName, pluginCapabilities, inputs);
this.scope = ScopeProvider.instance.get();
this.scope = SmartExecutorInitializator.getScope();
}
public JSONLaunchParameter(String pluginName, Map<String, Object> inputs, Scheduling scheduling) throws ParseException {
super(pluginName, inputs, scheduling);
this.scope = ScopeProvider.instance.get();
this.scope = SmartExecutorInitializator.getScope();
}
public JSONLaunchParameter(String pluginName, Map<String, String> pluginCapabilities, Map<String, Object> inputs, Scheduling scheduling) throws ParseException {
super(pluginName, pluginCapabilities, inputs, scheduling);
this.scope = ScopeProvider.instance.get();
this.scope = SmartExecutorInitializator.getScope();
}
@Deprecated
public JSONLaunchParameter(String pluginName, Map<String, Object> inputs, Scheduling scheduling, boolean persist) throws ParseException {
super(pluginName, inputs, scheduling, persist);
this.scope = ScopeProvider.instance.get();
this.scope = SmartExecutorInitializator.getScope();
}
@Deprecated
public JSONLaunchParameter(String pluginName, Map<String, String> pluginCapabilities, Map<String, Object> inputs, Scheduling scheduling, boolean persist) throws ParseException {
super(pluginName, pluginCapabilities, inputs, scheduling, persist);
this.scheduling = new JSONScheduling(scheduling);
this.scope = ScopeProvider.instance.get();
this.scope = SmartExecutorInitializator.getScope();
}
public JSONLaunchParameter(LaunchParameter parameter) throws ParseException {
super(parameter.getPluginName(), parameter.getPluginCapabilities(), parameter.getInputs(), parameter.getScheduling());
this.scheduling = new JSONScheduling(parameter.getScheduling());
this.scope = ScopeProvider.instance.get();
this.scope = SmartExecutorInitializator.getScope();
}
public JSONLaunchParameter(JSONObject jsonObject) throws JSONException, ParseException, ScopeNotMatchException {
@ -113,7 +113,7 @@ public class JSONLaunchParameter extends LaunchParameter {
this.usedBy = jsonObject.getString(USED_BY);
}
this.scope = ScopeProvider.instance.get();
this.scope = SmartExecutorInitializator.getScope();
if(jsonObject.has(SCOPE)){
String jsonScope = jsonObject.getString(SCOPE);
if(jsonScope.compareTo(scope)!=0){

View File

@ -15,10 +15,10 @@ import org.gcube.common.resources.gcore.ServiceEndpoint;
import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint;
import org.gcube.common.resources.gcore.ServiceEndpoint.Property;
import org.gcube.common.resources.gcore.utils.Group;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.resources.discovery.client.api.DiscoveryClient;
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
import org.gcube.resources.discovery.icclient.ICFactory;
import org.gcube.vremanagement.executor.SmartExecutorInitializator;
/**
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
@ -126,7 +126,7 @@ public class SmartExecutorPersistenceConfiguration {
List<ServiceEndpoint> serviceEndpoints = client.submit(query);
if(serviceEndpoints.size()>1){
query.addCondition(String.format("$resource/Profile/AccessPoint/Properties/Property/Name/text() eq '%s'", TARGET_SCOPE));
query.addCondition(String.format("$resource/Profile/AccessPoint/Properties/Property/Value/text() eq '%s'", ScopeProvider.instance.get()));
query.addCondition(String.format("$resource/Profile/AccessPoint/Properties/Property/Value/text() eq '%s'", SmartExecutorInitializator.getScope()));
serviceEndpoints = client.submit(query);
}
return serviceEndpoints.get(0);

View File

@ -11,10 +11,6 @@ import org.gcube.vremanagement.executor.plugin.PluginStateNotification;
/**
* Model the connector which create or open the connection to DB.
* Every implementation MUST take in account to store/query the records
* on the current scope which is not passed as argument but MUSt be retrieved
* using {#org.gcube.common.scope.api.ScopeProvider} facilities
* i.e. ScopeProvider.instance.get()
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
*/
public abstract class SmartExecutorPersistenceConnector implements PluginStateNotification {

View File

@ -6,7 +6,7 @@ package org.gcube.vremanagement.executor.persistence;
import java.util.HashMap;
import java.util.Map;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.vremanagement.executor.SmartExecutorInitializator;
import org.gcube.vremanagement.executor.persistence.couchdb.CouchDBPersistenceConnector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -42,28 +42,33 @@ public abstract class SmartExecutorPersistenceFactory {
* @return the persistenceConnector
*/
public static synchronized SmartExecutorPersistenceConnector getPersistenceConnector() throws Exception {
String scope = ScopeProvider.instance.get();
SmartExecutorPersistenceConnector persistence = getPersistenceConnector(scope);
SmartExecutorPersistenceConnector persistence =
getPersistenceConnector(SmartExecutorInitializator.getScope());
if(persistence==null){
logger.trace("Retrieving {} for scope {} not found on internal {}. Intializing it.",
SmartExecutorPersistenceConnector.class.getSimpleName(), scope, Map.class.getSimpleName());
SmartExecutorPersistenceConnector.class.getSimpleName(),
SmartExecutorInitializator.getScope(),
Map.class.getSimpleName());
String className = CouchDBPersistenceConnector.class.getSimpleName();
SmartExecutorPersistenceConfiguration configuration =
new SmartExecutorPersistenceConfiguration(CouchDBPersistenceConnector.class.getSimpleName());
new SmartExecutorPersistenceConfiguration(className);
persistence = new CouchDBPersistenceConnector(configuration);
persistenceConnectors.put(scope, persistence);
persistenceConnectors.put(SmartExecutorInitializator.getScope(),
persistence);
}
return persistence;
}
public static synchronized void closePersistenceConnector() throws Exception {
String scope = ScopeProvider.instance.get();
SmartExecutorPersistenceConnector persistence = getPersistenceConnector(scope);
SmartExecutorPersistenceConnector persistence =
getPersistenceConnector(SmartExecutorInitializator.getScope());
if(persistence!=null){
persistence.close();
persistenceConnectors.remove(scope);
persistenceConnectors.remove(SmartExecutorInitializator.getScope());
}
}

View File

@ -28,7 +28,7 @@ import org.ektorp.http.StdHttpClient;
import org.ektorp.http.StdHttpClient.Builder;
import org.ektorp.impl.StdCouchDbConnector;
import org.ektorp.impl.StdCouchDbInstance;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.vremanagement.executor.SmartExecutorInitializator;
import org.gcube.vremanagement.executor.api.types.LaunchParameter;
import org.gcube.vremanagement.executor.configuration.ScheduledTaskConfiguration;
import org.gcube.vremanagement.executor.configuration.jsonbased.JSONLaunchParameter;
@ -205,7 +205,7 @@ public class CouchDBPersistenceConnector extends SmartExecutorPersistenceConnect
ViewQuery query = new ViewQuery().designDocId(String.format("%s%s", MAP_REDUCE__DESIGN, PLUGIN_STATE_DOCUMENT));
String scope = ScopeProvider.instance.get();
String scope = SmartExecutorInitializator.getScope();
ArrayNode startKey = new ObjectMapper().createArrayNode();
startKey.add(scope);
ArrayNode endKey = new ObjectMapper().createArrayNode();
@ -273,7 +273,7 @@ public class CouchDBPersistenceConnector extends SmartExecutorPersistenceConnect
ViewQuery query = new ViewQuery().designDocId(String.format("%s%s", MAP_REDUCE__DESIGN, SCHEDULED_TASKS_DOCUMENT));
query = query.viewName(ORPHAN_VIEW);
String scope = ScopeProvider.instance.get();
String scope = SmartExecutorInitializator.getScope();
ArrayNode startKey = new ObjectMapper().createArrayNode();
startKey.add(scope);
ArrayNode endKey = new ObjectMapper().createArrayNode();
@ -316,7 +316,7 @@ public class CouchDBPersistenceConnector extends SmartExecutorPersistenceConnect
JSONObject obj = jlp.toJSON();
obj.append(TYPE_JSON_FIELD, SCHEDULED_TASK_TYPE);
obj.append(USED_BY_FIELD, consumerID);
obj.append(ScheduledTaskConfiguration.SCOPE, ScopeProvider.instance.get());
obj.append(ScheduledTaskConfiguration.SCOPE, SmartExecutorInitializator.getScope());
createItem(obj, uuid.toString());
} catch (Exception e) {
logger.error("Error Adding Scheduled Task UUID : {}, Consumer : {}, LaunchParameter : {}",

View File

@ -9,8 +9,8 @@ import java.util.UUID;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.node.ObjectNode;
import org.gcube.common.resources.gcore.GCoreEndpoint;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.smartgears.ContextProvider;
import org.gcube.vremanagement.executor.SmartExecutorInitializator;
import org.gcube.vremanagement.executor.plugin.PluginDeclaration;
import org.gcube.vremanagement.executor.plugin.PluginStateEvolution;
@ -76,7 +76,7 @@ public class PluginStateEvolutionObjectNode {
}
public static void addScope(ObjectNode objectNode){
objectNode.put(SCOPE_FIELD, ScopeProvider.instance.get());
objectNode.put(SCOPE_FIELD, SmartExecutorInitializator.getScope());
}
public static ObjectNode getObjectMapper(PluginStateEvolution pluginStateEvolution){

View File

@ -9,7 +9,7 @@ import java.util.List;
import java.util.UUID;
import org.acme.HelloWorldPluginDeclaration;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.vremanagement.executor.api.types.LaunchParameter;
import org.gcube.vremanagement.executor.configuration.ScheduledTaskConfiguration;
import org.gcube.vremanagement.executor.configuration.ScheduledTaskConfigurationFactory;
@ -17,6 +17,7 @@ import org.gcube.vremanagement.executor.persistence.couchdb.CouchDBPersistenceCo
import org.gcube.vremanagement.executor.plugin.PluginState;
import org.gcube.vremanagement.executor.plugin.PluginStateEvolution;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -29,13 +30,14 @@ public class SmartExecutorPersistenceConnectorTest {
private static Logger logger = LoggerFactory.getLogger(SmartExecutorPersistenceConnectorTest.class);
public static final String[] SCOPES = new String[]{"/gcube", "/gcube/devsec"};
public static final String GCUBE_SCOPE = SCOPES[0];
public static final String GCUBE_DEVSEC_SCOPE = SCOPES[1];
@Before
public void before() throws Exception{
SecurityTokenProvider.instance.set("82a84741-debe-4c90-a907-c429c8272071");
}
@Test
public void getConnectionTest() throws Exception {
ScopeProvider.instance.set(GCUBE_DEVSEC_SCOPE);
SmartExecutorPersistenceConnector persistenceConnector = SmartExecutorPersistenceFactory.getPersistenceConnector();
Assert.assertNotNull(persistenceConnector);
Assert.assertEquals(CouchDBPersistenceConnector.class, persistenceConnector.getClass());
@ -44,7 +46,6 @@ public class SmartExecutorPersistenceConnectorTest {
@Test
public void getPluginInstanceStateTest() throws Exception {
ScopeProvider.instance.set(GCUBE_DEVSEC_SCOPE);
SmartExecutorPersistenceConnector persistenceConnector = SmartExecutorPersistenceFactory.getPersistenceConnector();
UUID uuid = UUID.randomUUID();
@ -70,8 +71,6 @@ public class SmartExecutorPersistenceConnectorTest {
@Test
public void getAvailableScheduledTasksTest() throws Exception {
ScopeProvider.instance.set(GCUBE_DEVSEC_SCOPE);
ScheduledTaskConfiguration stc = ScheduledTaskConfigurationFactory.getLaunchConfiguration();
Assert.assertNotNull(stc);
Assert.assertEquals(CouchDBPersistenceConnector.class, stc.getClass());

View File

@ -9,11 +9,10 @@ import java.util.UUID;
import org.acme.HelloWorldPlugin;
import org.acme.HelloWorldPluginDeclaration;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.vremanagement.executor.exception.InputsNullException;
import org.gcube.vremanagement.executor.exception.InvalidInputsException;
import org.gcube.vremanagement.executor.persistence.SmartExecutorPersistenceConnector;
import org.gcube.vremanagement.executor.persistence.SmartExecutorPersistenceConnectorTest;
import org.gcube.vremanagement.executor.persistence.SmartExecutorPersistenceFactory;
import org.gcube.vremanagement.executor.plugin.PluginState;
import org.gcube.vremanagement.executor.plugin.PluginStateNotification;
@ -31,10 +30,10 @@ import org.slf4j.LoggerFactory;
public class RunnablePluginTest {
private static Logger logger = LoggerFactory.getLogger(SmartExecutorSchedulerTest.class);
@BeforeClass
@BeforeClass
public static void prepare() throws Exception{
try {
ScopeProvider.instance.set(SmartExecutorPersistenceConnectorTest.GCUBE_DEVSEC_SCOPE);
SecurityTokenProvider.instance.set("82a84741-debe-4c90-a907-c429c8272071");
SmartExecutorPersistenceFactory.getPersistenceConnector();
} catch (Exception e) {
logger.error("Unable to Create JDBCPersistenceConnector");

View File

@ -10,13 +10,12 @@ import java.util.UUID;
import org.acme.HelloWorldPlugin;
import org.acme.HelloWorldPluginDeclaration;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.vremanagement.executor.api.types.LaunchParameter;
import org.gcube.vremanagement.executor.api.types.Scheduling;
import org.gcube.vremanagement.executor.exception.PluginStateNotRetrievedException;
import org.gcube.vremanagement.executor.exception.UnableToInterruptTaskException;
import org.gcube.vremanagement.executor.persistence.SmartExecutorPersistenceConnector;
import org.gcube.vremanagement.executor.persistence.SmartExecutorPersistenceConnectorTest;
import org.gcube.vremanagement.executor.persistence.SmartExecutorPersistenceFactory;
import org.gcube.vremanagement.executor.plugin.PluginState;
import org.gcube.vremanagement.executor.scheduler.SmartExecutorScheduler;
@ -43,7 +42,7 @@ public class SmartExecutorSchedulerTest {
@BeforeClass
public static void prepare() throws Exception{
try {
ScopeProvider.instance.set(SmartExecutorPersistenceConnectorTest.GCUBE_DEVSEC_SCOPE);
SecurityTokenProvider.instance.set("82a84741-debe-4c90-a907-c429c8272071");
/*
pc = new JDBCPersistenceConnector(".");
SmartExecutorPersistenceConnector.setPersistenceConnector(pc);