root/Weektaken2/Weektaak3/ESB/src/hanze/ga/wt3/ftp/FTPApp2QChannel.java

User picture

Author: klystr

Revision: 369 («Previous)


File Size: 2.16 KB

(December 07, 2009 10:44 UTC) Over 2 years ago

Final

 
Show/hide line numbers
package hanze.ga.wt3.ftp;

import hanze.ga.wt3.channels.App2Q;
import hanze.ga.wt3.filters.FromIBGFilter;

import java.io.StringReader;
import java.io.StringWriter;

public class FTPApp2QChannel extends App2Q {

	private String ftpAddress;
	private String ftpInitDir;
	private String ftpUserName;
	private String ftpPassword;
	private ESBFTP ftpClient;

	public String getFtpAddress() {
		return ftpAddress;
	}

	public String getFtpInitDir() {
		return ftpInitDir;
	}

	public String getFtpPassword() {
		return ftpPassword;
	}

	public String getFtpUserName() {
		return ftpUserName;
	}

	private void readAndProcessFTP() {
		// Maak FTP-verbinding klaar
		this.ftpClient = new ESBFTP(this.getFtpAddress(), this.getFtpUserName(), this
				.getFtpPassword(), this.getChannelPort());

		FTPFile[] files = this.ftpClient.getFilesFromDir(this.getFtpInitDir());
		for (FTPFile ftpFile : files) {
			if (!ftpFile.isDirectory()) { // Alleen gewone bestanden
				String directory = this.getFtpInitDir() + "/" + ftpFile.getName();
				String unFiltered = this.undoFilter(this.ftpClient.pollFile(directory));
				this.getDestinationQueue().addToQueue(unFiltered);
				System.out.println("[" + this.getChannelName() + "] Got " + ftpFile.getName()
						+ " from FTP and put in queue " + this.getDestinationQueue().getqName());
			}
		}
	}

	@Override
	public void run() {
		try {
			while (true) {
				this.readAndProcessFTP();
				Thread.sleep(10000);
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	public void setFtpAddress(String ftpAddress) {
		this.ftpAddress = ftpAddress;
	}

	public void setFtpInitDir(String ftpInitDir) {
		this.ftpInitDir = ftpInitDir;
	}

	public void setFtpPassword(String ftpPassword) {
		this.ftpPassword = ftpPassword;
	}

	public void setFtpUserName(String ftpUserName) {
		this.ftpUserName = ftpUserName;
	}

	private String undoFilter(String fileContents) {
		StringWriter stringWriter = new StringWriter();
		FromIBGFilter fromFilter = new FromIBGFilter();
		fromFilter.doFilter(new StringReader(fileContents), stringWriter);

		return stringWriter.toString();
	}

}