forked from IQSS/dataverse
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(conf): introduce ConfigCheckService to validate config on startup …
…IQSS#9572 Starting with important local storage locations for the Dataverse application, this service uses EJB startup mechanisms to verify configuration bits on startup. Checks for the temp storage location and JSF upload location as crucial parts of the app, which, if not exist or write protected, while only cause errors and failures on the first data upload attempt. This is not desirable as it might cause users to be blocked.
- Loading branch information
1 parent
d71cdf2
commit a4ec3a6
Showing
3 changed files
with
77 additions
and
18 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
src/main/java/edu/harvard/iq/dataverse/settings/ConfigCheckService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package edu.harvard.iq.dataverse.settings; | ||
|
||
import edu.harvard.iq.dataverse.util.FileUtil; | ||
|
||
import javax.annotation.PostConstruct; | ||
import javax.ejb.DependsOn; | ||
import javax.ejb.Singleton; | ||
import javax.ejb.Startup; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Map; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
@Startup | ||
@Singleton | ||
@DependsOn("StartupFlywayMigrator") | ||
public class ConfigCheckService { | ||
|
||
private static final Logger logger = Logger.getLogger(ConfigCheckService.class.getCanonicalName()); | ||
|
||
public static class ConfigurationError extends RuntimeException { | ||
public ConfigurationError(String message) { | ||
super(message); | ||
} | ||
} | ||
|
||
@PostConstruct | ||
public void startup() { | ||
if (!checkSystemDirectories()) { | ||
throw new ConfigurationError("Not all configuration checks passed successfully. See logs above."); | ||
} | ||
} | ||
|
||
/** | ||
* In this method, we check the existence and write-ability of all important directories we use during | ||
* normal operations. It does not include checks for the storage system. If directories are not available, | ||
* try to create them (and fail when not allowed to). | ||
* | ||
* @return True if all checks successful, false otherwise. | ||
*/ | ||
public boolean checkSystemDirectories() { | ||
Map<Path, String> paths = Map.of( | ||
Path.of(JvmSettings.UPLOADS_DIRECTORY.lookup()), "temporary JSF upload space (see " + JvmSettings.UPLOADS_DIRECTORY.getScopedKey() + ")", | ||
Path.of(FileUtil.getFilesTempDirectory()), "temporary processing space (see " + JvmSettings.FILES_DIRECTORY.getScopedKey() + ")"); | ||
|
||
boolean success = true; | ||
for (Path path : paths.keySet()) { | ||
if (Files.notExists(path)) { | ||
try { | ||
Files.createDirectories(path); | ||
} catch (IOException e) { | ||
logger.log(Level.SEVERE, () -> "Could not create directory " + path + " for " + paths.get(path)); | ||
success = false; | ||
} | ||
} else if (!Files.isWritable(path)) { | ||
logger.log(Level.SEVERE, () -> "Directory " + path + " for " + paths.get(path) + " exists, but is not writeable"); | ||
success = false; | ||
} | ||
} | ||
return success; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters