Encapsulation API

    Actions can encapsulate various APIs, especially Java static methods for tool use.

1.Example

    The following example is the encapsulation of Apache's FileUtils copyURLToFile method.

1.1.Model code

<?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>

1.2.Attribute

    You can see that four attributes, source, destination, connetionTimeout and readTimeout, are defined in the model code above. These four attributes correspond to the four parameters of Apache's FileUtils copyURLToFile.

    In addition, the model also defines four methods: getSource, getDestination, getConnectionTimeout and getReadTimeout. These four methods can allow users to pass parameters flexibly, such as specifying constants, variables or ognl expressions by attributes, and users can also obtain custom parameters by rewriting the above methods. Mechanism.

1.3.Implement

    In the model example above, the run method is implemented in Java with the following code.

	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);
	}

2.Demonstration

    The following GIF video about 1 minute and 50 seconds demonstrates two usages of the above model CopyURLToFile. One is to input constants directly in the parameter attributes of the action model, the other is to transfer parameters through variables. The value of the parameter attributes of the action begins with var:

 

 

Copyright ©  2007-2019 XWorker.org  版权所有  沪ICP备08000575号