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.Process;
21 import nextgrid.api.pom.ProcessController;
22 import nextgrid.api.pom.ProcessException;
23 import nextgrid.api.pom.SplitJoinProcess;
24
25 /**
26 * Control process that executes all its children process concurrently, and
27 * waits until they all finish. This process implements a barrier
28 * synchronisation model for its children.
29 *
30 * @author Rodrigo Ruiz
31 */
32 public class SplitJoinProcessImpl extends ControlProcessImpl
33 implements SplitJoinProcess {
34
35 /**
36 * <code>serialVersionUID</code> attribute.
37 */
38 private static final long serialVersionUID = -3773310493929203714L;
39
40 /**
41 * Creates a new instance.
42 */
43 public SplitJoinProcessImpl() {
44 super(-1);
45 }
46
47 /**
48 * {@inheritDoc}
49 */
50 @Override
51 protected final void doValidate(ValidationType when) throws ProcessException {
52 // No extra validation required here
53 }
54
55 /**
56 * {@inheritDoc}
57 */
58 public void run(ProcessContext ctx) throws ProcessException, InterruptedException {
59 fireProcessStarted();
60 waitForInputs();
61
62 Process[] children = getChildren();
63
64 final int count = (children == null) ? 0 : children.length;
65 ProcessController[] controllers = new ProcessController[count];
66 for (int i = 0; i < count; i++) {
67 controllers[i] = children[i].enact(ctx);
68 controllers[i].start();
69 }
70
71 for (int i = 0; i < count; i++) {
72 if (!ctx.isRunning()) {
73 break;
74 }
75 controllers[i].join();
76 }
77
78 if (ctx.isRunning()) {
79 fireProcessFinished();
80 }
81 }
82
83 /**
84 * {@inheritDoc}
85 */
86 @Override protected void doReset() {
87 }
88
89 /**
90 * {@inheritDoc}
91 */
92 @Override public String toString() {
93 return "SplitJoinProcess#" + this.getId();
94 }
95 }