-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathModuleImpl.java
More file actions
331 lines (279 loc) · 8.06 KB
/
ModuleImpl.java
File metadata and controls
331 lines (279 loc) · 8.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package modules;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import common.parallelization.CallbackReceiver;
public abstract class ModuleImpl implements Module {
public static final String PROPERTYKEY_NAME = "name";
private CallbackReceiver callbackReceiver;
private String name;
private Properties properties = new Properties();
private Map<String, String> propertyDescriptions = new HashMap<String, String>();
private Map<String, String> propertyDefaultValues = new HashMap<String, String>();
private Map<String, Object> metadata = new HashMap<String, Object>();
private int status = Module.STATUSCODE_NOTYETRUN;
private String statusDetail = null;
private String description = "(no description)";
private Map<String,InputPort> inputPorts;
private Map<String,OutputPort> outputPorts;
private String category = null;
public ModuleImpl(CallbackReceiver callbackReceiver, Properties properties)
throws Exception {
super();
this.callbackReceiver = callbackReceiver;
this.setProperties(properties);
this.setStatusDetail(null);
// Determine default category from package name. Relies on a specific package hierarchy.
try {
this.category = this.getClass().getCanonicalName().split("\\.")[1].replaceAll("_", " ");
} catch (Exception e){
this.category = "uncategorized";
}
this.getPropertyDescriptions().put(PROPERTYKEY_NAME,
"The module instance's name");
// Add default values
this.getPropertyDefaultValues().put(PROPERTYKEY_NAME, "(unnamed module)");
// IO ports
this.inputPorts = new ConcurrentHashMap<String,InputPort>();
this.outputPorts = new ConcurrentHashMap<String,OutputPort>();
}
/**
* Adds the specified input port.
* @param port Port to add
*/
public void addInputPort(InputPort port){
this.inputPorts.put(port.getName(), port);
}
/**
* Adds the specified output port.
* @param port Port to add
*/
public void addOutputPort(OutputPort port){
this.outputPorts.put(port.getName(), port);
}
/**
* Inserts the default value into the property map
* if the correspondent key is not present yet.
*/
public void setDefaultsIfMissing() {
// Apply default values if necessary
Iterator<String> propertyKeys = this.propertyDefaultValues.keySet()
.iterator();
while (propertyKeys.hasNext()) {
String propertyKey = propertyKeys.next();
// Check whether key is missing in property map
if (!this.properties.containsKey(propertyKey)) {
// Set property to default value
this.properties.put(propertyKey,
this.propertyDefaultValues.get(propertyKey));
}
}
}
@Override
public void applyProperties() throws Exception {
if (this.getProperties().containsKey(PROPERTYKEY_NAME))
this.name = this.getProperties().getProperty(PROPERTYKEY_NAME, "unnamed module");
}
/**
* Closes all outputs on all output ports.
* @throws IOException Thrown if something goes wrong
*/
public void closeAllOutputs() throws IOException {
Iterator<OutputPort> outputPorts = this.outputPorts.values().iterator();
while (outputPorts.hasNext()){
outputPorts.next().close();
}
}
/**
* Reads the total remaining String from inputPort.
*
* Convenience method for module implementations that need the whole input
* present before processing can begin.
*
* @param inputPort the port to read from
* @return The String read
* @throws IOException if an IO-Error occurs
* @throws NotSupportedException if the InputPort does not provide a char pipe to read from.
* @throws InterruptedException if the Thread has been interrupted.
*/
protected String readStringFromInputPort(InputPort inputPort)
throws Exception {
if(!inputPort.isConnected()) {
throw new Exception("inputPort is not connected");
}
final StringBuilder stringBuilder = new StringBuilder();
int charCode = inputPort.getInputReader().read();
while (charCode != -1) {
if (Thread.interrupted()) {
throw new InterruptedException("Thread has been interrupted.");
}
stringBuilder.append((char) charCode);
charCode = inputPort.getInputReader().read();
}
return stringBuilder.toString();
}
/*
* @see parallelization.CallbackProcess#getRueckmeldungsEmpfaenger()
*/
@Override
public CallbackReceiver getCallbackReceiver() {
return callbackReceiver;
}
@Override
public String getName() {
return name;
}
@Override
public Properties getProperties() {
return properties;
}
@Override
public Map<String, String> getPropertyDescriptions() {
return propertyDescriptions;
}
@Override
public Map<String, String> getPropertyDefaultValues() {
return propertyDefaultValues;
}
@Override
public int getStatus() {
return status;
}
@Override
public abstract boolean process() throws Exception;
/*
* @see java.lang.Runnable#run()
*/
@Override
public void run() {
try {
// Update status
this.status = Module.STATUSCODE_RUNNING;
// Log message
Logger.getLogger("").log(
Level.INFO,
"Running module "
+ this.getProperties().getProperty(
ModuleImpl.PROPERTYKEY_NAME));
// Run process and determine result
Boolean result = this.process();
// Log message
Logger.getLogger("")
.log(Level.INFO,
"Module "
+ this.getProperties().getProperty(
ModuleImpl.PROPERTYKEY_NAME)
+ " finished.");
// Update status
if (result)
this.status = Module.STATUSCODE_SUCCESS;
else
this.status = Module.STATUSCODE_FAILURE;
// Return result
this.callbackReceiver.receiveCallback(Thread.currentThread(), result);
} catch (Exception e) {
this.status = Module.STATUSCODE_FAILURE;
this.callbackReceiver.receiveException(Thread.currentThread(), e);
}
}
/*
* @see
* parallelization.CallbackProcess#setRueckmeldungsEmpfaenger(parallelization
* .CallbackReceiver)
*/
@Override
public void setCallbackReceiver(CallbackReceiver callbackReceiver) {
this.callbackReceiver = callbackReceiver;
}
@Override
public void setName(String name) {
this.name = name;
if (this.name != null)
this.getProperties().setProperty(PROPERTYKEY_NAME, name);
else
this.getProperties().remove(PROPERTYKEY_NAME);
}
@Override
public void setProperties(Properties properties) throws Exception {
if (properties == null)
throw new Exception(this.getClass().getSimpleName()
+ " cannot handle null value as properties, sorry.");
this.properties = properties;
this.applyProperties();
}
/* (non-Javadoc)
* @see modularization.Module#resetOutputs()
*/
@Override
public void resetOutputs() throws IOException {
// Cycle through all output pipes & reset them
Iterator<OutputPort> ports = this.getOutputPorts().values().iterator();
while (ports.hasNext()){
ports.next().reset();
}
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return this.name;
}
/* (non-Javadoc)
* @see modularization.Module#getDescription()
*/
@Override
public String getDescription() {
return this.description;
}
/* (non-Javadoc)
* @see modularization.Module#setDescription(java.lang.String)
*/
@Override
public void setDescription(String desc) {
this.description = desc;
}
/* (non-Javadoc)
* @see modules.Module#getInputPorts()
*/
@Override
public Map<String,InputPort> getInputPorts() {
return this.inputPorts;
}
/* (non-Javadoc)
* @see modules.Module#getOutputPorts()
*/
@Override
public Map<String,OutputPort> getOutputPorts() {
return this.outputPorts;
}
@Override
public String getCategory() {
return category;
}
@Override
public void setCategory(String category) {
this.category = category;
}
@Override
public String getStatusDetail() {
return statusDetail;
}
@Override
public void setStatusDetail(String statusDetail) {
this.statusDetail = statusDetail;
}
@Override
public Map<String, Object> getMetadata() {
return metadata;
}
@Override
public void setMetadata(Map<String, Object> metadata) {
this.metadata = metadata;
}
}