01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.bpmscript;
18:
19: import java.sql.Timestamp;
20: import java.util.ArrayList;
21: import java.util.List;
22: import java.util.UUID;
23:
24: public class InMemoryProcessManager extends ProcessManager {
25:
26: @Override
27: public String createProcessInstance(String parentProcessInstanceId,
28: String processName, String operation)
29: throws BpmScriptException {
30: if (processInstances.size() > maxInstances) {
31: throw new BpmScriptException(
32: "Can't create any more process instances as maximum has been reached "
33: + maxInstances);
34: }
35: Process process = processesByName.get(processName);
36: ProcessInstance processInstance = new InMemoryProcessInstance(
37: UUID.randomUUID().toString(), parentProcessInstanceId,
38: operation, process);
39: processInstance.setProcessState(ProcessState.CREATED);
40: processInstances.put(processInstance.getId(), processInstance);
41: if (parentProcessInstanceId != null) {
42: List<IProcessInstance> children = processInstanceChildren
43: .get(parentProcessInstanceId);
44: if (children == null) {
45: children = new ArrayList<IProcessInstance>();
46: processInstanceChildren.put(parentProcessInstanceId,
47: children);
48: }
49: children.add(processInstance);
50: }
51: return processInstance.getId();
52: }
53:
54: public ExecutorResult doWithProcessInstance(
55: String processInstanceId, IProcessInstanceCallback callback)
56: throws Throwable {
57: IProcessInstance instance = checkoutProcessInstance(processInstanceId);
58: synchronized (instance) {
59: ExecutorResult executorResult = callback.execute(instance);
60: if (executorResult.getProcessState() == ProcessState.PAUSED) {
61: InMemoryPausedResult pausedResult = ((InMemoryPausedResult) executorResult);
62: checkinInstance(processInstanceId, pausedResult
63: .getContinuationObject(), pausedResult
64: .getStackTraceElements());
65: } else if (executorResult.getProcessState() == ProcessState.COMPLETED) {
66: complete(processInstanceId);
67: } else if (executorResult.getProcessState() == ProcessState.FAILED) {
68: FailedResult failedResult = ((FailedResult) executorResult);
69: Throwable throwable = failedResult.getThrowable();
70: failed(processInstanceId, throwable, failedResult
71: .getStackTraceElements());
72: throw throwable;
73: } else {
74: // noop
75: }
76: return executorResult;
77: }
78: }
79:
80: public void checkinInstance(String processInstanceId,
81: Object continuationObject,
82: StackTraceElement[] stackTraceElements) {
83: InMemoryProcessInstance instance = (InMemoryProcessInstance) processInstances
84: .get(processInstanceId);
85: instance.setContinuationObject(continuationObject);
86: instance.setLastModified(new Timestamp(System
87: .currentTimeMillis()));
88: instance.setStackTraceElements(stackTraceElements);
89: instance.setProcessState(ProcessState.PAUSED);
90: }
91:
92: }
|