Skip to content

Commit

Permalink
Update 1.1.0
Browse files Browse the repository at this point in the history
+ TemporaryResourcesProperties
+ Package rename
+ Property to set Temporary file cleaner service enabled
+ Minor refactor
  • Loading branch information
mrsaraira committed Mar 19, 2023
1 parent 7332471 commit 5ed25e8
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 24 deletions.
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
![Java CI with Maven](https://github.com/mrsaraira/temporary-resources/workflows/Java%20CI%20with%20Maven/badge.svg?branch=master)
## Main purpose
This library is created for auto-managing temporary resources cleaning with pre-defined scope by delegating invocation
of the cleaning logic to some Temporary resource cleaner.
of the cleaning logic to some Temporary resources cleaner.

## Features
A brief review of main features provided by the temporary resources library:
+ Temporary resources registration and abstractions infrastructure.
+ Temporary files cleaning service to register files to be auto-deleted.
+ Providing many scopes to register Temporary resources
+ Providing many scopes to register Temporary resources.
+ It is auto-configurable Spring Boot library – you don’t have to do anything more than including it as a dependency to
your application to make it work.

Expand All @@ -23,7 +23,7 @@ your application to make it work.
### Temporary files cleaner service
Service to automatically register files with pre-defined lifetime scope to be cleaned.

Temporary files - are files that eventually will be deleted. These files don't have to be placed in tempfiles directory.
Temporary files - are files that eventually will be deleted. These files don't have to be placed in temp/ directory.
This service registers temporary files and directories by default in the Application scope, or by predefining temporary
file lifetime scope after which it will be removed.

Expand All @@ -33,28 +33,30 @@ file lifetime scope after which it will be removed.

### Getting Started

1. Add temporary resources library dependency to your project. Latest version is *1.0.RELEASE*.
1. Add temporary resources library dependency to your project. Latest version is **1.1.0**.

Maven:
```xml
<dependency>
<groupId>io.github.mrsaraira</groupId>
<artifactId>temporary-resources</artifactId>
<version>1.0.RELEASE</version>
<version>1.1.0</version>
</dependency>
```

Gradle:
```
implementation 'io.github.mrsaraira:temporary-resources:1.0.RELEASE'
implementation 'io.github.mrsaraira:temporary-resources:1.1.0'
```

2. By default, the library is **enabled**, but it checks for Sping Web dependency to ensure that the context has additional
registered scopes: (session, request) for the library to work properly.
To enable/disable library auto-configuration you have to set property `temporary-resources.enabled` in your application
settings.
settings.<br>
To enable/disable Temporary files cleaner service set property: `temporary-files-cleaner-service-enabled`.

```yaml
temporary-resources:
enabled: true
temporary-files-cleaner-service-enabled: true
```
23 changes: 15 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.github.mrsaraira</groupId>
<artifactId>temporary-resources</artifactId>
<version>1.0.RELEASE</version>
<version>1.1.0</version>

<name>temporary-resources</name>
<description>Temporary resources management library</description>
Expand Down Expand Up @@ -46,11 +46,10 @@
</distributionManagement>

<properties>
<java.version>11</java.version>
<maven.compiler.plugin.version>3.8.0</maven.compiler.plugin.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.boot.version>2.7.9</spring.boot.version>
<spring.version>5.3.25</spring.version>
<apache.common.io.version>2.11.0</apache.common.io.version>
Expand All @@ -61,6 +60,7 @@
<javax.servlet.api.version>3.0.1</javax.servlet.api.version>
<slf4j-api.version>2.0.7</slf4j-api.version>
<lombok.version>1.18.26</lombok.version>
<maven-surefire-plugin.version>3.0.0</maven-surefire-plugin.version>
</properties>

<dependencies>
Expand All @@ -82,6 +82,12 @@
<version>${spring.boot.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>${spring.boot.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
Expand All @@ -103,12 +109,13 @@
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
<version>${javax.inject.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j-api.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
Expand Down Expand Up @@ -146,14 +153,14 @@
<version>${maven.compiler.plugin.version}</version>
<configuration>
<encoding>${project.build.sourceEncoding}</encoding>
<source>${java.version}</source>
<target>${java.version}</target>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0</version>
<version>${maven-surefire-plugin.version}</version>
</plugin>
</plugins>
</build>
Expand All @@ -169,7 +176,7 @@
<resource>
<directory>${project.basedir}</directory>
<includes>
<include>LICENSE*</include>
<include>LICENSE</include>
</includes>
</resource>
<resource>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

/**
* Abstract service defining all needed methods for implementations to work with temporary resources i.e.
* registering temporary resources with pre-defined scope and getting {@link TemporaryResourcesCleaner}
* registering temporary resources with pre-defined scope and creating {@link TemporaryResourcesCleaner}
* <br><br>
*
* @param <T> encapsulated resource type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
*/
public interface TemporaryResourcesCleanerFactory {

/**
* Create temporary resources cleaner with pre-defined scope.
*
* @param scope temporary resource lifetime scope
* @return temporary resources cleaner
*/
TemporaryResourcesCleaner getTemporaryResourcesCleaner(@NotNull TemporaryResourceLifetimeScope scope);

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import static io.github.mrsaraira.tempresources.config.TemporaryResourcesCleanerConfiguration.SESSION_TEMPORARY_RESOURCES_CLEANER_PROVIDER;

/**
* Temporary resources cleaner factory implementation to create Spring-managed temporary resources cleaners by pre-defined scope.
* Temporary resources cleaner factory implementation to create Spring-managed temporary resources cleaners with pre-defined scope.
*
* @author Takhsin Saraira
* @see TemporaryResourcesCleaner
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.github.mrsaraira.tempresources.config;

import io.github.mrsaraira.tempresources.files.TemporaryFilesCleanerService;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;

/**
* Temporary resources properties. Generates Spring configuration metadata.
*
* @author Takhsin Saraira
*/
@Data
@ConfigurationProperties(prefix = "temporary-resources")
public class TemporaryResourcesProperties {

/**
* Set temporary resources library configuration enabled
*/
private Boolean enabled;

/**
* Set temporary files cleaner service enabled
*
* @see TemporaryFilesCleanerService
*/
private Boolean temporaryFilesCleanerServiceEnabled;

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.mrsaraira.tempresources.file;
package io.github.mrsaraira.tempresources.files;

import io.github.mrsaraira.tempresources.TemporaryResource;
import io.github.mrsaraira.tempresources.TemporaryResourceLifetimeScope;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package io.github.mrsaraira.tempresources.file;
package io.github.mrsaraira.tempresources.files;

import io.github.mrsaraira.tempresources.BaseTemporaryResourcesCleanerService;
import io.github.mrsaraira.tempresources.TemporaryResource;
import io.github.mrsaraira.tempresources.TemporaryResourceLifetimeScope;
import lombok.Getter;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;

import java.io.File;

/**
* Service to automatically clean temporary files in pre-defined lifetime scope.
* Service to automatically clean temporary files with pre-defined lifetime scope.
* <br>
* Temporary files - are files that eventually will be deleted. These files don't have to be placed in temp/ directory.
* This service registers temporary files and directories by default in
Expand All @@ -26,6 +27,7 @@
* @see TemporaryResourceLifetimeScope
* @see TemporaryResource
*/
@ConditionalOnProperty(value = "temporary-resources.files.enabled", matchIfMissing = true)
@Slf4j
@Service
public class TemporaryFilesCleanerService extends BaseTemporaryResourcesCleanerService<File> {
Expand All @@ -41,9 +43,8 @@ private TemporaryFilesCleanerService() {
instance = this;
}


/**
* Register file to be removed in default scope {@link TemporaryResourceLifetimeScope#APPLICATION APPLICATION}
* Register file to be removed with default scope {@link TemporaryResourceLifetimeScope#APPLICATION APPLICATION}
*
* @param file file to register
* @return temporary file
Expand All @@ -53,7 +54,7 @@ public TemporaryFile register(@NonNull File file) {
}

/**
* Register file to be removed in pre-defined lifetime scope if not whitelisted.
* Register file to be removed with pre-defined lifetime scope.
*
* @param file file to register
* @param scope temporary file lifetime scope after which the file will be removed
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/application.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
temporary-resources:
enabled: true
temporary-files-cleaner-service-enabled: true
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.mrsaraira.tempresources.file;
package io.github.mrsaraira.tempresources.files;

import io.github.mrsaraira.tempresources.TemporaryResourceLifetimeScope;
import io.github.mrsaraira.tempresources.TemporaryResourcesCleanerFactory;
Expand Down Expand Up @@ -38,6 +38,7 @@ public void registerFileTest() {
final File tempFile = createTempFile(RandomStringUtils.randomAlphanumeric(6));
TemporaryFile temporaryFile = temporaryFilesCleanerService.register(tempFile, scope);
Assertions.assertEquals(scope, temporaryFile.getScope());
Assertions.assertEquals(temporaryFile.getResource(), tempFile);
context.getAutowireCapableBeanFactory().destroyBean(temporaryResourcesCleanerFactory.getTemporaryResourcesCleaner(scope));
Assertions.assertFalse(tempFile.exists());
}
Expand All @@ -50,6 +51,7 @@ public void registerFileDefaultScopeTest() {
final File tempFile = createTempFile(RandomStringUtils.randomAlphanumeric(6));
TemporaryFile temporaryFile = temporaryFilesCleanerService.register(tempFile);
Assertions.assertEquals(defaultScope, temporaryFile.getScope());
Assertions.assertEquals(tempFile, temporaryFile.getResource());
context.getAutowireCapableBeanFactory().destroyBean(temporaryResourcesCleanerFactory.getTemporaryResourcesCleaner(defaultScope));
Assertions.assertFalse(tempFile.exists());
}
Expand Down

0 comments on commit 5ed25e8

Please sign in to comment.