Skip to content

Commit 2885439

Browse files
committed
Use JythonService to manage cleanup
The JythonReferenceCleaner needs to be shut down when the Context is disposed. The JythonService provides us a dispose() hook to ensure this happens. To maintain the JythonReferenceCleaner, the JythonService must also be the gateway for creating JythonScriptEngines.
1 parent 63bade1 commit 2885439

File tree

4 files changed

+154
-13
lines changed

4 files changed

+154
-13
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* #%L
3+
* JSR-223-compliant Jython scripting language plugin.
4+
* %%
5+
* Copyright (C) 2008 - 2016 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
* #L%
30+
*/
31+
package org.scijava.plugins.scripting.jython;
32+
33+
import javax.script.ScriptEngine;
34+
35+
import org.scijava.log.LogService;
36+
import org.scijava.plugin.Parameter;
37+
import org.scijava.plugin.Plugin;
38+
import org.scijava.service.AbstractService;
39+
import org.scijava.service.Service;
40+
import org.scijava.thread.ThreadService;
41+
42+
/**
43+
* Default {@link JythonService} implementation. Maintains a
44+
* {@link JythonReferenceCleaner} instance.
45+
*
46+
* @author Mark Hiner hinerm at gmail.com
47+
*/
48+
@Plugin(type = Service.class)
49+
public class DefaultJythonService extends AbstractService implements JythonService {
50+
51+
// -- Fields --
52+
53+
private JythonReferenceCleaner cleaner;
54+
55+
// -- Parameters --
56+
57+
@Parameter
58+
private ThreadService threadService;
59+
60+
@Parameter
61+
private LogService logService;
62+
63+
// -- JythonService methods --
64+
65+
@Override
66+
public ScriptEngine getScriptEngine() {
67+
// TODO: Consider adapting the wrapped ScriptEngineFactory's
68+
// ScriptEngine.
69+
final JythonScriptEngine engine = new JythonScriptEngine();
70+
cleaner.queueCleanup(engine, threadService, logService);
71+
return engine;
72+
}
73+
74+
// -- Service methods --
75+
76+
@Override
77+
public void initialize() {
78+
cleaner = new JythonReferenceCleaner();
79+
}
80+
81+
// -- Disposable methods --
82+
83+
@Override
84+
public void dispose() {
85+
cleaner.shutDown();
86+
}
87+
}

src/main/java/org/scijava/plugins/scripting/jython/JythonReferenceCleaner.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ public class JythonReferenceCleaner {
5959
private final ReferenceQueue<JythonScriptEngine> queue =
6060
new ReferenceQueue<JythonScriptEngine>();
6161

62+
private boolean shutDown = false;
63+
6264
/** Queues the future cleanup operation on a separate thread, */
6365
public synchronized void queueCleanup(final JythonScriptEngine engine,
6466
final ThreadService threadService, final LogService log)
@@ -106,7 +108,7 @@ public void run() {
106108

107109
// Once we're done with our known phantom refs
108110
// we can let this thread shut down
109-
done = phantomReferences.size() == 0;
111+
done = phantomReferences.size() == 0 || shutDown;
110112
}
111113

112114
}
@@ -120,6 +122,10 @@ public void run() {
120122
}
121123
}
122124

125+
public void shutDown() {
126+
shutDown = true;
127+
}
128+
123129
// -- Helper classes --
124130

125131
/**

src/main/java/org/scijava/plugins/scripting/jython/JythonScriptLanguage.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,11 @@
3636
import org.python.core.PyNone;
3737
import org.python.core.PyObject;
3838
import org.python.core.PyString;
39-
import org.scijava.log.LogService;
39+
import org.scijava.Context;
4040
import org.scijava.plugin.Parameter;
4141
import org.scijava.plugin.Plugin;
4242
import org.scijava.script.AdaptedScriptLanguage;
4343
import org.scijava.script.ScriptLanguage;
44-
import org.scijava.thread.ThreadService;
4544

4645
/**
4746
* An adapter of the Jython interpreter to the SciJava scripting interface.
@@ -53,24 +52,20 @@
5352
@Plugin(type = ScriptLanguage.class, name = "Python")
5453
public class JythonScriptLanguage extends AdaptedScriptLanguage {
5554

56-
private JythonReferenceCleaner cleaner = new JythonReferenceCleaner();
57-
58-
@Parameter
59-
private ThreadService threadService;
60-
6155
@Parameter
62-
private LogService logService;
56+
private Context context;
6357

6458
public JythonScriptLanguage() {
6559
super("jython");
6660
}
6761

6862
@Override
6963
public ScriptEngine getScriptEngine() {
70-
// TODO: Consider adapting the wrapped ScriptEngineFactory's ScriptEngine.
71-
final JythonScriptEngine engine = new JythonScriptEngine();
72-
cleaner.queueCleanup(engine, threadService, logService);
73-
return engine;
64+
// NB: recursive priorities can only be resolved via inter-service
65+
// dependencies. There is no way to make the ScriptService
66+
// depend on the JythonService because of the hierarchy
67+
// of components. So we have to get the JythonService indirectly.
68+
return context.service(JythonService.class).getScriptEngine();
7469
}
7570

7671
@Override
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* #%L
3+
* JSR-223-compliant Jython scripting language plugin.
4+
* %%
5+
* Copyright (C) 2008 - 2016 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
* #L%
30+
*/
31+
package org.scijava.plugins.scripting.jython;
32+
33+
import javax.script.ScriptEngine;
34+
35+
import org.scijava.service.SciJavaService;
36+
import org.scijava.service.Service;
37+
38+
/**
39+
* {@link Service} for managing contextual Jython metadata.
40+
* <p>
41+
* The {@link #getScriptEngine()} method should be used to access
42+
* {@link JythonScriptEngine} instances, to ensure they are cleaned properly.
43+
* </p>
44+
*
45+
* @author Mark Hiner hinerm at gmail.com
46+
*/
47+
public interface JythonService extends SciJavaService {
48+
49+
/**
50+
* @return Get a Jython {@link ScriptEngine} instance.
51+
*/
52+
ScriptEngine getScriptEngine();
53+
}

0 commit comments

Comments
 (0)