使用动作可以封装各种API,尤其是工具用途的Java静态方法。
下面的示例是Apache的FileUtils的copyURLToFile方法的封装。
<?xml version="1.0" encoding="utf-8"?>
<thing name="CopyURLToFile" descriptors="xworker.lang.MetaDescriptor3/@thing" extends="xworker.lang.actions.SelfAction"
group="io.file,_actions.utils.file">
<description><![CDATA[文档放这里]]></description>
<actions>
<JavaAction name="run" outerClassName="xworker.io.file.FileActions" methodName="copyURLToFile"></JavaAction>
<GetObject name="getSource" _xmeta_id_="getSize" descriptors="xworker.lang.actions.ActionUtil/@GetData"
attributeName="source"></GetObject>
<GetObject name="getDestination" _xmeta_id_="getDestDir" descriptors="xworker.lang.actions.ActionUtil/@GetData"
attributeName="destination"></GetObject>
<GetInt name="getConnectionTimeout" descriptors="xworker.lang.actions.ActionUtil/@GetInt"
attributeName="connectionTimeout" defaultValue="0"></GetInt>
<GetInt name="getReadTimeout" descriptors="xworker.lang.actions.ActionUtil/@GetInt"
attributeName="readTimeout" defaultValue="0"></GetInt>
</actions>
<attribute name="name"></attribute>
<attribute name="label"></attribute>
<attribute name="source" _xmeta_id_="file" size="50"></attribute>
<attribute name="destination" _xmeta_id_="output" inputtype="file" size="50"></attribute>
<attribute name="connectionTimeout"></attribute>
<attribute name="readTimeout"></attribute>
<attribute name="description" inputtype="html"></attribute>
</thing>
可以看到在上面的模型代码中定义了source、destination、connetionTimeout和readTimeout四个属性,这四个属性分别对应于Apache的FileUtils的copyURLToFile的四个参数。
另外模型也定义了getSource、getDestination、getConnectionTimeout和getReadTimeout四个方法,这四个方法可以让使用者灵活的传参数,如可以通过属性指定常量、变量或ognl表达式等等,使用者也可以通过重写以上方法实现自定义的参数获取机制。
在上面的模型示例中run方法是用Java实现的,代码如下。
public static void copyURLToFile(ActionContext actionContext) throws IOException{
Thing self = actionContext.getObject("self");
File destination = getFile(self, "getDestination", actionContext);
URL source = (URL) self.doAction("getSource", actionContext);
int connectionTimeout = (Integer) self.doAction("getConnectionTimeout", actionContext);
int readTimeout = (Integer) self.doAction("getReadTimeout", actionContext);
FileUtils.copyURLToFile(source, destination, connectionTimeout, readTimeout);
}
下面1分50秒左右的GIF视频演示了上面的模型CopyURLToFile的两种用法,一是直接在动作模型的参数属性里输入常量,另一种是通过变量传递参数,通过变量传递参数时动作的参数属性的值是以var:开头的。
