如何从Xpages中的Java代理调用Java共享脚本库? [英] How to call a java shared script library from a Java agent in Xpages?
问题描述
我有一个设置为每天8:00运行的代理
我想编写一个Java代码(在共享库中)并使用参数从代理中调用该库.
I have an agent that is set to run every day at 8:00
I want to write a java code (in a shared library) and call that library from the agent with parameters.
例如:
代理代码:
// ....
checkAndSendMail(email_1);
checkAndSendMail(email_2);
// ....
java库代码:
public class Check{
public void checkAndSendMail(String email_param){
// ...
mail.send(email_param);
// ...
}
}
- 我可以从Xpages中的Java代理调用Java共享脚本库吗?
- 如果是,那怎么打电话?
- 获取包含您的类的设计说明(例如具有特殊的设计视图或Java NAPI)
- 使用DXL导出笔记
- 提取所有"$ ClassData"字段的内容
- Base64解码内容
- 跳过前42个字节,并使用您自己的类加载器加载结果字节数组(重写执行 defineClass 调用的 findClass 方法)
- 现在您可以在代理中实例化该类并通过反射访问它
- Get the design note containing your class (f.e. with a special design view or the Java NAPI)
- Export the note with DXL
- Extract the content all "$ClassData" fields
- Base64 decode the content
- Skip the first 42 bytes , and load the resulting byte array with your own class loader (override the findClass method which does a defineClass call)
- Now you can instantiate the class in your agent and access it via reflection
推荐答案
您可以执行此操作,但这只有在有很多开销"的情况下才有可能.假设要在代理中加载Java类,可以执行以下操作:
You can do this, but this is only possible with a lot of "overhead". Assuming you want to load a Java class in an Agent you could do the following:
如您所见,这是有可能的,但是要付出更多的努力,而不只是将DDE中的库加倍".
As you can see, it is possible, but for a higher effort than just "doubling" the libraries in the DDE.
这是代理的示例类加载器.已添加Base64编码的DXL. 代理实例化类 ch.hasselba.demo.LoadedClass 并调用方法 printTime():
Here is an example class loader for an agent. The Base64 encoded DXL is already added. The agent instantiates the class ch.hasselba.demo.LoadedClass and calls the method printTime():
package ch.hasselba.demo;
public class LoadedClass {
public void printTime(){
System.out.println("Time: " + System.currentTimeMillis() );
}
}
代理的代码(使用 lwpd.commons.jar )
import lotus.domino.AgentBase;
import com.ibm.commons.util.io.base64.Base64;
import java.lang.reflect.Method;
public class JavaAgent extends AgentBase {
public void NotesMain() {
try {
// trucated DXL string
String dataDXL = "YQAYAAAAAACqAgAAAQAAAAAAAAAAAAAAYAC8AgAAqgKqAgAAAAAAAAAAyv66vgAAADEALwcAAgEAFWNoL2hhc3NlbGJhL3hwYWdlcy9aWgcA";
// base64 decode the string
String b64 = Base64.decode(dataDXL);
byte[] b64Bytes = b64.getBytes();
byte[] classBytes = new byte[b64Bytes.length - 42];
// skip the first 42 bytes
System.arraycopy( b64Bytes, 42, classBytes, 0, b64Bytes.length - 42);
try {
// load the class
ByteClassLoader obj = new ByteClassLoader();
Class theClass = obj.findClass("ch.hasselba.demo.LoadedClass", classBytes);
// instantiate it
Object theInstance = theClass.newInstance();
// get the method printTime via Reflection & call it
Method theMethod = theInstance.getClass().getMethod("printTime", null);
theMethod.invoke( theInstance, null);
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
// the class loader
public static class ByteClassLoader extends ClassLoader {
public Class findClass(String name, byte[] data) {
return defineClass(name, data, 0, data.length);
}
}
}
这篇关于如何从Xpages中的Java代理调用Java共享脚本库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!