Enhancement on[Project Activity #11690]

git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/Common/workspace-task-executor-library@167241 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Francesco Mangiacrapa 2018-04-26 14:21:08 +00:00
parent 063842ad48
commit 9a9dab6c1e
2 changed files with 203 additions and 6 deletions

View File

@ -0,0 +1,137 @@
/**
*
*/
package org.gcube.common.workspacetaskexecutor;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang.Validate;
import org.gcube.common.homelibrary.home.HomeLibrary;
import org.gcube.common.homelibrary.home.exceptions.HomeNotFoundException;
import org.gcube.common.homelibrary.home.exceptions.InternalErrorException;
import org.gcube.common.homelibrary.home.workspace.Properties;
import org.gcube.common.homelibrary.home.workspace.Workspace;
import org.gcube.common.homelibrary.home.workspace.WorkspaceItem;
import org.gcube.common.homelibrary.home.workspace.exceptions.ItemNotFoundException;
import org.gcube.common.homelibrary.home.workspace.exceptions.WorkspaceFolderNotFoundException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The Class WsUtil.
*
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* Apr 26, 2018
*/
public class WsUtil {
private static Logger logger = LoggerFactory.getLogger(WsUtil.class);
/**
* Gets the workspace.
*
* @param username the username
* @return the workspace
* @throws InternalErrorException the internal error exception
* @throws HomeNotFoundException the home not found exception
* @throws WorkspaceFolderNotFoundException the workspace folder not found exception
*/
public static Workspace getWorkspace(String username) throws InternalErrorException, HomeNotFoundException, WorkspaceFolderNotFoundException{
logger.trace("Get Workspace");
Validate.notNull(username, "The username is null");
return HomeLibrary.getUserWorkspace(username);
}
/**
* Gets the item.
*
* @param username the username
* @param itemId the item id
* @return the item
* @throws InternalErrorException the internal error exception
* @throws HomeNotFoundException the home not found exception
* @throws WorkspaceFolderNotFoundException the workspace folder not found exception
* @throws ItemNotFoundException the item not found exception
*/
public static WorkspaceItem getItem(String username, String itemId) throws InternalErrorException, HomeNotFoundException, WorkspaceFolderNotFoundException, ItemNotFoundException{
logger.trace("Get Workspace Item");
Validate.notNull(itemId, "The itemId is null");
return getWorkspace(username).getItem(itemId);
}
/**
* Gets the properties.
*
* @param item the item
* @return the properties
*/
public static Map<String, String> getProperties(WorkspaceItem item) {
Properties properties;
try {
properties = item.getProperties();
if (properties == null)
return null;
return properties.getProperties();
}
catch (InternalErrorException e) {
return null;
}
}
/**
* Gets the properties.
*
* @param item the item
* @param propertyName the property name
* @return the properties
*/
public static String getPropertyValue(WorkspaceItem item, String propertyName){
Map<String, String> properties = getProperties(item);
if(properties==null)
return null;
return properties.get(propertyName);
}
/**
* Sets the property value.
*
* @param item the item
* @param propertyName the property name
* @param propertyValue the property value
* @return true, if successful
*/
public static boolean setPropertyValue(WorkspaceItem item, String propertyName, String propertyValue){
Map<String, String> properties = getProperties(item);
try {
if(properties==null){
properties = new HashMap<String, String>();
}
Properties propertiesOBJ = item.getProperties();
properties.put(propertyName, propertyValue);
propertiesOBJ.addProperties(properties);
return true;
}
catch (InternalErrorException e) {
return false;
}
}
}

View File

@ -1,25 +1,76 @@
package org.gcube.common.workspacetaskexecutor.dataminer;
import org.gcube.common.homelibrary.home.workspace.WorkspaceItem;
import org.gcube.common.workspacetaskexecutor.CheckableTask;
import org.gcube.common.workspacetaskexecutor.ConfigurableTask;
import org.gcube.common.workspacetaskexecutor.ExecutableTask;
import org.gcube.common.workspacetaskexecutor.TaskConfiguration;
import org.gcube.common.workspacetaskexecutor.WsUtil;
import org.gcube.common.workspacetaskexecutor.shared.ItemNotExecutable;
import org.gcube.common.workspacetaskexecutor.shared.dataminer.AlgorithmConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
// TODO: Auto-generated Javadoc
/**
* The Class WorkspaceThreddsSynchronize.
* The Class WorkspaceDataMinerTaskExecute.
*
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* Feb 14, 2018
* Apr 26, 2018
*/
public class WorkspaceDataMinerTaskExecute implements ExecutableTask<AlgorithmConfiguration>, ConfigurableTask<AlgorithmConfiguration>, CheckableTask<AlgorithmConfiguration>{
public class WorkspaceDataMinerTaskExecutor implements ExecutableTask<AlgorithmConfiguration>, ConfigurableTask<AlgorithmConfiguration>, CheckableTask<AlgorithmConfiguration>{
/** The logger. */
private static Logger logger = LoggerFactory.getLogger(WorkspaceDataMinerTaskExecute.class);
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;
/**
* 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");
}
/* (non-Javadoc)
* @see org.gcube.common.workspacetaskexecutor.CheckableTask#checkItemExecutable(java.lang.String)
@ -27,10 +78,19 @@ public class WorkspaceDataMinerTaskExecute implements ExecutableTask<AlgorithmCo
@Override
public AlgorithmConfiguration checkItemExecutable(String itemId) throws ItemNotExecutable, Exception {
// TODO Auto-generated method stub
checkOwner();
WorkspaceItem item = WsUtil.getItem(usernameOwner, itemId);
String propConfig = WsUtil.getPropertyValue(item, WS_TASK_TASK_CONF);
if(propConfig==null)
throw new ItemNotExecutable("The item id "+itemId+" has not a "+TaskConfiguration.class.getSimpleName());
//ObjectMapper mapper = new ObjectMapper();
return null;
}
/* (non-Javadoc)
* @see org.gcube.common.workspacetaskexecutor.ConfigurableTask#removeTaskConfig(java.lang.Object)
*/