diff options
| author | Andrew Branson <andrew.branson@cern.ch> | 2014-04-11 16:57:30 +0200 |
|---|---|---|
| committer | Andrew Branson <andrew.branson@cern.ch> | 2014-04-11 16:57:30 +0200 |
| commit | c42a902b973b1d24f2ef38cc9ed22ed0a059d13f (patch) | |
| tree | fd5ecdc3fc43dca0cb048ba72f692527efe23bc8 | |
| parent | c85dc62591ab2ce9eec3fd93004ba474f7b1fb19 (diff) | |
OutcomeInitiator interface to create initial states of outcomes if empty
in the Job. Called when job.getOutcome() is called when none exists.
Viewpoint last still overrides. Fixes #47
4 files changed, 86 insertions, 18 deletions
diff --git a/src/main/java/com/c2kernel/entity/agent/Job.java b/src/main/java/com/c2kernel/entity/agent/Job.java index ea28e38..e3f58e5 100644 --- a/src/main/java/com/c2kernel/entity/agent/Job.java +++ b/src/main/java/com/c2kernel/entity/agent/Job.java @@ -1,5 +1,7 @@ package com.c2kernel.entity.agent;
+import java.util.HashMap;
+
import com.c2kernel.common.InvalidDataException;
import com.c2kernel.common.ObjectNotFoundException;
import com.c2kernel.entity.C2KLocalObject;
@@ -10,7 +12,9 @@ import com.c2kernel.lookup.AgentPath; import com.c2kernel.lookup.EntityPath;
import com.c2kernel.lookup.InvalidEntityPathException;
import com.c2kernel.persistency.ClusterStorage;
+import com.c2kernel.persistency.ClusterStorageException;
import com.c2kernel.persistency.outcome.Outcome;
+import com.c2kernel.persistency.outcome.OutcomeInitiator;
import com.c2kernel.persistency.outcome.Schema;
import com.c2kernel.persistency.outcome.Viewpoint;
import com.c2kernel.process.Gateway;
@@ -63,6 +67,10 @@ public class Job implements C2KLocalObject private ItemProxy item = null;
private boolean outcomeSet;
+
+ // outcome initiator cache
+
+ static private HashMap<String, OutcomeInitiator> ocInitCache = new HashMap<String, OutcomeInitiator>();
/***************************************************************************
* Empty constructor for Castor
@@ -273,26 +281,72 @@ public class Job implements C2KLocalObject Logger.error(e);
}
}
+
+ public String getLastView() throws InvalidDataException {
+ String viewName = (String) getActProp("Viewpoint");
+ if (viewName.length() > 0) {
+ // find schema
+ String schemaName;
+ try {
+ schemaName = getSchemaName();
+ } catch (ObjectNotFoundException e1) {
+ throw new InvalidDataException("Schema "+getActProp("SchemaType")+" v"+getActProp("SchemaVersion")+" not found");
+ }
+
+ try {
+ Viewpoint view = (Viewpoint) Gateway.getStorage().get(getItemSysKey(),
+ ClusterStorage.VIEWPOINT + "/" + schemaName + "/" + viewName, null);
+ return view.getOutcome().getData();
+ } catch (ObjectNotFoundException ex) { // viewpoint doesn't exist yet
+ return null;
+ } catch (ClusterStorageException e) {
+ Logger.error(e);
+ throw new InvalidDataException("ViewpointOutcomeInitiator: ClusterStorageException loading viewpoint "
+ + ClusterStorage.VIEWPOINT + "/" + schemaName + "/" + viewName+" in syskey "+getItemSysKey());
+ }
+ }
+ else
+ return null;
+ }
+
+ public OutcomeInitiator getOutcomeInitiator() throws InvalidDataException {
+ String ocInitName = (String) getActProp("OutcomeInit");
+ OutcomeInitiator ocInit;
+ if (ocInitName.length() > 0) {
+ String ocPropName = "OutcomeInit."+ocInitName;
+ synchronized (ocInitCache) {
+ ocInit = ocInitCache.get(ocPropName);
+ if (ocInit == null) {
+ Object ocInitObj = Gateway.getProperties().get(ocPropName);
+ if (ocInitObj instanceof String) {
+ try {
+ ocInitObj = Class.forName((String)ocInitObj).newInstance();
+ } catch (Exception e) {
+ Logger.error(e);
+ throw new InvalidDataException("Could not instantiate OutcomeInstantiator "+ocInitObj+" for "+ocPropName);
+ }
+ }
+ ocInit = (OutcomeInitiator)ocInitObj; // throw runtime class cast if it isn't one
+ ocInitCache.put(ocPropName, ocInit);
+ }
+ }
+ return ocInit;
+ }
+ else
+ return null;
+ }
- public String getOutcomeString()
+ public String getOutcomeString() throws InvalidDataException
{
- if (outcomeData == null && isOutcomeRequired())
- {
- String viewName = (String) getActProp("Viewpoint");
- outcomeData = null;
- if (viewName.length() > 0)
- try
- {
- Viewpoint view = (Viewpoint) Gateway.getStorage().get(getItemSysKey(), ClusterStorage.VIEWPOINT + "/" + getSchemaName() + "/" + viewName, null);
- outcomeData = view.getOutcome().getData();
- outcomeSet = true;
- } catch (Exception ex)
- {
- outcomeData = null;
- outcomeSet = false;
- }
-
- }
+ if (outcomeData == null && transition.hasOutcome(actProps)) {
+ outcomeData = getLastView();
+ if (outcomeData == null) {
+ OutcomeInitiator ocInit = getOutcomeInitiator();
+ if (ocInit != null)
+ outcomeData = ocInit.initOutcome(this);
+ }
+ if (outcomeData != null) outcomeSet = true;
+ }
return outcomeData;
}
diff --git a/src/main/java/com/c2kernel/lifecycle/WfCastorHashMap.java b/src/main/java/com/c2kernel/lifecycle/WfCastorHashMap.java index 5a9d472..be2f043 100644 --- a/src/main/java/com/c2kernel/lifecycle/WfCastorHashMap.java +++ b/src/main/java/com/c2kernel/lifecycle/WfCastorHashMap.java @@ -23,5 +23,6 @@ public class WfCastorHashMap extends CastorHashMap put("StateMachineName", "Default");
put("StateMachineVersion", 0);
put("Viewpoint", "");
+ put("OutcomeInit", "");
}
}
diff --git a/src/main/java/com/c2kernel/persistency/outcome/OutcomeInitiator.java b/src/main/java/com/c2kernel/persistency/outcome/OutcomeInitiator.java new file mode 100644 index 0000000..82068bb --- /dev/null +++ b/src/main/java/com/c2kernel/persistency/outcome/OutcomeInitiator.java @@ -0,0 +1,10 @@ +package com.c2kernel.persistency.outcome;
+
+import com.c2kernel.common.InvalidDataException;
+import com.c2kernel.entity.agent.Job;
+
+public interface OutcomeInitiator {
+
+ public String initOutcome(Job job) throws InvalidDataException;
+
+}
diff --git a/src/main/java/com/c2kernel/utils/DescriptionObject.java b/src/main/java/com/c2kernel/utils/DescriptionObject.java index 4d7d108..f8ba72d 100644 --- a/src/main/java/com/c2kernel/utils/DescriptionObject.java +++ b/src/main/java/com/c2kernel/utils/DescriptionObject.java @@ -4,5 +4,8 @@ public interface DescriptionObject { public String getName();
public int getVersion();
+
+ public void setName(String name);
+ public void setVersion(int version);
}
|
