Enhancement on Project Activity #11690

git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/Common/workspace-task-executor-library@167293 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Francesco Mangiacrapa 2018-05-02 15:59:52 +00:00
parent 6a5ee65cd9
commit caa1debb49
5 changed files with 290 additions and 67 deletions

View File

@ -17,10 +17,10 @@ import org.slf4j.LoggerFactory;
/**
* The Class WorkspaceDataMinerTaskExecute.
* The Class WorkspaceDataMinerTaskExecutor.
*
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* Apr 26, 2018
* May 2, 2018
*/
public class WorkspaceDataMinerTaskExecutor implements ExecutableTask<TaskConfiguration, TaskExecutionStatus>, ConfigurableTask<TaskConfiguration>, CheckableTask<TaskConfiguration>{
@ -29,15 +29,20 @@ public class WorkspaceDataMinerTaskExecutor implements ExecutableTask<TaskConfig
private static WorkspaceDataMinerTaskExecutor INSTANCE = null;
public static final String WS_TASK_TASK_CONF = "WS-TASK.TASK-CONF";
public static final String WS_DM_TASK_TASK_CONF = "WS-DM-TASK.TASK-CONF";
private String usernameOwner;
private DataMinerAccessPoint dataMinerAP;
private JsonUtil<TaskConfiguration> jsonUtil = new JsonUtil<TaskConfiguration>();
private JsonUtil jsonUtil = new JsonUtil();
/**
* Gets the data miner access point.
*
* @return the data miner access point
*/
private DataMinerAccessPoint getDataMinerAccessPoint(){
if(dataMinerAP==null)
dataMinerAP = new DataMinerAccessPoint();
@ -92,6 +97,7 @@ public class WorkspaceDataMinerTaskExecutor implements ExecutableTask<TaskConfig
* Validate task configuration.
*
* @param taskConfiguration the task configuration
* @throws Exception the exception
*/
private static void ValidateTaskConfiguration(TaskConfiguration taskConfiguration) throws Exception {
Validate.notNull(taskConfiguration, "The "+TaskConfiguration.class.getSimpleName()+" is null");
@ -108,9 +114,9 @@ public class WorkspaceDataMinerTaskExecutor implements ExecutableTask<TaskConfig
TaskConfiguration conf = null;
checkOwner();
WorkspaceItem item = WsUtil.getItem(usernameOwner, workspaceItemId);
String propConfigValue = WsUtil.getPropertyValue(item, WS_TASK_TASK_CONF);
String propConfigValue = WsUtil.getPropertyValue(item, WS_DM_TASK_TASK_CONF);
logger.info("Read "+WS_TASK_TASK_CONF+" value: "+propConfigValue);
logger.info("Read "+WS_DM_TASK_TASK_CONF+" value: "+propConfigValue);
if(propConfigValue==null || propConfigValue.isEmpty())
throw new ItemNotExecutableException("The item id "+workspaceItemId+" has not a "+TaskConfiguration.class.getSimpleName());
@ -135,16 +141,16 @@ public class WorkspaceDataMinerTaskExecutor implements ExecutableTask<TaskConfig
checkOwner();
WorkspaceItem item = WsUtil.getItem(usernameOwner, workspaceItemId);
String propConfigValue = WsUtil.getPropertyValue(item, WS_TASK_TASK_CONF);
String propConfigValue = WsUtil.getPropertyValue(item, WS_DM_TASK_TASK_CONF);
if(propConfigValue==null || propConfigValue.isEmpty()){
logger.debug("The item: "+workspaceItemId+" has not a configuration "+WS_TASK_TASK_CONF);
logger.debug("The item: "+workspaceItemId+" has not a configuration "+WS_DM_TASK_TASK_CONF);
return false;
}
try{
jsonUtil.readObject(propConfigValue, TaskConfiguration.class);
logger.debug("The item: "+workspaceItemId+" has a valid "+WS_TASK_TASK_CONF+" configuration");
logger.debug("The item: "+workspaceItemId+" has a valid "+WS_DM_TASK_TASK_CONF+" configuration");
return true;
}catch(Exception e){
logger.error("Error on serializing configuration: "+propConfigValue, e);
@ -164,13 +170,13 @@ public class WorkspaceDataMinerTaskExecutor implements ExecutableTask<TaskConfig
checkOwner();
WorkspaceItem item = WsUtil.getItem(usernameOwner, taskConfiguration.getWorkspaceItemId());
String propConfig = WsUtil.getPropertyValue(item, WS_TASK_TASK_CONF);
String propConfig = WsUtil.getPropertyValue(item, WS_DM_TASK_TASK_CONF);
if(propConfig==null || propConfig.isEmpty())
throw new ItemNotExecutableException("The item id "+taskConfiguration.getWorkspaceItemId()+" has not a "+TaskConfiguration.class.getSimpleName());
WsUtil.setPropertyValue(item, WS_TASK_TASK_CONF, null);
logger.info("Set property value "+WS_TASK_TASK_CONF+" as null for the workspace item id: "+taskConfiguration.getWorkspaceItemId());
WsUtil.setPropertyValue(item, WS_DM_TASK_TASK_CONF, null);
logger.info("Set property value "+WS_DM_TASK_TASK_CONF+" as null for the workspace item id: "+taskConfiguration.getWorkspaceItemId());
return true;
}
@ -194,7 +200,7 @@ public class WorkspaceDataMinerTaskExecutor implements ExecutableTask<TaskConfig
}
if(jsonConfValue!=null){
WsUtil.setPropertyValue(item, WS_TASK_TASK_CONF, jsonConfValue);
WsUtil.setPropertyValue(item, WS_DM_TASK_TASK_CONF, jsonConfValue);
return true;
}

View File

@ -14,7 +14,6 @@ import java.util.Map;
*/
public interface BaseTaskConfiguration {
/**
* Gets the task id.
*
@ -46,4 +45,21 @@ public interface BaseTaskConfiguration {
Map<String, String> getMapParameters();
/**
* Gets the access key to retrieve this configuration from gcube properties saved in the workspace.
*
* @return the access key
*/
String getAccessKey();
/**
* Sets the access key to save this configuration in the workspace gcube properties.
*
* @param accessKey the access key
*/
void setAccessKey(String accessKey);
}

View File

@ -12,6 +12,7 @@ import org.gcube.common.workspacetaskexecutor.shared.BaseTaskConfiguration;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
/**
* The Class TaskConfiguration.
*
@ -24,42 +25,64 @@ public class TaskConfiguration implements BaseTaskConfiguration, Serializable {
*
*/
private static final long serialVersionUID = -3380573762288127547L;
private String algorithmId;
private String taskId;
@JsonIgnoreProperties
private String taskDescription; //optional
private String token;
private String workspaceItemId;
private Map<String, String> mapParameters;
@JsonIgnoreProperties
private Map<String, String> mapParameters; //optional
@JsonIgnoreProperties
private String accessKey;
/**
* Instantiates a new run algorithm configuration.
* Instantiates a new task configuration.
*/
public TaskConfiguration() {
}
/**
* Instantiates a new task configuration.
*
* @param algorithmId the algorithm id
* @param taskDescription the task description
* @param token the token
* @param workspaceItemId the workspace item id
* @param mapParameters the map parameters
* @param taskId
* @param taskDescription
* @param token
* @param workspaceItemId
* @param mapParameters
*/
public TaskConfiguration(
String algorithmId, String taskDescription, String token,
String taskId, String taskDescription, String token,
String workspaceItemId, Map<String, String> mapParameters) {
super();
this.algorithmId = algorithmId;
this.taskId = taskId;
this.taskDescription = taskDescription;
this.token = token;
this.workspaceItemId = workspaceItemId;
this.mapParameters = mapParameters;
}
/* (non-Javadoc)
* @see org.gcube.common.workspacetaskexecutor.shared.BaseTaskConfiguration#getAccessKey()
*/
@Override
public String getAccessKey() {
if(accessKey==null)
accessKey = hashCode()+"";
return accessKey;
}
/* (non-Javadoc)
* @see org.gcube.common.workspacetaskexecutor.shared.BaseTaskConfiguration#setAccessKey(java.lang.String)
*/
@Override
public void setAccessKey(String accessKey) {
this.accessKey = accessKey;
}
/* (non-Javadoc)
* @see org.gcube.common.workspacetaskexecutor.TaskConfiguration#getTaskId()
@ -67,22 +90,10 @@ public class TaskConfiguration implements BaseTaskConfiguration, Serializable {
@Override
public String getTaskId() {
return algorithmId;
return taskId;
}
/**
* Gets the algorithm id.
*
* @return the algorithmId
*/
public String getAlgorithmId() {
return algorithmId;
}
/**
* Gets the task description.
*
@ -131,17 +142,6 @@ public class TaskConfiguration implements BaseTaskConfiguration, Serializable {
/**
* Sets the algorithm id.
*
* @param algorithmId the algorithmId to set
*/
public void setAlgorithmId(String algorithmId) {
this.algorithmId = algorithmId;
}
/**
* Sets the task description.
@ -154,6 +154,14 @@ public class TaskConfiguration implements BaseTaskConfiguration, Serializable {
}
/**
* @param taskId the taskId to set
*/
public void setTaskId(String taskId) {
this.taskId = taskId;
}
/**
* Sets the token.
@ -178,7 +186,6 @@ public class TaskConfiguration implements BaseTaskConfiguration, Serializable {
}
/**
* Sets the map parameters.
*
@ -190,9 +197,6 @@ public class TaskConfiguration implements BaseTaskConfiguration, Serializable {
}
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@ -200,9 +204,36 @@ public class TaskConfiguration implements BaseTaskConfiguration, Serializable {
public int hashCode() {
int hash = 1;
hash = hash * 13 + (algorithmId == null ? 0 : algorithmId.hashCode());
hash = hash * 13 + (taskId == null ? 0 : taskId.hashCode());
hash = hash * 17 + (workspaceItemId == null ? 0 : workspaceItemId.hashCode());
return hash;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("TaskConfiguration [taskId=");
builder.append(taskId);
builder.append(", taskDescription=");
builder.append(taskDescription);
builder.append(", token=");
builder.append(token);
builder.append(", workspaceItemId=");
builder.append(workspaceItemId);
builder.append(", mapParameters=");
builder.append(mapParameters);
builder.append(", accessKey=");
builder.append(accessKey);
builder.append("]");
return builder.toString();
}
}

View File

@ -4,43 +4,58 @@
package org.gcube.common.workspacetaskexecutor.util;
import java.io.IOException;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
/**
* The Class JsonUtil.
*
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* Apr 26, 2018
* @param <T> the generic type
* May 2, 2018
*/
public class JsonUtil<T> {
public class JsonUtil {
private ObjectMapper mapper = new ObjectMapper();
private ObjectMapper objectMapper = new ObjectMapper();
/**
* Instantiates a new json util.
*/
public JsonUtil() {
objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
}
/**
* To json.
*
* @param <T> the generic type
* @param obj the obj
* @return the string
* @throws JsonProcessingException the json processing exception
*/
public String toJSON(T obj) throws JsonProcessingException{
public <T> String toJSON(T obj) throws JsonProcessingException{
// Convert object to JSON string
return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
return objectMapper.writeValueAsString(obj);
}
/**
* Read object.
*
* @param <T> the generic type
* @param json the json
* @param obj the obj
* @return the t
@ -48,8 +63,122 @@ public class JsonUtil<T> {
* @throws JsonMappingException the json mapping exception
* @throws IOException Signals that an I/O exception has occurred.
*/
public T readObject(String json, Class<T> obj) throws JsonParseException, JsonMappingException, IOException{
public <T> T readObject(String json, Class<T> obj) throws JsonParseException, JsonMappingException, IOException{
// Convert JSON string from file to Object
return mapper.readValue(json, obj);
return objectMapper.readValue(json, obj);
}
/**
* Read list.
*
* @param <T> the generic type
* @param arrayToJson the array to json
* @param mapType the map type
* @return the list
* @throws JsonParseException the json parse exception
* @throws JsonMappingException the json mapping exception
* @throws IOException Signals that an I/O exception has occurred.
*/
public <T> List<T> readList(String arrayToJson, TypeReference<List<T>> mapType) throws JsonParseException, JsonMappingException, IOException{
// Convert JSON string from file to Object
return objectMapper.readValue(arrayToJson, mapType);
}
/**
* To json array.
*
* @param <T> the generic type
* @param obj the obj
* @return the string
* @throws JsonProcessingException the json processing exception
*/
public <T> String toJSONArray(List<T> obj) throws JsonProcessingException{
// Convert List<T> to JSON string
return objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
}
/**
* Removes the json object with the (key,value) passed in input.
*
* @param jsonArray the json array
* @param key the key
* @param value the value
* @return the JSON array
*/
public JSONArray removeJsonObject(JSONArray jsonArray, String key, String value){
if (jsonArray != null) {
JSONArray newArray = new JSONArray(); //THE NEW JSON ARRAY
for (int i=0; i < jsonArray.length(); i++){
JSONObject itemArr;
try {
itemArr = (JSONObject) jsonArray.get(i);
//IF JSON OBJECT DOES NOT CONTAIN THE PROPERTY
//RETURNING IT IN THE NEW JSON ARRAY
if(!itemArr.getString(key).equals(value)){
newArray.put(itemArr);
}
}
catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return newArray;
}
return null;
}
/**
* Update the input json object.
* If the (key,value) is found in the jsonArray, it updates the object. Otherwise the jsonObject is added to jsonArray
*
* @param jsonArray the json array
* @param jsonObject the json object
* @param key the key
* @param value the value
* @return the JSON array
*/
public JSONArray updateJsonObject(JSONArray jsonArray, JSONObject jsonObject, String key, String value){
if (jsonArray != null) {
boolean found = false;
JSONArray newArray = new JSONArray(); //THE NEW JSON ARRAY
for (int i=0; i < jsonArray.length(); i++){
JSONObject itemArr;
try {
itemArr = (JSONObject) jsonArray.get(i);
//IF THE I-MO JSON OBJECT CONTAINS THE INPUT (KEY,VALUE)
//UPDATING IT WITH THE NEW INPUT OBJECT
if(itemArr.getString(key).equals(value)){
newArray.put(jsonObject);
found = true;
}else
newArray.put(itemArr);
}
catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(!found){
newArray.put(jsonObject);
}
return newArray;
}
return null;
}
}

View File

@ -3,12 +3,20 @@
*/
package org.gcube.common.workspacetaskexecutor;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.common.workspacetaskexecutor.dataminer.WorkspaceDataMinerTaskExecutor;
import org.gcube.common.workspacetaskexecutor.shared.dataminer.TaskConfiguration;
import org.gcube.common.workspacetaskexecutor.util.JsonUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
/**
@ -24,11 +32,14 @@ public class TestDataMinerTaskExecutor {
public static String WORKSPACE_FOLDER_ID = "f5c2f5ab-5c35-4418-8e60-d48d173538ed";
public static void main(String[] args) {
ScopeProvider.instance.set(SCOPE);
WorkspaceDataMinerTaskExecutor exec = WorkspaceDataMinerTaskExecutor.getInstance();
exec.withOwner(USERNAME);
jsonCheck();
// try {
// new TaskConfiguration(algorithmId, taskDescription, token, workspaceItemId, mapParameters)
@ -77,7 +88,37 @@ public class TestDataMinerTaskExecutor {
public static TaskConfiguration createDummyConfiguration(){
Map<String, String> mapParameters = new HashMap<String, String>();
mapParameters.put("publiclink", "this is the public link");
return new TaskConfiguration("this is the task id", null, "my token", WORKSPACE_FOLDER_ID, mapParameters);
return new TaskConfiguration(UUID.randomUUID().toString(), null, "my token", WORKSPACE_FOLDER_ID, mapParameters);
}
public static void jsonCheck(){
TaskConfiguration c1 = createDummyConfiguration();
TaskConfiguration c2 = createDummyConfiguration();
List<TaskConfiguration> listConfigurations = new ArrayList<>();
listConfigurations.add(c1);
listConfigurations.add(c2);
JsonUtil jUtil = new JsonUtil();
try {
String jsonArray = jUtil.toJSONArray(listConfigurations);
System.out.println("Json array: "+jsonArray);
TypeReference<List<TaskConfiguration>> mapType = new TypeReference<List<TaskConfiguration>>() {};
List<TaskConfiguration> listUnM = jUtil.readList(jsonArray, mapType);
System.out.println("Json array to listUnM...");
for (TaskConfiguration taskConfiguration : listUnM) {
System.out.println(taskConfiguration);
}
}
catch (JsonProcessingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}