Skip to content

Commit b5915ef

Browse files
committed
Merge branch 'scripting-fixes'
* Register script language names and extensions more intelligently * Stop auto-wrapping vanilla JSR-223 ScriptEngineFactory classes
2 parents d3cbee4 + cb1a16d commit b5915ef

File tree

2 files changed

+47
-34
lines changed

2 files changed

+47
-34
lines changed

src/main/java/org/scijava/script/DefaultScriptService.java

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -343,21 +343,7 @@ private synchronized void initScriptLanguageIndex() {
343343

344344
// add ScriptLanguage plugins
345345
for (final ScriptLanguage language : getInstances()) {
346-
index.add(language, false);
347-
}
348-
349-
// Now look for the ScriptEngines in javax.scripting. We only do that
350-
// now since the javax.scripting framework does not provide all the
351-
// functionality we might want to use in a SciJava application.
352-
final ScriptEngineManager manager = new ScriptEngineManager();
353-
for (final ScriptEngineFactory factory : manager.getEngineFactories()) {
354-
index.add(factory, true);
355-
}
356-
357-
// Inject the context into languages which need it: the
358-
// wrapped engine factories from the ScriptEngineManager.
359-
for (final ScriptLanguage language : index) {
360-
if (language.getContext() == null) language.setContext(getContext());
346+
index.add(language, true);
361347
}
362348

363349
scriptLanguageIndex = index;

src/main/java/org/scijava/script/ScriptLanguageIndex.java

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -75,32 +75,24 @@ public ScriptLanguageIndex(final LogService logService) {
7575
}
7676

7777
public boolean add(final ScriptEngineFactory factory, final boolean gently) {
78-
final String duplicateName = checkDuplicate(factory);
79-
if (duplicateName != null) {
80-
if (gently) return false;
81-
if (log != null) {
82-
log.warn("Duplicate scripting language '" +
83-
duplicateName + "': existing=" +
84-
byName.get(duplicateName).getClass().getName() +
85-
", new=" + factory.getClass().getName());
86-
}
87-
}
78+
boolean result = false;
8879

8980
final ScriptLanguage language = wrap(factory);
9081

9182
// add language names
92-
byName.put(language.getLanguageName(), language);
83+
result |= put("name", byName, language.getLanguageName(), language, gently);
9384
for (final String name : language.getNames()) {
94-
byName.put(name, language);
85+
result |= put("name", byName, name, language, gently);
9586
}
9687

9788
// add file extensions
9889
for (final String extension : language.getExtensions()) {
9990
if ("".equals(extension)) continue;
100-
byExtension.put(extension, language);
91+
result |= put("extension", byExtension, extension, language, gently);
10192
}
10293

103-
return super.add(language);
94+
result |= super.add(language);
95+
return result;
10496
}
10597

10698
public ScriptLanguage getByExtension(final String extension) {
@@ -137,18 +129,53 @@ public boolean add(final ScriptLanguage language) {
137129

138130
// -- Helper methods --
139131

140-
private String checkDuplicate(final ScriptEngineFactory factory) {
141-
for (final String name : factory.getNames()) {
142-
if (byName.containsKey(name)) {
143-
return name;
132+
private boolean put(final String type, final Map<String, ScriptLanguage> map,
133+
final String key, final ScriptLanguage value, final boolean gently)
134+
{
135+
final ScriptLanguage existing = map.get(key);
136+
137+
if (existing == value) {
138+
// Duplicate key/value pair; do not overwrite.
139+
if (log != null && log.isDebug()) {
140+
// In debug mode, warn about the duplicate (since it is atypical).
141+
log.debug(overwriteMessage(false, type, key, value, existing));
142+
}
143+
return false;
144+
}
145+
146+
if (existing != null) {
147+
// Conflicting value; behavior depends on mode.
148+
if (gently) {
149+
// Do not overwrite the previous value.
150+
if (log != null && log.isWarn()) {
151+
log.warn(overwriteMessage(false, type, key, value, existing));
152+
}
153+
return false;
154+
}
155+
if (log != null && log.isDebug()) {
156+
// In debug mode, warn about overwriting.
157+
log.debug(overwriteMessage(true, type, key, value, existing));
144158
}
145159
}
146-
return null;
160+
161+
map.put(key, value);
162+
return true;
147163
}
148164

165+
/** Helper method of {@link #add(ScriptEngineFactory, boolean)}. */
149166
private ScriptLanguage wrap(final ScriptEngineFactory factory) {
150167
if (factory instanceof ScriptLanguage) return (ScriptLanguage) factory;
151168
return new AdaptedScriptLanguage(factory);
152169
}
153170

171+
/** Helper method of {@link #put}. */
172+
private String overwriteMessage(final boolean overwrite, final String type,
173+
final String key, final ScriptLanguage proposed,
174+
final ScriptLanguage existing)
175+
{
176+
return (overwrite ? "Overwriting " : "Not overwriting ") + type + //
177+
" '" + key + "':\n\tproposed = " + proposed.getClass().getName() +
178+
"\n\texisting = " + existing.getClass().getName();
179+
}
180+
154181
}

0 commit comments

Comments
 (0)