DirCollThread.java 6.21 KB
package com.sitech.ismp.coll.busi;

import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.sitech.ismp.coll.busi.db.dao.TbTmpFileDao;
import com.sitech.ismp.coll.busi.db.domain.TbTmpFile;
import com.sitech.ismp.coll.busi.util.FilesOperation;
import com.sitech.util.DES3;
import com.sitech.util.RandomGUID;
import com.sitech.util.upload.FTPIF;
import com.sitech.util.upload.FTPSrv;
import com.sitech.util.upload.RemoteFile;
import com.sitech.util.upload.SFTPClient;

/**
 * 对目录进行文件数变化管理,核查增加和减少,并返回变化的文件名 结果以文件方式返回 文件命名格式:DirColl_GUID.txt 文件格式:
 * ---------------------------------- 172.21.1.100
 * /bnmsapp2/linxc/masteragent/logs NEW_FILES=null DELETE_FILES=null
 * UPDATE_FILES=文件名##上个周期大小##当前大小##上个周期权限##当前权限
 * ----------------------------------
 * 
 * @author linxc
 * 
 */
public class DirCollThread implements Runnable {
	private Logger logger = Logger.getLogger(FileFtpCollThread.class);

	private String ip;
	private String username;
	private String password;
	private String remoteDir;
	private String localDir;
	DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	public DirCollThread(String ip, String username, String password,
			String remoteDir, String localDir) {
		this.ip = ip;
		this.username = username;
		this.password = password = DES3.decrypt(password);
		this.remoteDir = remoteDir;
		this.localDir = localDir;
	}

	public void run() {
		logger.info("Start DirCollThread,IP=" + ip + ",USERNAME=" + username
				+ ", DIR=" + remoteDir);
		localDir += "/" + ip.replace(".", "_");
		File file = new File(localDir);
		if (!file.exists()) {
			file.mkdir();
		}

		FTPIF ftpClient = getFtpClient();

		if (ftpClient == null) {
			logger.warn("ftpClient == null, return!");
			return;
		}

		try {
			RemoteFile[] list = listRemoteFile(ftpClient);
			if (list == null) {
				return;
			}

			String result = compareWithLastCycle(list);

			String guid = RandomGUID.getRandomGUID();
			String fileName = localDir + "/DIR_" + guid + ".info";

			FilesOperation.createFiles(fileName, result);

		} catch (Exception e) {
			logger.error("Exception while DirCollThread.run().", e);
		} finally {
			if (ftpClient != null) {
				try {
					ftpClient.logout();
				} catch (Exception e) {
					logger.error("Exception while ftpClient.logout()", e);
				}
			}
		}
	}

	private String compareWithLastCycle(RemoteFile[] list) {
		String result = "";
		result += "TYPE=DIR\n";
		result += "IP_ADDR=" + ip + "\n";
		result += "DIRECTORY=" + remoteDir + "\n";

		int currFileNum = 0;
		Map<String, TbTmpFile> currFileMap = new HashMap<String, TbTmpFile>();
		for (RemoteFile currFile : list) {
			if (currFile.isDirectory()) {
				continue;
			}
			currFileNum++;
			TbTmpFile tbTmpFile = new TbTmpFile(currFile, ip, remoteDir);
			currFileMap.put(tbTmpFile.getID(), tbTmpFile);
		}

		TbTmpFileDao dao = new TbTmpFileDao();
		List<TbTmpFile> lastFileMap = dao.getTbTmpFile(ip, remoteDir);
		int lastFileNum = lastFileMap.size();
		
		result += "CURRENT_NUM=" + currFileNum + "\n";
		result += "LAST_NUM=" + lastFileNum + "\n";
		
		dao.deleteTbTmpFile(ip, remoteDir);
		for (TbTmpFile file : currFileMap.values()) {
			dao.insertTbTmpFile(file);
		}

		String addFileStr = "NEW_FILES=";
		String deleteFileStr = "DELETE_FILES=";
		String updateFileStr = "UPDATE_FILES=";

		String add = "";
		String delete = "";
		String update = "";

		for (TbTmpFile lastFile : lastFileMap) {
			String id = lastFile.getID();
			TbTmpFile currFile = currFileMap.get(id);

			if (currFile == null) {
				delete += lastFile.getFILE_ANME() + ",";
			} else if (!currFile.equals(lastFile)) {
				// 文件名##上个周期大小##当前大小##上个周期权限##当前权限
				update += currFile.getFILE_ANME() + "##" + lastFile.getSIZE()
						+ "##" + currFile.getSIZE() + "##"
						+ lastFile.getPERMISSIONS() + "##"
						+ currFile.getPERMISSIONS() + "##"
						+ dateFormat.format(lastFile.getLAST_MODIFY()) + "##"
						+ dateFormat.format(currFile.getLAST_MODIFY()) + ",";
				currFileMap.remove(id);
			} else {
				currFileMap.remove(id);
			}
		}

		if (currFileMap.size() > 0) {
			for (TbTmpFile file : currFileMap.values()) {
				add += file.getFILE_ANME() + ",";
			}
		}

		if (add.equals("")) {
			add = "null";
		} else {
			add = add.substring(0, add.length() - 1);
		}

		if (delete.equals("")) {
			delete = "null";
		} else {
			delete = delete.substring(0, delete.length() - 1);
		}

		if (update.equals("")) {
			update = "null";
		} else {
			update = update.substring(0, update.length() - 1);
		}

		addFileStr += add;
		deleteFileStr += delete;
		updateFileStr += update;

		result += addFileStr + "\n";
		result += deleteFileStr + "\n";
		result += updateFileStr + "\n";
		result += "COLL_TIME=" + dateFormat.format(new Date()) + "\n";

		logger.info("Result:\n" + result);
		return result;
	}

	private RemoteFile[] listRemoteFile(FTPIF ftpClient) {
		try {
			ftpClient.chdir(remoteDir);

			RemoteFile[] list = ftpClient.listRemoteFile(remoteDir);
			return list;
		} catch (Exception e) {
			logger.error("Exception while listRemoteFile()", e);
		}
		return null;
	}

	/**
	 * @return FTP连接
	 */
	private FTPIF getFtpClient() {
		FTPIF ftpClient = new FTPSrv();
		try {
			logger.info("ftp " + ip + ", LOGIN:" + username + "...");

			ftpClient.login(ip, username, password);

		} catch (Exception e) {
			logger.warn("ftp " + ip + ", LOGIN:" + username + " FAILED!");
			try {
				ftpClient = new SFTPClient();
				logger.info("sftp " + username + "@" + ip);
				ftpClient.login(ip, username, password);
			} catch (Exception e1) {
				logger.warn("sftp " + username + "@" + ip + " FAILED!");
				ftpClient = null;
			}
		}
		return ftpClient;
	}

	public static void main(String[] args) {
		String ip = "10.182.15.77";
		String username = "ibnms";
		String password = DES3.encrypt("?ismp#*2");
		String remoteDir = "/iBNMSConsole/topo/data_save";
		String localDir = "F:\\";

		DirCollThread thread = new DirCollThread(ip, username, password,
				remoteDir, localDir);

		thread.run();
	}
}