使用 JavaScript 脚本化 Java 应用
2010-02-24 00:00:00 来源:WEB开发网插件本身比较容易实现,包含一个名为 context 的 Map,以及一些 getter/setter:
代码 2. sTodo 插件实现
public class TodoPlugin implements Plugin {
private String name;
private String desc;
private Map<String, Object> context;
private ScriptEngine sengine;
private Invocable engine;
public TodoPlugin(String file, String name, String desc) {
this.name = name;
this.desc = desc;
context = new HashMap<String, Object>();
sengine = RuntimeEnv.getScriptEngine();
engine = RuntimeEnv.getInvocableEngine();
try {
sengine.eval(new java.io.FileReader(file));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (ScriptException e) {
e.printStackTrace();
}
}
public TodoPlugin(URL url) {
}
public Object execute(String function, Object... objects) {
Object result = null;
try {
result = engine.invokeFunction(function, objects);
} catch (ScriptException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
return result;
}
public List<String> getAvailiableFunctions() {
return null;
}
public String getDescription() {
return desc;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setDescription(String desc) {
this.desc = desc;
}
/**
* put value to plug-in context, and then put it into engine context
*/
public void putValueToContext(String key, Object obj) {
context.put(key, obj);
sengine.put(key, obj);
}
}
Tags:使用 JavaScript 脚本
编辑录入:爽爽 [复制链接] [打 印]更多精彩
赞助商链接