Source:
<code>public Rendition getDefaultRendition(DocumentModel doc, String reason, boolean store, Map<String, Serializable> extendedInfos) { Map<String, Object> context = new HashMap<>(); Map<String, Serializable> ei = extendedInfos == null ? Collections.emptyMap() : extendedInfos; NuxeoPrincipal currentUser = ClientLoginModule.getCurrentPrincipal(); context.put("Document", doc); context.put("Infos", ei); context.put("CurrentUser", currentUser); ScriptEngine engine; for (int i = defaultRenditionDescriptors.size() - 1; i >= 0; i--) { DefaultRenditionDescriptor desc = defaultRenditionDescriptors.get(i); if ((StringUtils.isEmpty(reason) && StringUtils.isEmpty(desc.reason)) || (reason != null && reason.equals(desc.reason))) { <START> engine = scriptEngineManager.getEngineByName(desc.getScriptLanguage()); <END> if (engine == null) { throw new NuxeoException("Engine not found for language: " + desc.getScriptLanguage()); } if (!(engine instanceof Invocable)) { throw new NuxeoException("Engine " + engine.getClass().getName() + " not Invocable for language: " + desc.getScriptLanguage()); } try { engine.eval(desc.getScript()); engine.getBindings(ScriptContext.ENGINE_SCOPE).putAll(context); Object result = ((Invocable) engine).invokeFunction("run"); if (result == null && desc.override) { break; } else { String defaultRenditionName = (String) result; if (!StringUtils.isBlank(defaultRenditionName)) { try { return getRendition(doc, defaultRenditionName, store); } catch (NuxeoException e) { log.error("Unable to use default rendition " + defaultRenditionName, e); } } } } catch (NoSuchMethodException e) { throw new NuxeoException("Script does not contain function: run() in defaultRendition: ", e); } catch (ScriptException e) { log.error("Failed to evaluate script: ", e); } } } if (log.isWarnEnabled()) { log.warn(String.format("Failed to get rendition name for reason %s", reason)); } return null; }</code><technical_language>nit: reuse engine move this loop? I guess safe assume scripts mess global context</technical_language>

Target:
public Rendition getDefaultRendition(DocumentModel doc, String reason, boolean store, Map<String, Serializable> extendedInfos) { Map<String, Object> context = new HashMap<>(); Map<String, Serializable> ei = extendedInfos == null ? Collections.emptyMap() : extendedInfos; NuxeoPrincipal currentUser = ClientLoginModule.getCurrentPrincipal(); context.put("Document", doc); context.put("Infos", ei); context.put("CurrentUser", currentUser); ScriptEngine engine = null; for (int i = defaultRenditionDescriptors.size() - 1; i >= 0; i--) { DefaultRenditionDescriptor desc = defaultRenditionDescriptors.get(i); if ((StringUtils.isEmpty(reason) && StringUtils.isEmpty(desc.reason)) || (reason != null && reason.equals(desc.reason))) { String scriptLanguage = desc.getScriptLanguage(); if (engine == null || !engine.getFactory().getNames().contains(scriptLanguage)) { engine = scriptEngineManager.getEngineByName(scriptLanguage); if (engine == null) { throw new NuxeoException("Engine not found for language: " + scriptLanguage); } } if (!(engine instanceof Invocable)) { throw new NuxeoException("Engine " + engine.getClass().getName() + " not Invocable for language: " + scriptLanguage); } try { engine.eval(desc.getScript()); engine.getBindings(ScriptContext.ENGINE_SCOPE).putAll(context); Object result = ((Invocable) engine).invokeFunction("run"); if (result == null && desc.override) { break; } else { String defaultRenditionName = (String) result; if (!StringUtils.isBlank(defaultRenditionName)) { try { return getRendition(doc, defaultRenditionName, store); } catch (NuxeoException e) { log.error("Unable to use default rendition " + defaultRenditionName, e); } } } } catch (NoSuchMethodException e) { throw new NuxeoException("Script does not contain function: run() in defaultRendition: ", e); } catch (ScriptException e) { log.error("Failed to evaluate script: ", e); } } } if (log.isWarnEnabled()) { log.warn(String.format("Failed to get rendition name for reason %s", reason)); } return null; }