Skip to content

Apache Lucene open-source search software

SOLR-6179: Fix unit test breakage by using InMemory storage if config dir is not writable.

Timothy Potter5c75969c047a
diff --git a/solr/core/src/java/org/apache/solr/rest/ManagedResourceStorage.java b/solr/core/src/java/org/apache/solr/rest/ManagedResourceStorage.java
index fce5c92e3d2..3e830fb67d7 100644
--- a/solr/core/src/java/org/apache/solr/rest/ManagedResourceStorage.java
+++ b/solr/core/src/java/org/apache/solr/rest/ManagedResourceStorage.java
@@ -117,7 +117,23 @@ public abstract class ManagedResourceStorage {
if (storageIO instanceof FileStorageIO) {
// using local fs, if storageDir is not set in the solrconfig.xml, assume the configDir for the core
if (initArgs.get(STORAGE_DIR_INIT_ARG) == null) {
- initArgs.add(STORAGE_DIR_INIT_ARG, resourceLoader.getConfigDir());
+ File configDir = new File(resourceLoader.getConfigDir());
+ boolean hasAccess = false;
+ try {
+ hasAccess = configDir.isDirectory() && configDir.canWrite();
+ } catch (java.security.AccessControlException ace) {}
+
+ if (hasAccess) {
+ initArgs.add(STORAGE_DIR_INIT_ARG, configDir.getAbsolutePath());
+ } else {
+ // most likely this is because of a unit test
+ // that doesn't have write-access to the config dir
+ // while this failover approach is not ideal, it's better
+ // than causing the core to fail esp. if managed resources aren't being used
+ log.warn("Cannot write to config directory "+configDir.getAbsolutePath()+
+ "; switching to use InMemory storage instead.");
+ storageIO = new ManagedResourceStorage.InMemoryStorageIO();
+ }
}
}