1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package com.gridsystems.nextgrid.api.pom;
19
20 import java.net.URI;
21 import java.util.PriorityQueue;
22
23 import nextgrid.api.env.ProcessEnvironment;
24 import nextgrid.api.pom.Grounding;
25 import nextgrid.api.pom.GroundingFactory;
26 import nextgrid.api.pom.GroundingFactoryFinder;
27 import nextgrid.api.pom.Process;
28 import nextgrid.api.pom.ProcessException;
29 import nextgrid.api.pom.Service;
30
31
32
33
34
35
36 public final class ServiceImpl extends ProcessImpl implements Service {
37
38
39
40
41 private static final long serialVersionUID = 3531778252650157690L;
42
43
44
45
46 private Grounding grounding;
47
48
49
50
51
52
53
54 private boolean discovered = true;
55
56
57
58
59 public ServiceImpl() {
60 super();
61 }
62
63
64
65
66 public Process findProcessById(URI id) {
67 if (id != null && this.getId().equals(id)) {
68 return this;
69 } else {
70 return null;
71 }
72 }
73
74
75
76
77
78
79
80
81 public void prioritise(ProcessEnvironment env, PriorityQueue<Process> queue) {
82
83 }
84
85
86
87
88 public void discover(ProcessEnvironment env) {
89
90 }
91
92
93
94
95 @Override
96 protected void doValidate(ValidationType type) throws ProcessException {
97 if (type == ValidationType.BEFORE_CHILDREN) {
98 getGrounding().validate();
99 }
100 }
101
102
103
104
105 public Grounding getGrounding() {
106 if (this.grounding == null) {
107
108 String type = this.getAttribute(Grounding.ATTR_GROUNDING_TYPE);
109 GroundingFactory gf = GroundingFactoryFinder.findFactory(type);
110 if (gf != null) {
111 this.grounding = gf.createGrounding();
112 }
113 }
114
115 return this.grounding;
116 }
117
118
119
120
121 public void setGrounding(Grounding g) {
122 if (g == null) {
123 this.grounding = null;
124 this.discovered = true;
125 } else {
126 this.grounding = g;
127 this.discovered = false;
128 }
129 }
130
131
132
133
134 @Override
135 public void doEvaluate(ProcessEnvironment env) throws ProcessException {
136
137 }
138
139
140
141
142 public void run(ProcessContext ctx) throws ProcessException, InterruptedException {
143 try {
144 Grounding g = this.getGrounding();
145 if (g == null) {
146 throw new ProcessException("Grounding not found for service " + this.getId());
147 } else {
148 waitForInputs();
149 ENACTOR_LOG.debug("Service '" + this.getName() + "': grounding invoked");
150 fireProcessStarted();
151 g.invoke(ctx, this);
152 fireProcessFinished();
153 }
154 } catch (ProcessException e) {
155 fireProcessFailed(e);
156 throw e;
157 } catch (RuntimeException e) {
158 throw e;
159 }
160 }
161
162
163
164
165 @Override protected void doReset() {
166 if (discovered) {
167 this.grounding = null;
168 }
169 }
170
171
172
173
174 @Override protected void resetChildren() {
175
176 }
177
178
179
180
181 @Override public String toString() {
182 return "Service#" + this.getId();
183 }
184
185 }