本文整理汇总了Java中javax.script.ScriptEngineFactory.getScriptEngine方法的典型用法代码示例。如果您正苦于以下问题:Java ScriptEngineFactory.getScriptEngine方法的具体用法?Java ScriptEngineFactory.getScriptEngine怎么用?Java ScriptEngineFactory.getScriptEngine使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.script.ScriptEngineFactory
的用法示例。
在下文中一共展示了ScriptEngineFactory.getScriptEngine方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: analyseProvides
import javax.script.ScriptEngineFactory; //导入方法依赖的package包/类
private void analyseProvides() {
ScriptEngineFactory nsef = new NashornScriptEngineFactory();
ScriptEngine engine = nsef.getScriptEngine();
try {
//Load the script
String script = getScriptBuffer().toString(Charset.forName("UTF-8"));
engine.eval(script);
//clear out the current provides list
provides.clear();
//Check if the functions we are monitoring exist or not
for (Function resourceFunction : getFunctions()) {
Boolean exists = (Boolean) engine.eval(generateCheck(resourceFunction.getFunctionName()));
if (exists) {
provides.add(resourceFunction);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
示例2: testJavascriptHelloWorld
import javax.script.ScriptEngineFactory; //导入方法依赖的package包/类
@Test
public void testJavascriptHelloWorld() {
if (java15) {
return;
}
ScriptEngine jsEngine = null;
for (ScriptEngineFactory factory : mgr.getEngineFactories()) {
if (factory.getEngineName().toLowerCase().matches(".*(rhino|javascript|ecma).*")) {
jsEngine = factory.getScriptEngine();
}
}
if (null == jsEngine) {
log.error("Javascript is not supported in this build");
}
try {
jsEngine.eval("print('Javascript - Hello, world!\\n')");
} catch (Throwable ex) {
System.err.println("Get by name failed for: javascript");
}
}
示例3: L2ScriptEngineManager
import javax.script.ScriptEngineFactory; //导入方法依赖的package包/类
private L2ScriptEngineManager()
{
ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
List<ScriptEngineFactory> factories = scriptEngineManager.getEngineFactories();
if (USE_COMPILED_CACHE)
_cache = this.loadCompiledScriptCache();
else
_cache = null;
_log.info("Initializing Script Engine Manager");
for (ScriptEngineFactory factory : factories)
try
{
ScriptEngine engine = factory.getScriptEngine();
boolean reg = false;
for (String name : factory.getNames())
{
ScriptEngine existentEngine = _nameEngines.get(name);
if (existentEngine != null)
{
double engineVer = Double.parseDouble(factory.getEngineVersion());
double existentEngVer = Double.parseDouble(existentEngine.getFactory().getEngineVersion());
if (engineVer <= existentEngVer)
continue;
}
reg = true;
_nameEngines.put(name, engine);
}
if (reg)
_log.info("Script Engine: " + factory.getEngineName() + " " + factory.getEngineVersion() + " - Language: " + factory.getLanguageName() + " - Language Version: " + factory.getLanguageVersion());
for (String ext : factory.getExtensions())
if (!ext.equals("java") || factory.getLanguageName().equals("java"))
_extEngines.put(ext, engine);
}
catch (Exception e)
{
_log.warning("Failed initializing factory. ");
e.printStackTrace();
}
this.preConfigure();
}
示例4: testKotlin
import javax.script.ScriptEngineFactory; //导入方法依赖的package包/类
@Test
public void testKotlin() throws ScriptException {
ScriptEngineFactory factory = new ScriptEngineManager().getEngineByExtension("kts").getFactory();
ScriptEngine engine = factory.getScriptEngine();
engine.eval("val x = 3");
engine.eval("class A");
engine.eval("A()");
engine.eval("fun a()\n {\n val y=1\nprintln(y)\n}\n");
engine.eval("fun onStartup() {\n" + " var x = \"abc\"\n" + " println(x)\n" + "}");
}
示例5: retrieveEngineByName
import javax.script.ScriptEngineFactory; //导入方法依赖的package包/类
/**
* Retrieve a {@link ScriptEngine} from the given {@link ScriptEngineManager}
* by name, delegating to {@link ScriptEngineManager#getEngineByName} but
* throwing a descriptive exception if not found or if initialization failed.
* @param scriptEngineManager the ScriptEngineManager to use
* @param engineName the name of the engine
* @return a corresponding ScriptEngine (never {@code null})
* @throws IllegalArgumentException if no matching engine has been found
* @throws IllegalStateException if the desired engine failed to initialize
*/
public static ScriptEngine retrieveEngineByName(ScriptEngineManager scriptEngineManager, String engineName) {
ScriptEngine engine = scriptEngineManager.getEngineByName(engineName);
if (engine == null) {
Set<String> engineNames = new LinkedHashSet<String>();
for (ScriptEngineFactory engineFactory : scriptEngineManager.getEngineFactories()) {
List<String> factoryNames = engineFactory.getNames();
if (factoryNames.contains(engineName)) {
// Special case: getEngineByName returned null but engine is present...
// Let's assume it failed to initialize (which ScriptEngineManager silently swallows).
// If it happens to initialize fine now, alright, but we really expect an exception.
try {
engine = engineFactory.getScriptEngine();
engine.setBindings(scriptEngineManager.getBindings(), ScriptContext.GLOBAL_SCOPE);
}
catch (Throwable ex) {
throw new IllegalStateException("Script engine with name '" + engineName +
"' failed to initialize", ex);
}
}
engineNames.addAll(factoryNames);
}
throw new IllegalArgumentException("Script engine with name '" + engineName +
"' not found; registered engine names: " + engineNames);
}
return engine;
}
示例6: resolveScriptEngine
import javax.script.ScriptEngineFactory; //导入方法依赖的package包/类
@SuppressWarnings({"rawtypes"})
@Override
public ScriptEngine resolveScriptEngine(String name) {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(configFile.openStream()));
String className = in.readLine();
in.close();
Class cls = bundle.loadClass(className);
if (!ScriptEngineFactory.class.isAssignableFrom(cls)) {
throw new IllegalStateException("Invalid ScriptEngineFactory: " + cls.getName());
}
ScriptEngineFactory factory = (ScriptEngineFactory) cls.newInstance();
List<String> names = factory.getNames();
for (String test : names) {
if (test.equals(name)) {
ClassLoader old = Thread.currentThread().getContextClassLoader();
ScriptEngine engine;
try {
// JRuby seems to require the correct TCCL to call
// getScriptEngine
Thread.currentThread().setContextClassLoader(factory.getClass().getClassLoader());
engine = factory.getScriptEngine();
} finally {
Thread.currentThread().setContextClassLoader(old);
}
LOGGER.trace("Resolved ScriptEngineFactory: {} for expected name: {}", engine, name);
return engine;
}
}
LOGGER.debug("ScriptEngineFactory: {} does not match expected name: {}", factory.getEngineName(), name);
return null;
} catch (Exception e) {
LOGGER.warn("Cannot create ScriptEngineFactory: {}", e.getClass().getName(), e);
return null;
}
}
示例7: resolveScriptEngine
import javax.script.ScriptEngineFactory; //导入方法依赖的package包/类
public ScriptEngine resolveScriptEngine(String name) {
try {
ScriptEngineFactory factory = getFactory();
if (factory != null) {
List<String> names = getScriptNames(factory);
for (String test : names) {
if (test.equals(name)) {
ClassLoader old = Thread.currentThread().getContextClassLoader();
ScriptEngine engine;
try {
// JRuby seems to require the correct TCCL to call getScriptEngine
Thread.currentThread().setContextClassLoader(factory.getClass().getClassLoader());
engine = factory.getScriptEngine();
} finally {
Thread.currentThread().setContextClassLoader(old);
}
LOG.trace("Resolved ScriptEngineFactory: {} for expected name: {}", engine, name);
return engine;
}
}
LOG.debug("ScriptEngineFactory: {} does not match expected name: {}", factory.getEngineName(), name);
return null;
}
} catch (Exception e) {
LOG.warn("Cannot create ScriptEngineFactory: " + e.getClass().getName(), e);
return null;
}
return null;
}
示例8: createScriptEngine
import javax.script.ScriptEngineFactory; //导入方法依赖的package包/类
/**
* Provides a ScriptEngine corresponding to the currently selected script engine name.
* ScriptEngineManager.getEngineByName() doesn't use find ScriptEngineFactory.getName(), which
* is what we used to populate the list. So just search the list of factories until a match is
* found, then create and return a script engine.
*
* @return a Script Engine corresponding to the currently specified name, or null if none is found.
*/
protected ScriptEngine createScriptEngine() {
//
ScriptEngineFactory factory = scriptEngineFactoryMap.get(scriptEngineName);
if (factory == null) {
return null;
}
return factory.getScriptEngine();
}
示例9: findScriptEngine
import javax.script.ScriptEngineFactory; //导入方法依赖的package包/类
public static ScriptEngine findScriptEngine(final String engine) {
final ScriptEngineManager engineManager = new ScriptEngineManager();
final List<ScriptEngineFactory> engineFactories = engineManager.getEngineFactories();
for (final ScriptEngineFactory engineFactory : engineFactories) {
if (isSameEngine(engineFactory, engine)) {
return engineFactory.getScriptEngine();
}
}
return null;
}
示例10: resolveScriptEngine
import javax.script.ScriptEngineFactory; //导入方法依赖的package包/类
public ScriptEngine resolveScriptEngine(String name) {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(configFile.openStream()));
String className = in.readLine();
in.close();
Class cls = bundle.loadClass(className);
if (!ScriptEngineFactory.class.isAssignableFrom(cls)) {
throw new IllegalStateException("Invalid ScriptEngineFactory: " + cls.getName());
}
ScriptEngineFactory factory = (ScriptEngineFactory) cls.newInstance();
List<String> names = factory.getNames();
for (String test : names) {
if (test.equals(name)) {
ClassLoader old = Thread.currentThread().getContextClassLoader();
ScriptEngine engine;
try {
// JRuby seems to require the correct TCCL to call getScriptEngine
Thread.currentThread().setContextClassLoader(factory.getClass().getClassLoader());
engine = factory.getScriptEngine();
} finally {
Thread.currentThread().setContextClassLoader(old);
}
LOGGER.finest("Resolved ScriptEngineFactory: " + engine + " for expected name: " + name);
return engine;
}
}
LOGGER.fine("ScriptEngineFactory: " + factory.getEngineName() + " does not match expected name: " + name);
return null;
} catch (Exception e) {
LOGGER.log(Level.WARNING, "Cannot create ScriptEngineFactory: " + e.getClass().getName(), e);
return null;
}
}
示例11: getScriptEngineDirect
import javax.script.ScriptEngineFactory; //导入方法依赖的package包/类
@Override
public ScriptEngine getScriptEngineDirect(String language)
throws TeiidProcessingException {
if (this.scriptEngineManager == null) {
this.scriptEngineManager = new ScriptEngineManager();
}
ScriptEngine engine = null;
if (allowedLanguages == null || allowedLanguages.contains(language)) {
/*
* because of state caching in the engine, we'll return a new instance for each
* usage. we can pool if needed and add a returnEngine method
*/
ScriptEngineFactory sef = this.scriptEngineFactories.get(language);
if (sef != null) {
try {
engine = sef.getScriptEngine();
engine.setBindings(scriptEngineManager.getBindings(), ScriptContext.ENGINE_SCOPE);
} catch (Exception e) {
//just swallow the exception to mimic the jsr behavior
}
}
engine = this.scriptEngineManager.getEngineByName(language);
}
if (engine == null) {
Set<String> names = new LinkedHashSet<String>();
for (ScriptEngineFactory factory : this.scriptEngineManager.getEngineFactories()) {
names.addAll(factory.getNames());
}
if (allowedLanguages != null) {
names.retainAll(allowedLanguages);
}
names.add(ObjectTable.DEFAULT_LANGUAGE);
throw new TeiidProcessingException(QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31109, language, names));
}
this.scriptEngineFactories.put(language, engine.getFactory());
return engine;
}
示例12: getScriptEngine
import javax.script.ScriptEngineFactory; //导入方法依赖的package包/类
public ScriptEngine getScriptEngine(String engineInfo){
ScriptEngineFactory factory=engines.get(engineInfo);
if(factory==null){
javax.script.ScriptEngineManager manager=new javax.script.ScriptEngineManager();
List<ScriptEngineFactory> fs=manager.getEngineFactories();
int bestScore=0;
for(ScriptEngineFactory f : fs){
int s=matchEngine(engineInfo,f);
if(s==10) {
factory=f;
break;
}
else if(s>bestScore){
bestScore=s;
factory=f;
}
}
engines.put(engineInfo,factory);
}
if(factory==null) return null;
ScriptEngine engine = factory.getScriptEngine();
if(engine != null) {
try {
String engineName = factory.getEngineName();
if(engineName != null && engineName.toLowerCase().contains("nashorn")) {
// https://bugs.openjdk.java.net/browse/JDK-8025132
engine.eval("load('nashorn:mozilla_compat.js');");
}
}
catch(Exception e) {}
}
return engine;
}
示例13: resolveScriptEngine
import javax.script.ScriptEngineFactory; //导入方法依赖的package包/类
@Override
public ScriptEngine resolveScriptEngine(String name) {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(
configFile.openStream()));
String className = removeCommentsFromInput(in);
in.close();
Class<?> cls = bundle.loadClass(className);
if (!ScriptEngineFactory.class.isAssignableFrom(cls)) {
throw new IllegalStateException("Invalid ScriptEngineFactory: "
+ cls.getName());
}
ScriptEngineFactory factory = (ScriptEngineFactory) cls
.newInstance();
List<String> names = factory.getNames();
for (String n : names) {
if (n.equals(name)) {
ClassLoader old = Thread.currentThread()
.getContextClassLoader();
ScriptEngine engine;
try {
// JRuby seems to require the correct TCCL to call
// getScriptEngine
Thread.currentThread().setContextClassLoader(
factory.getClass().getClassLoader());
engine = factory.getScriptEngine();
} finally {
Thread.currentThread().setContextClassLoader(old);
}
LOGGER.finest("Resolved ScriptEngineFactory: " + engine
+ " for expected name: " + name);
return engine;
}
}
LOGGER.fine("ScriptEngineFactory: " + factory.getEngineName()
+ " does not match expected name: " + name);
return null;
} catch (Exception e) {
LOGGER.log(Level.WARNING, "Cannot create ScriptEngineFactory: "
+ e.getClass().getName(), e);
return null;
}
}