Skip to content

Commit

Permalink
moved filename constant to AppConstant class
Browse files Browse the repository at this point in the history
  • Loading branch information
Warded120 committed Feb 20, 2025
1 parent 615b5e9 commit 68ff066
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ public class AppConstant {
public static final String USER_PLACEHOLDER = "{user}";
public static final String TWO_USERS = "TWO_USERS";
public static final String THREE_OR_MORE_USERS = "THREE_OR_MORE_USERS";
public static final String DOTENV_FILENAME = "secretKeys.env";
}
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,6 @@ public class ErrorMessage {
public static final String LOG_FILE_NOT_FOUND = "No file found with name: %s";
public static final String CANNOT_READ_LOG_FILE = "Error reading log file: %s";
public static final String BAD_SECRET_KEY = "The given secret key is incorrect";
public static final String DOTENV_DELETED_OR_NOT_FOUND = ".env file already deleted or not found";
public static final String CANNOT_DELETE_DOTENV = "Failed to delete .env file";
public static final String FUNCTIONALITY_NOT_AVAILABLE = "Functionality is not available";
}
3 changes: 2 additions & 1 deletion service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<commons.collections4.version>4.4</commons.collections4.version>
<google.cloud.storage.version>2.29.1</google.cloud.storage.version>
<apache.commons.version>3.13.0</apache.commons.version>
<io.github.cdimascio.dotenv.version>3.1.0</io.github.cdimascio.dotenv.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -131,7 +132,7 @@
<dependency>
<groupId>io.github.cdimascio</groupId>
<artifactId>dotenv-java</artifactId>
<version>3.1.0</version>
<version>${io.github.cdimascio.dotenv.version}</version>
</dependency>
</dependencies>
</project>
5 changes: 2 additions & 3 deletions service/src/main/java/greencity/config/DotenvConfig.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package greencity.config;

import greencity.constant.AppConstant;
import greencity.constant.ErrorMessage;
import greencity.exception.exceptions.FunctionalityNotAvailableException;
import io.github.cdimascio.dotenv.Dotenv;
Expand All @@ -11,13 +12,11 @@
@Configuration
@Lazy
public class DotenvConfig {
private static final String DOTENV_FILENAME = "secretKeys.env";

@Bean
Dotenv dotenv() {
try {
return Dotenv.configure()
.filename(DOTENV_FILENAME)
.filename(AppConstant.DOTENV_FILENAME)
.load();
} catch (DotenvException ex) {
throw new FunctionalityNotAvailableException(ErrorMessage.FUNCTIONALITY_NOT_AVAILABLE);
Expand Down
11 changes: 3 additions & 8 deletions service/src/main/java/greencity/service/DotenvServiceImpl.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package greencity.service;

import greencity.constant.AppConstant;
import greencity.constant.ErrorMessage;
import greencity.exception.exceptions.BadSecretKeyException;
import greencity.exception.exceptions.FunctionalityNotAvailableException;
Expand All @@ -19,8 +20,6 @@ public class DotenvServiceImpl implements DotenvService {
private Dotenv dotenv;
private final PasswordEncoder passwordEncoder;

private static final String DOTENV_FILENAME = "secretKeys.env";

public DotenvServiceImpl(@Qualifier("DotenvPasswordEncoder") PasswordEncoder passwordEncoder,
Dotenv dotenv) {
this.passwordEncoder = passwordEncoder;
Expand All @@ -47,13 +46,9 @@ public void validateSecretKey(String secretKey) {
public void deleteDotenvFile(String secretKey) {
validateSecretKey(secretKey);

String dotenvFilePath = System.getProperty("user.dir") + File.separator + DOTENV_FILENAME;
String dotenvFilePath = System.getProperty("user.dir") + File.separator + AppConstant.DOTENV_FILENAME;
File dotenvFile = new File(dotenvFilePath);

if (!Files.exists(dotenvFile.toPath())) {
throw new FunctionalityNotAvailableException(ErrorMessage.DOTENV_DELETED_OR_NOT_FOUND);
}

try {
if (!Files.deleteIfExists(dotenvFile.toPath())) {
throw new FunctionalityNotAvailableException(ErrorMessage.CANNOT_DELETE_DOTENV);
Expand All @@ -77,7 +72,7 @@ public void deleteDotenvFile(String secretKey) {
void reloadEnvFile() {
try {
dotenv = Dotenv.configure()
.filename(DOTENV_FILENAME)
.filename(AppConstant.DOTENV_FILENAME)
.load();
} catch (DotenvException ex) {
throw new FunctionalityNotAvailableException(ErrorMessage.FUNCTIONALITY_NOT_AVAILABLE);
Expand Down

0 comments on commit 68ff066

Please sign in to comment.