245 lines
8.1 KiB
Java
245 lines
8.1 KiB
Java
package org.gcube.common.workspacetaskexecutor.dataminer;
|
|
|
|
import org.apache.commons.lang.Validate;
|
|
import org.gcube.common.homelibrary.home.workspace.WorkspaceItem;
|
|
import org.gcube.common.workspacetaskexecutor.shared.CheckableTask;
|
|
import org.gcube.common.workspacetaskexecutor.shared.ConfigurableTask;
|
|
import org.gcube.common.workspacetaskexecutor.shared.ExecutableTask;
|
|
import org.gcube.common.workspacetaskexecutor.shared.dataminer.TaskConfiguration;
|
|
import org.gcube.common.workspacetaskexecutor.shared.dataminer.TaskExecutionStatus;
|
|
import org.gcube.common.workspacetaskexecutor.shared.exception.ItemNotExecutableException;
|
|
import org.gcube.common.workspacetaskexecutor.shared.exception.TaskErrorException;
|
|
import org.gcube.common.workspacetaskexecutor.shared.exception.TaskNotExecutableException;
|
|
import org.gcube.common.workspacetaskexecutor.util.JsonUtil;
|
|
import org.gcube.common.workspacetaskexecutor.util.WsUtil;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
/**
|
|
* The Class WorkspaceDataMinerTaskExecute.
|
|
*
|
|
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
|
|
* Apr 26, 2018
|
|
*/
|
|
public class WorkspaceDataMinerTaskExecutor implements ExecutableTask<TaskConfiguration, TaskExecutionStatus>, ConfigurableTask<TaskConfiguration>, CheckableTask<TaskConfiguration>{
|
|
|
|
/** The logger. */
|
|
private static Logger logger = LoggerFactory.getLogger(WorkspaceDataMinerTaskExecutor.class);
|
|
|
|
private static WorkspaceDataMinerTaskExecutor INSTANCE = null;
|
|
|
|
public static final String WS_TASK_TASK_CONF = "WS-TASK.TASK-CONF";
|
|
|
|
private String usernameOwner;
|
|
|
|
private DataMinerAccessPoint dataMinerAP;
|
|
|
|
private JsonUtil<TaskConfiguration> jsonUtil = new JsonUtil<TaskConfiguration>();
|
|
|
|
|
|
private DataMinerAccessPoint getDataMinerAccessPoint(){
|
|
if(dataMinerAP==null)
|
|
dataMinerAP = new DataMinerAccessPoint();
|
|
|
|
return dataMinerAP;
|
|
}
|
|
|
|
/**
|
|
* Instantiates a new workspace data miner task executor.
|
|
*/
|
|
private WorkspaceDataMinerTaskExecutor() {
|
|
}
|
|
|
|
|
|
/**
|
|
* With owner.
|
|
*
|
|
* @param usernameOwner the username owner
|
|
*/
|
|
public void withOwner(String usernameOwner){
|
|
this.usernameOwner = usernameOwner;
|
|
}
|
|
|
|
|
|
/**
|
|
* Gets the single instance of WorkspaceDataMinerTaskExecutor.
|
|
*
|
|
* @return single instance of WorkspaceDataMinerTaskExecutor
|
|
*/
|
|
public static WorkspaceDataMinerTaskExecutor getInstance() {
|
|
if (INSTANCE == null) {
|
|
INSTANCE = new WorkspaceDataMinerTaskExecutor();
|
|
}
|
|
return INSTANCE;
|
|
}
|
|
|
|
|
|
/**
|
|
* Check owner.
|
|
*
|
|
* @throws Exception the exception
|
|
*/
|
|
private void checkOwner() throws Exception {
|
|
|
|
if(usernameOwner==null || usernameOwner.isEmpty())
|
|
throw new Exception("You must set a valid 'usernameOwner'. Using the method #withOwner");
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
* Validate task configuration.
|
|
*
|
|
* @param taskConfiguration the task configuration
|
|
*/
|
|
private static void ValidateTaskConfiguration(TaskConfiguration taskConfiguration) throws Exception {
|
|
Validate.notNull(taskConfiguration, "The "+TaskConfiguration.class.getSimpleName()+" is null");
|
|
Validate.notNull(taskConfiguration.getWorkspaceItemId(), "The WorkspaceItem Id in the configuration is null");
|
|
Validate.notNull(taskConfiguration.getTaskId(), "The Task Id in the configuration is null");
|
|
}
|
|
|
|
/* (non-Javadoc)
|
|
* @see org.gcube.common.workspacetaskexecutor.CheckableTask#checkItemExecutable(java.lang.String)
|
|
*/
|
|
@Override
|
|
public TaskConfiguration checkItemExecutable(String workspaceItemId) throws ItemNotExecutableException, Exception {
|
|
|
|
TaskConfiguration conf = null;
|
|
checkOwner();
|
|
WorkspaceItem item = WsUtil.getItem(usernameOwner, workspaceItemId);
|
|
String propConfigValue = WsUtil.getPropertyValue(item, WS_TASK_TASK_CONF);
|
|
|
|
logger.info("Read "+WS_TASK_TASK_CONF+" value: "+propConfigValue);
|
|
|
|
if(propConfigValue==null || propConfigValue.isEmpty())
|
|
throw new ItemNotExecutableException("The item id "+workspaceItemId+" has not a "+TaskConfiguration.class.getSimpleName());
|
|
|
|
try{
|
|
conf = jsonUtil.readObject(propConfigValue, TaskConfiguration.class);
|
|
}catch(Exception e){
|
|
logger.error("Error on serializing configuration: "+propConfigValue, e);
|
|
throw new ItemNotExecutableException("The item id "+workspaceItemId+" has a wrong "+TaskConfiguration.class.getSimpleName()+". Do you want create a new one?");
|
|
}
|
|
|
|
logger.info("Found configuration: "+conf);
|
|
|
|
return conf;
|
|
}
|
|
|
|
/* (non-Javadoc)
|
|
* @see org.gcube.common.workspacetaskexecutor.CheckableTask#isItemExecutable(java.lang.String)
|
|
*/
|
|
@Override
|
|
public Boolean isItemExecutable(String workspaceItemId) throws ItemNotExecutableException, Exception {
|
|
|
|
checkOwner();
|
|
WorkspaceItem item = WsUtil.getItem(usernameOwner, workspaceItemId);
|
|
String propConfigValue = WsUtil.getPropertyValue(item, WS_TASK_TASK_CONF);
|
|
|
|
if(propConfigValue==null || propConfigValue.isEmpty()){
|
|
logger.debug("The item: "+workspaceItemId+" has not a configuration "+WS_TASK_TASK_CONF);
|
|
return false;
|
|
}
|
|
|
|
try{
|
|
jsonUtil.readObject(propConfigValue, TaskConfiguration.class);
|
|
logger.debug("The item: "+workspaceItemId+" has a valid "+WS_TASK_TASK_CONF+" configuration");
|
|
return true;
|
|
}catch(Exception e){
|
|
logger.error("Error on serializing configuration: "+propConfigValue, e);
|
|
return false;
|
|
}
|
|
|
|
}
|
|
|
|
|
|
/* (non-Javadoc)
|
|
* @see org.gcube.common.workspacetaskexecutor.ConfigurableTask#removeTaskConfig(java.lang.Object)
|
|
*/
|
|
@Override
|
|
public Boolean removeTaskConfiguration(TaskConfiguration taskConfiguration) throws ItemNotExecutableException, Exception {
|
|
|
|
ValidateTaskConfiguration(taskConfiguration);
|
|
checkOwner();
|
|
|
|
WorkspaceItem item = WsUtil.getItem(usernameOwner, taskConfiguration.getWorkspaceItemId());
|
|
String propConfig = WsUtil.getPropertyValue(item, WS_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());
|
|
return true;
|
|
|
|
}
|
|
|
|
/* (non-Javadoc)
|
|
* @see org.gcube.common.workspacetaskexecutor.ConfigurableTask#addTaskConfig(java.lang.Object)
|
|
*/
|
|
@Override
|
|
public Boolean setTaskConfiguration(TaskConfiguration taskConfiguration) throws Exception {
|
|
|
|
ValidateTaskConfiguration(taskConfiguration);
|
|
checkOwner();
|
|
WorkspaceItem item = WsUtil.getItem(usernameOwner, taskConfiguration.getWorkspaceItemId());
|
|
String jsonConfValue = null;
|
|
|
|
try{
|
|
jsonConfValue = jsonUtil.toJSON(taskConfiguration);
|
|
logger.debug("The json configuration is: "+jsonConfValue);
|
|
}catch(Exception e){
|
|
throw new Exception("This "+taskConfiguration+" is wrong!. Please create a new one");
|
|
}
|
|
|
|
if(jsonConfValue!=null){
|
|
WsUtil.setPropertyValue(item, WS_TASK_TASK_CONF, jsonConfValue);
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/* (non-Javadoc)
|
|
* @see org.gcube.common.workspacetaskexecutor.ExecutableTask#doRun(org.gcube.common.workspacetaskexecutor.BaseTaskConfiguration)
|
|
*/
|
|
@Override
|
|
public TaskExecutionStatus doRun(TaskConfiguration taskConfiguration)
|
|
throws ItemNotExecutableException, TaskNotExecutableException, Exception {
|
|
|
|
ValidateTaskConfiguration(taskConfiguration);
|
|
checkOwner();
|
|
|
|
TaskConfiguration conf = checkItemExecutable(taskConfiguration.getWorkspaceItemId());
|
|
|
|
DataMinerAccessPoint dap = getDataMinerAccessPoint();
|
|
return dap.doRunTask(conf);
|
|
}
|
|
|
|
/* (non-Javadoc)
|
|
* @see org.gcube.common.workspacetaskexecutor.ExecutableTask#stopRun(org.gcube.common.workspacetaskexecutor.BaseTaskConfiguration)
|
|
*/
|
|
@Override
|
|
public Boolean stopRun(TaskConfiguration taskConfiguration)
|
|
throws TaskErrorException, Exception {
|
|
|
|
// TODO Auto-generated method stub
|
|
return null;
|
|
}
|
|
|
|
/* (non-Javadoc)
|
|
* @see org.gcube.common.workspacetaskexecutor.ExecutableTask#monitorRunStatus(org.gcube.common.workspacetaskexecutor.BaseTaskConfiguration)
|
|
*/
|
|
@Override
|
|
public TaskExecutionStatus monitorRunStatus(
|
|
TaskConfiguration taskConfiguration)
|
|
throws TaskErrorException, Exception {
|
|
|
|
ValidateTaskConfiguration(taskConfiguration);
|
|
DataMinerAccessPoint dap = getDataMinerAccessPoint();
|
|
return dap.getTaskStatus(taskConfiguration);
|
|
}
|
|
|
|
}
|