feat(file-cleaner): refactored class names and dir structure

This commit is contained in:
Begerad, Stefan 2021-08-08 01:27:49 -04:00
parent df1e03d3dd
commit 97c6d13971
11 changed files with 0 additions and 2581 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,59 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.begerad.file-age-rm</groupId>
<artifactId>file-age-rm</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.9.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
<transformers>
<transformer implementation=
"org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>de.begerad.fileagerm.Main</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -1,38 +0,0 @@
package de.begerad.fileagerm;
import java.io.File;
import static de.begerad.fileagerm.Main.LOG;
public class FileCleaner {
/**
* Clean-up files older than a certain time
*
* @param age age of files to be cleaned up
* @param dirPath directory path of files to be cleaned up
*/
public static void deleteFilesByAge(int age, String dirPath) {
File directory = new File(dirPath);
if (directory.exists()) {
File[] listFiles = FileExplorer.listFiles(dirPath);
LOG.debug("listFiles.length: {}", listFiles.length);
long purgeTime = System.currentTimeMillis() - (age * 24L * 60L * 60L * 1000L);
for (File listFile : listFiles) {
if (listFile.lastModified() < purgeTime) {
if (!listFile.delete()) {
LOG.warn("file could NOT be deleted: {}", listFile.getName());
} else {
LOG.debug("file deleted: {}", listFile.getName());
}
} else {
LOG.debug("file is too young to be deleted: {}", listFile.getName());
}
}
} else {
LOG.warn("directory does not exist: {}", directory);
}
}
}

View File

@ -1,57 +0,0 @@
package de.begerad.fileagerm;
import java.io.File;
import java.io.FileFilter;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class FileExplorer {
private final String directory;
public FileExplorer(String directory) {
this.directory = directory;
}
/**
* @return set containing list of file paths
*/
public Set<String> listFilesUsingJavaIO() {
return Stream.of(new File(directory).listFiles())
.filter(file -> !file.isDirectory())
.map(File::getName)
.collect(Collectors.toSet());
}
/**
* @return array containing list of File objects
*/
public File[] listFilesUsingFileFilter() {
//Creating a File object for directory
File dir = new File(directory);
//Creating filter for directory files
FileFilter fileFilter = new FileFilter() {
public boolean accept(File dir) {
return dir.isFile();
}
};
return dir.listFiles(fileFilter);
}
/**
* @return array containing list of File objects
*/
public static File[] listFiles(String directory) {
//Creating a File object for directory
File dir = new File(directory);
//Creating filter for directory files
FileFilter fileFilter = new FileFilter() {
public boolean accept(File dir) {
return dir.isFile();
}
};
return dir.listFiles(fileFilter);
}
}

View File

@ -1,80 +0,0 @@
package de.begerad.fileagerm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.Set;
import static de.begerad.fileagerm.FileCleaner.deleteFilesByAge;
public class Main {
public static String dirPath = "";
public final static Logger LOG = LoggerFactory.getLogger(Main.class);
public static void main(String[] args) {
LOG.debug("main() started...");
//check user input for directory
if (args.length < 1) {
System.out.println("Please enter directory as first parameter.");
return;
}
dirPath = args[0];
if (!(new File(dirPath).isDirectory())) {
System.out.println("first parameter: "
+ dirPath
+ " is NOT a valid directory.");
return;
}
try {
System.out.println("dir path: "
+ (new File(dirPath).getCanonicalPath()));
LOG.debug("dir path: {}",
(new File(dirPath).getCanonicalPath()));
} catch (IOException e) {
e.printStackTrace();
}
FileExplorer fileList = new FileExplorer(dirPath);
File[] list = fileList.listFilesUsingFileFilter();
LOG.debug("List of files in the specified directory:");
if (list != null) {
for (File file : list) {
LOG.debug("file name: {}", file.getName());
LOG.debug("file path: {}", file.getPath());
LOG.debug("file abs path: {}", file.getAbsolutePath());
try {
LOG.debug("file can path: {}", file.getCanonicalPath());
} catch (IOException e) {
e.printStackTrace();
}
}
} else {
LOG.warn("List of files is empty");
}
Set<String> setList = fileList.listFilesUsingJavaIO();
LOG.debug("List of files in the specified directory:");
for (String file : setList) {
LOG.debug("{}", file);
}
int age = 1;
long purgeTime = System.currentTimeMillis() - (age * 24L * 60L * 60L * 1000L);
LOG.debug("age in ms: {}", purgeTime);
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_MONTH, age * -1);
purgeTime = cal.getTimeInMillis();
LOG.debug("age in ms: {}", purgeTime);
deleteFilesByAge(age, dirPath);
LOG.debug("main() done.");
}
}

View File

@ -1,31 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Appenders>
<RollingFile name="rollingFile"
fileName="log.txt"
filePattern="log-%d{yyyy-MM-dd}.txt"
ignoreExceptions="false"
>
<PatternLayout>
<Pattern>[%-5p] %d{yyyy-MM-dd HH:mm:ss.SSS} %c{1} %m%n</Pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="10MB" />
</Policies>
<DefaultRolloverStrategy max="5" />
</RollingFile>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} %c{1} - %msg%n" />
</Console>
</Appenders>
<Loggers>
<Logger name="de.begerad" level="debug" additivity="true">
<appender-ref ref="rollingFile" level="debug" />
</Logger>
<Root level="debug" additivity="false">
<appender-ref ref="console" />
</Root>
</Loggers>
</Configuration>

View File

@ -1,59 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.begerad.file-age-rm</groupId>
<artifactId>file-age-rm</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.9.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
<transformers>
<transformer implementation=
"org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>de.begerad.fileagerm.Main</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -1,38 +0,0 @@
package de.begerad.fileagerm;
import java.io.File;
import static de.begerad.fileagerm.Main.LOG;
public class FileCleaner {
/**
* Clean-up files older than a certain time
*
* @param age age of files to be cleaned up
* @param dirPath directory path of files to be cleaned up
*/
public static void deleteFilesByAge(int age, String dirPath) {
File directory = new File(dirPath);
if (directory.exists()) {
File[] listFiles = FileExplorer.listFiles(dirPath);
LOG.debug("listFiles.length: {}", listFiles.length);
long purgeTime = System.currentTimeMillis() - (age * 24L * 60L * 60L * 1000L);
for (File listFile : listFiles) {
if (listFile.lastModified() < purgeTime) {
if (!listFile.delete()) {
LOG.warn("file could NOT be deleted: {}", listFile.getName());
} else {
LOG.debug("file deleted: {}", listFile.getName());
}
} else {
LOG.debug("file is too young to be deleted: {}", listFile.getName());
}
}
} else {
LOG.warn("directory does not exist: {}", directory);
}
}
}

View File

@ -1,57 +0,0 @@
package de.begerad.fileagerm;
import java.io.File;
import java.io.FileFilter;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class FileExplorer {
private final String directory;
public FileExplorer(String directory) {
this.directory = directory;
}
/**
* @return set containing list of file paths
*/
public Set<String> listFilesUsingJavaIO() {
return Stream.of(new File(directory).listFiles())
.filter(file -> !file.isDirectory())
.map(File::getName)
.collect(Collectors.toSet());
}
/**
* @return array containing list of File objects
*/
public File[] listFilesUsingFileFilter() {
//Creating a File object for directory
File dir = new File(directory);
//Creating filter for directory files
FileFilter fileFilter = new FileFilter() {
public boolean accept(File dir) {
return dir.isFile();
}
};
return dir.listFiles(fileFilter);
}
/**
* @return array containing list of File objects
*/
public static File[] listFiles(String directory) {
//Creating a File object for directory
File dir = new File(directory);
//Creating filter for directory files
FileFilter fileFilter = new FileFilter() {
public boolean accept(File dir) {
return dir.isFile();
}
};
return dir.listFiles(fileFilter);
}
}

View File

@ -1,80 +0,0 @@
package de.begerad.fileagerm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.Set;
import static de.begerad.fileagerm.FileCleaner.deleteFilesByAge;
public class Main {
public static String dirPath = "";
public final static Logger LOG = LoggerFactory.getLogger(Main.class);
public static void main(String[] args) {
LOG.debug("main() started...");
//check user input for directory
if (args.length < 1) {
System.out.println("Please enter directory as first parameter.");
return;
}
dirPath = args[0];
if (!(new File(dirPath).isDirectory())) {
System.out.println("first parameter: "
+ dirPath
+ " is NOT a valid directory.");
return;
}
try {
System.out.println("dir path: "
+ (new File(dirPath).getCanonicalPath()));
LOG.debug("dir path: {}",
(new File(dirPath).getCanonicalPath()));
} catch (IOException e) {
e.printStackTrace();
}
FileExplorer fileList = new FileExplorer(dirPath);
File[] list = fileList.listFilesUsingFileFilter();
LOG.debug("List of files in the specified directory:");
if (list != null) {
for (File file : list) {
LOG.debug("file name: {}", file.getName());
LOG.debug("file path: {}", file.getPath());
LOG.debug("file abs path: {}", file.getAbsolutePath());
try {
LOG.debug("file can path: {}", file.getCanonicalPath());
} catch (IOException e) {
e.printStackTrace();
}
}
} else {
LOG.warn("List of files is empty");
}
Set<String> setList = fileList.listFilesUsingJavaIO();
LOG.debug("List of files in the specified directory:");
for (String file : setList) {
LOG.debug("{}", file);
}
int age = 1;
long purgeTime = System.currentTimeMillis() - (age * 24L * 60L * 60L * 1000L);
LOG.debug("age in ms: {}", purgeTime);
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_MONTH, age * -1);
purgeTime = cal.getTimeInMillis();
LOG.debug("age in ms: {}", purgeTime);
deleteFilesByAge(age, dirPath);
LOG.debug("main() done.");
}
}

View File

@ -1,31 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Appenders>
<RollingFile name="rollingFile"
fileName="log.txt"
filePattern="log-%d{yyyy-MM-dd}.txt"
ignoreExceptions="false"
>
<PatternLayout>
<Pattern>[%-5p] %d{yyyy-MM-dd HH:mm:ss.SSS} %c{1} %m%n</Pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="10MB" />
</Policies>
<DefaultRolloverStrategy max="5" />
</RollingFile>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} %c{1} - %msg%n" />
</Console>
</Appenders>
<Loggers>
<Logger name="de.begerad" level="debug" additivity="true">
<appender-ref ref="rollingFile" level="debug" />
</Logger>
<Root level="debug" additivity="false">
<appender-ref ref="console" />
</Root>
</Loggers>
</Configuration>