本文整理汇总了Java中javax.script.CompiledScript类的典型用法代码示例。如果您正苦于以下问题:Java CompiledScript类的具体用法?Java CompiledScript怎么用?Java CompiledScript使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
CompiledScript类属于javax.script包,在下文中一共展示了CompiledScript类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: invokeFunctionWithCustomScriptContextTest
import javax.script.CompiledScript; //导入依赖的package包/类
@Test
public void invokeFunctionWithCustomScriptContextTest() throws Exception {
final ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
// create an engine and a ScriptContext, but don't set it as default
final ScriptContext scriptContext = new SimpleScriptContext();
// Set some value in the context
scriptContext.setAttribute("myString", "foo", ScriptContext.ENGINE_SCOPE);
// Evaluate script with custom context and get back a function
final String script = "function (c) { return myString.indexOf(c); }";
final CompiledScript compiledScript = ((Compilable)engine).compile(script);
final Object func = compiledScript.eval(scriptContext);
// Invoked function should be able to see context it was evaluated with
final Object result = ((Invocable) engine).invokeMethod(func, "call", func, "o", null);
assertTrue(((Number)result).intValue() == 1);
}
示例2: init
import javax.script.CompiledScript; //导入依赖的package包/类
private void init(Snapshot snapshot) throws RuntimeException {
this.snapshot = snapshot;
try {
ScriptEngineManager manager = new ScriptEngineManager();
engine = manager.getEngineByName("JavaScript"); // NOI18N
InputStream strm = getInitStream();
CompiledScript cs = ((Compilable)engine).compile(new InputStreamReader(strm));
cs.eval();
Object heap = ((Invocable)engine).invokeFunction("wrapHeapSnapshot", snapshot); // NOI18N
engine.put("heap", heap); // NOI18N
engine.put("cancelled", cancelled); // NOI18N
} catch (Exception ex) {
LOGGER.log(Level.SEVERE, "Error initializing snapshot", ex); // NOI18N
throw new RuntimeException(ex);
}
}
示例3: setExternalName
import javax.script.CompiledScript; //导入依赖的package包/类
public void setExternalName ( final CompiledScript script ) throws Exception
{
final CompoundManager manager = new CompoundManager ();
for ( final ExternalValue v : SelectionHelper.iterable ( this.selection, ExternalValue.class ) )
{
final EditingDomain domain = AdapterFactoryEditingDomain.getEditingDomainFor ( v );
if ( domain == null )
{
continue;
}
final String name = evalName ( script, v );
manager.append ( domain, SetCommand.create ( domain, v, ComponentPackage.Literals.EXTERNAL_VALUE__SOURCE_NAME, name ) );
}
manager.executeAll ();
}
示例4: evalName
import javax.script.CompiledScript; //导入依赖的package包/类
public static String evalName ( final CompiledScript script, final ExternalValue v ) throws Exception
{
final SimpleScriptContext ctx = new SimpleScriptContext ();
ctx.setAttribute ( "item", v, ScriptContext.ENGINE_SCOPE ); //$NON-NLS-1$
final Object result = Scripts.executeWithClassLoader ( Activator.class.getClassLoader (), new Callable<Object> () {
@Override
public Object call () throws Exception
{
return script.eval ( ctx );
}
} );
if ( result == null )
{
return null;
}
return result.toString ();
}
示例5: main
import javax.script.CompiledScript; //导入依赖的package包/类
public static void main(String args[]) throws Exception {
ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
ScriptEngine nashorn = scriptEngineManager.getEngineByName("nashorn");
String name = "Mahesh";
System.out.println(nashorn.getClass());
Integer result = null;
try {
nashorn.eval("print('" + name + "')");
result = (Integer) nashorn.eval("10 + 2");
} catch (ScriptException e) {
System.out.println("Error executing script: " + e.getMessage());
}
jdk.nashorn.api.scripting.NashornScriptEngine x = (jdk.nashorn.api.scripting.NashornScriptEngine)
nashorn;
CompiledScript v = x.compile("1+2");
System.out.println(v.eval());
}
示例6: deserialize
import javax.script.CompiledScript; //导入依赖的package包/类
public static VirtualChestItem deserialize(VirtualChestPlugin plugin, DataView data) throws InvalidDataException
{
DataView serializedStack = data.getView(ITEM).orElseThrow(() -> new InvalidDataException("Expected Item"));
String requirementString = data.getString(REQUIREMENTS).orElse("");
Tuple<String, CompiledScript> requirements = plugin.getScriptManager().prepare(requirementString);
List<DataView> primaryList = getViewListOrSingletonList(PRIMARY_ACTION, data);
VirtualChestActionDispatcher primaryAction = new VirtualChestActionDispatcher(primaryList);
List<DataView> secondaryList = getViewListOrSingletonList(SECONDARY_ACTION, data);
VirtualChestActionDispatcher secondaryAction = new VirtualChestActionDispatcher(secondaryList);
List<DataView> primaryShiftList = getViewListOrSingletonList(PRIMARY_SHIFT_ACTION, data);
List<DataView> primaryShiftListFinal = primaryShiftList.isEmpty() ? primaryList : primaryShiftList;
VirtualChestActionDispatcher primaryShiftAction = new VirtualChestActionDispatcher(primaryShiftListFinal);
List<DataView> secondaryShiftList = getViewListOrSingletonList(SECONDARY_SHIFT_ACTION, data);
List<DataView> secondaryShiftListFinal = secondaryShiftList.isEmpty() ? secondaryList : secondaryShiftList;
VirtualChestActionDispatcher secondaryShiftAction = new VirtualChestActionDispatcher(secondaryShiftListFinal);
List<String> ignoredPermissions = data.getStringList(IGNORED_PERMISSIONS).orElse(ImmutableList.of());
return new VirtualChestItem(plugin, serializedStack, requirements,
primaryAction, secondaryAction, primaryShiftAction, secondaryShiftAction, ignoredPermissions);
}
示例7: VirtualChestItem
import javax.script.CompiledScript; //导入依赖的package包/类
private VirtualChestItem(
VirtualChestPlugin plugin,
DataView serializedStack,
Tuple<String, CompiledScript> requirements,
VirtualChestActionDispatcher primaryAction,
VirtualChestActionDispatcher secondaryAction,
VirtualChestActionDispatcher primaryShiftAction,
VirtualChestActionDispatcher secondaryShiftAction,
List<String> ignoredPermissions)
{
this.plugin = plugin;
this.serializer = new VirtualChestItemStackSerializer(plugin);
this.serializedStack = serializedStack;
this.requirements = requirements;
this.primaryAction = primaryAction;
this.secondaryAction = secondaryAction;
this.primaryShiftAction = primaryShiftAction;
this.secondaryShiftAction = secondaryShiftAction;
this.ignoredPermissions = ignoredPermissions;
}
示例8: executeScript
import javax.script.CompiledScript; //导入依赖的package包/类
/**
* Executes a compiled script and returns the result.
*
* @param script Compiled script.
* @param functionName Optional function or method to invoke.
* @param scriptContext Script execution context.
* @return The script result.
* @throws IllegalArgumentException
*/
public static Object executeScript(CompiledScript script, String functionName, ScriptContext scriptContext, Object[] args) throws ScriptException, NoSuchMethodException {
Assert.notNull("script", script, "scriptContext", scriptContext);
Object result = script.eval(scriptContext);
if (UtilValidate.isNotEmpty(functionName)) {
if (Debug.verboseOn()) {
Debug.logVerbose("Invoking function/method " + functionName, module);
}
ScriptEngine engine = script.getEngine();
try {
Invocable invocableEngine = (Invocable) engine;
result = invocableEngine.invokeFunction(functionName, args == null ? EMPTY_ARGS : args);
} catch (ClassCastException e) {
throw new ScriptException("Script engine " + engine.getClass().getName() + " does not support function/method invocations");
}
}
return result;
}
示例9: test
import javax.script.CompiledScript; //导入依赖的package包/类
@Test
public final void test() throws Exception {
String csvMapperScript = "mapFunction = function(inputHeaders, inputField, inputValue, outputField, line) return inputValue end";
String simpleScript = "return inputValue";
CompiledScript compiledScript = (CompiledScript) ((Compilable) scriptEngine).compile(simpleScript);
Bindings bindings = scriptEngine.createBindings();
// inputHeaders, inputField, inputValue, outputField, line
bindings.put("inputHeaders", "");
bindings.put("inputField", "");
bindings.put("inputValue", "testreturnvalue");
bindings.put("outputField", "");
bindings.put("line", "");
String result = (String) compiledScript.eval(bindings);
System.out.println(result);
assertEquals("testreturnvalue", result);
}
示例10: runMap
import javax.script.CompiledScript; //导入依赖的package包/类
@SuppressWarnings("unchecked")
@Override
public List<Object> runMap(CallingContext context, RaptureScript script, RaptureDataContext data, Map<String, Object> parameters) {
try {
ScriptEngine engine = engineRef.get();
CompiledScript cScript = getMapScript(engine, script);
addStandardContext(context, engine);
engine.put(PARAMS, parameters);
engine.put(DATA, JacksonUtil.getHashFromObject(data));
Kernel.getKernel().getStat().registerRunScript();
return (List<Object>) cScript.eval();
} catch (ScriptException e) {
Kernel.writeAuditEntry(EXCEPTION, 2, e.getMessage());
throw RaptureExceptionFactory.create(HttpURLConnection.HTTP_INTERNAL_ERROR, "Error running script " + script.getName(), e);
}
}
示例11: runOperation
import javax.script.CompiledScript; //导入依赖的package包/类
@Override
public String runOperation(CallingContext context, RaptureScript script, String ctx, Map<String, Object> params) {
// Get the script from the implementation bit, set up the helpers into
// the context and execute...
try {
ScriptEngine engine = engineRef.get();
CompiledScript cScript = getOperationScript(engine, script);
addStandardContext(context, engine);
engine.put(PARAMS, params);
engine.put(CTX, ctx);
Kernel.getKernel().getStat().registerRunScript();
return JacksonUtil.jsonFromObject(cScript.eval());
} catch (ScriptException e) {
Kernel.writeAuditEntry(EXCEPTION, 2, e.getMessage());
throw RaptureExceptionFactory.create(HttpURLConnection.HTTP_INTERNAL_ERROR, "Error running script " + script.getName(), e);
}
}
示例12: runProgram
import javax.script.CompiledScript; //导入依赖的package包/类
@Override
public String runProgram(CallingContext context, IActivityInfo activity, RaptureScript script, Map<String, Object> extraParams) {
try {
ScriptEngine engine = engineRef.get();
CompiledScript cScript = getProgramScript(engine, script);
addStandardContext(context, engine);
for (Map.Entry<String, ?> entry : extraParams.entrySet()) {
engine.put(entry.getKey(), entry.getValue());
}
if (Kernel.getKernel().getStat() != null) {
Kernel.getKernel().getStat().registerRunScript();
}
return JacksonUtil.jsonFromObject(cScript.eval());
} catch (ScriptException e) {
Kernel.writeAuditEntry(EXCEPTION, 2, e.getMessage());
throw RaptureExceptionFactory.create(HttpURLConnection.HTTP_INTERNAL_ERROR, "Error running script " + script.getName(), e);
}
}
示例13: invokeFunctionWithCustomScriptContextTest
import javax.script.CompiledScript; //导入依赖的package包/类
@Test
public void invokeFunctionWithCustomScriptContextTest() throws Exception {
final ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
// create an engine and a ScriptContext, but don't set it as default
ScriptContext scriptContext = new SimpleScriptContext();
// Set some value in the context
scriptContext.setAttribute("myString", "foo", ScriptContext.ENGINE_SCOPE);
// Evaluate script with custom context and get back a function
final String script = "function (c) { return myString.indexOf(c); }";
CompiledScript compiledScript = ((Compilable)engine).compile(script);
Object func = compiledScript.eval(scriptContext);
// Invoked function should be able to see context it was evaluated with
Object result = ((Invocable) engine).invokeMethod(func, "call", func, "o", null);
assertTrue(((Number)result).intValue() == 1);
}
示例14: evaluateActivationExpression
import javax.script.CompiledScript; //导入依赖的package包/类
public static Boolean evaluateActivationExpression(Bindings bindings, Expression activationExpression) {
Boolean expressionResult;
if(activationExpression!=null) {
CompiledScript script = activationExpression.compiledScript;
if(script!=null) {
try {
Object evaluationResult = script.eval(bindings);
if(evaluationResult instanceof Boolean) {
expressionResult = (Boolean) evaluationResult;
} else {
expressionResult = false;
}
} catch (ScriptException e) {
expressionResult = false;
}
} else {
expressionResult = true;
}
} else {
expressionResult = true;
}
return expressionResult;
}
示例15: evaluate
import javax.script.CompiledScript; //导入依赖的package包/类
public Object evaluate(final String expression, final Map<String, ?> values) throws ExpressionEvaluationException {
LOG.debug("Evaluating JavaScript expression: {1}", expression);
try {
final Bindings scope = engine.createBindings();
for (final Entry<String, ?> entry : values.entrySet()) {
scope.put(entry.getKey(), entry.getValue());
}
if (compilable != null) {
CompiledScript compiled = compiledCache.get(expression);
if (compiled == null) {
compiled = compilable.compile(expression);
compiledCache.put(expression, compiled);
}
return compiled.eval(scope);
}
return engine.eval(expression, scope);
} catch (final ScriptException ex) {
throw new ExpressionEvaluationException("Evaluating JavaScript expression failed: " + expression, ex);
}
}