Skip to content

Commit

Permalink
Use the engineFactory as the hash key
Browse files Browse the repository at this point in the history
Signed-off-by: Jimmy Tanagra <jcode@tanagra.id.au>
  • Loading branch information
jimtng committed Apr 2, 2023
1 parent fc09b44 commit 7990a14
Showing 1 changed file with 6 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class ScriptTransformationServiceFactory {

private final ComponentFactory<ScriptTransformationService> scriptTransformationFactory;

private final Map<String, ComponentInstance<ScriptTransformationService>> scriptTransformations = new ConcurrentHashMap<>();
private final Map<ScriptEngineFactory, ComponentInstance<ScriptTransformationService>> scriptTransformations = new ConcurrentHashMap<>();

@Activate
public ScriptTransformationServiceFactory(
Expand All @@ -69,28 +69,22 @@ public void setScriptEngineFactory(ScriptEngineFactory engineFactory) {
return;
}

scriptTransformations.computeIfAbsent(scriptType.get(), type -> {
ScriptEngine scriptEngine = engineFactory.createScriptEngine(type);
scriptTransformations.computeIfAbsent(engineFactory, factory -> {
ScriptEngine scriptEngine = engineFactory.createScriptEngine(scriptType.get());
if (scriptEngine == null) {
return null;
}
String languageName = ScriptEngineFactoryHelper.getLanguageName(scriptEngine.getFactory());
Dictionary<String, Object> properties = new Hashtable<>();
properties.put(TransformationService.SERVICE_PROPERTY_NAME, type.toUpperCase());
properties.put(TransformationService.SERVICE_PROPERTY_NAME, scriptType.get().toUpperCase());
properties.put(TransformationService.SERVICE_PROPERTY_LABEL, "SCRIPT " + languageName);
properties.put(ScriptTransformationService.SCRIPT_TYPE_PROPERTY_NAME, type);
properties.put(ScriptTransformationService.SCRIPT_TYPE_PROPERTY_NAME, scriptType.get());
return scriptTransformationFactory.newInstance(properties);
});
}

public void unsetScriptEngineFactory(ScriptEngineFactory engineFactory) {
Optional<String> scriptType = ScriptEngineFactoryHelper.getPreferredExtension(engineFactory);
if (scriptType.isEmpty()) {
return;
}

ComponentInstance<ScriptTransformationService> toBeUnregistered = scriptTransformations
.remove(scriptType.get());
ComponentInstance<ScriptTransformationService> toBeUnregistered = scriptTransformations.remove(engineFactory);
if (toBeUnregistered != null) {
unregisterService(toBeUnregistered);
}
Expand Down

0 comments on commit 7990a14

Please sign in to comment.