Folder Health Monitor Java Program
Write a Java program to monitor the health of folder called ‘secured’. Monitoring includes folder size should not exceed 100 MB, archive older files to another folder called ‘archive’ if entire file size exceeds 100 MB but it should retain latest files (upto 100 MB) in ‘secured’ folder and also perform auto-delete of any executable (like .bat or .sh) which are placed in ‘secured’ folder.
Input:
- Read files from ‘temp’ folder and write into ‘secured’ folder
- Multiple files to be copied in ‘secured’ folder in window of every 2 minutes
Output:
- Monitoring Scheduler should run on 5 every minutes
- Performs above operation and shows report of current size of secured folder, any files archived, count of archived files if any and deleted file names if any.
CopyFilesScheduler.Java
package com.sneppets.fhm; import java.util.Timer; public class CopyFilesScheduler { public static void main (String[] args) throws InterruptedException { Timer time = new Timer(); CopyFilesTask scft = new CopyFilesTask(); time.schedule(scft,0,10000); //every 10 secs //for demo purpose only. /*for (int i = 0; i <= 5; i++) { System.out.println("Execution in Main Thread...." + i); Thread.sleep(2000); if (i == 5) { System.out.println("CopyFilesScheduler Terminates"); System.exit(0); } }*/ } }
CopyFilesTask.java
package com.sneppets.fhm; import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; import java.util.TimerTask; /* * To run multiple files copy task every 10 seconds */ public class CopyFilesTask extends TimerTask{ Path tempFolder = Paths.get("C:/work/temp"); Path securedFolder = Paths.get("C:/work/secured"); //multiple files copy task here @Override public void run() { System.out.println("Copying Files every 10 seconds"); File folder = new File(tempFolder.toString()); File[] listOfFiles = folder.listFiles(); try { if (listOfFiles.length > 0) { for (File file : listOfFiles){ File source = new File(tempFolder + "\\" + file.getName()); File destination = new File(securedFolder + "\\" + file.getName()); Files.copy(source.toPath(), destination.toPath(), StandardCopyOption.REPLACE_EXISTING); } } } catch (IOException e) { e.printStackTrace(); } catch (Exception ex){ ex.printStackTrace(); } } }
MonitoringScheduler.java
package com.sneppets.fhm; import java.util.Timer; public class MonitoringScheduler { public static void main (String[] args){ Timer time = new Timer(); FolderMonitoringTask fmt = new FolderMonitoringTask(); time.schedule(fmt,0,20000); //every 20 secs } }
FolderMonitoringTask.java
package com.sneppets.fhm; import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.List; import java.util.TimerTask; public class FolderMonitoringTask extends TimerTask{ Path securedFolder = Paths.get("C:/work/secured"); Path archiveFolder = Paths.get("C:/work/archive"); @Override public void run() { System.out.println("Monitoring Folder every 2 seconds...."); long currentFolderSize = 0; try{ currentFolderSize = getSecuredFolderSize(securedFolder.toString()); System.out.println("Current Size of Secured Folder is :" + currentFolderSize); if (currentFolderSize > 900000){ archiveOlderFiles(securedFolder.toString(), archiveFolder.toString()); } } catch (Exception e){ e.printStackTrace(); } } private static void archiveOlderFiles(String securedFolderPath, String archiveFolderPath) { System.out.println("Archiving Older Files Started....."); long folderSize = 0; File folder = new File(securedFolderPath); File[] listOfFiles = folder.listFiles(); List<File> listOfArchiveFiles = new ArrayList<File>(); //Sorting array of files based on last modified date Arrays.sort(listOfFiles, new Comparator<File>(){ @Override public int compare(File file1, File file2) { return Long.valueOf(file2.lastModified()).compareTo(file1.lastModified()); } }); //retain latest files up to 900000 in the secured folder and //archive the older files to archive folder if (listOfFiles.length > 0) { for (File file : listOfFiles){ folderSize += file.length(); if (folderSize > 900000){ listOfArchiveFiles.add(file); } } System.out.println("No. of files to be archived : " + listOfArchiveFiles.size()); moveFilesToArchiveFolder(listOfArchiveFiles.toArray(), securedFolderPath, archiveFolderPath); } } private static void moveFilesToArchiveFolder( Object[] listOfArchiveFiles, String securedFolderPath, String archiveFolderPath) { System.out.println("Moving older files to archive folder..."); int archivedFilesCount = 0; try{ for(int i=0; i<listOfArchiveFiles.length; i++){ File sharedFolderFile = new File(listOfArchiveFiles[i].toString()); File source = new File(securedFolderPath + "\\" + sharedFolderFile.getName()); File destination = new File(archiveFolderPath + "\\" + sharedFolderFile.getName()); //Move the file as an atomic file system operation. //atomic means that an operation either succeeds and has an effect or has fails and has no effect; //an operation must not fail and have an effect. Files.move(source.toPath(), destination.toPath(), StandardCopyOption.ATOMIC_MOVE); archivedFilesCount += 1; System.out.println("Number of files archived: " + archivedFilesCount); } System.out.println("Total count of archived files : " + archivedFilesCount); } catch (IOException e) { e.printStackTrace(); } catch(Exception e){ e.printStackTrace(); } } private static long getSecuredFolderSize(String securedFolderPath) { System.out.println("Fetching current secured folder size..."); long size = 0; File folder = new File(securedFolderPath); File[] listOfFiles = folder.listFiles(); if (listOfFiles.length > 0) { for (File file : listOfFiles){ if (file.toString().contains(".bat") || file.toString().contains(".sh")){ file.delete(); System.out.println("File deleted :" + file.getName()); continue; } if(file.isFile()){ size += file.length(); } } } return size; } }
Output:
Monitoring Folder every 2 seconds.... Fetching current secured folder size... File deleted :mvn.bat File deleted :mvnDebug.bat Current Size of Secured Folder is :3008301 Archiving Older Files Started..... No. of files to be archived : 16 Moving older files to archive folder... Number of files archived: 1 Number of files archived: 2 Number of files archived: 3 Number of files archived: 4 Number of files archived: 5 Number of files archived: 6 Number of files archived: 7 Number of files archived: 8 Number of files archived: 9 Number of files archived: 10 Number of files archived: 11 Number of files archived: 12 Number of files archived: 13 Number of files archived: 14 Number of files archived: 15 Number of files archived: 16 Total count of archived files : 16 Monitoring Folder every 2 seconds.... Fetching current secured folder size... Current Size of Secured Folder is :432246 Monitoring Folder every 2 seconds.... Fetching current secured folder size... Current Size of Secured Folder is :432246 Monitoring Folder every 2 seconds.... Fetching current secured folder size... Current Size of Secured Folder is :432246 Monitoring Folder every 2 seconds.... Fetching current secured folder size... Current Size of Secured Folder is :432246
Thank you so much and it is very helpful.
Nice one. Thank you.