Repository: oodt Updated Branches: refs/heads/master 9b7a5e438 -> a82ee5db9
Fix for OODT-213 Reading/Writing API for XML-based Workflow Task policy. Project: http://git-wip-us.apache.org/repos/asf/oodt/repo Commit: http://git-wip-us.apache.org/repos/asf/oodt/commit/a82ee5db Tree: http://git-wip-us.apache.org/repos/asf/oodt/tree/a82ee5db Diff: http://git-wip-us.apache.org/repos/asf/oodt/diff/a82ee5db Branch: refs/heads/master Commit: a82ee5db935218e5c32dba2325518a254b8e1b4f Parents: 9b7a5e4 Author: Chris Mattmann <[email protected]> Authored: Fri Jul 14 21:26:36 2017 -0700 Committer: Chris Mattmann <[email protected]> Committed: Fri Jul 14 21:26:36 2017 -0700 ---------------------------------------------------------------------- CHANGES.txt | 2 + .../workflow/policy/EnvSavingConfiguration.java | 62 ++++++ .../policy/EnvVarSavingConfigReader.java | 53 +++++ .../policy/PolicyAwareWorkflowTask.java | 117 ++++++++++ .../cas/workflow/policy/TaskPolicyMetKeys.java | 52 +++++ .../cas/workflow/policy/TaskPolicyReader.java | 212 ++++++++++++++++++ .../cas/workflow/policy/TaskPolicyWriter.java | 222 +++++++++++++++++++ .../cas/workflow/policy/TaskRequestFactory.java | 130 +++++++++++ .../policy/TaskRequestProcessorMetKeys.java | 38 ++++ 9 files changed, 888 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/oodt/blob/a82ee5db/CHANGES.txt ---------------------------------------------------------------------- diff --git a/CHANGES.txt b/CHANGES.txt index 74e2e82..74e802d 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -3,6 +3,8 @@ Apache OODT Change Log Release 1.1 - Current Development +* OODT-213 Reading/Writing API for XML-based Workflow Task policy (mattmann) + * OODT-775 XSD's for pushpull's mimetypes.xml (Lewis John McGibbney via mattmann) * OODT-847 New FilemgrMetExtractor to extract File System Attributes (Aditya Dhulipala, rverma via mattmann) http://git-wip-us.apache.org/repos/asf/oodt/blob/a82ee5db/workflow/src/main/java/org/apache/oodt/cas/workflow/policy/EnvSavingConfiguration.java ---------------------------------------------------------------------- diff --git a/workflow/src/main/java/org/apache/oodt/cas/workflow/policy/EnvSavingConfiguration.java b/workflow/src/main/java/org/apache/oodt/cas/workflow/policy/EnvSavingConfiguration.java new file mode 100644 index 0000000..a3c9d50 --- /dev/null +++ b/workflow/src/main/java/org/apache/oodt/cas/workflow/policy/EnvSavingConfiguration.java @@ -0,0 +1,62 @@ +//Copyright (c) 2008, California Institute of Technology. +//ALL RIGHTS RESERVED. U.S. Government sponsorship acknowledged. +// +//$Id$ + +package org.apache.oodt.cas.workflow.policy; + +//JDK imports +import java.util.Properties; + +//OODT imports +import org.apache.oodt.cas.workflow.structs.WorkflowTaskConfiguration; + +/** + * @author mattmann + * @version $Revision$ + * + * <p> + * A {@link WorkflowTaskConfiguration} that preserves whether or not a task config + * property was envReplace'd or not. + * </p>. + */ +public class EnvSavingConfiguration extends WorkflowTaskConfiguration { + + private Properties envReplaceMap; + + public EnvSavingConfiguration() { + super(); + envReplaceMap = new Properties(); + } + + /* + * (non-Javadoc) + * + * @see gov.nasa.jpl.oodt.cas.workflow.structs.WorkflowTaskConfiguration#addConfigProperty(java.lang.String, + * java.lang.String) + */ + public void addConfigProperty(String propName, String propVal) { + addConfigProperty(propName, propVal, false); + + } + + /** + * Adds a configuration property, along with its envReplace + * information. + * + * @param propName The workflow config property name. + * @param propVal The workflow config property val. + * @param isReplace Whether or not the property should be envReplaced. + */ + public void addConfigProperty(String propName, String propVal, + boolean isReplace) { + super.addConfigProperty(propName, propVal); + envReplaceMap.put(propName, String.valueOf(isReplace)); + } + + public boolean isReplace(String propName) { + return Boolean.valueOf((String) this.envReplaceMap.get(propName)) + .booleanValue(); + } + +} http://git-wip-us.apache.org/repos/asf/oodt/blob/a82ee5db/workflow/src/main/java/org/apache/oodt/cas/workflow/policy/EnvVarSavingConfigReader.java ---------------------------------------------------------------------- diff --git a/workflow/src/main/java/org/apache/oodt/cas/workflow/policy/EnvVarSavingConfigReader.java b/workflow/src/main/java/org/apache/oodt/cas/workflow/policy/EnvVarSavingConfigReader.java new file mode 100644 index 0000000..d9d1cf1 --- /dev/null +++ b/workflow/src/main/java/org/apache/oodt/cas/workflow/policy/EnvVarSavingConfigReader.java @@ -0,0 +1,53 @@ +//Copyright (c) 2008, California Institute of Technology. +//ALL RIGHTS RESERVED. U.S. Government sponsorship acknowledged. +// +//$Id$ + +package org.apache.oodt.cas.workflow.policy; + +//JDK imports +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +/** + * @author mattmann + * @version $Revision$ + * + * <p> + * Provides a utility method to read in a {@link WorkflowConfiguration} from an + * XML {@link Node}, preserving its envReplace information. + * </p>. + */ +public class EnvVarSavingConfigReader { + + private EnvVarSavingConfigReader() throws InstantiationException { + throw new InstantiationException("Don't construct readers!"); + } + + public static EnvSavingConfiguration getConfiguration(Node node) { + Element configNode = (Element) node; + + NodeList configProperties = configNode.getElementsByTagName("property"); + + EnvSavingConfiguration config = null; + + if (configProperties == null) { + return null; + } + + config = new EnvSavingConfiguration(); + for (int i = 0; i < configProperties.getLength(); i++) { + Element propElem = (Element) configProperties.item(i); + String value = propElem.getAttribute("value"); + boolean doReplace = Boolean.valueOf( + propElem.getAttribute("envReplace")).booleanValue(); + + config.addConfigProperty(propElem.getAttribute("name"), value, + doReplace); + } + + return config; + } + +} http://git-wip-us.apache.org/repos/asf/oodt/blob/a82ee5db/workflow/src/main/java/org/apache/oodt/cas/workflow/policy/PolicyAwareWorkflowTask.java ---------------------------------------------------------------------- diff --git a/workflow/src/main/java/org/apache/oodt/cas/workflow/policy/PolicyAwareWorkflowTask.java b/workflow/src/main/java/org/apache/oodt/cas/workflow/policy/PolicyAwareWorkflowTask.java new file mode 100644 index 0000000..1d57780 --- /dev/null +++ b/workflow/src/main/java/org/apache/oodt/cas/workflow/policy/PolicyAwareWorkflowTask.java @@ -0,0 +1,117 @@ +//Copyright (c) 2008, California Institute of Technology. +//ALL RIGHTS RESERVED. U.S. Government sponsorship acknowledged. +// +//$Id$ + +package org.apache.oodt.cas.workflow.policy; + +//OODT imports +import java.util.Iterator; + +import org.apache.oodt.cas.workflow.structs.WorkflowCondition; +import org.apache.oodt.cas.workflow.structs.WorkflowTask; + +/** + * @author mattmann + * @version $Revision$ + * + * <p> + * A {@link WorkflowTask} that is aware of what policy directory + * that it comes from. + * </p>. + */ +public class PolicyAwareWorkflowTask extends WorkflowTask { + + private String policyDirPath; + + public PolicyAwareWorkflowTask() { + super(); + this.policyDirPath = null; + } + + public PolicyAwareWorkflowTask(WorkflowTask task) { + super(task.getTaskId(), task.getTaskName(), task.getTaskConfig(), task + .getConditions(), task.getTaskInstanceClassName(), task + .getOrder()); + this.setRequiredMetFields(task.getRequiredMetFields()); + } + + /** + * @return the policyDirPath + */ + public String getPolicyDirPath() { + return policyDirPath; + } + + /** + * @param policyDirPath + * the policyDirPath to set + */ + public void setPolicyDirPath(String policyDirPath) { + this.policyDirPath = policyDirPath; + } + + public String toString() { + StringBuffer buf = new StringBuffer(); + buf.append("task: [name="); + buf.append(this.taskName); + buf.append(",policyDirPath="); + buf.append(this.policyDirPath); + buf.append(",id="); + buf.append(this.taskId); + buf.append(",class="); + buf.append(this.taskInstanceClassName); + buf.append(",conditions=["); + + if (this.getConditions() != null && this.getConditions().size() > 0) { + for (Iterator i = this.getConditions().iterator(); i.hasNext();) { + WorkflowCondition cond = (WorkflowCondition) i.next(); + buf.append(cond.getConditionId()); + buf.append(","); + } + + buf.deleteCharAt(buf.length() - 1); + } + + buf.append("]"); + buf.append(",reqMetFields=["); + + if (this.requiredMetFields != null && this.requiredMetFields.size() > 0) { + for (Iterator i = this.requiredMetFields.iterator(); i.hasNext();) { + String metField = (String) i.next(); + buf.append(metField); + buf.append(","); + } + + buf.deleteCharAt(buf.length() - 1); + } + + buf.append("]"); + buf.append(",taskConfig=["); + + if (this.taskConfig != null + && this.taskConfig.getProperties().keySet() != null + && this.taskConfig.getProperties().keySet().size() > 0) { + for (Iterator i = this.taskConfig.getProperties().keySet() + .iterator(); i.hasNext();) { + String propName = (String) i.next(); + EnvSavingConfiguration config = (EnvSavingConfiguration) this.taskConfig; + String propVal = this.taskConfig.getProperty(propName); + buf.append("[propName="); + buf.append(propName); + buf.append(",propVal="); + buf.append(propVal); + buf.append(",envReplace="); + buf.append(String.valueOf(config.isReplace(propName))); + buf.append("],"); + } + + buf.deleteCharAt(buf.length() - 1); + } + + buf.append("]"); + + return buf.toString(); + } + +} http://git-wip-us.apache.org/repos/asf/oodt/blob/a82ee5db/workflow/src/main/java/org/apache/oodt/cas/workflow/policy/TaskPolicyMetKeys.java ---------------------------------------------------------------------- diff --git a/workflow/src/main/java/org/apache/oodt/cas/workflow/policy/TaskPolicyMetKeys.java b/workflow/src/main/java/org/apache/oodt/cas/workflow/policy/TaskPolicyMetKeys.java new file mode 100644 index 0000000..8b7eafd --- /dev/null +++ b/workflow/src/main/java/org/apache/oodt/cas/workflow/policy/TaskPolicyMetKeys.java @@ -0,0 +1,52 @@ +//Copyright (c) 2008, California Institute of Technology. +//ALL RIGHTS RESERVED. U.S. Government sponsorship acknowledged. +// +//$Id$ + +package org.apache.oodt.cas.workflow.policy; + +/** + * @author mattmann + * @version $Revision$ + * + * <p>Met keys for writing out the tasks.xml policy file</p>. + */ +public interface TaskPolicyMetKeys { + + public static final String CAS_TASKS_OUTER_ELEM = "cas:tasks"; + + public static final String CAS_XML_NS_DESC = "xmlns:cas"; + + public static final String CAS_NS = "http://oodt.jpl.nasa.gov/1.0/cas"; + + public static final String TASK_ELEM = "task"; + + public static final String TASK_ID_ATTR= "id"; + + public static final String TASK_NAME_ATTR = "name"; + + public static final String TASK_INST_CLASS_ATTR = "class"; + + public static final String TASK_CONDITIONS_ELEM = "conditions"; + + public static final String TASK_COND_ELEM = "condition"; + + public static final String TASK_COND_ID_ATTR = "id"; + + public static final String TASK_REQ_MET_FIELDS_ELEM = "requiredMetFields"; + + public static final String TASK_REQ_MET_FIELD_ELEM = "metfield"; + + public static final String TASK_REQ_MET_FIELD_NAME_ATTR = "name"; + + public static final String TASK_CONFIG_ELEM = "configuration"; + + public static final String PROPERTY_ELEM = "property"; + + public static final String PROPERTY_ELEM_NAME_ATTR = "name"; + + public static final String PROPERTY_ELEM_VALUE_ATTR = "value"; + + public static final String PROPERTY_ELEM_ENVREPLACE_ATTR = "envReplace"; + +} http://git-wip-us.apache.org/repos/asf/oodt/blob/a82ee5db/workflow/src/main/java/org/apache/oodt/cas/workflow/policy/TaskPolicyReader.java ---------------------------------------------------------------------- diff --git a/workflow/src/main/java/org/apache/oodt/cas/workflow/policy/TaskPolicyReader.java b/workflow/src/main/java/org/apache/oodt/cas/workflow/policy/TaskPolicyReader.java new file mode 100644 index 0000000..3a0dc41 --- /dev/null +++ b/workflow/src/main/java/org/apache/oodt/cas/workflow/policy/TaskPolicyReader.java @@ -0,0 +1,212 @@ +//Copyright (c) 2008, California Institute of Technology. +//ALL RIGHTS RESERVED. U.S. Government sponsorship acknowledged. +// +//$Id$ + +package org.apache.oodt.cas.workflow.policy; + +//OODT imports +import org.apache.oodt.cas.workflow.repository.XMLWorkflowRepository;//for javadoc +import org.apache.oodt.cas.workflow.structs.WorkflowCondition; +import org.apache.oodt.cas.workflow.structs.WorkflowTask; +import org.apache.oodt.cas.workflow.util.XmlStructFactory; + +//JDK imports +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.Comparator; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.TreeMap; +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; +import org.xml.sax.InputSource; + +/** + * @author mattmann + * @version $Revision$ + * + * <p> + * A reader to read in the {@link WorkflowTask} policy managed by the + * {@link XMLWorkflowRepository} + * </p>. + */ +public final class TaskPolicyReader { + + /* our log stream */ + private static final Logger LOG = Logger.getLogger(TaskPolicyReader.class + .getName()); + + private TaskPolicyReader() throws InstantiationException { + throw new InstantiationException("Don't construct reader classes!"); + } + + public static Map loadTasks(List dirUris) { + + HashMap conditionMap = loadConditions(dirUris); + Map taskMap = new TreeMap(new Comparator() { + + public int compare(Object o1, Object o2) { + String t1 = (String) o1; + String t2 = (String) o2; + + return t1.compareTo(t2); + } + + }); + + if (dirUris != null && dirUris.size() > 0) { + for (Iterator i = dirUris.iterator(); i.hasNext();) { + String dirUri = (String) i.next(); + + try { + File workflowDir = new File(new URI(dirUri)); + if (workflowDir.isDirectory()) { + String workflowDirStr = workflowDir.getAbsolutePath(); + + if (!workflowDirStr.endsWith("/")) { + workflowDirStr += "/"; + } + + Document taskRoot = getDocumentRoot(workflowDirStr + + "tasks.xml"); + + Element taskElement = taskRoot.getDocumentElement(); + + NodeList taskElemList = taskElement + .getElementsByTagName("task"); + + if (taskElemList != null + && taskElemList.getLength() > 0) { + for (int j = 0; j < taskElemList.getLength(); j++) { + Element taskElem = (Element) taskElemList + .item(j); + WorkflowTask task = XmlStructFactory + .getWorkflowTask(taskElem, conditionMap); + task.setTaskConfig(EnvVarSavingConfigReader + .getConfiguration(taskElem)); + PolicyAwareWorkflowTask pTask = new PolicyAwareWorkflowTask( + task); + pTask.setPolicyDirPath(workflowDirStr); + if (task != null) { + taskMap.put(pTask.getTaskId(), pTask); + } + } + + } + } + } catch (URISyntaxException e) { + LOG + .log( + Level.WARNING, + "DirUri: " + + dirUri + + " is not a directory: skipping task loading for it."); + } + + } + } + + return taskMap; + } + + private static HashMap loadConditions(List dirUris) { + HashMap conditionMap = new HashMap(); + + if (dirUris != null && dirUris.size() > 0) { + for (Iterator i = dirUris.iterator(); i.hasNext();) { + String dirUri = (String) i.next(); + + try { + File workflowDir = new File(new URI(dirUri)); + if (workflowDir.isDirectory()) { + String workflowDirStr = workflowDir.getAbsolutePath(); + + if (!workflowDirStr.endsWith("/")) { + workflowDirStr += "/"; + } + + Document conditionRoot = getDocumentRoot(workflowDirStr + + "conditions.xml"); + + Element conditionElement = conditionRoot + .getDocumentElement(); + + NodeList conditionElemList = conditionElement + .getElementsByTagName("condition"); + + if (conditionElemList != null + && conditionElemList.getLength() > 0) { + for (int j = 0; j < conditionElemList.getLength(); j++) { + Element conditionElem = (Element) conditionElemList + .item(j); + WorkflowCondition condition = XmlStructFactory + .getWorkflowCondition(conditionElem); + if (condition != null) { + conditionMap.put( + condition.getConditionId(), + condition); + } + } + + } + } + } catch (URISyntaxException e) { + LOG + .log( + Level.WARNING, + "DirUri: " + + dirUri + + " is not a directory: skipping condition loading for it."); + } + + } + } + + return conditionMap; + } + + private static Document getDocumentRoot(String xmlFile) { + // open up the XML file + DocumentBuilderFactory factory = null; + DocumentBuilder parser = null; + Document document = null; + InputSource inputSource = null; + + InputStream xmlInputStream = null; + + try { + xmlInputStream = new File(xmlFile).toURL().openStream(); + } catch (IOException e) { + LOG.log(Level.WARNING, + "IOException when getting input stream from [" + xmlFile + + "]: returning null document root"); + return null; + } + + inputSource = new InputSource(xmlInputStream); + + try { + factory = DocumentBuilderFactory.newInstance(); + parser = factory.newDocumentBuilder(); + document = parser.parse(inputSource); + } catch (Exception e) { + LOG.warning("Unable to parse xml file [" + xmlFile + "]." + + "Reason is [" + e + "]"); + return null; + } + + return document; + } + +} http://git-wip-us.apache.org/repos/asf/oodt/blob/a82ee5db/workflow/src/main/java/org/apache/oodt/cas/workflow/policy/TaskPolicyWriter.java ---------------------------------------------------------------------- diff --git a/workflow/src/main/java/org/apache/oodt/cas/workflow/policy/TaskPolicyWriter.java b/workflow/src/main/java/org/apache/oodt/cas/workflow/policy/TaskPolicyWriter.java new file mode 100644 index 0000000..b699c47 --- /dev/null +++ b/workflow/src/main/java/org/apache/oodt/cas/workflow/policy/TaskPolicyWriter.java @@ -0,0 +1,222 @@ +//Copyright (c) 2008, California Institute of Technology. +//ALL RIGHTS RESERVED. U.S. Government sponsorship acknowledged. +// +//$Id$ + +package org.apache.oodt.cas.workflow.policy; + +//OODT imports +import org.apache.oodt.commons.xml.XMLUtils; +import org.apache.oodt.cas.workflow.repository.XMLWorkflowRepository; +import org.apache.oodt.cas.workflow.structs.WorkflowCondition; + +//JDK imports +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.util.Comparator; +import java.util.Iterator; +import java.util.Map; +import java.util.TreeMap; +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import org.w3c.dom.Document; +import org.w3c.dom.Element; + +/** + * @author mattmann + * @version $Revision$ + * + * <p> + * A writer class to write out {@link WorkflowTask} policy that is managed by + * the {@link XMLWorkflowRepository} + * </p>. + */ +public final class TaskPolicyWriter implements TaskPolicyMetKeys { + + private static final String TASKS_XML_FILE_NAME = "tasks.xml"; + + /* our log stream */ + private static final Logger LOG = Logger.getLogger(TaskPolicyWriter.class + .getName()); + + private TaskPolicyWriter() throws InstantiationException { + throw new InstantiationException("Don't construct writers!"); + } + + public static void writeTask(PolicyAwareWorkflowTask updateTask, Map taskMap) { + String taskXmlFileFullPath = ensureEndingSlash(updateTask + .getPolicyDirPath()) + + TASKS_XML_FILE_NAME; + + // do the update + if (taskMap.containsKey(updateTask.getTaskId())) { + taskMap.put(updateTask.getTaskId(), updateTask); + } + + // subset the task map list to only be tasks from the given + // updateTask.getPolicyDirPath + Map taskSubsetMap = subsetByPolicyDirPath(taskMap, updateTask + .getPolicyDirPath()); + + try { + XMLUtils.writeXmlToStream(getTaskXmlDocument(taskSubsetMap), + new FileOutputStream(new File(taskXmlFileFullPath))); + LOG.log(Level.INFO, "Successfully updated task policy file: [" + + taskXmlFileFullPath + "]"); + } catch (FileNotFoundException e) { + e.printStackTrace(); + LOG.log(Level.WARNING, "Unable to write task policy file: [" + + taskXmlFileFullPath + "]: file not found"); + } + + } + + private static Document getTaskXmlDocument(Map policyAwareTasks) { + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + factory.setNamespaceAware(true); + Document document = null; + + try { + DocumentBuilder builder = factory.newDocumentBuilder(); + document = builder.newDocument(); + + Element root = (Element) document + .createElement(CAS_TASKS_OUTER_ELEM); + root.setAttribute(CAS_XML_NS_DESC, CAS_NS); + document.appendChild(root); + + if (policyAwareTasks != null && policyAwareTasks.size() > 0) { + for (Iterator i = policyAwareTasks.keySet().iterator(); i + .hasNext();) { + String taskId = (String) i.next(); + PolicyAwareWorkflowTask task = (PolicyAwareWorkflowTask) policyAwareTasks + .get(taskId); + + Element taskElem = (Element) document + .createElement(TASK_ELEM); + taskElem.setAttribute(TASK_ID_ATTR, task.getTaskId()); + taskElem.setAttribute(TASK_NAME_ATTR, task.getTaskName()); + taskElem.setAttribute(TASK_INST_CLASS_ATTR, task + .getTaskInstanceClassName()); + + if (task.getConditions() != null + && task.getConditions().size() > 0) { + Element conditionsElem = (Element) document + .createElement(TASK_CONDITIONS_ELEM); + for (Iterator j = task.getConditions().iterator(); j + .hasNext();) { + WorkflowCondition cond = (WorkflowCondition) j + .next(); + Element condElem = (Element) document + .createElement(TASK_COND_ELEM); + condElem.setAttribute(TASK_COND_ID_ATTR, cond + .getConditionId()); + conditionsElem.appendChild(condElem); + } + + taskElem.appendChild(conditionsElem); + } + + if (task.getRequiredMetFields() != null + && task.getRequiredMetFields().size() > 0) { + Element reqMetFieldsElem = (Element) document + .createElement(TASK_REQ_MET_FIELDS_ELEM); + + for (Iterator j = task.getRequiredMetFields() + .iterator(); j.hasNext();) { + String metField = (String) j.next(); + Element reqMetFieldElem = (Element) document + .createElement(TASK_REQ_MET_FIELD_ELEM); + reqMetFieldElem.setAttribute( + TASK_REQ_MET_FIELD_NAME_ATTR, metField); + reqMetFieldsElem.appendChild(reqMetFieldElem); + } + + taskElem.appendChild(reqMetFieldsElem); + } + if (task.getTaskConfig() != null + && task.getTaskConfig().getProperties().keySet() + .size() > 0) { + Element taskConfigElem = (Element) document + .createElement(TASK_CONFIG_ELEM); + for (Iterator j = task.getTaskConfig().getProperties() + .keySet().iterator(); j.hasNext();) { + String propName = (String) j.next(); + String propVal = task.getTaskConfig().getProperty( + propName); + + EnvSavingConfiguration config = (EnvSavingConfiguration) task + .getTaskConfig(); + Element configPropElem = (Element) document + .createElement(PROPERTY_ELEM); + if (config.isReplace(propName)) { + configPropElem.setAttribute( + PROPERTY_ELEM_ENVREPLACE_ATTR, String + .valueOf(true)); + } + + configPropElem.setAttribute( + PROPERTY_ELEM_NAME_ATTR, propName); + configPropElem.setAttribute( + PROPERTY_ELEM_VALUE_ATTR, propVal); + taskConfigElem.appendChild(configPropElem); + } + + taskElem.appendChild(taskConfigElem); + } + + root.appendChild(taskElem); + } + } + + return document; + } catch (ParserConfigurationException pce) { + LOG.log(Level.WARNING, "Error generating tasks xml document!: " + + pce.getMessage()); + } + + return null; + } + + private static String ensureEndingSlash(String path) { + String fixedPath = path; + + if (!fixedPath.endsWith("/")) { + fixedPath += "/"; + } + + return fixedPath; + } + + private static Map subsetByPolicyDirPath(Map origMap, String policyDirPath) { + Map newMap = new TreeMap(new Comparator() { + + public int compare(Object o1, Object o2) { + String t1 = (String) o1; + String t2 = (String) o2; + + return t1.compareTo(t2); + } + + }); + + if (origMap != null && origMap.keySet() != null + && origMap.keySet().size() > 0) { + for (Iterator i = origMap.keySet().iterator(); i.hasNext();) { + String taskId = (String) i.next(); + PolicyAwareWorkflowTask task = (PolicyAwareWorkflowTask) origMap + .get(taskId); + if (task.getPolicyDirPath().equals(policyDirPath)) { + newMap.put(taskId, task); + } + } + } + + return newMap; + } + +} http://git-wip-us.apache.org/repos/asf/oodt/blob/a82ee5db/workflow/src/main/java/org/apache/oodt/cas/workflow/policy/TaskRequestFactory.java ---------------------------------------------------------------------- diff --git a/workflow/src/main/java/org/apache/oodt/cas/workflow/policy/TaskRequestFactory.java b/workflow/src/main/java/org/apache/oodt/cas/workflow/policy/TaskRequestFactory.java new file mode 100644 index 0000000..6e6e62b --- /dev/null +++ b/workflow/src/main/java/org/apache/oodt/cas/workflow/policy/TaskRequestFactory.java @@ -0,0 +1,130 @@ +//Copyright (c) 2008, California Institute of Technology. +//ALL RIGHTS RESERVED. U.S. Government sponsorship acknowledged. +// +//$Id$ + +package org.apache.oodt.cas.workflow.policy; + +//OODT imports +import org.apache.oodt.cas.workflow.structs.WorkflowCondition; + +//JDK imports +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Vector; + +/** + * @author mattmann + * @version $Revision$ + * + * <p> + * A Jsp utility that processes a {@link PolicyAwareWorkflowTask} that is given + * in the form of a HttpServletRequestWrapper#getParameterMap(). + * </p>. + */ +public class TaskRequestFactory implements TaskRequestProcessorMetKeys { + + private TaskRequestFactory() throws InstantiationException { + throw new InstantiationException("Don't construct factories!"); + } + + /** + * Processes a given {@link PolicyAwareWorkflowTask} and unmarshalls it out + * of the given {@link Map}. + * + * @param paramMap + * A parameter {@link Map}, generated by a call to + * {@link HttpServletRequestWrapper#getParameterMap()}. + * @return An unmarshalled, {@link PolicyAwareWorkflowTask}. + */ + public static PolicyAwareWorkflowTask getTask(Map paramMap) { + PolicyAwareWorkflowTask task = new PolicyAwareWorkflowTask(); + task.setTaskName(getFirst((String[]) paramMap.get(TASK_NAME))); + task.setTaskId(getFirst((String[]) paramMap.get(UPDATE_TASK_ID))); + task.setPolicyDirPath(getFirst((String[]) paramMap + .get(TASK_POLICY_DIR_PATH))); + task.setTaskInstanceClassName(getFirst((String[]) paramMap + .get(TASK_CLASS_NAME))); + int reqMetFieldCount = Integer.parseInt(getFirst((String[]) paramMap + .get(REQ_MET_FIELDS_COUNT))); + if (reqMetFieldCount > 0) { + List metFields = new Vector(); + + for (int i = 0; i < reqMetFieldCount; i++) { + metFields.add(getFirst((String[]) paramMap + .get(REQ_MET_FIELD_TAGBASE + (i)))); + } + + task.setRequiredMetFields(metFields); + } else + task.setRequiredMetFields(Collections.EMPTY_LIST); + + String[] selectedCondIds = (String[]) paramMap.get(TASK_CONDITION_IDS); + + if (selectedCondIds != null && selectedCondIds.length > 0) { + List conditions = new Vector(); + for (int i = 0; i < selectedCondIds.length; i++) { + WorkflowCondition cond = new WorkflowCondition(); + cond.setConditionId(selectedCondIds[i]); + conditions.add(cond); + } + + task.setConditions(conditions); + } else + task.setConditions(Collections.EMPTY_LIST); + + int numConfigProperties = Integer.parseInt(getFirst((String[]) paramMap + .get(TASK_CONFIG_FIELD_COUNT))); + if (numConfigProperties > 0) { + EnvSavingConfiguration config = new EnvSavingConfiguration(); + for (int i = 0; i < numConfigProperties; i++) { + String propName = getFirst((String[]) paramMap + .get(TASK_CONFIG_PROPNAME_BASE + (i))); + String propVal = getFirst((String[]) paramMap + .get(TASK_CONFIG_PROPVAL_BASE + (i))); + config.addConfigProperty(propName, propVal); + } + + task.setTaskConfig(config); + } + + return task; + } + + /** + * Merges the <code>newTask</code> {@link PolicyAwareWorkflowTask} into + * the <code>orig</code> {@link PolicyAwareWorkflowTask}, taking care not + * to overwrite the {@link EnvSavingConfiguration}, and only its + * {@link EnvSavingConfiguration#getProperties()}. + * + * @param newTask + * The new {@link PolicyAwareWorkflowTask}, whose values will + * overwrite those of <code>orig</code>. + * @param orig + * The destination for the new values provided by + * <code>newTask</code>. + */ + public static void merge(PolicyAwareWorkflowTask newTask, + PolicyAwareWorkflowTask orig) { + orig.setTaskId(newTask.getTaskId()); + orig.setTaskName(newTask.getTaskName()); + orig.setTaskInstanceClassName(newTask.getTaskInstanceClassName()); + orig.setPolicyDirPath(newTask.getPolicyDirPath()); + orig.setConditions(newTask.getConditions()); + orig.setRequiredMetFields(newTask.getRequiredMetFields()); + // only copy through properties, don't reset config, because + // we need to save the orig envReplace info + orig.getTaskConfig().getProperties().clear(); + orig.getTaskConfig().getProperties().putAll( + newTask.getTaskConfig().getProperties()); + } + + private static String getFirst(String[] strArray) { + if (strArray != null && strArray.length >= 1) { + return strArray[0]; + } else + return null; + } + +} http://git-wip-us.apache.org/repos/asf/oodt/blob/a82ee5db/workflow/src/main/java/org/apache/oodt/cas/workflow/policy/TaskRequestProcessorMetKeys.java ---------------------------------------------------------------------- diff --git a/workflow/src/main/java/org/apache/oodt/cas/workflow/policy/TaskRequestProcessorMetKeys.java b/workflow/src/main/java/org/apache/oodt/cas/workflow/policy/TaskRequestProcessorMetKeys.java new file mode 100644 index 0000000..ccccd49 --- /dev/null +++ b/workflow/src/main/java/org/apache/oodt/cas/workflow/policy/TaskRequestProcessorMetKeys.java @@ -0,0 +1,38 @@ +//Copyright (c) 2008, California Institute of Technology. +//ALL RIGHTS RESERVED. U.S. Government sponsorship acknowledged. +// +//$Id$ + +package org.apache.oodt.cas.workflow.policy; + + +/** + * @author mattmann + * @version $Revision$ + * + * <p>Met keys for unpackaging a {@link PolicyAwareWorkflowTask} out of a + * {@link HttpServletRequestWrapper}</p>. + */ +public interface TaskRequestProcessorMetKeys { + + public static final String TASK_POLICY_DIR_PATH = "taskPolicyDirPath"; + + public static final String UPDATE_TASK_ID = "updateTaskId"; + + public static final String TASK_NAME = "task_name"; + + public static final String TASK_CLASS_NAME = "task_class_name"; + + public static final String TASK_CONDITION_IDS = "selectedConditionIds"; + + public static final String REQ_MET_FIELDS_COUNT = "reqMetFieldCnt"; + + public static final String REQ_MET_FIELD_TAGBASE = "reqMetField"; + + public static final String TASK_CONFIG_FIELD_COUNT = "taskConfigCnt"; + + public static final String TASK_CONFIG_PROPNAME_BASE = "taskConfigPropName"; + + public static final String TASK_CONFIG_PROPVAL_BASE = "taskConfigPropValue"; + +}
