-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSciJavaGateway.java
More file actions
115 lines (98 loc) · 3.62 KB
/
SciJavaGateway.java
File metadata and controls
115 lines (98 loc) · 3.62 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
package org.knime.scijava.commands;
import java.util.Arrays;
import java.util.List;
import org.knime.scijava.commands.converter.KNIMEConverterService;
import org.knime.scijava.commands.module.NodeModuleService;
import org.knime.scijava.commands.settings.NodeSettingsService;
import org.knime.scijava.commands.settings.SettingsModelTypeService;
import org.knime.scijava.core.ResourceAwareClassLoader;
import org.knime.scijava.core.SubContext;
import org.knime.scijava.core.pluginindex.ReusablePluginIndex;
import org.scijava.Context;
import org.scijava.command.CommandService;
import org.scijava.display.DisplayPostprocessor;
import org.scijava.object.ObjectService;
import org.scijava.plugin.DefaultPluginFinder;
import org.scijava.plugin.PluginIndex;
import org.scijava.plugin.PluginService;
import org.scijava.prefs.PrefService;
import org.scijava.script.ScriptService;
import org.scijava.service.Service;
import org.scijava.ui.UIService;
import org.scijava.widget.WidgetService;
/**
* Gateway to the SciJava world.
*
* @author Christian Dietz, University of Konstanz
*/
public class SciJavaGateway {
/** singleton instance */
protected static SciJavaGateway m_instance = null;
/** the gateways class loader */
protected ResourceAwareClassLoader m_classLoader = null;
/**
* the cached plugin index. Building the plugin index only needs to be done
* once.
*/
protected PluginIndex m_pluginIndex = null;
/**
* The global context for all KNIP-2.0 Nodes.
*/
private Context m_globalContext;
/** a list of services which need to be present in newly created contexts */
protected static List<Class<? extends Service>> requiredServices = Arrays
.<Class<? extends Service>> asList(PrefService.class,
ObjectService.class, WidgetService.class, UIService.class,
KNIMEConverterService.class, CommandService.class,
NodeModuleService.class, SettingsModelTypeService.class,
NodeSettingsService.class, ScriptService.class);
/**
* Constructor. Only to be called from {@link #get()}.
*/
private SciJavaGateway() {
m_classLoader = new ResourceAwareClassLoader(
getClass().getClassLoader(), getClass());
m_pluginIndex = new ReusablePluginIndex(
new DefaultPluginFinder(m_classLoader));
}
/**
* Get the Gateway instance.
*
* @return the singletons instance
*/
public static synchronized SciJavaGateway get() {
if (m_instance == null) {
m_instance = new SciJavaGateway();
}
return m_instance;
}
/**
* Create a new Scijava {@link Context} with Services required for the
* ScriptingNode.
*
* @return the created context
*/
public Context createSubContext() {
final Context context = new SubContext(getGlobalContext(),
requiredServices,
new PluginIndex(new DefaultPluginFinder(m_classLoader)));
// cleanup unwanted services
final PluginService plugins = context.getService(PluginService.class);
plugins.removePlugin(plugins.getPlugin(DisplayPostprocessor.class));
return context;
}
public Context getGlobalContext() {
if (m_globalContext == null) {
m_globalContext = new Context(m_pluginIndex);
}
return m_globalContext;
}
/**
* Get the {@link ResourceAwareClassLoader} used by this Gateways contexts.
*
* @return class loader for the contexts
*/
public ResourceAwareClassLoader getClassLoader() {
return m_classLoader;
}
}