我在检查时有这个遗留的Web应用程序,它利用Rhino编写一些数据库导入任务的脚本。脚本任务正确执行。
但是,我似乎无法在JS脚本文件中遇到断点。我甚至不确定是否可以通过这种方式调试JS脚本,所以如果有人能提供更多见解或建议......核心设置如下:
调试器(监听器):
// executes when the app is launched...
if (!ContextFactory.hasExplicitGlobal()) {
ContextFactory cxf = new ContextFactory();
ContextFactory.initGlobal(cxf);
String rhino = "transport=socket,suspend=y,address=9999";
System.out.println(">>>>>> RHINO <<<<<<");
RhinoDebugger debugger = new RhinoDebugger(rhino);
debugger.start();
cxf.addListener(debugger);
}
...
脚本编写上下文:
@Component("importDebugMockup")
public class ImportDebugMockup extends Import {
@Autowired private SomeDAO someDAO;
@Autowired private SomeOtherDAO someOtherDAO;
...
private ContextFactory cxf;
private Document doc;
public ImportDebugMockup() {
this.cxf = ContextFactory.getGlobal();
}
...
@Transactional
public Map<String, Object> doImport(final String scriptName, final String filePath)
throws ScriptException, IOException {
final Map<String, Object> results = new HashMap<>();
final String scriptSource = this.readJSFileToString(filePath, Charset.forName("UTF-8"));
Context context = this.cxf.enterContext();
try {
Scriptable scope = new ImporterTopLevel(context);
ScriptableObject.putProperty(scope, "doc", doc);
ScriptableObject.putProperty(scope, "someDAO", this.someDAO);
ScriptableObject.putProperty(scope, "someOtherDAO", this.someOtherDAO);
...
ScriptableObject.putProperty(scope, "results", results);
Object functionArgs[] = { "null" };
String scriptExecName = "script" + scriptName + ".js";
context.evaluateString(scope, scriptSource, scriptExecName, 1, null);
Function fct = (Function) scope.get("doImport", scope); // call doImport()
Object result = fct.call(context, scope, scope, functionArgs);
} finally {
Context.exit();
}
return results;
}
}
脚本:
importPackage(java.io);
importPackage(some.package);
// ...
// some utility functions here
// ...
function doImport() {
...
var i = 0; // set breakpoint here - can't hit it
someDAO.doSomething(...); // using propagated java object
...
someOtherDAO.doSomethingElse();
...
}
编辑
远程JS调试配置(使用Mozilla Rhino - 在端口9999处连接连接器)已设置,例如在this article中。 Source被配置为指向JS脚本所在的目录,但它不会挂起断点...