Skip to content

Commit ea21253

Browse files
committed
Merge pull request #10 from knime-ip/statelessness
makes sense! It also simplifies to code I think. Thank you!
2 parents 79fa8bc + 28758ab commit ea21253

16 files changed

+472
-153
lines changed

org.knime.knip.scijava.commands.testing/src/org/knime/knip/scijava/commands/testing/KnimeProcessorTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
import org.knime.core.data.def.LongCell;
2727
import org.knime.core.data.def.StringCell;
2828
import org.knime.knip.scijava.commands.DefaultKnimePostprocessor;
29-
import org.knime.knip.scijava.commands.KnimeInputDataTableService;
30-
import org.knime.knip.scijava.commands.KnimeOutputDataTableService;
29+
import org.knime.knip.scijava.commands.KNIMEInputDataTableService;
30+
import org.knime.knip.scijava.commands.KNIMEOutputDataTableService;
3131
import org.knime.knip.scijava.commands.adapter.InputAdapterService;
3232
import org.knime.knip.scijava.commands.adapter.OutputAdapterService;
3333
import org.knime.knip.scijava.commands.mapping.ColumnInputMappingKnimePreprocessor;
@@ -57,14 +57,14 @@ public class KnimeProcessorTest {
5757
@Parameter
5858
CommandService m_commandService;
5959
@Parameter
60-
KnimeInputDataTableService m_inputTableService;
60+
KNIMEInputDataTableService m_inputTableService;
6161
@Parameter
62-
KnimeOutputDataTableService m_outputTableService;
62+
KNIMEOutputDataTableService m_outputTableService;
6363
@Parameter
6464
ColumnModuleItemMappingService m_cimService;
6565

6666
protected static List<Class<? extends Service>> requiredServices = Arrays.<Class<? extends Service>> asList(
67-
KnimeInputDataTableService.class, KnimeOutputDataTableService.class, CommandService.class,
67+
KNIMEInputDataTableService.class, KNIMEOutputDataTableService.class, CommandService.class,
6868
ColumnModuleItemMappingService.class, InputAdapterService.class, OutputAdapterService.class);
6969

7070
private static final DataTableSpec m_spec;

org.knime.knip.scijava.commands/META-INF/MANIFEST.MF

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@ Export-Package: org.knime.knip.scijava.commands,
3030
org.knime.knip.scijava.commands.settings.types,
3131
org.knime.knip.scijava.commands.widget,
3232
org.knime.knip.scijava.commands.widget.impl
33+
Import-Package: org.slf4j
Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,56 @@
11
package org.knime.knip.scijava.commands;
22

3+
import java.lang.ref.WeakReference;
4+
35
import org.knime.core.data.DataRow;
46
import org.knime.core.data.DataTableSpec;
57
import org.scijava.Priority;
68
import org.scijava.plugin.Plugin;
79
import org.scijava.service.AbstractService;
810

911
/**
10-
* Default implementation of InputDataRowService.
12+
* Default implementation of InputDataRowService. Holds a {@link DataRow} and a
13+
* {@link DataTableSpec} via a {@link WeakReference} to ensure that they can be
14+
* garbage collected once they are not referenced outside this Service.
1115
*
1216
* @author Jonathan Hale (University of Konstanz)
13-
*
1417
*/
1518
@Plugin(type = InputDataRowService.class, priority = DefaultInputDataRowService.PRIORITY)
16-
public class DefaultInputDataRowService extends AbstractService implements
17-
InputDataRowService {
18-
19+
public class DefaultInputDataRowService extends AbstractService
20+
implements InputDataRowService {
21+
1922
/**
2023
* Priority of this {@link Plugin}
2124
*/
2225
public static final double PRIORITY = Priority.NORMAL_PRIORITY;
2326

24-
private DataRow m_row = null;
25-
private DataTableSpec m_spec = null;
27+
private WeakReference<DataRow> m_row = new WeakReference<>(null);
28+
private WeakReference<DataTableSpec> m_spec = new WeakReference<>(null);
2629

2730
/**
2831
* {@inheritDoc}
2932
*/
3033
@Override
3134
public DataRow getInputDataRow() {
32-
return m_row;
35+
return m_row.get();
3336
}
3437

3538
/**
3639
* Set the contained DataRow;
40+
*
3741
* @param dataRow
3842
*/
39-
public void setDataRow(DataRow dataRow) {
40-
m_row = dataRow;
43+
public void setDataRow(final DataRow dataRow) {
44+
m_row = new WeakReference<>(dataRow);
4145
}
4246

4347
@Override
4448
public DataTableSpec getInputDataTableSpec() {
45-
return m_spec;
49+
return m_spec.get();
4650
}
47-
48-
public void setDataTableSpec(DataTableSpec spec) {
49-
m_spec = spec;
51+
52+
public void setDataTableSpec(final DataTableSpec spec) {
53+
m_spec = new WeakReference<>(spec);
5054
}
51-
55+
5256
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package org.knime.knip.scijava.commands;
2+
3+
import org.knime.knip.scijava.commands.adapter.InputAdapterService;
4+
import org.knime.knip.scijava.commands.adapter.OutputAdapterService;
5+
import org.knime.knip.scijava.commands.mapping.ColumnToInputMappingService;
6+
import org.knime.knip.scijava.commands.mapping.OutputToColumnMappingService;
7+
import org.knime.knip.scijava.commands.settings.NodeSettingsService;
8+
import org.knime.knip.scijava.commands.settings.SettingsModelTypeService;
9+
import org.scijava.Context;
10+
import org.scijava.plugin.Parameter;
11+
import org.slf4j.Logger;
12+
import org.slf4j.LoggerFactory;
13+
14+
/**
15+
* Default implementation of {@link KNIMEScijavaContext}. Enables convenient
16+
* access to the KNIME related services in a Scijava Context.
17+
*
18+
* @author Jonathan Hale (University of Konstanz)
19+
*/
20+
public class DefaultKNIMEScijavaContext implements KNIMEScijavaContext {
21+
22+
private Logger m_log = LoggerFactory.getLogger(getClass());
23+
24+
@Parameter
25+
private Context m_context;
26+
27+
@Parameter
28+
private InputAdapterService inputAdapterService;
29+
@Parameter
30+
private OutputAdapterService outputAdapterService;
31+
@Parameter
32+
private KNIMEInputDataTableService inputTableService;
33+
@Parameter
34+
private KNIMEOutputDataTableService outputTableService;
35+
@Parameter
36+
private ColumnToInputMappingService inputMappingService;
37+
@Parameter
38+
private OutputToColumnMappingService outputMappingService;
39+
@Parameter
40+
private SettingsModelTypeService settingsModelTypesService;
41+
@Parameter
42+
private NodeSettingsService nodeSettingsService;
43+
@Parameter
44+
private KNIMEExecutionService executionService;
45+
46+
@Override
47+
public Context context() {
48+
return m_context;
49+
}
50+
51+
@Override
52+
public Context getContext() {
53+
return context();
54+
}
55+
56+
/**
57+
* {@inheritDoc}
58+
*
59+
* @throws IllegalArgumentException
60+
* If a service is missing
61+
*/
62+
@Override
63+
public void setContext(Context context) throws IllegalArgumentException {
64+
if (m_context == context) {
65+
m_log.warn(
66+
"CODING PROBLEM - Scijava context set mutiple times. Should only be set once.");
67+
return;
68+
}
69+
context.inject(this);
70+
}
71+
72+
@Override
73+
public InputAdapterService inputAdapters() {
74+
return inputAdapterService;
75+
}
76+
77+
@Override
78+
public OutputAdapterService outputAdapters() {
79+
return outputAdapterService;
80+
}
81+
82+
@Override
83+
public KNIMEInputDataTableService inputTable() {
84+
return inputTableService;
85+
}
86+
87+
@Override
88+
public KNIMEOutputDataTableService outputTable() {
89+
return outputTableService;
90+
}
91+
92+
@Override
93+
public ColumnToInputMappingService inputMapping() {
94+
return inputMappingService;
95+
}
96+
97+
@Override
98+
public OutputToColumnMappingService outputMapping() {
99+
return outputMappingService;
100+
}
101+
102+
@Override
103+
public SettingsModelTypeService settingsModelTypes() {
104+
return settingsModelTypesService;
105+
}
106+
107+
@Override
108+
public NodeSettingsService nodeSettings() {
109+
return nodeSettingsService;
110+
}
111+
112+
@Override
113+
public KNIMEExecutionService execution() {
114+
return executionService;
115+
}
116+
117+
}
Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,47 @@
11
package org.knime.knip.scijava.commands;
22

3+
import java.lang.ref.WeakReference;
4+
35
import org.knime.core.node.ExecutionContext;
46
import org.scijava.Priority;
57
import org.scijava.plugin.Plugin;
68
import org.scijava.service.AbstractService;
79

810
/**
9-
* Default implementation of KnimeExecutionService.
11+
* Default implementation of KnimeExecutionService. Holds a KNIME Node
12+
* {@link ExecutionContext} in a {@link WeakReference}, which ensures that the
13+
* {@link ExecutionContext} can be garbage collected when execution of a Node
14+
* terminates.
1015
*
1116
* @author Jonathan Hale (University of Konstanz)
12-
*
17+
* @see ExecutionContext
1318
*/
14-
@Plugin(type = KnimeExecutionService.class, priority = DefaultKnimeExecutionService.PRIORITY)
15-
public class DefaultKnimeExecutionService extends AbstractService implements
16-
KnimeExecutionService {
19+
@Plugin(type = KNIMEExecutionService.class, priority = DefaultKnimeExecutionService.PRIORITY)
20+
public class DefaultKnimeExecutionService extends AbstractService
21+
implements KNIMEExecutionService {
1722

1823
/**
1924
* Priority of this {@link Plugin}
2025
*/
2126
public static final double PRIORITY = Priority.NORMAL_PRIORITY;
2227

23-
private ExecutionContext m_exec = null;
28+
private WeakReference<ExecutionContext> m_exec = new WeakReference<>(null);
2429

25-
public void setExecutionContex(ExecutionContext e) {
26-
m_exec = e;
30+
/**
31+
* Set the {@link ExecutionContext}. Note that this service holds the
32+
* {@link ExecutionContext} in a {@link WeakReference}, which means that the
33+
* reference needs to be kept valid outside the service.
34+
*
35+
* @param context
36+
*/
37+
@Override
38+
public void setExecutionContext(final ExecutionContext context) {
39+
m_exec = new WeakReference<>(context);
2740
}
28-
41+
2942
@Override
3043
public ExecutionContext getExecutionContext() {
31-
return m_exec;
44+
return m_exec.get();
3245
}
3346

3447
}
Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,45 @@
11
package org.knime.knip.scijava.commands;
22

3+
import java.lang.ref.WeakReference;
4+
35
import org.knime.core.data.DataRow;
46
import org.scijava.Priority;
57
import org.scijava.plugin.Plugin;
68
import org.scijava.service.AbstractService;
79

810
/**
9-
* Default implementation of OutputDataRowService.
11+
* Default implementation of OutputDataRowService. Holds a {@link WeakReference}
12+
* to a {@link DataRow} to ensure that it can be garbage collected once the
13+
* {@link DataRow} is not referenced outside of this service.
1014
*
1115
* @author Jonathan Hale (University of Konstanz)
1216
*
1317
*/
1418
@Plugin(type = OutputDataRowService.class, priority = DefaultOutputDataRowService.PRIORITY)
15-
public class DefaultOutputDataRowService extends AbstractService implements
16-
OutputDataRowService {
19+
public class DefaultOutputDataRowService extends AbstractService
20+
implements OutputDataRowService {
1721

1822
/**
1923
* Priority of this {@link Plugin}
2024
*/
2125
public static final double PRIORITY = Priority.NORMAL_PRIORITY;
2226

23-
private DataRow m_row = null;
27+
private WeakReference<DataRow> m_row = new WeakReference<>(null);
2428

2529
/**
26-
*{@inheritDoc}
30+
* {@inheritDoc}
2731
*/
2832
@Override
2933
public void setOutputDataRow(DataRow r) {
30-
m_row = r;
34+
m_row = new WeakReference<>(r);
3135
}
3236

3337
/**
34-
*{@inheritDoc}
38+
* {@inheritDoc}
3539
*/
3640
@Override
3741
public DataRow getOutputDataRow() {
38-
return m_row;
42+
return m_row.get();
3943
}
4044

4145
}
Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,23 @@
1010
* <p>
1111
* KnimeExecutionService plugins discoverable at runtime must implement this
1212
* interface and be annotated with @{@link Plugin} with attribute
13-
* {@link Plugin#type()} = {@link KnimeExecutionService}.class.
13+
* {@link Plugin#type()} = {@link KNIMEExecutionService}.class.
1414
* </p>
1515
*
1616
* @author Jonathan Hale (University of Konstanz)
1717
*
1818
*/
19-
public interface KnimeExecutionService extends Service {
19+
public interface KNIMEExecutionService extends Service {
2020

21+
/**
22+
* @return the ExecutionContext held by this service, or null if there is
23+
* none.
24+
*/
2125
ExecutionContext getExecutionContext();
2226

27+
/**
28+
* Set the ExecutionContext for this service to hold.
29+
*/
30+
void setExecutionContext(ExecutionContext context);
31+
2332
}

0 commit comments

Comments
 (0)