package com.cv.file.backup.write;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
/**
* @author Chandra Vardhan
*
*/
public class FileSystemTimer {
private static final Logger LOGGER = Logger.getLogger(FileSystemTimer.class);
private Date sourceLastModifiedTime = null;
public static void main(String[] args) {
FileSystemTimer fs = new FileSystemTimer();
String timeDiff = AppInit.timeDifferenceBetweenExecutions;
long timeDifferenceForExec = 0;
if (timeDiff != null) {
String[] values = timeDiff.split("\\*");
for (String number : values) {
if (number != null) {
if (number.endsWith("L") || number.endsWith("l")) {
number = number.substring(0, number.length() - 1);
}
}
if (timeDifferenceForExec == 0) {
timeDifferenceForExec = Long.parseLong(number);
} else {
timeDifferenceForExec = timeDifferenceForExec * Long.parseLong(number);
}
}
}
String timeToSchedule = AppInit.timeToSchedule;
Calendar date = Calendar.getInstance();
if (timeToSchedule != null) {
String[] timeValues = timeToSchedule.split("\\.");
if (timeValues != null && timeValues.length > 1) {
date.set(Calendar.HOUR, Integer.parseInt(timeValues[0]));
date.set(Calendar.MINUTE, Integer.parseInt(timeValues[1]));
String meridian = timeValues[2];
if (meridian != null && meridian.equalsIgnoreCase("AM")) {
date.set(Calendar.AM_PM, 0);
} else {
date.set(Calendar.AM_PM, 1);
}
}
} else {
date.set(Calendar.HOUR, 11);
date.set(Calendar.MINUTE, 0);
date.set(Calendar.AM_PM, 1);
}
date.set(Calendar.SECOND, 0);
date.set(Calendar.MILLISECOND, 0);
Timer timer = new Timer();
TimerTask dailyTask = new TimerTask() {
@Override
public void run() {
LOGGER.debug("Timer executing at : : " + new Date());
String authMode = AppInit.authMode;
if (authMode != null && authMode.equalsIgnoreCase("key")) {
fs.fileSentBackup();
} else {
fs.fileSentBackup();
}
}
};
// schedule the task to run starting now and then every hour...
timer.schedule(dailyTask, date.getTime(), timeDifferenceForExec);
}
public void fileSentBackup() {
LOGGER.debug("In FileSystemTimer.fileSentBackup() method");
try {
File[] listOfFiles = new File(AppInit.keyAuthSourceDirectory).listFiles();
String destination = AppInit.keyAuthDestinationDirectory;
if (destination != null) {
File desFile = new File(destination);
if (!desFile.exists()) {
desFile.mkdirs();
}
}
if (listOfFiles != null && listOfFiles.length > 0) {
for (File file : listOfFiles) {
if (file != null && file.isFile()) {
String fileName = file.getName();
try {
if (fileName != null) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS");
Date nextDate = null;
if (sourceLastModifiedTime == null) {
sourceLastModifiedTime = sdf.parse(sdf.format(file.lastModified()));
} else {
nextDate = sdf.parse(sdf.format(file.lastModified()));
}
boolean flag = false;
if (nextDate != null && sourceLastModifiedTime != null) {
if (nextDate.after(sourceLastModifiedTime)) {
flag = true;
}
sourceLastModifiedTime = nextDate;
} else if (sourceLastModifiedTime != null) {
flag = true;
}
if (!flag) {
EmailUtility.sendEmail(" No file to process today !!!",
"No file to process today : : " + new Date());
break;
}
String dateStr = sdf.format(new Date());
dateStr = dateStr.replaceAll("/", "_");
dateStr = dateStr.replaceAll(" ", "_");
dateStr = dateStr.replaceAll(":", "_");
dateStr = dateStr.replaceAll("\\.", "_");
InputStream inputStream = new FileInputStream(file);
LOGGER.debug("Coping file to SFTP location ...");
IOUtils.copy(inputStream, new FileOutputStream(destination + "\\" + fileName));
LOGGER.debug(fileName + " File has been copied to SFTP location ...");
String backUpDir = AppInit.keyAuthBackupDirectory;
File backDir = new File(backUpDir);
if (backDir != null && !backDir.exists()) {
backDir.mkdirs();
}
String backupFile = dateStr + "_" + fileName;
try {
InputStream inputStream2 = new FileInputStream(file);
LOGGER.debug("Coping file to backup location : : " + backDir + "\\" + backupFile);
IOUtils.copy(inputStream2, new FileOutputStream(backDir + "\\" + backupFile));
LOGGER.debug("File has been copied to backup location : : " + backDir + "\\"
+ backupFile);
} catch (Exception e) {
EmailUtility.sendEmail("Problem while taking the file backup !!!",
"There is a problem while taking the file backup !!!");
LOGGER.error("Problem while taking the file backup !!!");
}
}
} catch (Exception e) {
EmailUtility.sendEmail("Problem while accessing source directory",
"Problem while accessing source directory");
LOGGER.error("Problem while accessing source directory" + e.getLocalizedMessage());
}
}
}
} else {
EmailUtility.sendEmail(
"There are no files available in the " + AppInit.keyAuthSourceDirectory + " directory!!!",
"There are no files available in the " + AppInit.keyAuthSourceDirectory + " directory!!!");
LOGGER.debug("There are no files available in the " + AppInit.keyAuthSourceDirectory + " directory");
}
} catch (Exception e) {
EmailUtility.sendEmail("Problem while accessing source directory of SFTP",
"There is a problem while accessing the Source directory.");
LOGGER.error("There is a problem while accessing the Source directory " + e.getMessage());
} finally {
}
LOGGER.debug("Out FileSystemTimer.fileSentBackup() method");
}
} |
No comments:
Post a Comment