001/* 002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl-2.1.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Stephane Lacoin <slacoin@nuxeo.com> 016 * Vladimir Pasquier <vpasquier@nuxeo.com> 017 */ 018package org.nuxeo.automation.scripting.internals; 019 020import javax.script.ScriptEngineFactory; 021import javax.script.ScriptEngineManager; 022 023import org.apache.commons.logging.Log; 024import org.apache.commons.logging.LogFactory; 025import org.nuxeo.automation.scripting.api.AutomationScriptingConstants; 026import org.nuxeo.runtime.api.Framework; 027 028/** 029 * This factory configures the Nashorn engine and register it following the JVM version. 030 * 031 * @since 7.3 032 */ 033public class ScriptingFactory { 034 035 private static final Log log = LogFactory.getLog(ScriptingFactory.class); 036 037 protected ScriptEngineManager scriptEngineManager = new ScriptEngineManager(); 038 039 protected void install() { 040 scriptEngineManager.registerEngineName(AutomationScriptingConstants.NX_NASHORN, newFactory()); 041 } 042 043 protected ScriptEngineFactory newFactory() { 044 String version = Framework.getProperty("java.version", "1.8"); 045 // Check if jdk8 046 if (version.contains(AutomationScriptingConstants.NASHORN_JAVA_VERSION)) { 047 // Check if version < jdk8u25 -> no cache. 048 if (version.compareTo(AutomationScriptingConstants.COMPLIANT_JAVA_VERSION_CACHE) < 0) { 049 log.warn(AutomationScriptingConstants.NASHORN_WARN_CACHE); 050 return new ScriptingCache(false); 051 // Check if jdk8u25 <= version < jdk8u40 -> only cache. 052 } else if (version.compareTo(AutomationScriptingConstants.COMPLIANT_JAVA_VERSION_CACHE) >= 0 053 && version.compareTo(AutomationScriptingConstants.COMPLIANT_JAVA_VERSION_CLASS_FILTER) < 0) { 054 if (Boolean.valueOf(Framework.getProperty(AutomationScriptingConstants.AUTOMATION_SCRIPTING_PRECOMPILE, 055 AutomationScriptingConstants.DEFAULT_PRECOMPILE_STATUS))) { 056 log.warn(AutomationScriptingConstants.NASHORN_WARN_CLASS_FILTER); 057 return new ScriptingCache(true); 058 } else { 059 log.warn(AutomationScriptingConstants.NASHORN_WARN_CLASS_FILTER); 060 return new ScriptingCache(false); 061 } 062 // Check if version >= jdk8u40 -> cache + class filter 063 } else if (version.compareTo(AutomationScriptingConstants.COMPLIANT_JAVA_VERSION_CLASS_FILTER) >= 0) { 064 try { 065 if (Boolean.valueOf(Framework.getProperty( 066 AutomationScriptingConstants.AUTOMATION_SCRIPTING_PRECOMPILE, 067 AutomationScriptingConstants.DEFAULT_PRECOMPILE_STATUS))) { 068 return new ScriptingCacheClassFilter(true); 069 } else { 070 return new ScriptingCacheClassFilter(false); 071 } 072 } catch (NoClassDefFoundError cause) { 073 log.warn(AutomationScriptingConstants.NASHORN_WARN_CLASS_FILTER); 074 return new ScriptingCache(true); 075 } 076 } 077 } 078 log.warn(AutomationScriptingConstants.NASHORN_WARN_VERSION); 079 throw new UnsupportedOperationException(); 080 } 081}