本文整理汇总了Java中javax.script.Invocable.invokeFunction方法的典型用法代码示例。如果您正苦于以下问题:Java Invocable.invokeFunction方法的具体用法?Java Invocable.invokeFunction怎么用?Java Invocable.invokeFunction使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.script.Invocable
的用法示例。
在下文中一共展示了Invocable.invokeFunction方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: executeJs
import javax.script.Invocable; //导入方法依赖的package包/类
public Object executeJs(String js,@Nullable String funcName,Object... args){
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("javascript");
try {
Object res=engine.eval(js);
if(StringUtils.isNotBlank(funcName)){
if (engine instanceof Invocable) {
Invocable invoke = (Invocable) engine;
res = invoke.invokeFunction(funcName, args);
}
}
return res;
} catch (Exception e) {
log.error("",e);
}
return null;
}
示例2: main
import javax.script.Invocable; //导入方法依赖的package包/类
public static void main(final String[] args) throws Exception {
final ScriptEngineManager manager = new ScriptEngineManager();
final ScriptEngine engine = manager.getEngineByName("nashorn");
// JavaScript code in a String
final String script = "function hello(name) { print('Hello, ' + name); }";
// evaluate script
engine.eval(script);
// javax.script.Invocable is an optional interface.
// Check whether your script engine implements or not!
// Note that the JavaScript engine implements Invocable interface.
final Invocable inv = (Invocable) engine;
// invoke the global function named "hello"
inv.invokeFunction("hello", "Scripting!!" );
}
示例3: getProxyInfo
import javax.script.Invocable; //导入方法依赖的package包/类
public ProxyInfo[] getProxyInfo(URL paramURL) throws Exception {
ProxyInfo[] arrayOfProxyInfo = null;
if (this.engine != null) {
try {
this.engine.put("obj", this);
//System.out.println(this.autoProxyScript.toString());
this.engine.eval(this.autoProxyScript.toString());
if ((this.engine instanceof Invocable)) {
Invocable localInvocable = (Invocable) this.engine;
Object localObject = localInvocable.invokeFunction(
"FindProxyForURL", new Object[] {
paramURL.toString(), paramURL.getHost() });
arrayOfProxyInfo = extractAutoProxySetting((String) localObject);
}
} catch (Exception localException) {
localException.printStackTrace();
}
}
// if (arrayOfProxyInfo == null) {
// arrayOfProxyInfo = fallbackGetProxyInfo(paramURL);
// }
return arrayOfProxyInfo;
}
示例4: getScriptedAttributesFromFile
import javax.script.Invocable; //导入方法依赖的package包/类
private Map<String, Object> getScriptedAttributesFromFile(final Map<String, Object> attributes) throws Exception {
final String engineName = getScriptEngineName();
final ScriptEngine engine = new ScriptEngineManager().getEngineByName(engineName);
if (engine == null || StringUtils.isBlank(engineName)) {
LOGGER.warn("Script engine is not available for [{}]", engineName);
return new HashMap<>();
}
final File theScriptFile = ResourceUtils.getResourceFrom(this.scriptFile).getFile();
if (theScriptFile.exists()) {
LOGGER.debug("Created object instance from class [{}]", theScriptFile.getCanonicalPath());
final Object[] args = {attributes, LOGGER};
LOGGER.debug("Executing script's run method, with parameters [{}]", args);
engine.eval(new FileReader(theScriptFile));
final Invocable invocable = (Invocable) engine;
final Map<String, Object> personAttributesMap = (Map<String, Object>) invocable.invokeFunction("run", args);
LOGGER.debug("Final set of attributes determined by the script are [{}]", personAttributesMap);
return personAttributesMap;
}
LOGGER.warn("[{}] script [{}] does not exist, or cannot be loaded", StringUtils.capitalize(engineName), scriptFile);
return new HashMap<>();
}
示例5: act
import javax.script.Invocable; //导入方法依赖的package包/类
public final void act(final MapleClient c, final MapleReactor reactor) {
try {
final Invocable iv = getInvocable("reactor/" + reactor.getReactorId() + ".js", c);
if (iv == null) {
return;
}
final ScriptEngine scriptengine = (ScriptEngine) iv;
ReactorActionManager rm = new ReactorActionManager(c, reactor);
scriptengine.put("rm", rm);
iv.invokeFunction("act");
} catch (ScriptException | NoSuchMethodException e) {
System.err.println("Error executing reactor script. ReactorID: " + reactor.getReactorId() + ", ReactorName: " + reactor.getName() + ":" + e);
}
}
示例6: main
import javax.script.Invocable; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
System.out.println("\nTest8\n");
ScriptEngineManager m = new ScriptEngineManager();
ScriptEngine e = Helper.getJsEngine(m);
if (e == null) {
System.out.println("Warning: No js engine found; test vacuously passes.");
return;
}
e.eval(new FileReader(
new File(System.getProperty("test.src", "."), "Test8.js")));
Invocable inv = (Invocable)e;
inv.invokeFunction("main", "Mustang");
// use method of a specific script object
Object scriptObj = e.get("scriptObj");
inv.invokeMethod(scriptObj, "main", "Mustang");
}
示例7: startItemScript
import javax.script.Invocable; //导入方法依赖的package包/类
public final void startItemScript(final MapleClient c, final int npc, final String script) {
final Lock lock = c.getNPCLock();
lock.lock();
try {
if (!cms.containsKey(c) && c.canClickNPC()) {
final Invocable iv = getInvocable("item/" + script + ".js", c, true);
if (iv == null) {
System.out.println("New scripted item : " + script);
dispose(c);
return;
}
final ScriptEngine scriptengine = (ScriptEngine) iv;
final NPCConversationManager cm = new NPCConversationManager(c, npc, -1, script, (byte) -1, iv);
cms.put(c, cm);
scriptengine.put("im", cm);
c.getPlayer().setConversation(1);
c.setClickedNPC();
iv.invokeFunction("use");
}
} catch (final ScriptException | NoSuchMethodException e) {
System.err.println("Error executing Item script, SCRIPT : " + script + ". " + e);
e.printStackTrace();
dispose(c);
} finally {
lock.unlock();
}
}
示例8: parse
import javax.script.Invocable; //导入方法依赖的package包/类
@Override
public void parse(SqlEntity entity) {
String entityHash = hashEntity(entity);
SqlEntity se = sqlValues.get(entityHash);
if (se != null){
return ;
}
try {
ScriptEngine engine = jsFunctions.get(entity.getPrefix());
jsFunctions.put(entityHash,engine);
String namespace = null;
if (engine == null){
engine = engineManager.getEngineByName(JAVASCRIPT);
engine.put(JSContext.CONTEXT,ctx.getCtx());
engine.eval(reader);
jsFunctions.put(entity.getPrefix(),engine);
}
namespace = String.valueOf(((Map<String,Object>)engine.get(CONTEXT)).get(NAMESPACE));
if (StringUtils.isEmpty(namespace)){
return;
}
if (!entity.getPrefix().equals(namespace)){
return ;
}
Invocable func = (Invocable) engine;
Map<String,Object> params = entity.getParams();
String sql = (String) func.invokeFunction(entity.getSuffix(),params);
sqlBuilder.buildSql(sql);
sqlBuilder.buildParams(params);
SqlEntity sqlEntity = sqlBuilder.buildEntity(entity.getBinder());
cache.addSqlEntity(sqlEntity);
sqlValues.put(entityHash,sqlEntity);
} catch (Exception e) {
e.printStackTrace();
}
}
示例9: endQuest
import javax.script.Invocable; //导入方法依赖的package包/类
public final void endQuest(final MapleClient c, final int npcid, final int questid, final boolean customEnd) {
if (!customEnd && !MapleQuest.getInstance(questid).canComplete(c.getPlayer(), npcid)) {
return;
}
final Lock lock = c.getNPCLock();
lock.lock();
try {
if (!cms.containsKey(c) && c.canClickNPC()) {
final Invocable iv = getInvocable("quest/" + questid + ".js", c, true);
if (iv == null) {
dispose(c);
return;
}
final ScriptEngine scriptengine = (ScriptEngine) iv;
final NPCConversationManager cm = new NPCConversationManager(c, npcid, questid, null, (byte) 1, iv);
cms.put(c, cm);
scriptengine.put("qm", cm);
c.getPlayer().setConversation(1);
c.setClickedNPC();
iv.invokeFunction("end", (byte) 1, (byte) 0, 0); // start it off as something
}
} catch (ScriptException | NoSuchMethodException e) {
System.err.println("Error executing Quest script. (" + questid + ")..NPCID: " + npcid + ":" + e);
e.printStackTrace();
dispose(c);
} finally {
lock.unlock();
}
}
示例10: evalScript
import javax.script.Invocable; //导入方法依赖的package包/类
private Object evalScript(String script, Order order, Reader scriptReader)
throws ScriptException, NoSuchMethodException {
final Object result;
final ScriptEngine engine = manager.getEngineByName("JavaScript");
try {
engine.eval(scriptReader);
Invocable inv = (Invocable) engine;
result = inv.invokeFunction("isInconsistent", order);
} catch (ScriptException | NoSuchMethodException se) {
log.error("The script {} thruw up", script);
log.error("Script executing exception", se);
throw se;
}
return result;
}
示例11: invokeJS
import javax.script.Invocable; //导入方法依赖的package包/类
public static List<String> invokeJS(String js, int y, int ch) {
ArrayList<String> pagsList = new ArrayList<String>();
System.out.println(StringUtility.substring(js, ";for(var i=0;i<", ";i++"));
String str = js.substring(0, js.indexOf("var pt="));
str = str.replace("ge('TheImg').src", "var src");
String unuseScript = StringUtility.substring(str, "\'.jpg\';", "break;");
System.out.println(str);
System.out.println("unuseScript==" + unuseScript);
str = str.replace(unuseScript, "");
System.out.println("str-->" + str);
String varSrc = StringUtility.substring(str, "ci = i; ", "break;");
System.out.println("varSrc-->" + varSrc);
String getPageJS = String.format(buildGetPagesJS(), varSrc);
str = str.replace(varSrc, "");
str = str.replace("break;", getPageJS);
String script = "function sp2(ch, y){" + str + "} " + buildNviewJS();
System.out.println("script-->" + script);
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");
try {
Bindings bind = engine.createBindings();
bind.put("pagsList", pagsList);
engine.setBindings(bind, ScriptContext.ENGINE_SCOPE);
engine.eval(script);
Invocable inv = (Invocable) engine;
inv.invokeFunction("sp2", ch, y);
} catch (Exception e) {
e.printStackTrace();
}
return pagsList;
}
示例12: call
import javax.script.Invocable; //导入方法依赖的package包/类
/**
* Call the script function of given name passing the
* given arguments.
*/
public Object call(String name, Object[] args) {
Invocable invocable = (Invocable)engine;
try {
return invocable.invokeFunction(name, args);
} catch (RuntimeException re) {
throw re;
} catch (Exception exp) {
throw new RuntimeException(exp);
}
}
示例13: start
import javax.script.Invocable; //导入方法依赖的package包/类
public void start(MapleClient c, int npc, String filename, MapleCharacter chr) {
try {
NPCConversationManager cm = new NPCConversationManager(c, npc);
if (cms.containsKey(c)) {
dispose(c);
return;
}
cms.put(c, cm);
Invocable iv = null;
if (filename != null) {
iv = getInvocable("npc/world" + c.getWorld() + "/" + filename + ".js", c);
}
if (iv == null) {
iv = getInvocable("npc/world" + c.getWorld() + "/" + npc + ".js", c);
}
if (iv == null || NPCScriptManager.getInstance() == null) {
dispose(c);
return;
}
engine.put("cm", cm);
scripts.put(c, iv);
try {
iv.invokeFunction("start");
} catch (final NoSuchMethodException nsme) {
try {
iv.invokeFunction("start", chr);
} catch (final NoSuchMethodException nsma) {
iv.invokeFunction("action", (byte) 1, (byte) 0, 0);
}
}
} catch (final UndeclaredThrowableException | ScriptException ute) {
FilePrinter.printError(FilePrinter.NPC + npc + ".txt", ute);
notice(c, npc);
dispose(c);
} catch (final Exception e) {
FilePrinter.printError(FilePrinter.NPC + npc + ".txt", e);
notice(c, npc);
dispose(c);
}
}
示例14: act
import javax.script.Invocable; //导入方法依赖的package包/类
public void act(MapleClient c, MapleReactor reactor) {
try {
ReactorActionManager rm = new ReactorActionManager(c, reactor);
Invocable iv = getInvocable("reactor/" + reactor.getId() + ".js", c);
// System.out.println(reactor.getId());
if (iv == null) {
return;
}
engine.put("rm", rm);
iv.invokeFunction("act");
} catch (final ScriptException | NoSuchMethodException | NullPointerException e) {
FilePrinter.printError(FilePrinter.REACTOR + reactor.getId() + ".txt", e);
}
}
示例15: start
import javax.script.Invocable; //导入方法依赖的package包/类
public void start(MapleClient c, short questid, int npc) {
MapleQuest quest = MapleQuest.getInstance(questid);
if (!c.getPlayer().getQuest(quest).getStatus().equals(MapleQuestStatus.Status.NOT_STARTED) || !c.getPlayer().getMap().containsNPC(npc)) {
dispose(c);
return;
}
try {
QuestActionManager qm = new QuestActionManager(c, questid, npc, true);
if (qms.containsKey(c)) {
return;
}
qms.put(c, qm);
Invocable iv = getInvocable("quest/" + questid + ".js", c);
if (iv == null) {
qm.dispose();
return;
}
engine.put("qm", qm);
scripts.put(c, iv);
iv.invokeFunction("start", (byte) 1, (byte) 0, 0);
} catch (final UndeclaredThrowableException ute) {
FilePrinter.printError(FilePrinter.QUEST + questid + ".txt", ute);
dispose(c);
} catch (final Throwable t) {
FilePrinter.printError(FilePrinter.QUEST + getQM(c).getQuest() + ".txt", t);
dispose(c);
}
}