org.openxma.xmadsl.FileCopyComponent Maven / Gradle / Ivy
package org.openxma.xmadsl;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.emf.mwe.core.WorkflowContext;
import org.eclipse.emf.mwe.core.issues.Issues;
import org.eclipse.emf.mwe.core.monitor.ProgressMonitor;
public class FileCopyComponent extends org.eclipse.emf.mwe.core.lib.AbstractWorkflowComponent2 {
private static final String COMPONENT_NAME = "File Copy";
private Log log = LogFactory.getLog(getClass());
private String sourceFile = null;
private String targetDir = null;
public void setSourceFile(String sourceFile) {
this.sourceFile = sourceFile;
}
public void setTargetDir(String targetDir) {
this.targetDir = targetDir;
}
@Override
protected void checkConfigurationInternal(Issues issues) {
if (targetDir == null || !new File(targetDir).getParentFile().exists()) {
issues.addError("Property targetDir not configured properly");
}
if (sourceFile == null) {
issues.addError("Property sourceFile not configured properly");
}
}
@Override
protected void invokeInternal(WorkflowContext ctx, ProgressMonitor monitor,
Issues issues) {
File target = new File(targetDir);
if (!target.exists()) {
target.mkdir();
}
File resource = new File(sourceFile);
File copy = new File(target.getAbsolutePath() + File.separatorChar
+ resource.getName());
try {
InputStream is = new FileInputStream(resource);
copy.createNewFile();
FileOutputStream fwr = new FileOutputStream(copy);
byte[] buff = new byte[1024];
int read;
while ((read = is.read(buff)) != -1) {
fwr.write(buff, 0, read);
}
if (log.isDebugEnabled()) {
log.debug("Copied " + copy);
}
} catch (IOException e) {
log.error(e);
}
}
public String getComponentName() {
return COMPONENT_NAME;
}
}