1 /*
2 Copyright (C) 2006 Grid Systems, S.A.
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 package com.gridsystems.nextgrid.api.pom;
19
20 import nextgrid.api.pom.AsyncProcess;
21 import nextgrid.api.pom.LoopProcess;
22 import nextgrid.api.pom.Process;
23 import nextgrid.api.pom.ProcessController;
24 import nextgrid.api.pom.ProcessException;
25
26 /**
27 * Control process that executes all its children sub-processes concurrently.
28 * <p>
29 * This process do not wait for its children to finish their execution. The
30 * programmer can use {@link nextgrid.api.pom.SplitJoinProcess}
31 * to get a process with this behaviour.
32 *
33 * @author Rodrigo Ruiz
34 */
35 public final class AsyncProcessImpl extends ControlProcessImpl
36 implements AsyncProcess {
37
38 /**
39 * <code>serialVersionUID</code> attribute.
40 */
41 private static final long serialVersionUID = 8003088574283848405L;
42
43 /**
44 * Creates a new instance.
45 */
46 public AsyncProcessImpl() {
47 super(-1);
48 }
49
50 /**
51 * {@inheritDoc}
52 */
53 @Override
54 protected void doValidate(ValidationType when) throws ProcessException {
55 if (when == ValidationType.BEFORE_CHILDREN) {
56 Process parent = this.getParent();
57 while (parent != null) {
58 if (parent instanceof LoopProcess) {
59 throw new ProcessException("Asynchronous processes inside loops not allowed");
60 } else {
61 parent = parent.getParent();
62 }
63 }
64 }
65 }
66
67 /**
68 * {@inheritDoc}
69 */
70 public void run(ProcessContext ctx) throws ProcessException, InterruptedException {
71 fireProcessStarted();
72 waitForInputs();
73
74 Process[] children = getChildren();
75
76 final int count = (children == null) ? 0 : children.length;
77 ProcessController[] controllers = new ProcessController[count];
78 for (int i = 0; i < count; i++) {
79 controllers[i] = children[i].enact(ctx);
80 controllers[i].start();
81 }
82
83 if (ctx.isRunning()) {
84 fireProcessFinished();
85 }
86 }
87
88 /**
89 * {@inheritDoc}
90 */
91 @Override protected void doReset() {
92 }
93
94 /**
95 * {@inheritDoc}
96 */
97 @Override public String toString() {
98 return "AsyncProcess#" + this.getId();
99 }
100 }