本文整理匯總了Java中javax.script.ScriptEngineFactory類的典型用法代碼示例。如果您正苦於以下問題:Java ScriptEngineFactory類的具體用法?Java ScriptEngineFactory怎麽用?Java ScriptEngineFactory使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
ScriptEngineFactory類屬於javax.script包,在下文中一共展示了ScriptEngineFactory類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: isJavaScriptAvailable
import javax.script.ScriptEngineFactory; //導入依賴的package包/類
private boolean isJavaScriptAvailable() {
if(isJSAvailableChecked) {
return isJSAvailable;
}
ScriptEngineManager mgr = new ScriptEngineManager();
List<ScriptEngineFactory> factories = mgr.getEngineFactories();
for (ScriptEngineFactory factory: factories) {
List<String> engNames = factory.getNames();
for(String name: engNames) {
if(name.equalsIgnoreCase("js") || name.equalsIgnoreCase("javascript")) { //NOI18N
isJSAvailableChecked = true;
isJSAvailable = true;
return isJSAvailable;
}
}
}
isJSAvailableChecked = true;
isJSAvailable = false;
return isJSAvailable;
}
示例2: getEngineInfo
import javax.script.ScriptEngineFactory; //導入依賴的package包/類
private String getEngineInfo(ScriptEngine engine) {
StringBuilder sb = new StringBuilder();
ScriptEngineFactory f = engine.getFactory();
sb.append("LanguageName=");
sb.append("\"").append(f.getLanguageName()).append("\"");
sb.append(" ");
sb.append("LanguageVersion=");
sb.append("\"").append(f.getLanguageVersion()).append("\"");
sb.append(" ");
sb.append("EngineName=");
sb.append("\"").append(f.getEngineName()).append("\"");
sb.append(" ");
sb.append("EngineNameAliases=");
sb.append(Arrays.toString(f.getNames().toArray(new String[f.getNames().size()])));
sb.append(" ");
sb.append("EngineVersion=");
sb.append("\"").append(f.getEngineVersion()).append("\"");
return sb.toString();
}
示例3: execute
import javax.script.ScriptEngineFactory; //導入依賴的package包/類
@Override
@PreAuthorize("checkPermission('Scripts')")
public ScriptOptionsInterface execute(GetScriptOptionsRpcRequest request, SessionContext context) {
ScriptOptionsInterface options = new ScriptOptionsInterface();
for (ScriptEngineFactory factory: new ScriptEngineManager().getEngineFactories())
options.addEngine(factory.getLanguageName());
for (Right right: Right.values()) {
if (!right.hasType() || right.type().equals(Session.class) || right.type().equals(Department.class) || right.type().equals(SubjectArea.class))
options.addPermission(right.toString());
}
options.setCanAdd(context.hasPermission(Right.ScriptEdit));
return options;
}
示例4: testIteratorOrder
import javax.script.ScriptEngineFactory; //導入依賴的package包/類
/**
* Basic test of iterator() to test iteration order. Providers deployed
* as named modules should be found before providers deployed on the class
* path.
*/
@Test
public void testIteratorOrder() {
ServiceLoader<ScriptEngineFactory> loader
= ServiceLoader.load(ScriptEngineFactory.class);
boolean foundUnnamed = false;
for (ScriptEngineFactory factory : collectAll(loader)) {
if (factory.getClass().getModule().isNamed()) {
if (foundUnnamed) {
assertTrue(false, "Named module element after unnamed");
}
} else {
foundUnnamed = true;
}
}
}
示例5: testSingleton
import javax.script.ScriptEngineFactory; //導入依賴的package包/類
/**
* Basic test of the public static provider method. BananaScriptEngine
* defines a provider method that returns the same instance.
*/
@Test
public void testSingleton() {
Optional<Provider<ScriptEngineFactory>> oprovider
= ServiceLoader.load(ScriptEngineFactory.class)
.stream()
.filter(p -> p.type().getName().equals("org.banana.BananaScriptEngineFactory"))
.findFirst();
assertTrue(oprovider.isPresent());
Provider<ScriptEngineFactory> provider = oprovider.get();
// invoke Provider::get twice
ScriptEngineFactory factory1 = provider.get();
ScriptEngineFactory factory2 = provider.get();
assertTrue(factory1 == factory2);
}
示例6: testStreamOrder
import javax.script.ScriptEngineFactory; //導入依賴的package包/類
/**
* Basic test of stream() to ensure that elements for providers in named
* modules come before elements for providers in unnamed modules.
*/
@Test
public void testStreamOrder() {
List<Class<?>> types = ServiceLoader.load(ScriptEngineFactory.class)
.stream()
.map(Provider::type)
.collect(Collectors.toList());
boolean foundUnnamed = false;
for (Class<?> factoryClass : types) {
if (factoryClass.getModule().isNamed()) {
if (foundUnnamed) {
assertTrue(false, "Named module element after unnamed");
}
} else {
foundUnnamed = true;
}
}
}
示例7: testWithPlatformClassLoader
import javax.script.ScriptEngineFactory; //導入依賴的package包/類
/**
* Basic test ServiceLoader.load specifying the platform class loader.
* The providers on the module path and class path should not be located.
*/
@Test
public void testWithPlatformClassLoader() {
ClassLoader pcl = ClassLoader.getPlatformClassLoader();
// iterator
ServiceLoader<ScriptEngineFactory> loader
= ServiceLoader.load(ScriptEngineFactory.class, pcl);
Set<String> names = collectAll(loader)
.stream()
.map(ScriptEngineFactory::getEngineName)
.collect(Collectors.toSet());
assertFalse(names.contains("BananaScriptEngine"));
assertFalse(names.contains("PearScriptEngine"));
// stream
names = ServiceLoader.load(ScriptEngineFactory.class, pcl)
.stream()
.map(Provider::get)
.map(ScriptEngineFactory::getEngineName)
.collect(Collectors.toSet());
assertFalse(names.contains("BananaScriptEngine"));
assertFalse(names.contains("PearScriptEngine"));
}
示例8: testWithCustomLayer1
import javax.script.ScriptEngineFactory; //導入依賴的package包/類
/**
* Basic test of ServiceLoader.load, using the class loader for
* a module in a custom layer as the context.
*/
@Test
public void testWithCustomLayer1() {
ModuleLayer layer = createCustomLayer("bananascript");
ClassLoader loader = layer.findLoader("bananascript");
List<ScriptEngineFactory> providers
= collectAll(ServiceLoader.load(ScriptEngineFactory.class, loader));
// should have at least 2 x bananascript + pearscript
assertTrue(providers.size() >= 3);
// first element should be the provider in the custom layer
ScriptEngineFactory factory = providers.get(0);
assertTrue(factory.getClass().getClassLoader() == loader);
assertTrue(factory.getClass().getModule().getLayer() == layer);
assertTrue(factory.getEngineName().equals("BananaScriptEngine"));
// remainder should be the boot layer
providers.remove(0);
Set<String> names = providers.stream()
.map(ScriptEngineFactory::getEngineName)
.collect(Collectors.toSet());
assertTrue(names.contains("BananaScriptEngine"));
assertTrue(names.contains("PearScriptEngine"));
}
示例9: testWithCustomLayer2
import javax.script.ScriptEngineFactory; //導入依賴的package包/類
/**
* Basic test of ServiceLoader.load using a custom Layer as the context.
*/
@Test
public void testWithCustomLayer2() {
ModuleLayer layer = createCustomLayer("bananascript");
List<ScriptEngineFactory> factories
= collectAll(ServiceLoader.load(layer, ScriptEngineFactory.class));
// should have at least 2 x bananascript
assertTrue(factories.size() >= 2);
// first element should be the provider in the custom layer
ScriptEngineFactory factory = factories.get(0);
assertTrue(factory.getClass().getModule().getLayer() == layer);
assertTrue(factory.getEngineName().equals("BananaScriptEngine"));
// remainder should be the boot layer
factories.remove(0);
Set<String> names = factories.stream()
.map(ScriptEngineFactory::getEngineName)
.collect(Collectors.toSet());
assertTrue(names.contains("BananaScriptEngine"));
assertFalse(names.contains("PearScriptEngine"));
}
示例10: factoryClassLoaderTest
import javax.script.ScriptEngineFactory; //導入依賴的package包/類
@Test
public void factoryClassLoaderTest() {
final ScriptEngineManager sm = new ScriptEngineManager();
for (final ScriptEngineFactory fac : sm.getEngineFactories()) {
if (fac instanceof NashornScriptEngineFactory) {
final NashornScriptEngineFactory nfac = (NashornScriptEngineFactory)fac;
final MyClassLoader loader = new MyClassLoader();
// set the classloader as app class loader
final ScriptEngine e = nfac.getScriptEngine(loader);
try {
e.eval("Packages.foo");
// check that the class loader was attempted
assertTrue(loader.reached(), "did not reach class loader!");
} catch (final ScriptException se) {
se.printStackTrace();
fail(se.getMessage());
}
return;
}
}
fail("Cannot find nashorn factory!");
}
示例11: factoryOptionsTest
import javax.script.ScriptEngineFactory; //導入依賴的package包/類
@Test
public void factoryOptionsTest() {
final ScriptEngineManager sm = new ScriptEngineManager();
for (final ScriptEngineFactory fac : sm.getEngineFactories()) {
if (fac instanceof NashornScriptEngineFactory) {
final NashornScriptEngineFactory nfac = (NashornScriptEngineFactory)fac;
// specify --no-syntax-extensions flag
final String[] options = new String[] { "--no-syntax-extensions" };
final ScriptEngine e = nfac.getScriptEngine(options);
try {
// try nashorn specific extension
e.eval("var f = funtion(x) 2*x;");
fail("should have thrown exception!");
} catch (final Exception ex) {
//empty
}
return;
}
}
fail("Cannot find nashorn factory!");
}
示例12: noLoaderPerCompilerTest
import javax.script.ScriptEngineFactory; //導入依賴的package包/類
@Test
/**
* Test repeated evals with --loader-per-compile=false
* We used to get "class redefinition error".
*/
public void noLoaderPerCompilerTest() {
final ScriptEngineManager sm = new ScriptEngineManager();
for (final ScriptEngineFactory fac : sm.getEngineFactories()) {
if (fac instanceof NashornScriptEngineFactory) {
final NashornScriptEngineFactory nfac = (NashornScriptEngineFactory)fac;
final String[] options = new String[] { "--loader-per-compile=false" };
final ScriptEngine e = nfac.getScriptEngine(options);
try {
e.eval("2 + 3");
e.eval("4 + 4");
} catch (final ScriptException se) {
se.printStackTrace();
fail(se.getMessage());
}
return;
}
}
fail("Cannot find nashorn factory!");
}
示例13: noLoaderPerCompilerWithSameNameTest
import javax.script.ScriptEngineFactory; //導入依賴的package包/類
@Test
/**
* Test that we can use same script name in repeated evals with --loader-per-compile=false
* We used to get "class redefinition error" as name was derived from script name.
*/
public void noLoaderPerCompilerWithSameNameTest() {
final ScriptEngineManager sm = new ScriptEngineManager();
for (final ScriptEngineFactory fac : sm.getEngineFactories()) {
if (fac instanceof NashornScriptEngineFactory) {
final NashornScriptEngineFactory nfac = (NashornScriptEngineFactory)fac;
final String[] options = new String[] { "--loader-per-compile=false" };
final ScriptEngine e = nfac.getScriptEngine(options);
e.put(ScriptEngine.FILENAME, "test.js");
try {
e.eval("2 + 3");
e.eval("4 + 4");
} catch (final ScriptException se) {
se.printStackTrace();
fail(se.getMessage());
}
return;
}
}
fail("Cannot find nashorn factory!");
}
示例14: testEvaluation
import javax.script.ScriptEngineFactory; //導入依賴的package包/類
/**
* Tests the evaluation of a simple java script expression executed on a set of objects.
*/
@Test
public void testEvaluation() {
setSystemAdministratorContext();
// as a test print scripting language names
final ScriptEngineManager manager = new ScriptEngineManager();
for (final ScriptEngineFactory sef : manager.getEngineFactories()) {
log.debug(sef.getEngineName());
}
final List<Table> tables = OBDal.getInstance().createCriteria(Table.class).list();
boolean found = false;
for (final Table t : tables) {
final String script = Table.PROPERTY_CLIENT + "." + Client.PROPERTY_ID + " == '0' && "
+ Table.PROPERTY_DBTABLENAME + "== 'AD_Client' && " + Table.PROPERTY_DATAACCESSLEVEL
+ " > 5";
final Boolean result = Evaluator.getInstance().evaluateBoolean(t, script);
log.debug(t.getName() + " : " + result);
found = found || result;
if (found) {
break;
}
}
assertTrue(found);
}
示例15: lookupScriptEngineFactory
import javax.script.ScriptEngineFactory; //導入依賴的package包/類
protected static ScriptEngineFactory lookupScriptEngineFactory(String language) {
ScriptEngineManager manager = new ScriptEngineManager();
for (ScriptEngineFactory factory : manager.getEngineFactories()) {
// some script names has alias
String[] names = getScriptNames(language);
for (String name : names) {
if (factory.getLanguageName().equals(name)) {
return factory;
}
}
}
// fallback to get engine by name
ScriptEngine engine = createScriptEngine(language, true);
if (engine != null) {
return engine.getFactory();
}
return null;
}