Custom Task Guide

Custom Task Guide

Implement the Execute Method

Implement the Execute Method

With validation complete it is time to write the core piece of code, which is the execute method to copy the file. Keep in mind that projects and tasks in
Managed File Transfer
support variables and expressions. If you decide to support variables and expressions in your task (in one or more attributes), you must write code to evaluate any expression specified for any attributes. In this example, we will support expressions for both source file and destination directory attributes. With the execute method implemented, the code looks like this:
package com.example; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.List; import com.linoma.commons.StringUtilities; import com.linoma.dpa.ExecutionException; import com.linoma.dpa.Message; import com.linoma.ga.projects.CustomTask; import com.linoma.ga.projects.TaskContainer; import com.linoma.dpa.ValidationException; public class CopyTask extends CustomTask { private static final String TAG_NAME = "example:copy"; private static final String ATTR_SOURCE_FILE = "sourceFile"; private static final String ATTR_DESTINATION_DIRECTORY = "destinationDirectory"; private String sourceFileAttribute = null; private String destinationDirectoryAttribute = null; private File sourceFile = null; private File destinationDirectory = null; public CopyTask(TaskContainer parent) { super(parent); label = TAG_NAME; } @Override public String getTagName() { return TAG_NAME; } @Override public String getDisplayString() { return label; } @Override public String getVersion() { return "1.0"; } @Override public String getVendor() { return "Example, Inc."; } public String getSourceFileAttribute() { return sourceFileAttribute; } public void setSourceFileAttribute(String sourceFileAttribute) { this.sourceFileAttribute = sourceFileAttribute; } public String getDestinationDirectoryAttribute() { return destinationDirectoryAttribute; } public void setDestinationDirectoryAttribute( String destinationDirectoryAttribute) { this.destinationDirectoryAttribute = destinationDirectoryAttribute; } @Override public void validateAttributes() throws ValidationException { // A list to hold any validation errors List<Message> errors = new ArrayList<Message>(); // Call the validateAttributes of the super class try { super.validateAttributes(); } catch (ValidationException exp) { // If the attributes on the super class fail to validate, add the // errors to the list. errors.addAll(exp.getMessages()); } // Perform our task-specific validation. if (StringUtilities.isEmpty(sourceFileAttribute)) { errors.add(new Message("Source File is required")); } if (StringUtilities.isEmpty(destinationDirectoryAttribute)) { errors.add(new Message("Destination Directory is required")); } if (!errors.isEmpty()) { throw new ValidationException(errors); } } @Override public void validate() throws ValidationException { // Simply call the validateAttributes to validate all the attributes. validateAttributes(); } public void execute() throws ExecutionException { internalValidate(); try { copy(); } catch (IOException exp) { throw new ExecutionException(exp.getMessage(), exp); } } private void internalValidate() throws ExecutionException { String temp = project.expandVariables(sourceFileAttribute); if (StringUtilities.isEmpty(temp)) { throw new ExecutionException("Source File is required"); } sourceFile = new File(temp); temp = project.expandVariables(destinationDirectoryAttribute); if (StringUtilities.isEmpty(temp)) { throw new ExecutionException("Destination Directory is required"); } destinationDirectory = new File(temp); } private long copy() throws IOException { InputStream in = null; OutputStream out = null; try { final int bufferSize = 4096; in = new BufferedInputStream(new FileInputStream(sourceFile), bufferSize); out = new BufferedOutputStream(new FileOutputStream(new File( destinationDirectory, sourceFile.getName())), bufferSize); byte[] buffer = new byte[bufferSize]; int bytesRead = 0; long bytesCopied = 0L; while ((bytesRead = in.read(buffer)) != -1) { out.write(buffer, 0, bytesRead); bytesCopied += bytesRead; } return bytesCopied; } finally { if (in != null) { try { in.close(); } catch (IOException exp) { exp.printStackTrace(); } } if (out != null) { try { out.close(); } catch (IOException exp) { exp.printStackTrace(); } } } } }
The execute method is setup to call the internallValidate() method. The purpose of this method is to evaluate any expressions specified for the source file and destination directory attributes. The method then ensures that the evaluated values are still valid for the task; i.e. the values are not null and not empty. If they are valid, the internalValidate method initializes the sourceFile and destinationDirectory instance variables. It then calls the other private method, copy() to actually copy the file.

0 COMMENTS

We’d like to hear from you!