From 0f3aecfdffb8cdf4d48ab4d0fc43860c7f959ab4 Mon Sep 17 00:00:00 2001 From: Alan Woodward Date: Fri, 20 Nov 2015 12:12:05 +0000 Subject: [PATCH] SOLR-8302: SolrResourceLoader takes a Path for its instance directory git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1715342 13f79535-47bb-0310-9956-ffa450edef68 --- solr/CHANGES.txt | 3 + .../dataimport/SimplePropertiesWriter.java | 6 +- .../dataimport/TestZKPropertiesWriter.java | 2 +- .../extraction/ParseContextConfigTest.java | 4 +- .../apache/solr/hadoop/SolrRecordWriter.java | 29 +- .../solr/morphlines/solr/SolrLocator.java | 11 +- .../solr/cloud/CloudConfigSetService.java | 4 +- .../java/org/apache/solr/cloud/CloudUtil.java | 2 +- .../solr/cloud/ZkSolrResourceLoader.java | 20 +- .../src/java/org/apache/solr/core/Config.java | 38 +- .../apache/solr/core/ConfigSetService.java | 5 +- .../org/apache/solr/core/CoreContainer.java | 29 +- .../java/org/apache/solr/core/NodeConfig.java | 9 +- .../java/org/apache/solr/core/SolrConfig.java | 80 ++-- .../java/org/apache/solr/core/SolrCore.java | 42 +- .../apache/solr/core/SolrResourceLoader.java | 360 +++++++++++------- .../org/apache/solr/core/SolrXmlConfig.java | 21 +- .../solr/handler/admin/CoreAdminHandler.java | 15 +- .../solr/handler/admin/SystemInfoHandler.java | 2 +- .../solr/response/TextResponseWriter.java | 17 +- .../org/apache/solr/schema/IndexSchema.java | 34 +- .../schema/ManagedIndexSchemaFactory.java | 35 +- .../solr/servlet/SolrDispatchFilter.java | 13 +- .../TestWordDelimiterFilterFactory.java | 8 +- .../TestEmbeddedSolrServerConstructors.java | 10 +- .../CollectionsAPIDistributedZkTest.java | 8 +- .../apache/solr/cloud/SolrXmlInZkTest.java | 24 +- .../cloud/TestLeaderElectionZkExpiry.java | 11 +- .../org/apache/solr/cloud/TestZkChroot.java | 15 +- .../apache/solr/core/ResourceLoaderTest.java | 154 ++++---- .../solr/core/TestArbitraryIndexDir.java | 41 +- .../test/org/apache/solr/core/TestConfig.java | 12 +- .../solr/core/TestConfigSetProperties.java | 21 +- .../org/apache/solr/core/TestConfigSets.java | 22 +- .../apache/solr/core/TestCoreContainer.java | 32 +- .../apache/solr/core/TestCoreDiscovery.java | 2 +- .../org/apache/solr/core/TestLazyCores.java | 10 +- .../solr/core/TestShardHandlerFactory.java | 8 +- .../org/apache/solr/core/TestSolrXml.java | 6 +- .../admin/CoreAdminCreateDiscoverTest.java | 5 +- .../apache/solr/rest/TestManagedResource.java | 5 +- .../solr/rest/TestManagedResourceStorage.java | 11 +- .../org/apache/solr/rest/TestRestManager.java | 5 +- .../org/apache/solr/schema/DateFieldTest.java | 3 +- .../OpenExchangeRatesOrgProviderTest.java | 10 +- .../solr/schema/PrimitiveFieldTypeTest.java | 10 +- .../solr/update/SolrIndexConfigTest.java | 8 +- .../solr/util/TestSystemIdResolver.java | 13 +- .../apache/solr/common/util/JavaBinCodec.java | 29 +- .../AbstractEmbeddedSolrServerTestCase.java | 15 +- .../embedded/TestEmbeddedSolrServer.java | 14 +- .../solrj/embedded/TestSolrProperties.java | 9 - .../client/solrj/request/TestCoreAdmin.java | 12 +- .../response/NoOpResponseParserTest.java | 18 +- .../solrj/response/QueryResponseTest.java | 23 +- .../response/TestClusteringResponse.java | 2 +- .../solr/common/util/ContentStreamTest.java | 5 +- .../java/org/apache/solr/SolrTestCaseJ4.java | 54 +-- .../org/apache/solr/util/TestHarness.java | 12 +- 59 files changed, 686 insertions(+), 742 deletions(-) diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt index ce34e6b10a1..74da6d4b3a3 100644 --- a/solr/CHANGES.txt +++ b/solr/CHANGES.txt @@ -543,6 +543,9 @@ Other Changes * SOLR-8315: Removed default core checks in the dispatch filter since we don't have a default core anymore (Varun Thacker) +* SOLR-8302: SolrResourceLoader now takes a Path as its instance directory (Alan + Woodward, Shawn Heisey) + ================== 5.3.1 ================== Bug Fixes diff --git a/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/SimplePropertiesWriter.java b/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/SimplePropertiesWriter.java index 22d4bc8fe59..93481bb8611 100644 --- a/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/SimplePropertiesWriter.java +++ b/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/SimplePropertiesWriter.java @@ -17,8 +17,6 @@ package org.apache.solr.handler.dataimport; * limitations under the License. */ -import static org.apache.solr.handler.dataimport.DataImportHandlerException.SEVERE; - import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; @@ -41,6 +39,8 @@ import org.apache.solr.core.SolrCore; import org.apache.solr.core.SolrResourceLoader; import org.slf4j.Logger; import org.slf4j.LoggerFactory; + +import static org.apache.solr.handler.dataimport.DataImportHandlerException.SEVERE; /** *

* Writes properties using {@link Properties#store} . @@ -116,7 +116,7 @@ public class SimplePropertiesWriter extends DIHProperties { } else { SolrCore core = dataImporter.getCore(); if (core == null) { - configDir = SolrResourceLoader.locateSolrHome(); + configDir = SolrResourceLoader.locateSolrHome().toString(); } else { configDir = core.getResourceLoader().getConfigDir(); } diff --git a/solr/contrib/dataimporthandler/src/test/org/apache/solr/handler/dataimport/TestZKPropertiesWriter.java b/solr/contrib/dataimporthandler/src/test/org/apache/solr/handler/dataimport/TestZKPropertiesWriter.java index 01681862aad..4d5293aef77 100644 --- a/solr/contrib/dataimporthandler/src/test/org/apache/solr/handler/dataimport/TestZKPropertiesWriter.java +++ b/solr/contrib/dataimporthandler/src/test/org/apache/solr/handler/dataimport/TestZKPropertiesWriter.java @@ -62,7 +62,7 @@ public class TestZKPropertiesWriter extends AbstractDataImportHandlerTestCase { "dataimport-solrconfig.xml", "dataimport-schema.xml"); //initCore("solrconfig.xml", "schema.xml", getFile("dih/solr").getAbsolutePath()); - cc = createDefaultCoreContainer(getFile("dih/solr").getAbsolutePath()); + cc = createDefaultCoreContainer(getFile("dih/solr").toPath()); } @Before diff --git a/solr/contrib/extraction/src/test/org/apache/solr/handler/extraction/ParseContextConfigTest.java b/solr/contrib/extraction/src/test/org/apache/solr/handler/extraction/ParseContextConfigTest.java index f9f7bcb507f..6098efcb569 100644 --- a/solr/contrib/extraction/src/test/org/apache/solr/handler/extraction/ParseContextConfigTest.java +++ b/solr/contrib/extraction/src/test/org/apache/solr/handler/extraction/ParseContextConfigTest.java @@ -18,6 +18,8 @@ package org.apache.solr.handler.extraction; */ import javax.xml.parsers.DocumentBuilderFactory; +import java.nio.file.Paths; + import org.apache.solr.SolrTestCaseJ4; import org.apache.solr.core.SolrResourceLoader; import org.apache.tika.parser.ParseContext; @@ -43,7 +45,7 @@ public class ParseContextConfigTest extends SolrTestCaseJ4 { entry.appendChild(property); entries.appendChild(entry); - ParseContext parseContext = new ParseContextConfig(new SolrResourceLoader("."), entries).create(); + ParseContext parseContext = new ParseContextConfig(new SolrResourceLoader(Paths.get(".")), entries).create(); PDFParserConfig pdfParserConfig = parseContext.get(PDFParserConfig.class); diff --git a/solr/contrib/map-reduce/src/java/org/apache/solr/hadoop/SolrRecordWriter.java b/solr/contrib/map-reduce/src/java/org/apache/solr/hadoop/SolrRecordWriter.java index b96b0f469ad..e34b32ffe8b 100644 --- a/solr/contrib/map-reduce/src/java/org/apache/solr/hadoop/SolrRecordWriter.java +++ b/solr/contrib/map-reduce/src/java/org/apache/solr/hadoop/SolrRecordWriter.java @@ -16,6 +16,19 @@ */ package org.apache.solr.hadoop; +import java.io.IOException; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Locale; +import java.util.Properties; +import java.util.Set; +import java.util.concurrent.TimeUnit; + import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.filecache.DistributedCache; import org.apache.hadoop.fs.FileSystem; @@ -35,18 +48,6 @@ import org.apache.solr.core.SolrResourceLoader; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Locale; -import java.util.Properties; -import java.util.Set; -import java.util.concurrent.TimeUnit; - class SolrRecordWriter extends RecordWriter { private static final Logger LOG = LoggerFactory.getLogger(SolrRecordWriter.class); @@ -139,12 +140,12 @@ class SolrRecordWriter extends RecordWriter { String dataDirStr = solrDataDir.toUri().toString(); - SolrResourceLoader loader = new SolrResourceLoader(solrHomeDir.toString(), null, null); + SolrResourceLoader loader = new SolrResourceLoader(Paths.get(solrHomeDir.toString()), null, null); LOG.info(String .format(Locale.ENGLISH, "Constructed instance information solr.home %s (%s), instance dir %s, conf dir %s, writing index to solr.data.dir %s, with permdir %s", - solrHomeDir, solrHomeDir.toUri(), loader.getInstanceDir(), + solrHomeDir, solrHomeDir.toUri(), loader.getInstancePath(), loader.getConfigDir(), dataDirStr, outputShardDir)); // TODO: This is fragile and should be well documented diff --git a/solr/contrib/morphlines-core/src/java/org/apache/solr/morphlines/solr/SolrLocator.java b/solr/contrib/morphlines-core/src/java/org/apache/solr/morphlines/solr/SolrLocator.java index 10a80ff953b..7fe18f8bd38 100644 --- a/solr/contrib/morphlines-core/src/java/org/apache/solr/morphlines/solr/SolrLocator.java +++ b/solr/contrib/morphlines-core/src/java/org/apache/solr/morphlines/solr/SolrLocator.java @@ -16,6 +16,11 @@ */ package org.apache.solr.morphlines.solr; +import javax.xml.parsers.ParserConfigurationException; +import java.io.File; +import java.io.IOException; +import java.nio.file.Paths; + import com.google.common.base.Preconditions; import com.google.common.io.Files; import com.typesafe.config.Config; @@ -39,10 +44,6 @@ import org.slf4j.LoggerFactory; import org.xml.sax.InputSource; import org.xml.sax.SAXException; -import javax.xml.parsers.ParserConfigurationException; -import java.io.File; -import java.io.IOException; - /** * Set of configuration parameters that identify the location and schema of a Solr server or * SolrCloud; Based on this information this class can return the schema and a corresponding @@ -149,7 +150,7 @@ public class SolrLocator { LOG.debug("SolrLocator loading IndexSchema from dir {}", mySolrHomeDir); try { - SolrResourceLoader loader = new SolrResourceLoader(mySolrHomeDir); + SolrResourceLoader loader = new SolrResourceLoader(Paths.get(mySolrHomeDir)); SolrConfig solrConfig = new SolrConfig(loader, "solrconfig.xml", null); InputSource is = new InputSource(loader.openSchema("schema.xml")); is.setSystemId(SystemIdResolver.createSystemIdFromResourceName("schema.xml")); diff --git a/solr/core/src/java/org/apache/solr/cloud/CloudConfigSetService.java b/solr/core/src/java/org/apache/solr/cloud/CloudConfigSetService.java index c371c749796..da73c8932fa 100644 --- a/solr/core/src/java/org/apache/solr/cloud/CloudConfigSetService.java +++ b/solr/core/src/java/org/apache/solr/cloud/CloudConfigSetService.java @@ -17,6 +17,8 @@ package org.apache.solr.cloud; +import java.nio.file.Paths; + import org.apache.solr.core.ConfigSetService; import org.apache.solr.core.CoreDescriptor; import org.apache.solr.core.SolrResourceLoader; @@ -35,7 +37,7 @@ public class CloudConfigSetService extends ConfigSetService { // TODO: Shouldn't the collection node be created by the Collections API? zkController.createCollectionZkNode(cd.getCloudDescriptor()); String configName = zkController.getZkStateReader().readConfigName(cd.getCollectionName()); - return new ZkSolrResourceLoader(cd.getInstanceDir(), configName, parentLoader.getClassLoader(), + return new ZkSolrResourceLoader(Paths.get(cd.getInstanceDir()), configName, parentLoader.getClassLoader(), cd.getSubstitutableProperties(), zkController); } diff --git a/solr/core/src/java/org/apache/solr/cloud/CloudUtil.java b/solr/core/src/java/org/apache/solr/cloud/CloudUtil.java index b9f16c7b0ee..da6850d7c34 100644 --- a/solr/core/src/java/org/apache/solr/cloud/CloudUtil.java +++ b/solr/core/src/java/org/apache/solr/cloud/CloudUtil.java @@ -102,7 +102,7 @@ public class CloudUtil { public static String unifiedResourcePath(SolrResourceLoader loader) { return (loader instanceof ZkSolrResourceLoader) ? ((ZkSolrResourceLoader) loader).getConfigSetZkPath() + "/" : - loader.getConfigDir(); + loader.getConfigDir() + File.separator; } /**Read the list of public keys from ZK diff --git a/solr/core/src/java/org/apache/solr/cloud/ZkSolrResourceLoader.java b/solr/core/src/java/org/apache/solr/cloud/ZkSolrResourceLoader.java index 0c1ac9c6acc..b979c3f5173 100644 --- a/solr/core/src/java/org/apache/solr/cloud/ZkSolrResourceLoader.java +++ b/solr/core/src/java/org/apache/solr/cloud/ZkSolrResourceLoader.java @@ -17,6 +17,14 @@ package org.apache.solr.cloud; * limitations under the License. */ +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Path; +import java.util.List; +import java.util.Properties; + import org.apache.solr.common.SolrException; import org.apache.solr.common.SolrException.ErrorCode; import org.apache.solr.common.cloud.ZkConfigManager; @@ -27,13 +35,6 @@ import org.apache.solr.schema.ZkIndexSchemaReader; import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.data.Stat; -import java.io.ByteArrayInputStream; -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.util.List; -import java.util.Properties; - /** * ResourceLoader that works with ZooKeeper. * @@ -44,8 +45,7 @@ public class ZkSolrResourceLoader extends SolrResourceLoader { private ZkController zkController; private ZkIndexSchemaReader zkIndexSchemaReader; - public ZkSolrResourceLoader(String instanceDir, String configSet, - ZkController zooKeeperController) { + public ZkSolrResourceLoader(Path instanceDir, String configSet, ZkController zooKeeperController) { super(instanceDir); this.zkController = zooKeeperController; configSetZkPath = ZkConfigManager.CONFIGS_ZKNODE + "/" + configSet; @@ -58,7 +58,7 @@ public class ZkSolrResourceLoader extends SolrResourceLoader { * otherwise it will attempt to resolve resources using any jar files found in * the "lib/" directory in the specified instance directory. */ - public ZkSolrResourceLoader(String instanceDir, String configSet, ClassLoader parent, + public ZkSolrResourceLoader(Path instanceDir, String configSet, ClassLoader parent, Properties coreProperties, ZkController zooKeeperController) { super(instanceDir, parent, coreProperties); this.zkController = zooKeeperController; diff --git a/solr/core/src/java/org/apache/solr/core/Config.java b/solr/core/src/java/org/apache/solr/core/Config.java index c30b1cd2983..938f2514a3b 100644 --- a/solr/core/src/java/org/apache/solr/core/Config.java +++ b/solr/core/src/java/org/apache/solr/core/Config.java @@ -17,24 +17,6 @@ package org.apache.solr.core; -import org.apache.lucene.util.Version; -import org.apache.solr.cloud.ZkSolrResourceLoader; -import org.apache.solr.common.SolrException; -import org.apache.solr.update.SolrIndexConfig; -import org.apache.solr.util.DOMUtil; -import org.apache.solr.util.SystemIdResolver; -import org.apache.solr.common.util.XMLErrorLogger; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.w3c.dom.Document; -import org.w3c.dom.Element; -import org.w3c.dom.NamedNodeMap; -import org.w3c.dom.Node; -import org.w3c.dom.NodeList; -import org.xml.sax.InputSource; -import org.xml.sax.SAXException; -import org.apache.commons.io.IOUtils; - import javax.xml.namespace.QName; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; @@ -48,7 +30,6 @@ import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; - import java.io.IOException; import java.io.InputStream; import java.text.ParseException; @@ -63,6 +44,23 @@ import java.util.TreeMap; import java.util.TreeSet; import java.util.concurrent.atomic.AtomicBoolean; +import org.apache.commons.io.IOUtils; +import org.apache.lucene.util.Version; +import org.apache.solr.cloud.ZkSolrResourceLoader; +import org.apache.solr.common.SolrException; +import org.apache.solr.common.util.XMLErrorLogger; +import org.apache.solr.util.DOMUtil; +import org.apache.solr.util.SystemIdResolver; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; + /** * */ @@ -110,7 +108,7 @@ public class Config { public Config(SolrResourceLoader loader, String name, InputSource is, String prefix, boolean substituteProps) throws ParserConfigurationException, IOException, SAXException { if( loader == null ) { - loader = new SolrResourceLoader( null ); + loader = new SolrResourceLoader(SolrResourceLoader.locateSolrHome()); } this.loader = loader; this.name = name; diff --git a/solr/core/src/java/org/apache/solr/core/ConfigSetService.java b/solr/core/src/java/org/apache/solr/core/ConfigSetService.java index 1120ea52eb0..e63e7b5a803 100644 --- a/solr/core/src/java/org/apache/solr/core/ConfigSetService.java +++ b/solr/core/src/java/org/apache/solr/core/ConfigSetService.java @@ -156,7 +156,7 @@ public abstract class ConfigSetService { @Override public SolrResourceLoader createCoreResourceLoader(CoreDescriptor cd) { Path instanceDir = locateInstanceDir(cd); - return new SolrResourceLoader(instanceDir.toString(), parentLoader.getClassLoader(), cd.getSubstitutableProperties()); + return new SolrResourceLoader(instanceDir, parentLoader.getClassLoader(), cd.getSubstitutableProperties()); } @Override @@ -204,7 +204,8 @@ public abstract class ConfigSetService { Path schemaFile = Paths.get(solrConfig.getResourceLoader().getConfigDir()).resolve(resourceNameToBeUsed); if (Files.exists(schemaFile)) { try { - return schemaCache.get(cacheName(schemaFile), new Callable() { + String cachedName = cacheName(schemaFile); + return schemaCache.get(cachedName, new Callable() { @Override public IndexSchema call() throws Exception { logger.info("Creating new index schema for core {}", cd.getName()); diff --git a/solr/core/src/java/org/apache/solr/core/CoreContainer.java b/solr/core/src/java/org/apache/solr/core/CoreContainer.java index bae252e5345..b042d0c7132 100644 --- a/solr/core/src/java/org/apache/solr/core/CoreContainer.java +++ b/solr/core/src/java/org/apache/solr/core/CoreContainer.java @@ -17,8 +17,9 @@ package org.apache.solr.core; -import java.io.File; import java.io.IOException; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.Collection; import java.util.List; @@ -61,7 +62,6 @@ import org.apache.solr.security.PKIAuthenticationPlugin; import org.apache.solr.security.SecurityPluginHolder; import org.apache.solr.update.UpdateShardHandler; import org.apache.solr.util.DefaultSolrThreadFactory; -import org.apache.solr.util.FileUtils; import org.apache.zookeeper.KeeperException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -178,7 +178,7 @@ public class CoreContainer { * @see #load() */ public CoreContainer(SolrResourceLoader loader) { - this(SolrXmlConfig.fromSolrHome(loader, loader.getInstanceDir())); + this(SolrXmlConfig.fromSolrHome(loader, loader.getInstancePath())); } /** @@ -188,7 +188,7 @@ public class CoreContainer { * @see #load() */ public CoreContainer(String solrHome) { - this(new SolrResourceLoader(solrHome)); + this(new SolrResourceLoader(Paths.get(solrHome))); } /** @@ -216,7 +216,7 @@ public class CoreContainer { public CoreContainer(NodeConfig config, Properties properties, CoresLocator locator, boolean asyncSolrCoreLoad) { this.loader = config.getSolrResourceLoader(); - this.solrHome = loader.getInstanceDir(); + this.solrHome = loader.getInstancePath().toString(); this.cfg = checkNotNull(config); this.coresLocator = locator; this.containerProperties = new Properties(properties); @@ -340,6 +340,10 @@ public class CoreContainer { cfg = null; containerProperties = null; } + + public static CoreContainer createAndLoad(Path solrHome) { + return createAndLoad(solrHome, solrHome.resolve(SolrXmlConfig.SOLR_XML_FILE)); + } /** * Create a new CoreContainer and load its cores @@ -347,7 +351,7 @@ public class CoreContainer { * @param configFile the file containing this container's configuration * @return a loaded CoreContainer */ - public static CoreContainer createAndLoad(String solrHome, File configFile) { + public static CoreContainer createAndLoad(Path solrHome, Path configFile) { SolrResourceLoader loader = new SolrResourceLoader(solrHome); CoreContainer cc = new CoreContainer(SolrXmlConfig.fromFile(loader, configFile)); try { @@ -375,15 +379,18 @@ public class CoreContainer { * Load the cores defined for this CoreContainer */ public void load() { - log.info("Loading cores into CoreContainer [instanceDir={}]", loader.getInstanceDir()); + log.info("Loading cores into CoreContainer [instanceDir={}]", loader.getInstancePath()); // add the sharedLib to the shared resource loader before initializing cfg based plugins String libDir = cfg.getSharedLibDirectory(); if (libDir != null) { - File f = FileUtils.resolvePath(new File(solrHome), libDir); - log.info("loading shared library: " + f.getAbsolutePath()); - loader.addToClassLoader(libDir, null, false); - loader.reloadLuceneSPI(); + Path libPath = loader.getInstancePath().resolve(libDir); + try { + loader.addToClassLoader(SolrResourceLoader.getURLs(libPath)); + loader.reloadLuceneSPI(); + } catch (IOException e) { + log.warn("Couldn't add files from {} to classpath: {}", libPath, e.getMessage()); + } } diff --git a/solr/core/src/java/org/apache/solr/core/NodeConfig.java b/solr/core/src/java/org/apache/solr/core/NodeConfig.java index cca24758103..3fb64473751 100644 --- a/solr/core/src/java/org/apache/solr/core/NodeConfig.java +++ b/solr/core/src/java/org/apache/solr/core/NodeConfig.java @@ -18,7 +18,6 @@ package org.apache.solr.core; */ import java.nio.file.Path; -import java.nio.file.Paths; import java.util.Properties; import org.apache.solr.common.SolrException; @@ -221,17 +220,17 @@ public class NodeConfig { public NodeConfigBuilder(String nodeName, SolrResourceLoader loader) { this.nodeName = nodeName; this.loader = loader; - this.coreRootDirectory = Paths.get(loader.getInstanceDir()); - this.configSetBaseDirectory = Paths.get(loader.getInstanceDir()).resolve("configsets"); + this.coreRootDirectory = loader.getInstancePath(); + this.configSetBaseDirectory = loader.getInstancePath().resolve("configsets"); } public NodeConfigBuilder setCoreRootDirectory(String coreRootDirectory) { - this.coreRootDirectory = Paths.get(loader.getInstanceDir()).resolve(coreRootDirectory); + this.coreRootDirectory = loader.getInstancePath().resolve(coreRootDirectory); return this; } public NodeConfigBuilder setConfigSetBaseDirectory(String configSetBaseDirectory) { - this.configSetBaseDirectory = Paths.get(loader.resolve(configSetBaseDirectory)); + this.configSetBaseDirectory = loader.getInstancePath().resolve(configSetBaseDirectory); return this; } diff --git a/solr/core/src/java/org/apache/solr/core/SolrConfig.java b/solr/core/src/java/org/apache/solr/core/SolrConfig.java index f657fc044b9..e00a836473f 100644 --- a/solr/core/src/java/org/apache/solr/core/SolrConfig.java +++ b/solr/core/src/java/org/apache/solr/core/SolrConfig.java @@ -20,29 +20,19 @@ package org.apache.solr.core; import javax.xml.parsers.ParserConfigurationException; import javax.xml.xpath.XPathConstants; - -import java.io.File; -import java.io.FileFilter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.net.MalformedURLException; +import java.net.URL; import java.nio.charset.StandardCharsets; -import java.util.ArrayList; -import java.util.Collections; -import java.util.EnumSet; -import java.util.HashMap; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Locale; -import java.util.Map; -import java.util.Properties; -import java.util.Set; -import java.util.UUID; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; import com.google.common.collect.ImmutableList; - import org.apache.lucene.index.IndexDeletionPolicy; import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.util.Version; @@ -68,8 +58,6 @@ import org.apache.solr.update.UpdateLog; import org.apache.solr.update.processor.UpdateRequestProcessorChain; import org.apache.solr.update.processor.UpdateRequestProcessorFactory; import org.apache.solr.util.DOMUtil; -import org.apache.solr.util.FileUtils; -import org.apache.solr.util.RegexFileFilter; import org.noggit.JSONParser; import org.noggit.ObjectBuilder; import org.slf4j.Logger; @@ -79,9 +67,9 @@ import org.w3c.dom.NodeList; import org.xml.sax.InputSource; import org.xml.sax.SAXException; -import static org.apache.solr.common.util.Utils.makeMap; import static org.apache.solr.common.params.CommonParams.NAME; import static org.apache.solr.common.params.CommonParams.PATH; +import static org.apache.solr.common.util.Utils.makeMap; import static org.apache.solr.core.ConfigOverlay.ZNODEVER; import static org.apache.solr.core.SolrConfig.PluginOpts.LAZY; import static org.apache.solr.core.SolrConfig.PluginOpts.MULTI_OK; @@ -166,7 +154,7 @@ public class SolrConfig extends Config implements MapSerializable { * @param name the configuration name used by the loader if the stream is null * @param is the configuration stream */ - public SolrConfig(String instanceDir, String name, InputSource is) + public SolrConfig(Path instanceDir, String name, InputSource is) throws ParserConfigurationException, IOException, SAXException { this(new SolrResourceLoader(instanceDir), name, is); } @@ -179,7 +167,7 @@ public class SolrConfig extends Config implements MapSerializable { if (loader instanceof ZkSolrResourceLoader) { resource = name; } else { - resource = loader.getConfigDir() + name; + resource = Paths.get(loader.getConfigDir()).resolve(name).toString(); } throw new SolrException(ErrorCode.SERVER_ERROR, "Error loading solr config from " + resource, e); } @@ -745,32 +733,38 @@ public class SolrConfig extends Config implements MapSerializable { log.info("Adding specified lib dirs to ClassLoader"); SolrResourceLoader loader = getResourceLoader(); + List urls = new ArrayList<>(); - try { - for (int i = 0; i < nodes.getLength(); i++) { - Node node = nodes.item(i); - - String baseDir = DOMUtil.getAttr(node, "dir"); - String path = DOMUtil.getAttr(node, PATH); - if (null != baseDir) { - // :TODO: add support for a simpler 'glob' mutually exclusive of regex - String regex = DOMUtil.getAttr(node, "regex"); - FileFilter filter = (null == regex) ? null : new RegexFileFilter(regex); - loader.addToClassLoader(baseDir, filter, false); - } else if (null != path) { - final File file = FileUtils.resolvePath(new File(loader.getInstanceDir()), path); - loader.addToClassLoader(file.getParent(), new FileFilter() { - @Override - public boolean accept(File pathname) { - return pathname.equals(file); - } - }, false); - } else { - throw new RuntimeException( - "lib: missing mandatory attributes: 'dir' or 'path'"); + for (int i = 0; i < nodes.getLength(); i++) { + Node node = nodes.item(i); + String baseDir = DOMUtil.getAttr(node, "dir"); + String path = DOMUtil.getAttr(node, PATH); + if (null != baseDir) { + // :TODO: add support for a simpler 'glob' mutually exclusive of regex + Path dir = loader.getInstancePath().resolve(baseDir); + String regex = DOMUtil.getAttr(node, "regex"); + try { + if (regex == null) + urls.addAll(SolrResourceLoader.getURLs(dir)); + else + urls.addAll(SolrResourceLoader.getFilteredURLs(dir, regex)); + } catch (IOException e) { + log.warn("Couldn't add files from {} filtered by {} to classpath: {}", dir, regex, e.getMessage()); } + } else if (null != path) { + final Path dir = loader.getInstancePath().resolve(path); + try { + urls.add(dir.toUri().toURL()); + } catch (MalformedURLException e) { + log.warn("Couldn't add file {} to classpath: {}", dir, e.getMessage()); + } + } else { + throw new RuntimeException("lib: missing mandatory attributes: 'dir' or 'path'"); } - } finally { + } + + if (urls.size() > 0) { + loader.addToClassLoader(urls); loader.reloadLuceneSPI(); } } diff --git a/solr/core/src/java/org/apache/solr/core/SolrCore.java b/solr/core/src/java/org/apache/solr/core/SolrCore.java index 61868cb648a..7a15562d3f1 100644 --- a/solr/core/src/java/org/apache/solr/core/SolrCore.java +++ b/solr/core/src/java/org/apache/solr/core/SolrCore.java @@ -17,9 +17,6 @@ package org.apache.solr.core; -import static com.google.common.base.Preconditions.*; -import static org.apache.solr.common.params.CommonParams.*; - import java.io.Closeable; import java.io.File; import java.io.FileNotFoundException; @@ -31,22 +28,7 @@ import java.lang.reflect.Constructor; import java.net.URL; import java.nio.charset.StandardCharsets; import java.nio.file.NoSuchFileException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.Date; -import java.util.HashMap; -import java.util.HashSet; -import java.util.IdentityHashMap; -import java.util.LinkedHashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Locale; -import java.util.Map; -import java.util.Properties; -import java.util.Set; -import java.util.StringTokenizer; +import java.util.*; import java.util.concurrent.Callable; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CopyOnWriteArrayList; @@ -94,20 +76,7 @@ import org.apache.solr.handler.component.SearchComponent; import org.apache.solr.logging.MDCLoggingContext; import org.apache.solr.request.SolrQueryRequest; import org.apache.solr.request.SolrRequestHandler; -import org.apache.solr.response.BinaryResponseWriter; -import org.apache.solr.response.CSVResponseWriter; -import org.apache.solr.response.JSONResponseWriter; -import org.apache.solr.response.PHPResponseWriter; -import org.apache.solr.response.PHPSerializedResponseWriter; -import org.apache.solr.response.PythonResponseWriter; -import org.apache.solr.response.QueryResponseWriter; -import org.apache.solr.response.RawResponseWriter; -import org.apache.solr.response.RubyResponseWriter; -import org.apache.solr.response.SchemaXmlResponseWriter; -import org.apache.solr.response.SmileResponseWriter; -import org.apache.solr.response.SolrQueryResponse; -import org.apache.solr.response.SortingResponseWriter; -import org.apache.solr.response.XMLResponseWriter; +import org.apache.solr.response.*; import org.apache.solr.response.transform.TransformerFactory; import org.apache.solr.rest.ManagedResourceStorage; import org.apache.solr.rest.ManagedResourceStorage.StorageIO; @@ -147,6 +116,9 @@ import org.apache.zookeeper.data.Stat; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import static com.google.common.base.Preconditions.checkNotNull; +import static org.apache.solr.common.params.CommonParams.PATH; + /** * */ @@ -763,7 +735,7 @@ public final class SolrCore implements SolrInfoMBean, Closeable { this.dataDir = initDataDir(dataDir, config, coreDescriptor); this.ulogDir = initUpdateLogDir(coreDescriptor); - log.info("[{}] Opening new SolrCore at [{}], dataDir=[{}]", logid, resourceLoader.getInstanceDir(), dataDir); + log.info("[{}] Opening new SolrCore at [{}], dataDir=[{}]", logid, resourceLoader.getInstancePath(), dataDir); checkVersionFieldExistsInSchema(schema, coreDescriptor); @@ -2465,7 +2437,7 @@ public final class SolrCore implements SolrInfoMBean, Closeable { lst.add("coreName", name==null ? "(null)" : name); lst.add("startTime", startTime); lst.add("refCount", getOpenCount()); - lst.add("instanceDir", resourceLoader.getInstanceDir()); + lst.add("instanceDir", resourceLoader.getInstancePath()); lst.add("indexDir", getIndexDir()); CoreDescriptor cd = getCoreDescriptor(); diff --git a/solr/core/src/java/org/apache/solr/core/SolrResourceLoader.java b/solr/core/src/java/org/apache/solr/core/SolrResourceLoader.java index e6272d258e5..695dd9e943e 100644 --- a/solr/core/src/java/org/apache/solr/core/SolrResourceLoader.java +++ b/solr/core/src/java/org/apache/solr/core/SolrResourceLoader.java @@ -23,22 +23,23 @@ import javax.naming.NamingException; import javax.naming.NoInitialContextException; import java.io.Closeable; import java.io.File; -import java.io.FileFilter; -import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.lang.reflect.Constructor; -import java.net.MalformedURLException; -import java.net.URI; import java.net.URL; import java.net.URLClassLoader; import java.nio.charset.CharacterCodingException; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; +import java.nio.file.DirectoryStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.PathMatcher; import java.nio.file.Paths; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -70,7 +71,6 @@ import org.apache.solr.schema.ManagedIndexSchemaFactory; import org.apache.solr.schema.SimilarityFactory; import org.apache.solr.search.QParserPlugin; import org.apache.solr.update.processor.UpdateRequestProcessorFactory; -import org.apache.solr.util.FileUtils; import org.apache.solr.util.plugin.SolrCoreAware; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -91,7 +91,7 @@ public class SolrResourceLoader implements ResourceLoader,Closeable }; protected URLClassLoader classLoader; - private final String instanceDir; + private final Path instanceDir; private String dataDir; private final List waitingForCore = Collections.synchronizedList(new ArrayList()); @@ -116,6 +116,40 @@ public class SolrResourceLoader implements ResourceLoader,Closeable } return managedResourceRegistry; } + + public SolrResourceLoader() { + this(SolrResourceLoader.locateSolrHome(), null, null); + } + + /** + *

+ * This loader will delegate to the context classloader when possible, + * otherwise it will attempt to resolve resources using any jar files + * found in the "lib/" directory in the specified instance directory. + * If the instance directory is not specified (=null), SolrResourceLoader#locateInstanceDir will provide one. + */ + public SolrResourceLoader(Path instanceDir, ClassLoader parent) + { + this(instanceDir, parent, null); + } + + /** Use {@link #SolrResourceLoader(Path, ClassLoader)} */ + @Deprecated + public SolrResourceLoader(String instanceDir, ClassLoader parent) { + this(Paths.get(instanceDir), parent); + } + + public SolrResourceLoader(Path instanceDir) { + this(instanceDir, null, null); + } + + /** + * Use {@link #SolrResourceLoader(Path)} + */ + @Deprecated + public SolrResourceLoader(String instanceDir) { + this(Paths.get(instanceDir)); + } /** *

@@ -127,86 +161,78 @@ public class SolrResourceLoader implements ResourceLoader,Closeable * @param instanceDir - base directory for this resource loader, if null locateSolrHome() will be used. * @see #locateSolrHome */ - public SolrResourceLoader( String instanceDir, ClassLoader parent, Properties coreProperties ) - { - if( instanceDir == null ) { - this.instanceDir = SolrResourceLoader.locateSolrHome(); - log.info("new SolrResourceLoader for deduced Solr Home: '{}'", - this.instanceDir); + public SolrResourceLoader(Path instanceDir, ClassLoader parent, Properties coreProperties) { + if (instanceDir == null) { + this.instanceDir = SolrResourceLoader.locateSolrHome().toAbsolutePath().normalize(); + log.info("new SolrResourceLoader for deduced Solr Home: '{}'", this.instanceDir); } else{ - this.instanceDir = normalizeDir(instanceDir); - log.info("new SolrResourceLoader for directory: '{}'", - this.instanceDir); + this.instanceDir = instanceDir.toAbsolutePath().normalize(); + log.info("new SolrResourceLoader for directory: '{}'", this.instanceDir); } - - this.classLoader = createClassLoader(null, parent); + + if (parent == null) + parent = Thread.currentThread().getContextClassLoader(); + this.classLoader = new URLClassLoader(new URL[0], parent); + /* * Skip the lib subdirectory when we are loading from the solr home. * Otherwise load it, so core lib directories still get loaded. * The default sharedLib will pick this up later, and if the user has * changed sharedLib, then we don't want to load that location anyway. */ - if (! this.instanceDir.equals(SolrResourceLoader.locateSolrHome())) { - addToClassLoader("./lib/", null, true); - reloadLuceneSPI(); + if (!this.instanceDir.equals(SolrResourceLoader.locateSolrHome())) { + Path libDir = this.instanceDir.resolve("lib"); + if (Files.exists(libDir)) { + try { + addToClassLoader(getURLs(libDir)); + } catch (IOException e) { + log.warn("Couldn't add files from {} to classpath: {}", libDir, e.getMessage()); + } + reloadLuceneSPI(); + } } this.coreProperties = coreProperties; } /** - *

- * This loader will delegate to the context classloader when possible, - * otherwise it will attempt to resolve resources using any jar files - * found in the "lib/" directory in the specified instance directory. - * If the instance directory is not specified (=null), SolrResourceLoader#locateInstanceDir will provide one. + * Use {@link #SolrResourceLoader(Path, ClassLoader, Properties)} */ - public SolrResourceLoader( String instanceDir, ClassLoader parent ) - { - this(instanceDir, parent, null); + @Deprecated + public SolrResourceLoader(String instanceDir, ClassLoader parent, Properties properties) { + this(Paths.get(instanceDir), parent, properties); } /** - * Adds every file/dir found in the baseDir which passes the specified Filter - * to the ClassLoader used by this ResourceLoader. This method MUST + * Adds URLs to the ResourceLoader's internal classloader. This method MUST * only be called prior to using this ResourceLoader to get any resources, otherwise * its behavior will be non-deterministic. You also have to {link @reloadLuceneSPI} * before using this ResourceLoader. - * - *

This method will quietly ignore missing or non-directory baseDir - * folder. * - * @param baseDir base directory whose children (either jars or directories of - * classes) will be in the classpath, will be resolved relative - * the instance dir. - * @param filter The filter files must satisfy, if null all files will be accepted. - * @param quiet Be quiet if baseDir does not point to a directory or if no file is - * left after applying the filter. + * @param urls the URLs of files to add */ - void addToClassLoader(final String baseDir, final FileFilter filter, boolean quiet) { - File base = FileUtils.resolvePath(new File(getInstanceDir()), baseDir); - if (base != null && base.exists() && base.isDirectory()) { - File[] files = base.listFiles(filter); - if (files == null || files.length == 0) { - if (!quiet) { - log.warn("No files added to classloader from lib: " - + baseDir + " (resolved as: " + base.getAbsolutePath() + ")."); - } - } else { - this.classLoader = replaceClassLoader(classLoader, base, filter); - } - } else { - if (!quiet) { - log.warn("Can't find (or read) directory to add to classloader: " - + baseDir + " (resolved as: " + base.getAbsolutePath() + ")."); - } + void addToClassLoader(List urls) { + URLClassLoader newLoader = addURLsToClassLoader(classLoader, urls); + if (newLoader != classLoader) { + this.classLoader = newLoader; } } + + /** + * Adds URLs to the ResourceLoader's internal classloader. This method MUST + * only be called prior to using this ResourceLoader to get any resources, otherwise + * its behavior will be non-deterministic. You also have to {link @reloadLuceneSPI} + * before using this ResourceLoader. + * + * @param urls the URLs of files to add + */ + void addToClassLoader(URL... urls) { + addToClassLoader(Arrays.asList(urls)); + } /** * Reloads all Lucene SPI implementations using the new classloader. - * This method must be called after {@link #addToClassLoader(String, FileFilter, boolean)} - * and {@link #addToClassLoader(String,FileFilter,boolean)} before using - * this ResourceLoader. + * This method must be called after {@link #addToClassLoader(List)} + * and before using this ResourceLoader. */ void reloadLuceneSPI() { // Codecs: @@ -218,55 +244,75 @@ public class SolrResourceLoader implements ResourceLoader,Closeable TokenFilterFactory.reloadTokenFilters(this.classLoader); TokenizerFactory.reloadTokenizers(this.classLoader); } - - private static URLClassLoader replaceClassLoader(final URLClassLoader oldLoader, - final File base, - final FileFilter filter) { - if (null != base && base.canRead() && base.isDirectory()) { - File[] files = base.listFiles(filter); - - if (null == files || 0 == files.length) return oldLoader; - - URL[] oldElements = oldLoader.getURLs(); - URL[] elements = new URL[oldElements.length + files.length]; - System.arraycopy(oldElements, 0, elements, 0, oldElements.length); - - for (int j = 0; j < files.length; j++) { - try { - URL element = files[j].toURI().normalize().toURL(); - log.info("Adding '" + element.toString() + "' to classloader"); - elements[oldElements.length + j] = element; - } catch (MalformedURLException e) { - SolrException.log(log, "Can't add element to classloader: " + files[j], e); - } - } - ClassLoader oldParent = oldLoader.getParent(); - IOUtils.closeWhileHandlingException(oldLoader); // best effort - return URLClassLoader.newInstance(elements, oldParent); + + private static URLClassLoader addURLsToClassLoader(final URLClassLoader oldLoader, List urls) { + if (urls.size() == 0) { + return oldLoader; } - // are we still here? - return oldLoader; + + List allURLs = new ArrayList<>(); + allURLs.addAll(Arrays.asList(oldLoader.getURLs())); + allURLs.addAll(urls); + for (URL url : urls) { + log.info("Adding '{}' to classloader", url.toString()); + } + + ClassLoader oldParent = oldLoader.getParent(); + IOUtils.closeWhileHandlingException(oldLoader); + return URLClassLoader.newInstance(allURLs.toArray(new URL[allURLs.size()]), oldParent); } - + /** - * Convenience method for getting a new ClassLoader using all files found - * in the specified lib directory. + * Utility method to get the URLs of all paths under a given directory that match a filter + * @param libDir the root directory + * @param filter the filter + * @return all matching URLs + * @throws IOException on error */ - static URLClassLoader createClassLoader(final File libDir, ClassLoader parent) { - if ( null == parent ) { - parent = Thread.currentThread().getContextClassLoader(); + public static List getURLs(Path libDir, DirectoryStream.Filter filter) throws IOException { + List urls = new ArrayList<>(); + try (DirectoryStream directory = Files.newDirectoryStream(libDir, filter)) { + for (Path element : directory) { + urls.add(element.toUri().normalize().toURL()); + } } - return replaceClassLoader(URLClassLoader.newInstance(new URL[0], parent), - libDir, null); + return urls; } - - public SolrResourceLoader( String instanceDir ) - { - this( instanceDir, null, null ); + + /** + * Utility method to get the URLs of all paths under a given directory + * @param libDir the root directory + * @return all subdirectories as URLs + * @throws IOException on error + */ + public static List getURLs(Path libDir) throws IOException { + return getURLs(libDir, new DirectoryStream.Filter() { + @Override + public boolean accept(Path entry) throws IOException { + return true; + } + }); + } + + /** + * Utility method to get the URLs of all paths under a given directory that match a regex + * @param libDir the root directory + * @param regex the regex as a String + * @return all matching URLs + * @throws IOException on error + */ + public static List getFilteredURLs(Path libDir, String regex) throws IOException { + final PathMatcher matcher = libDir.getFileSystem().getPathMatcher("regex:" + regex); + return getURLs(libDir, new DirectoryStream.Filter() { + @Override + public boolean accept(Path entry) throws IOException { + return matcher.matches(entry.getFileName()); + } + }); } /** Ensures a directory name always ends with a '/'. */ - public static String normalizeDir(String path) { + public static String normalizeDir(String path) { return ( path != null && (!(path.endsWith("/") || path.endsWith("\\"))) )? path + File.separator : path; } @@ -280,7 +326,7 @@ public class SolrResourceLoader implements ResourceLoader,Closeable } public String getConfigDir() { - return instanceDir + "conf" + File.separator; + return instanceDir.resolve("conf").toString(); } public String getDataDir() { @@ -316,6 +362,16 @@ public class SolrResourceLoader implements ResourceLoader,Closeable public InputStream openConfig(String name) throws IOException { return openResource(name); } + + private Path checkPathIsSafe(Path pathToCheck) throws IOException { + if (Boolean.getBoolean("solr.allow.unsafe.resourceloading")) + return pathToCheck; + pathToCheck = pathToCheck.normalize(); + if (pathToCheck.startsWith(instanceDir)) + return pathToCheck; + throw new IOException("File " + pathToCheck + " is outside resource loader dir " + instanceDir + + "; set -Dsolr.allow.unsafe.resourceloading=true to allow unsafe loading"); + } /** Opens any resource by its name. * By default, this will look in multiple locations to load the resource: @@ -327,52 +383,55 @@ public class SolrResourceLoader implements ResourceLoader,Closeable */ @Override public InputStream openResource(String resource) throws IOException { - InputStream is=null; - try { - File f0 = new File(resource), f = f0; - if (!f.isAbsolute()) { - // try $CWD/$configDir/$resource - f = new File(getConfigDir() + resource).getAbsoluteFile(); - } - boolean found = f.isFile() && f.canRead(); - if (!found) { // no success with $CWD/$configDir/$resource - f = f0.getAbsoluteFile(); - found = f.isFile() && f.canRead(); - } - // check that we don't escape instance dir - if (found) { - if (!Boolean.parseBoolean(System.getProperty("solr.allow.unsafe.resourceloading", "false"))) { - final URI instanceURI = new File(getInstanceDir()).getAbsoluteFile().toURI().normalize(); - final URI fileURI = f.toURI().normalize(); - if (instanceURI.relativize(fileURI) == fileURI) { - // no URI relativize possible, so they don't share same base folder - throw new IOException("For security reasons, SolrResourceLoader cannot load files from outside the instance's directory: " + f + - "; if you want to override this safety feature and you are sure about the consequences, you can pass the system property "+ - "-Dsolr.allow.unsafe.resourceloading=true to your JVM"); - } - } - // relativize() returned a relative, new URI, so we are fine! - return new FileInputStream(f); - } - // Delegate to the class loader (looking into $INSTANCE_DIR/lib jars). - // We need a ClassLoader-compatible (forward-slashes) path here! - is = classLoader.getResourceAsStream(resource.replace(File.separatorChar, '/')); - // This is a hack just for tests (it is not done in ZKResourceLoader)! - // -> the getConfigDir's path must not be absolute! - if (is == null && System.getProperty("jetty.testMode") != null && !new File(getConfigDir()).isAbsolute()) { - is = classLoader.getResourceAsStream((getConfigDir() + resource).replace(File.separatorChar, '/')); - } - } catch (IOException ioe) { - throw ioe; - } catch (Exception e) { - throw new IOException("Error opening " + resource, e); + + Path inConfigDir = getInstancePath().resolve("conf").resolve(resource); + if (Files.exists(inConfigDir) && Files.isReadable(inConfigDir)) { + return Files.newInputStream(checkPathIsSafe(inConfigDir)); } - if (is==null) { - throw new SolrResourceNotFoundException("Can't find resource '" + resource + "' in classpath or '" + new File(getConfigDir()).getAbsolutePath() + "'"); + + Path inInstanceDir = getInstancePath().resolve(resource); + if (Files.exists(inInstanceDir) && Files.isReadable(inInstanceDir)) { + return Files.newInputStream(checkPathIsSafe(inInstanceDir)); + } + + // Delegate to the class loader (looking into $INSTANCE_DIR/lib jars). + // We need a ClassLoader-compatible (forward-slashes) path here! + InputStream is = classLoader.getResourceAsStream(resource.replace(File.separatorChar, '/')); + + // This is a hack just for tests (it is not done in ZKResourceLoader)! + // TODO can we nuke this? + if (is == null && System.getProperty("jetty.testMode") != null) { + is = classLoader.getResourceAsStream(("conf/" + resource).replace(File.separatorChar, '/')); + } + + if (is == null) { + throw new SolrResourceNotFoundException("Can't find resource '" + resource + "' in classpath or '" + instanceDir + "'"); } return is; } + /** + * Report the location of a resource found by the resource loader + */ + public String resourceLocation(String resource) { + Path inConfigDir = getInstancePath().resolve("conf").resolve(resource); + if (Files.exists(inConfigDir) && Files.isReadable(inConfigDir)) + return inConfigDir.toAbsolutePath().normalize().toString(); + + Path inInstanceDir = getInstancePath().resolve(resource); + if (Files.exists(inInstanceDir) && Files.isReadable(inInstanceDir)) + return inInstanceDir.toAbsolutePath().normalize().toString(); + + try (InputStream is = classLoader.getResourceAsStream(resource.replace(File.separatorChar, '/'))) { + if (is != null) + return "classpath:" + resource; + } catch (IOException e) { + // ignore + } + + return resource; + } + /** * Accesses a resource by name and returns the (non comment) lines * containing data. @@ -699,7 +758,7 @@ public class SolrResourceLoader implements ResourceLoader,Closeable * @return A normalized solrhome * @see #normalizeDir(String) */ - public static String locateSolrHome() { + public static Path locateSolrHome() { String home = null; // Try JNDI @@ -729,13 +788,21 @@ public class SolrResourceLoader implements ResourceLoader,Closeable home = project + '/'; log.info(project + " home defaulted to '" + home + "' (could not find system property or JNDI)"); } - return normalizeDir( home ); + return Paths.get(home); } - - public String getInstanceDir() { + /** + * @return the instance path for this resource loader + */ + public Path getInstancePath() { return instanceDir; } + + /** Use {@link #getInstancePath()} */ + @Deprecated + public String getInstanceDir() { + return instanceDir.toString(); + } /** * Keep a list of classes that are allowed to implement each 'Aware' interface @@ -836,7 +903,4 @@ public class SolrResourceLoader implements ResourceLoader,Closeable } } - public String resolve(String pathToResolve) { - return Paths.get(instanceDir).resolve(pathToResolve).toString(); - } } diff --git a/solr/core/src/java/org/apache/solr/core/SolrXmlConfig.java b/solr/core/src/java/org/apache/solr/core/SolrXmlConfig.java index 6565a06af9e..a8b7145f068 100644 --- a/solr/core/src/java/org/apache/solr/core/SolrXmlConfig.java +++ b/solr/core/src/java/org/apache/solr/core/SolrXmlConfig.java @@ -21,10 +21,9 @@ import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpressionException; import java.io.ByteArrayInputStream; -import java.io.File; -import java.io.FileInputStream; import java.io.InputStream; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.nio.file.Path; import java.util.HashSet; import java.util.Map; @@ -99,16 +98,16 @@ public class SolrXmlConfig { return fillSolrSection(configBuilder, entries); } - public static NodeConfig fromFile(SolrResourceLoader loader, File configFile) { + public static NodeConfig fromFile(SolrResourceLoader loader, Path configFile) { - log.info("Loading container configuration from {}", configFile.getAbsolutePath()); + log.info("Loading container configuration from {}", configFile); - if (!configFile.exists()) { + if (!Files.exists(configFile)) { throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, - "solr.xml does not exist in " + configFile.getAbsolutePath() + " cannot start Solr"); + "solr.xml does not exist in " + configFile.getParent() + " cannot start Solr"); } - try (InputStream inputStream = new FileInputStream(configFile)) { + try (InputStream inputStream = Files.newInputStream(configFile)) { return fromInputStream(loader, inputStream); } catch (SolrException exc) { throw exc; @@ -136,13 +135,13 @@ public class SolrXmlConfig { } } - public static NodeConfig fromSolrHome(SolrResourceLoader loader, String solrHome) { - return fromFile(loader, new File(solrHome, SOLR_XML_FILE)); + public static NodeConfig fromSolrHome(SolrResourceLoader loader, Path solrHome) { + return fromFile(loader, solrHome.resolve(SOLR_XML_FILE)); } public static NodeConfig fromSolrHome(Path solrHome) { - SolrResourceLoader loader = new SolrResourceLoader(solrHome.toString()); - return fromSolrHome(loader, solrHome.toString()); + SolrResourceLoader loader = new SolrResourceLoader(solrHome); + return fromSolrHome(loader, solrHome); } private static void checkForIllegalConfig(Config config) { diff --git a/solr/core/src/java/org/apache/solr/handler/admin/CoreAdminHandler.java b/solr/core/src/java/org/apache/solr/handler/admin/CoreAdminHandler.java index 7c6c1ba1c9c..3f64ed75d68 100644 --- a/solr/core/src/java/org/apache/solr/handler/admin/CoreAdminHandler.java +++ b/solr/core/src/java/org/apache/solr/handler/admin/CoreAdminHandler.java @@ -19,18 +19,7 @@ package org.apache.solr.handler.admin; import java.io.File; import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.Iterator; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Locale; -import java.util.Map; -import java.util.Properties; +import java.util.*; import java.util.concurrent.ExecutorService; import java.util.concurrent.Future; @@ -1191,7 +1180,7 @@ public class CoreAdminHandler extends RequestHandlerBase { try (SolrCore core = cores.getCore(cname)) { if (core != null) { info.add(NAME, core.getName()); - info.add("instanceDir", normalizePath(core.getResourceLoader().getInstanceDir())); + info.add("instanceDir", core.getResourceLoader().getInstancePath().toString()); info.add("dataDir", normalizePath(core.getDataDir())); info.add("config", core.getConfigResource()); info.add("schema", core.getSchemaResource()); diff --git a/solr/core/src/java/org/apache/solr/handler/admin/SystemInfoHandler.java b/solr/core/src/java/org/apache/solr/handler/admin/SystemInfoHandler.java index 73f49da0cdd..5608704a716 100644 --- a/solr/core/src/java/org/apache/solr/handler/admin/SystemInfoHandler.java +++ b/solr/core/src/java/org/apache/solr/handler/admin/SystemInfoHandler.java @@ -142,7 +142,7 @@ public class SystemInfoHandler extends RequestHandlerBase // Solr Home SimpleOrderedMap dirs = new SimpleOrderedMap<>(); dirs.add( "cwd" , new File( System.getProperty("user.dir")).getAbsolutePath() ); - dirs.add( "instance", new File( core.getResourceLoader().getInstanceDir() ).getAbsolutePath() ); + dirs.add("instance", core.getResourceLoader().getInstancePath().toString()); try { dirs.add( "data", core.getDirectoryFactory().normalize(core.getDataDir())); } catch (IOException e) { diff --git a/solr/core/src/java/org/apache/solr/response/TextResponseWriter.java b/solr/core/src/java/org/apache/solr/response/TextResponseWriter.java index 5bef8a5ccf0..3b9fea629a0 100644 --- a/solr/core/src/java/org/apache/solr/response/TextResponseWriter.java +++ b/solr/core/src/java/org/apache/solr/response/TextResponseWriter.java @@ -19,25 +19,30 @@ package org.apache.solr.response; import java.io.IOException; import java.io.Writer; -import java.util.*; +import java.nio.file.Path; +import java.util.Arrays; +import java.util.Calendar; +import java.util.Date; +import java.util.Iterator; +import java.util.Map; -import org.apache.solr.client.solrj.io.stream.TupleStream; -import org.apache.solr.client.solrj.io.Tuple; import org.apache.lucene.index.StorableField; import org.apache.lucene.index.StoredDocument; import org.apache.lucene.util.BytesRef; +import org.apache.solr.client.solrj.io.Tuple; +import org.apache.solr.client.solrj.io.stream.TupleStream; import org.apache.solr.common.EnumFieldValue; import org.apache.solr.common.SolrDocument; import org.apache.solr.common.SolrDocumentList; import org.apache.solr.common.util.Base64; -import org.apache.solr.util.DateFormatUtil; -import org.apache.solr.util.FastWriter; import org.apache.solr.common.util.NamedList; import org.apache.solr.request.SolrQueryRequest; import org.apache.solr.schema.IndexSchema; import org.apache.solr.schema.SchemaField; import org.apache.solr.search.DocList; import org.apache.solr.search.ReturnFields; +import org.apache.solr.util.DateFormatUtil; +import org.apache.solr.util.FastWriter; /** Base class for text-oriented response writers. * @@ -162,6 +167,8 @@ public abstract class TextResponseWriter { writeNamedList(name, (NamedList)val); } else if (val instanceof TupleStream) { writeTupleStream((TupleStream) val); + } else if (val instanceof Path) { + writeStr(name, ((Path) val).toAbsolutePath().toString(), true); } else if (val instanceof Iterable) { writeArray(name,((Iterable)val).iterator()); } else if (val instanceof Object[]) { diff --git a/solr/core/src/java/org/apache/solr/schema/IndexSchema.java b/solr/core/src/java/org/apache/solr/schema/IndexSchema.java index 40d63df0238..5d33f3e015d 100644 --- a/solr/core/src/java/org/apache/solr/schema/IndexSchema.java +++ b/solr/core/src/java/org/apache/solr/schema/IndexSchema.java @@ -17,28 +17,13 @@ package org.apache.solr.schema; -import java.io.IOException; -import java.io.Writer; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.Comparator; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Locale; -import java.util.Map; -import java.util.Set; -import java.util.SortedMap; -import java.util.SortedSet; -import java.util.TreeMap; -import java.util.TreeSet; -import java.util.regex.Pattern; - import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpressionException; +import java.io.IOException; +import java.io.Writer; +import java.util.*; +import java.util.regex.Pattern; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.DelegatingAnalyzerWrapper; @@ -53,7 +38,6 @@ import org.apache.lucene.index.StoredDocument; import org.apache.lucene.search.similarities.Similarity; import org.apache.lucene.uninverting.UninvertingReader; import org.apache.lucene.util.Version; -import org.apache.solr.cloud.CloudUtil; import org.apache.solr.common.SolrException; import org.apache.solr.common.SolrException.ErrorCode; import org.apache.solr.common.params.SolrParams; @@ -449,9 +433,6 @@ public class IndexSchema { } protected void readSchema(InputSource is) { - String resourcePath = CloudUtil.unifiedResourcePath(loader) + resourceName; - log.info("Reading Solr Schema from " + resourcePath); - try { // pass the config resource loader to avoid building an empty one for no reason: // in the current case though, the stream is valid so we wont load the resource by name @@ -597,13 +578,12 @@ public class IndexSchema { postReadInform(); } catch (SolrException e) { - throw new SolrException(ErrorCode.getErrorCode(e.code()), e.getMessage() + ". Schema file is " + - resourcePath, e); + throw new SolrException(ErrorCode.getErrorCode(e.code()), + "Can't load schema " + loader.resourceLocation(resourceName) + ": " + e.getMessage(), e); } catch(Exception e) { // unexpected exception... throw new SolrException(ErrorCode.SERVER_ERROR, - "Schema Parsing Failed: " + e.getMessage() + ". Schema file is " + resourcePath, - e); + "Can't load schema " + loader.resourceLocation(resourceName) + ": " + e.getMessage(), e); } // create the field analyzers diff --git a/solr/core/src/java/org/apache/solr/schema/ManagedIndexSchemaFactory.java b/solr/core/src/java/org/apache/solr/schema/ManagedIndexSchemaFactory.java index 160085d68a7..70290f4a8c6 100644 --- a/solr/core/src/java/org/apache/solr/schema/ManagedIndexSchemaFactory.java +++ b/solr/core/src/java/org/apache/solr/schema/ManagedIndexSchemaFactory.java @@ -16,6 +16,11 @@ package org.apache.solr.schema; * limitations under the License. */ +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; + import org.apache.commons.io.IOUtils; import org.apache.solr.cloud.ZkController; import org.apache.solr.cloud.ZkSolrResourceLoader; @@ -36,11 +41,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.xml.sax.InputSource; -import java.io.ByteArrayInputStream; -import java.io.File; -import java.io.IOException; -import java.io.InputStream; - /** Factory for ManagedIndexSchema */ public class ManagedIndexSchemaFactory extends IndexSchemaFactory implements SolrCoreAware { private static final Logger log = LoggerFactory.getLogger(ManagedIndexSchemaFactory.class); @@ -284,7 +284,7 @@ public class ManagedIndexSchemaFactory extends IndexSchemaFactory implements Sol + "nor under SolrConfig.getConfigDir() or the current directory." + " PLEASE REMOVE THIS FILE."); } else { - File upgradedSchemaFile = new File(nonManagedSchemaFile.getPath() + UPGRADED_SCHEMA_EXTENSION); + File upgradedSchemaFile = new File(nonManagedSchemaFile + UPGRADED_SCHEMA_EXTENSION); if (nonManagedSchemaFile.renameTo(upgradedSchemaFile)) { // Set the resource name to the managed schema so that the CoreAdminHandler returns a findable filename schema.setResourceName(managedSchemaResourceName); @@ -309,25 +309,10 @@ public class ManagedIndexSchemaFactory extends IndexSchemaFactory implements Sol *@return the File for the named resource, or null if it can't be found */ private File locateConfigFile(String resource) { - File located = null; - File file = new File(resource); - if (file.isAbsolute()) { - if (file.isFile() && file.canRead()) { - located = file; - } - } else { - // try $configDir/$resource - File fileUnderConfigDir = new File(config.getResourceLoader().getConfigDir() + resource); - if (fileUnderConfigDir.isFile() && fileUnderConfigDir.canRead()) { - located = fileUnderConfigDir; - } else { - // no success with $configDir/$resource - try $CWD/$resource - if (file.isFile() && file.canRead()) { - located = file; - } - } - } - return located; + String location = config.getResourceLoader().resourceLocation(resource); + if (location.equals(resource) || location.startsWith("classpath:")) + return null; + return new File(location); } /** diff --git a/solr/core/src/java/org/apache/solr/servlet/SolrDispatchFilter.java b/solr/core/src/java/org/apache/solr/servlet/SolrDispatchFilter.java index 10b6f95c812..93975eb6f15 100644 --- a/solr/core/src/java/org/apache/solr/servlet/SolrDispatchFilter.java +++ b/solr/core/src/java/org/apache/solr/servlet/SolrDispatchFilter.java @@ -26,6 +26,8 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.ByteArrayInputStream; import java.io.IOException; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.Properties; import java.util.concurrent.atomic.AtomicBoolean; @@ -102,11 +104,10 @@ public class SolrDispatchFilter extends BaseSolrFilter { extraProperties = new Properties(); String solrHome = (String) config.getServletContext().getAttribute(SOLRHOME_ATTRIBUTE); - if (solrHome == null) - solrHome = SolrResourceLoader.locateSolrHome(); ExecutorUtil.addThreadLocalProvider(SolrRequestInfo.getInheritableThreadLocalProvider()); - this.cores = createCoreContainer(solrHome, extraProperties); + this.cores = createCoreContainer(solrHome == null ? SolrResourceLoader.locateSolrHome() : Paths.get(solrHome), + extraProperties); this.httpClient = cores.getUpdateShardHandler().getHttpClient(); log.info("user.dir=" + System.getProperty("user.dir")); } @@ -126,7 +127,7 @@ public class SolrDispatchFilter extends BaseSolrFilter { * Override this to change CoreContainer initialization * @return a CoreContainer to hold this server's cores */ - protected CoreContainer createCoreContainer(String solrHome, Properties extraProperties) { + protected CoreContainer createCoreContainer(Path solrHome, Properties extraProperties) { NodeConfig nodeConfig = loadNodeConfig(solrHome, extraProperties); cores = new CoreContainer(nodeConfig, extraProperties, true); cores.load(); @@ -138,7 +139,7 @@ public class SolrDispatchFilter extends BaseSolrFilter { * This may also be used by custom filters to load relevant configuration. * @return the NodeConfig */ - public static NodeConfig loadNodeConfig(String solrHome, Properties nodeProperties) { + public static NodeConfig loadNodeConfig(Path solrHome, Properties nodeProperties) { SolrResourceLoader loader = new SolrResourceLoader(solrHome, null, nodeProperties); if (!StringUtils.isEmpty(System.getProperty("solr.solrxml.location"))) { @@ -159,7 +160,7 @@ public class SolrDispatchFilter extends BaseSolrFilter { } log.info("Loading solr.xml from SolrHome (not found in ZooKeeper)"); } - return SolrXmlConfig.fromSolrHome(loader, loader.getInstanceDir()); + return SolrXmlConfig.fromSolrHome(loader, loader.getInstancePath()); } public CoreContainer getCores() { diff --git a/solr/core/src/test/org/apache/solr/analysis/TestWordDelimiterFilterFactory.java b/solr/core/src/test/org/apache/solr/analysis/TestWordDelimiterFilterFactory.java index 1464364fc78..fc9aec19304 100644 --- a/solr/core/src/test/org/apache/solr/analysis/TestWordDelimiterFilterFactory.java +++ b/solr/core/src/test/org/apache/solr/analysis/TestWordDelimiterFilterFactory.java @@ -17,17 +17,15 @@ package org.apache.solr.analysis; * limitations under the License. */ -import java.io.StringReader; import java.util.HashMap; import java.util.Map; import org.apache.lucene.analysis.BaseTokenStreamTestCase; -import org.apache.lucene.analysis.MockTokenizer; import org.apache.lucene.analysis.TokenStream; -import org.apache.lucene.util.Version; -import org.apache.solr.SolrTestCaseJ4; import org.apache.lucene.analysis.miscellaneous.WordDelimiterFilterFactory; import org.apache.lucene.analysis.util.ResourceLoader; +import org.apache.lucene.util.Version; +import org.apache.solr.SolrTestCaseJ4; import org.apache.solr.core.SolrResourceLoader; import org.junit.BeforeClass; import org.junit.Test; @@ -199,7 +197,7 @@ public class TestWordDelimiterFilterFactory extends SolrTestCaseJ4 { @Test public void testCustomTypes() throws Exception { String testText = "I borrowed $5,400.00 at 25% interest-rate"; - ResourceLoader loader = new SolrResourceLoader("solr/collection1"); + ResourceLoader loader = new SolrResourceLoader(TEST_PATH().resolve("collection1")); Map args = new HashMap<>(); args.put("luceneMatchVersion", Version.LATEST.toString()); args.put("generateWordParts", "1"); diff --git a/solr/core/src/test/org/apache/solr/client/solrj/embedded/TestEmbeddedSolrServerConstructors.java b/solr/core/src/test/org/apache/solr/client/solrj/embedded/TestEmbeddedSolrServerConstructors.java index 255800f0af9..59fd6afbe89 100644 --- a/solr/core/src/test/org/apache/solr/client/solrj/embedded/TestEmbeddedSolrServerConstructors.java +++ b/solr/core/src/test/org/apache/solr/client/solrj/embedded/TestEmbeddedSolrServerConstructors.java @@ -17,6 +17,10 @@ package org.apache.solr.client.solrj.embedded; * limitations under the License. */ +import java.io.IOException; +import java.nio.file.Path; +import java.nio.file.Paths; + import org.apache.solr.SolrTestCaseJ4; import org.apache.solr.client.solrj.SolrQuery; import org.apache.solr.client.solrj.request.CoreAdminRequest; @@ -25,10 +29,6 @@ import org.apache.solr.core.NodeConfig; import org.apache.solr.core.SolrResourceLoader; import org.junit.Test; -import java.io.IOException; -import java.nio.file.Path; -import java.nio.file.Paths; - public class TestEmbeddedSolrServerConstructors extends SolrTestCaseJ4 { @Test @@ -43,7 +43,7 @@ public class TestEmbeddedSolrServerConstructors extends SolrTestCaseJ4 { public void testNodeConfigConstructor() throws Exception { Path path = createTempDir(); - SolrResourceLoader loader = new SolrResourceLoader(path.toString()); + SolrResourceLoader loader = new SolrResourceLoader(path); NodeConfig config = new NodeConfig.NodeConfigBuilder("testnode", loader) .setConfigSetBaseDirectory(Paths.get(TEST_HOME()).resolve("configsets").toString()) .build(); diff --git a/solr/core/src/test/org/apache/solr/cloud/CollectionsAPIDistributedZkTest.java b/solr/core/src/test/org/apache/solr/cloud/CollectionsAPIDistributedZkTest.java index 25d3ba3977a..8af6c83ac83 100644 --- a/solr/core/src/test/org/apache/solr/cloud/CollectionsAPIDistributedZkTest.java +++ b/solr/core/src/test/org/apache/solr/cloud/CollectionsAPIDistributedZkTest.java @@ -909,14 +909,12 @@ public class CollectionsAPIDistributedZkTest extends AbstractFullDistribZkTestBa for (SolrCore core : theCores) { // look for core props file - assertTrue("Could not find expected core.properties file", - new File((String) core.getStatistics().get("instanceDir"), - "core.properties").exists()); + Path instancedir = (Path) core.getStatistics().get("instanceDir"); + assertTrue("Could not find expected core.properties file", Files.exists(instancedir.resolve("core.properties"))); Path expected = Paths.get(jetty.getSolrHome()).toAbsolutePath().resolve("cores").resolve(core.getName()); - Path reported = Paths.get((String) core.getStatistics().get("instanceDir")); - assertTrue("Expected: " + expected + "\nFrom core stats: " + reported, Files.isSameFile(expected, reported)); + assertTrue("Expected: " + expected + "\nFrom core stats: " + instancedir, Files.isSameFile(expected, instancedir)); } } diff --git a/solr/core/src/test/org/apache/solr/cloud/SolrXmlInZkTest.java b/solr/core/src/test/org/apache/solr/cloud/SolrXmlInZkTest.java index ff672213bf2..49177a3ab09 100644 --- a/solr/core/src/test/org/apache/solr/cloud/SolrXmlInZkTest.java +++ b/solr/core/src/test/org/apache/solr/cloud/SolrXmlInZkTest.java @@ -16,8 +16,12 @@ package org.apache.solr.cloud; * the License. */ -import com.carrotsearch.randomizedtesting.rules.SystemPropertiesRestoreRule; +import java.io.File; +import java.nio.charset.StandardCharsets; +import java.nio.file.Path; +import java.util.Properties; +import com.carrotsearch.randomizedtesting.rules.SystemPropertiesRestoreRule; import org.apache.commons.io.FileUtils; import org.apache.solr.SolrTestCaseJ4; import org.apache.solr.common.SolrException; @@ -25,16 +29,11 @@ import org.apache.solr.common.cloud.SolrZkClient; import org.apache.solr.common.cloud.ZkStateReader; import org.apache.solr.core.NodeConfig; import org.apache.solr.servlet.SolrDispatchFilter; -import org.junit.After; import org.junit.Rule; import org.junit.Test; import org.junit.rules.RuleChain; import org.junit.rules.TestRule; -import java.io.File; -import java.nio.charset.StandardCharsets; -import java.util.Properties; - public class SolrXmlInZkTest extends SolrTestCaseJ4 { @Rule @@ -51,11 +50,11 @@ public class SolrXmlInZkTest extends SolrTestCaseJ4 { private NodeConfig cfg; private void setUpZkAndDiskXml(boolean toZk, boolean leaveOnLocal) throws Exception { - File tmpDir = createTempDir().toFile(); - File solrHome = new File(tmpDir, "home"); - copyMinConf(new File(solrHome, "myCollect")); + Path tmpDir = createTempDir(); + Path solrHome = tmpDir.resolve("home"); + copyMinConf(new File(solrHome.toFile(), "myCollect")); if (leaveOnLocal) { - FileUtils.copyFile(new File(SolrTestCaseJ4.TEST_HOME(), "solr-stress-new.xml"), new File(solrHome, "solr.xml")); + FileUtils.copyFile(new File(SolrTestCaseJ4.TEST_HOME(), "solr-stress-new.xml"), new File(solrHome.toFile(), "solr.xml")); } ignoreException("No UpdateLog found - cannot sync"); @@ -63,8 +62,7 @@ public class SolrXmlInZkTest extends SolrTestCaseJ4 { System.setProperty("zkClientTimeout", "8000"); - zkDir = tmpDir.getAbsolutePath() + File.separator - + "zookeeper" + System.nanoTime() + "/server1/data"; + zkDir = tmpDir.resolve("zookeeper" + System.nanoTime()).resolve("server1").resolve("data").toString(); zkServer = new ZkTestServer(zkDir); zkServer.run(); System.setProperty("zkHost", zkServer.getZkAddress()); @@ -86,7 +84,7 @@ public class SolrXmlInZkTest extends SolrTestCaseJ4 { props.setProperty("solr.test.sys.prop1", "propone"); props.setProperty("solr.test.sys.prop2", "proptwo"); - cfg = SolrDispatchFilter.loadNodeConfig(solrHome.getAbsolutePath(), props); + cfg = SolrDispatchFilter.loadNodeConfig(solrHome, props); log.info("####SETUP_END " + getTestName()); } diff --git a/solr/core/src/test/org/apache/solr/cloud/TestLeaderElectionZkExpiry.java b/solr/core/src/test/org/apache/solr/cloud/TestLeaderElectionZkExpiry.java index 66f8b9020ca..dc60fd2f8a4 100644 --- a/solr/core/src/test/org/apache/solr/cloud/TestLeaderElectionZkExpiry.java +++ b/solr/core/src/test/org/apache/solr/cloud/TestLeaderElectionZkExpiry.java @@ -17,6 +17,11 @@ package org.apache.solr.cloud; * limitations under the License. */ +import java.nio.file.Path; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.TimeUnit; + import org.apache.solr.SolrTestCaseJ4; import org.apache.solr.common.cloud.SolrZkClient; import org.apache.solr.core.CloudConfig; @@ -25,10 +30,6 @@ import org.apache.solr.core.CoreDescriptor; import org.apache.zookeeper.KeeperException; import org.junit.Test; -import java.util.Collections; -import java.util.List; -import java.util.concurrent.TimeUnit; - public class TestLeaderElectionZkExpiry extends SolrTestCaseJ4 { public static final String SOLRXML = ""; private static final int MAX_NODES = 16; @@ -37,7 +38,7 @@ public class TestLeaderElectionZkExpiry extends SolrTestCaseJ4 { @Test public void testLeaderElectionWithZkExpiry() throws Exception { String zkDir = createTempDir("zkData").toFile().getAbsolutePath(); - String ccDir = createTempDir("testLeaderElectionWithZkExpiry-solr").toFile().getAbsolutePath(); + Path ccDir = createTempDir("testLeaderElectionWithZkExpiry-solr"); CoreContainer cc = createCoreContainer(ccDir, SOLRXML); final ZkTestServer server = new ZkTestServer(zkDir); server.setTheTickTime(1000); diff --git a/solr/core/src/test/org/apache/solr/cloud/TestZkChroot.java b/solr/core/src/test/org/apache/solr/cloud/TestZkChroot.java index cc492b46b97..fc1882a0740 100644 --- a/solr/core/src/test/org/apache/solr/cloud/TestZkChroot.java +++ b/solr/core/src/test/org/apache/solr/cloud/TestZkChroot.java @@ -17,7 +17,8 @@ package org.apache.solr.cloud; * limitations under the License. */ -import java.io.File; +import java.nio.file.Path; +import java.nio.file.Paths; import org.apache.solr.SolrJettyTestBase; import org.apache.solr.SolrTestCaseJ4; @@ -34,7 +35,7 @@ import org.slf4j.LoggerFactory; public class TestZkChroot extends SolrTestCaseJ4 { protected static Logger log = LoggerFactory.getLogger(TestZkChroot.class); protected CoreContainer cores = null; - private String home; + private Path home; protected ZkTestServer zkServer; protected String zkDir; @@ -47,7 +48,7 @@ public class TestZkChroot extends SolrTestCaseJ4 { zkDir = createTempDir("zkData").toFile().getAbsolutePath(); zkServer = new ZkTestServer(zkDir); zkServer.run(); - home = SolrJettyTestBase.legacyExampleCollection1SolrHome(); + home = Paths.get(SolrJettyTestBase.legacyExampleCollection1SolrHome()); } @@ -79,7 +80,7 @@ public class TestZkChroot extends SolrTestCaseJ4 { SolrZkClient zkClient2 = null; try { - cores = CoreContainer.createAndLoad(home, new File(home, "solr.xml")); + cores = CoreContainer.createAndLoad(home); zkClient = cores.getZkController().getZkClient(); assertTrue(zkClient.exists("/clusterstate.json", true)); @@ -110,7 +111,7 @@ public class TestZkChroot extends SolrTestCaseJ4 { AbstractZkTestCase.TIMEOUT); assertFalse("Path '" + chroot + "' should not exist before the test", zkClient.exists(chroot, true)); - cores = CoreContainer.createAndLoad(home, new File(home, "solr.xml")); + cores = CoreContainer.createAndLoad(home); fail("There should be a zk exception, as the initial path doesn't exist"); } catch (ZooKeeperException e) { // expected @@ -138,7 +139,7 @@ public class TestZkChroot extends SolrTestCaseJ4 { AbstractZkTestCase.TIMEOUT); assertFalse("Path '" + chroot + "' should not exist before the test", zkClient.exists(chroot, true)); - cores = CoreContainer.createAndLoad(home, new File(home, "solr.xml")); + cores = CoreContainer.createAndLoad(home); assertTrue( "solrconfig.xml should have been uploaded to zk to the correct config directory", zkClient.exists(chroot + ZkConfigManager.CONFIGS_ZKNODE + "/" @@ -164,7 +165,7 @@ public class TestZkChroot extends SolrTestCaseJ4 { assertTrue(zkClient.exists(chroot, true)); assertFalse(zkClient.exists(chroot + "/clusterstate.json", true)); - cores = CoreContainer.createAndLoad(home, new File(home, "solr.xml")); + cores = CoreContainer.createAndLoad(home); assertTrue(zkClient.exists(chroot + "/clusterstate.json", true)); } finally { if (cores != null) cores.shutdown(); diff --git a/solr/core/src/test/org/apache/solr/core/ResourceLoaderTest.java b/solr/core/src/test/org/apache/solr/core/ResourceLoaderTest.java index 9a1e44d8ad2..decb9d2f4a3 100644 --- a/solr/core/src/test/org/apache/solr/core/ResourceLoaderTest.java +++ b/solr/core/src/test/org/apache/solr/core/ResourceLoaderTest.java @@ -17,27 +17,12 @@ package org.apache.solr.core; -import junit.framework.Assert; - -import org.apache.lucene.analysis.TokenStream; -import org.apache.lucene.analysis.core.KeywordTokenizerFactory; -import org.apache.lucene.analysis.ngram.NGramFilterFactory; -import org.apache.lucene.util.IOUtils; -import org.apache.solr.SolrTestCaseJ4; -import org.apache.solr.common.SolrException; -import org.apache.solr.handler.admin.LukeRequestHandler; -import org.apache.solr.handler.component.FacetComponent; -import org.apache.solr.response.JSONResponseWriter; -import org.apache.lucene.analysis.util.ResourceLoaderAware; -import org.apache.lucene.analysis.util.TokenFilterFactory; -import org.apache.solr.util.plugin.SolrCoreAware; - -import java.io.File; -import java.io.FileFilter; -import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.nio.charset.CharacterCodingException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Arrays; import java.util.HashMap; import java.util.List; @@ -45,46 +30,46 @@ import java.util.Map; import java.util.jar.JarEntry; import java.util.jar.JarOutputStream; +import junit.framework.Assert; +import org.apache.lucene.analysis.TokenStream; +import org.apache.lucene.analysis.core.KeywordTokenizerFactory; +import org.apache.lucene.analysis.ngram.NGramFilterFactory; +import org.apache.lucene.analysis.util.ResourceLoaderAware; +import org.apache.lucene.analysis.util.TokenFilterFactory; +import org.apache.solr.SolrTestCaseJ4; +import org.apache.solr.common.SolrException; +import org.apache.solr.handler.admin.LukeRequestHandler; +import org.apache.solr.handler.component.FacetComponent; +import org.apache.solr.response.JSONResponseWriter; +import org.apache.solr.util.plugin.SolrCoreAware; + import static org.apache.solr.core.SolrResourceLoader.assertAwareCompatibility; +import static org.hamcrest.core.Is.is; + +public class ResourceLoaderTest extends SolrTestCaseJ4 { -public class ResourceLoaderTest extends SolrTestCaseJ4 -{ public void testInstanceDir() throws Exception { - SolrResourceLoader loader = new SolrResourceLoader(null); - String instDir = loader.getInstanceDir(); - assertTrue(instDir + " is not equal to " + "solr/", instDir.equals("solr/") == true); - loader.close(); - - loader = new SolrResourceLoader("solr"); - instDir = loader.getInstanceDir(); - assertTrue(instDir + " is not equal to " + "solr/", instDir.equals("solr" + File.separator) == true); - loader.close(); - } - - public void testEscapeInstanceDir() throws Exception { - File temp = createTempDir("testEscapeInstanceDir").toFile(); - try { - temp.mkdirs(); - new File(temp, "dummy.txt").createNewFile(); - File instanceDir = new File(temp, "instance"); - instanceDir.mkdir(); - new File(instanceDir, "conf").mkdir(); - SolrResourceLoader loader = new SolrResourceLoader(instanceDir.getAbsolutePath()); - try { - loader.openResource("../../dummy.txt").close(); - fail(); - } catch (IOException ioe) { - assertTrue(ioe.getMessage().startsWith("For security reasons, SolrResourceLoader")); - } - loader.close(); - } finally { - IOUtils.rm(temp.toPath()); + try (SolrResourceLoader loader = new SolrResourceLoader()) { + assertThat(loader.getInstancePath(), is(Paths.get("solr").toAbsolutePath())); } } - public void testAwareCompatibility() throws Exception - { - SolrResourceLoader loader = new SolrResourceLoader( "." ); + public void testEscapeInstanceDir() throws Exception { + + Path temp = createTempDir("testEscapeInstanceDir"); + Files.write(temp.resolve("dummy.txt"), new byte[]{}); + Path instanceDir = temp.resolve("instance"); + Files.createDirectories(instanceDir.resolve("conf")); + try (SolrResourceLoader loader = new SolrResourceLoader(instanceDir)) { + loader.openResource("../../dummy.txt").close(); + fail(); + } catch (IOException ioe) { + assertTrue(ioe.getMessage().contains("is outside resource loader dir")); + } + + } + + public void testAwareCompatibility() throws Exception { Class clazz = ResourceLoaderAware.class; // Check ResourceLoaderAware valid objects @@ -126,13 +111,12 @@ public class ResourceLoaderTest extends SolrTestCaseJ4 } catch( SolrException ex ) { } // OK } - - loader.close(); + } public void testBOMMarkers() throws Exception { final String fileWithBom = "stopwithbom.txt"; - SolrResourceLoader loader = new SolrResourceLoader("solr/collection1"); + SolrResourceLoader loader = new SolrResourceLoader(TEST_PATH().resolve("collection1")); // preliminary sanity check InputStream bomStream = loader.openResource(fileWithBom); @@ -160,7 +144,7 @@ public class ResourceLoaderTest extends SolrTestCaseJ4 public void testWrongEncoding() throws Exception { String wrongEncoding = "stopwordsWrongEncoding.txt"; - SolrResourceLoader loader = new SolrResourceLoader("solr/collection1"); + SolrResourceLoader loader = new SolrResourceLoader(TEST_PATH().resolve("collection1")); // ensure we get our exception try { loader.getLines(wrongEncoding); @@ -172,48 +156,41 @@ public class ResourceLoaderTest extends SolrTestCaseJ4 } public void testClassLoaderLibs() throws Exception { - File tmpRoot = createTempDir("testClassLoaderLibs").toFile(); + Path tmpRoot = createTempDir("testClassLoaderLibs"); - File lib = new File(tmpRoot, "lib"); - lib.mkdirs(); + Path lib = tmpRoot.resolve("lib"); + Files.createDirectories(lib); - JarOutputStream jar1 = new JarOutputStream(new FileOutputStream(new File(lib, "jar1.jar"))); - jar1.putNextEntry(new JarEntry("aLibFile")); - jar1.closeEntry(); - jar1.close(); + try (JarOutputStream os = new JarOutputStream(Files.newOutputStream(lib.resolve("jar1.jar")))) { + os.putNextEntry(new JarEntry("aLibFile")); + os.closeEntry(); + } - File otherLib = new File(tmpRoot, "otherLib"); - otherLib.mkdirs(); + Path otherLib = tmpRoot.resolve("otherLib"); + Files.createDirectories(otherLib); - JarOutputStream jar2 = new JarOutputStream(new FileOutputStream(new File(otherLib, "jar2.jar"))); - jar2.putNextEntry(new JarEntry("explicitFile")); - jar2.closeEntry(); - jar2.close(); - JarOutputStream jar3 = new JarOutputStream(new FileOutputStream(new File(otherLib, "jar3.jar"))); - jar3.putNextEntry(new JarEntry("otherFile")); - jar3.closeEntry(); - jar3.close(); + try (JarOutputStream os = new JarOutputStream(Files.newOutputStream(otherLib.resolve("jar2.jar")))) { + os.putNextEntry(new JarEntry("explicitFile")); + os.closeEntry(); + } + try (JarOutputStream os = new JarOutputStream(Files.newOutputStream(otherLib.resolve("jar3.jar")))) { + os.putNextEntry(new JarEntry("otherFile")); + os.closeEntry(); + } - SolrResourceLoader loader = new SolrResourceLoader(tmpRoot.getAbsolutePath()); + SolrResourceLoader loader = new SolrResourceLoader(tmpRoot); // ./lib is accessible by default assertNotNull(loader.getClassLoader().getResource("aLibFile")); - // file filter works (and doesn't add other files in the same dir) - final File explicitFileJar = new File(otherLib, "jar2.jar").getAbsoluteFile(); - loader.addToClassLoader("otherLib", - new FileFilter() { - @Override - public boolean accept(File pathname) { - return pathname.equals(explicitFileJar); - } - }, false); + // add inidividual jars from other paths + loader.addToClassLoader(otherLib.resolve("jar2.jar").toUri().toURL()); + assertNotNull(loader.getClassLoader().getResource("explicitFile")); assertNull(loader.getClassLoader().getResource("otherFile")); - - // null file filter means accept all (making otherFile accessible) - loader.addToClassLoader("otherLib", null, false); + // add all jars from another path + loader.addToClassLoader(SolrResourceLoader.getURLs(otherLib)); assertNotNull(loader.getClassLoader().getResource("otherFile")); loader.close(); } @@ -231,9 +208,10 @@ public class ResourceLoaderTest extends SolrTestCaseJ4 } } - + + @SuppressWarnings("deprecation") public void testLoadDeprecatedFactory() throws Exception { - SolrResourceLoader loader = new SolrResourceLoader("solr/collection1"); + SolrResourceLoader loader = new SolrResourceLoader(Paths.get("solr/collection1")); // ensure we get our exception loader.newInstance(DeprecatedTokenFilterFactory.class.getName(), TokenFilterFactory.class, null, new Class[] { Map.class }, new Object[] { new HashMap() }); diff --git a/solr/core/src/test/org/apache/solr/core/TestArbitraryIndexDir.java b/solr/core/src/test/org/apache/solr/core/TestArbitraryIndexDir.java index 8b005b685c2..a975849af7a 100644 --- a/solr/core/src/test/org/apache/solr/core/TestArbitraryIndexDir.java +++ b/solr/core/src/test/org/apache/solr/core/TestArbitraryIndexDir.java @@ -16,6 +16,7 @@ */ package org.apache.solr.core; +import javax.xml.parsers.ParserConfigurationException; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; @@ -24,8 +25,7 @@ import java.io.Writer; import java.nio.charset.StandardCharsets; import java.util.Properties; -import javax.xml.parsers.ParserConfigurationException; - +import com.carrotsearch.randomizedtesting.rules.SystemPropertiesRestoreRule; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; @@ -35,56 +35,35 @@ import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.store.Directory; import org.apache.lucene.util.IOUtils; import org.apache.solr.common.SolrException; -import org.apache.solr.common.params.CommonParams; import org.apache.solr.handler.IndexFetcher; import org.apache.solr.util.AbstractSolrTestCase; -import org.apache.solr.util.TestHarness; -import org.junit.AfterClass; import org.junit.BeforeClass; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TestRule; import org.xml.sax.SAXException; /** * */ -public class TestArbitraryIndexDir extends AbstractSolrTestCase{ +public class TestArbitraryIndexDir extends AbstractSolrTestCase { + + @Rule + public TestRule testRules = new SystemPropertiesRestoreRule(); // TODO: fix this test to not require FSDirectory - static String savedFactory; + @BeforeClass public static void beforeClass() { // this test wants to start solr, and then open a separate indexwriter of its own on the same dir. System.setProperty("enable.update.log", "false"); // schema12 doesn't support _version_ - savedFactory = System.getProperty("solr.DirectoryFactory"); System.setProperty("solr.directoryFactory", "org.apache.solr.core.MockFSDirectoryFactory"); } - @AfterClass - public static void afterClass() { - if (savedFactory == null) { - System.clearProperty("solr.directoryFactory"); - } else { - System.setProperty("solr.directoryFactory", savedFactory); - } - } @Override public void setUp() throws Exception { super.setUp(); - - File tmpDataDir = createTempDir().toFile(); - - solrConfig = TestHarness.createConfig(getSolrHome(), "solrconfig.xml"); - h = new TestHarness( tmpDataDir.getAbsolutePath(), - solrConfig, - "schema12.xml"); - lrf = h.getRequestFactory - ("standard",0,20,CommonParams.VERSION,"2.2"); - } - - @Override - public void tearDown() throws Exception { - super.tearDown(); - + initCore("solrconfig.xml", "schema12.xml"); } @Test diff --git a/solr/core/src/test/org/apache/solr/core/TestConfig.java b/solr/core/src/test/org/apache/solr/core/TestConfig.java index 73f67433c2a..63af0c047f7 100644 --- a/solr/core/src/test/org/apache/solr/core/TestConfig.java +++ b/solr/core/src/test/org/apache/solr/core/TestConfig.java @@ -17,6 +17,10 @@ package org.apache.solr.core; +import javax.xml.xpath.XPathConstants; +import java.io.IOException; +import java.io.InputStream; + import org.apache.lucene.index.ConcurrentMergeScheduler; import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.index.TieredMergePolicy; @@ -31,10 +35,6 @@ import org.junit.Test; import org.w3c.dom.Node; import org.w3c.dom.NodeList; -import javax.xml.xpath.XPathConstants; -import java.io.IOException; -import java.io.InputStream; - public class TestConfig extends SolrTestCaseJ4 { @BeforeClass @@ -113,7 +113,7 @@ public class TestConfig extends SolrTestCaseJ4 { int numDefaultsTested = 0; int numNullDefaults = 0; - SolrConfig sc = new SolrConfig(new SolrResourceLoader("solr/collection1"), "solrconfig-defaults.xml", null); + SolrConfig sc = new SolrConfig(new SolrResourceLoader(TEST_PATH().resolve("collection1")), "solrconfig-defaults.xml", null); SolrIndexConfig sic = sc.indexConfig; ++numDefaultsTested; assertEquals("default useCompoundFile", false, sic.getUseCompoundFile()); @@ -151,7 +151,7 @@ public class TestConfig extends SolrTestCaseJ4 { // sanity check that sys propertis are working as expected public void testSanityCheckTestSysPropsAreUsed() throws Exception { - SolrConfig sc = new SolrConfig(new SolrResourceLoader("solr/collection1"), "solrconfig-basic.xml", null); + SolrConfig sc = new SolrConfig(new SolrResourceLoader(TEST_PATH().resolve("collection1")), "solrconfig-basic.xml", null); SolrIndexConfig sic = sc.indexConfig; assertEquals("ramBufferSizeMB sysprop", diff --git a/solr/core/src/test/org/apache/solr/core/TestConfigSetProperties.java b/solr/core/src/test/org/apache/solr/core/TestConfigSetProperties.java index 7dcbcc562a3..fd5e745f4b0 100644 --- a/solr/core/src/test/org/apache/solr/core/TestConfigSetProperties.java +++ b/solr/core/src/test/org/apache/solr/core/TestConfigSetProperties.java @@ -17,22 +17,23 @@ package org.apache.solr.core; * limitations under the License. */ +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Map; + import com.carrotsearch.randomizedtesting.rules.SystemPropertiesRestoreRule; import com.google.common.collect.ImmutableMap; -import java.util.Map; -import org.apache.commons.io.FileUtils; +import org.apache.solr.SolrTestCaseJ4; import org.apache.solr.common.SolrException; import org.apache.solr.common.SolrException.ErrorCode; import org.apache.solr.common.util.NamedList; -import org.apache.solr.SolrTestCaseJ4; import org.junit.Rule; import org.junit.Test; import org.junit.rules.RuleChain; import org.junit.rules.TestRule; import org.noggit.JSONUtil; -import java.io.File; - public class TestConfigSetProperties extends SolrTestCaseJ4 { @Rule @@ -80,14 +81,14 @@ public class TestConfigSetProperties extends SolrTestCaseJ4 { } private NamedList createConfigSetProps(String props) throws Exception { - File testDirectory = createTempDir().toFile(); + Path testDirectory = createTempDir(); String filename = "configsetprops.json"; if (props != null) { - File confDir = new File(testDirectory, "conf"); - FileUtils.forceMkdir(confDir); - FileUtils.write(new File(confDir, filename), new StringBuilder(props)); + Path confDir = testDirectory.resolve("conf"); + Files.createDirectories(confDir); + Files.write(confDir.resolve(filename), props.getBytes(StandardCharsets.UTF_8)); } - SolrResourceLoader loader = new SolrResourceLoader(testDirectory.getAbsolutePath()); + SolrResourceLoader loader = new SolrResourceLoader(testDirectory); return ConfigSetProperties.readFromResourceLoader(loader, filename); } } diff --git a/solr/core/src/test/org/apache/solr/core/TestConfigSets.java b/solr/core/src/test/org/apache/solr/core/TestConfigSets.java index 1baf1212095..ba226d6419f 100644 --- a/solr/core/src/test/org/apache/solr/core/TestConfigSets.java +++ b/solr/core/src/test/org/apache/solr/core/TestConfigSets.java @@ -19,6 +19,7 @@ package org.apache.solr.core; import java.io.File; import java.io.IOException; +import java.nio.file.Path; import java.nio.file.Paths; import com.carrotsearch.randomizedtesting.rules.SystemPropertiesRestoreRule; @@ -42,11 +43,11 @@ public class TestConfigSets extends SolrTestCaseJ4 { public static String solrxml = "${configsets:configsets}"; public CoreContainer setupContainer(String configSetsBaseDir) { - File testDirectory = createTempDir().toFile(); + Path testDirectory = createTempDir(); System.setProperty("configsets", configSetsBaseDir); - SolrResourceLoader loader = new SolrResourceLoader(testDirectory.getAbsolutePath()); + SolrResourceLoader loader = new SolrResourceLoader(testDirectory); CoreContainer container = new CoreContainer(SolrXmlConfig.fromString(loader, solrxml)); container.load(); @@ -55,7 +56,7 @@ public class TestConfigSets extends SolrTestCaseJ4 { @Test public void testDefaultConfigSetBasePathResolution() throws IOException { - try (SolrResourceLoader loader = new SolrResourceLoader(new File("/path/to/solr/home").getAbsolutePath())) { + try (SolrResourceLoader loader = new SolrResourceLoader(Paths.get("/path/to/solr/home"))) { NodeConfig config = SolrXmlConfig.fromString(loader, "configsets"); @@ -72,12 +73,13 @@ public class TestConfigSets extends SolrTestCaseJ4 { public void testConfigSetServiceFindsConfigSets() { CoreContainer container = null; try { - container = setupContainer(getFile("solr/configsets").getAbsolutePath()); - String testDirectory = new File(container.getResourceLoader().getInstanceDir()).getAbsolutePath(); + container = setupContainer(TEST_PATH().resolve("configsets").toString()); + Path testDirectory = container.getResourceLoader().getInstancePath(); - SolrCore core1 = container.create(new CoreDescriptor(container, "core1", testDirectory + "core1", "configSet", "configset-2")); + SolrCore core1 = container.create(new CoreDescriptor(container, "core1", testDirectory.resolve("core1").toString(), + "configSet", "configset-2")); assertThat(core1.getCoreDescriptor().getName(), is("core1")); - assertThat(core1.getDataDir(), is(testDirectory + "core1" + File.separator + "data" + File.separator)); + assertThat(Paths.get(core1.getDataDir()).toString(), is(testDirectory.resolve("core1").resolve("data").toString())); } finally { if (container != null) @@ -90,9 +92,9 @@ public class TestConfigSets extends SolrTestCaseJ4 { CoreContainer container = null; try { container = setupContainer(getFile("solr/configsets").getAbsolutePath()); - String testDirectory = container.getResourceLoader().getInstanceDir(); + Path testDirectory = container.getResourceLoader().getInstancePath(); - container.create(new CoreDescriptor(container, "core1", testDirectory + "/core1", "configSet", "nonexistent")); + container.create(new CoreDescriptor(container, "core1", testDirectory.resolve("core1").toString(), "configSet", "nonexistent")); fail("Expected core creation to fail"); } catch (Exception e) { @@ -116,7 +118,7 @@ public class TestConfigSets extends SolrTestCaseJ4 { String csd = configSetsDir.getAbsolutePath(); System.setProperty("configsets", csd); - SolrResourceLoader loader = new SolrResourceLoader(testDirectory.getAbsolutePath()); + SolrResourceLoader loader = new SolrResourceLoader(testDirectory.toPath()); CoreContainer container = new CoreContainer(SolrXmlConfig.fromString(loader, solrxml)); container.load(); diff --git a/solr/core/src/test/org/apache/solr/core/TestCoreContainer.java b/solr/core/src/test/org/apache/solr/core/TestCoreContainer.java index 3216edfc0ba..44c90a3246d 100644 --- a/solr/core/src/test/org/apache/solr/core/TestCoreContainer.java +++ b/solr/core/src/test/org/apache/solr/core/TestCoreContainer.java @@ -17,19 +17,6 @@ package org.apache.solr.core; -import org.apache.commons.io.FileUtils; -import org.apache.lucene.util.IOUtils; -import org.apache.solr.SolrTestCaseJ4; -import org.apache.solr.common.SolrException; -import org.apache.solr.handler.admin.CollectionsHandler; -import org.apache.solr.handler.admin.CoreAdminHandler; -import org.apache.solr.handler.admin.InfoHandler; -import org.apache.solr.handler.admin.ConfigSetsHandler; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Test; -import org.xml.sax.SAXParseException; - import java.io.File; import java.io.FileOutputStream; import java.nio.file.Path; @@ -42,6 +29,19 @@ import java.util.jar.JarEntry; import java.util.jar.JarOutputStream; import java.util.regex.Pattern; +import org.apache.commons.io.FileUtils; +import org.apache.lucene.util.IOUtils; +import org.apache.solr.SolrTestCaseJ4; +import org.apache.solr.common.SolrException; +import org.apache.solr.handler.admin.CollectionsHandler; +import org.apache.solr.handler.admin.ConfigSetsHandler; +import org.apache.solr.handler.admin.CoreAdminHandler; +import org.apache.solr.handler.admin.InfoHandler; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.xml.sax.SAXParseException; + import static org.hamcrest.CoreMatchers.not; import static org.hamcrest.CoreMatchers.nullValue; import static org.hamcrest.core.Is.is; @@ -75,7 +75,7 @@ public class TestCoreContainer extends SolrTestCaseJ4 { } private CoreContainer init(Path homeDirectory, String xml) throws Exception { - SolrResourceLoader loader = new SolrResourceLoader(homeDirectory.toString()); + SolrResourceLoader loader = new SolrResourceLoader(homeDirectory); CoreContainer ret = new CoreContainer(SolrXmlConfig.fromString(loader, xml)); ret.load(); return ret; @@ -213,7 +213,7 @@ public class TestCoreContainer extends SolrTestCaseJ4 { MockCoresLocator cl = new MockCoresLocator(); - SolrResourceLoader resourceLoader = new SolrResourceLoader(createTempDir().toString()); + SolrResourceLoader resourceLoader = new SolrResourceLoader(createTempDir()); System.setProperty("configsets", getFile("solr/configsets").getAbsolutePath()); @@ -469,7 +469,7 @@ public class TestCoreContainer extends SolrTestCaseJ4 { // init the CoreContainer with the mix of ok/bad cores MockCoresLocator cl = new MockCoresLocator(); - SolrResourceLoader resourceLoader = new SolrResourceLoader(createTempDir().toString()); + SolrResourceLoader resourceLoader = new SolrResourceLoader(createTempDir()); System.setProperty("configsets", getFile("solr/configsets").getAbsolutePath()); diff --git a/solr/core/src/test/org/apache/solr/core/TestCoreDiscovery.java b/solr/core/src/test/org/apache/solr/core/TestCoreDiscovery.java index e1d5e23fba7..971e9860163 100644 --- a/solr/core/src/test/org/apache/solr/core/TestCoreDiscovery.java +++ b/solr/core/src/test/org/apache/solr/core/TestCoreDiscovery.java @@ -457,7 +457,7 @@ public class TestCoreDiscovery extends SolrTestCaseJ4 { @Test public void testRootDirectoryResolution() { - SolrResourceLoader loader = new SolrResourceLoader(solrHomeDirectory.getAbsolutePath()); + SolrResourceLoader loader = new SolrResourceLoader(solrHomeDirectory.toPath()); NodeConfig config = SolrXmlConfig.fromString(loader, "relative"); assertThat(config.getCoreRootDirectory().toString(), containsString(solrHomeDirectory.getAbsolutePath())); diff --git a/solr/core/src/test/org/apache/solr/core/TestLazyCores.java b/solr/core/src/test/org/apache/solr/core/TestLazyCores.java index 1457c3823f0..2f15644865a 100644 --- a/solr/core/src/test/org/apache/solr/core/TestLazyCores.java +++ b/solr/core/src/test/org/apache/solr/core/TestLazyCores.java @@ -80,7 +80,7 @@ public class TestLazyCores extends SolrTestCaseJ4 { copyMinConf(new File(solrHomeDirectory, "collection" + idx)); } - SolrResourceLoader loader = new SolrResourceLoader(solrHomeDirectory.getAbsolutePath()); + SolrResourceLoader loader = new SolrResourceLoader(solrHomeDirectory.toPath()); NodeConfig config = new NodeConfig.NodeConfigBuilder("testNode", loader).setTransientCacheSize(4).build(); return createCoreContainer(config, testCores); } @@ -559,12 +559,8 @@ public class TestLazyCores extends SolrTestCaseJ4 { writeCustomConfig(coreName, min_config, bad_schema, rand_snip); } - // Write the solr.xml file. Cute how minimal it can be now.... - File solrXml = new File(solrHomeDirectory, "solr.xml"); - FileUtils.write(solrXml, "", Charsets.UTF_8.toString()); - - SolrResourceLoader loader = new SolrResourceLoader(solrHomeDirectory.getAbsolutePath()); - NodeConfig config = SolrXmlConfig.fromFile(loader, solrXml); + SolrResourceLoader loader = new SolrResourceLoader(solrHomeDirectory.toPath()); + NodeConfig config = SolrXmlConfig.fromString(loader, ""); // OK this should succeed, but at the end we should have recorded a series of errors. return createCoreContainer(config, new CorePropertiesLocator(config.getCoreRootDirectory())); diff --git a/solr/core/src/test/org/apache/solr/core/TestShardHandlerFactory.java b/solr/core/src/test/org/apache/solr/core/TestShardHandlerFactory.java index 849146800c7..f156c4431c8 100644 --- a/solr/core/src/test/org/apache/solr/core/TestShardHandlerFactory.java +++ b/solr/core/src/test/org/apache/solr/core/TestShardHandlerFactory.java @@ -17,19 +17,21 @@ package org.apache.solr.core; +import java.nio.file.Path; +import java.nio.file.Paths; + import org.apache.solr.SolrTestCaseJ4; import org.apache.solr.common.util.NamedList; import org.apache.solr.handler.component.ShardHandlerFactory; -import java.io.File; - /** * Tests specifying a custom ShardHandlerFactory */ public class TestShardHandlerFactory extends SolrTestCaseJ4 { public void testXML() throws Exception { - CoreContainer cc = CoreContainer.createAndLoad(TEST_HOME(), new File(TEST_HOME(), "solr-shardhandler.xml")); + Path home = Paths.get(TEST_HOME()); + CoreContainer cc = CoreContainer.createAndLoad(home, home.resolve("solr-shardhandler.xml")); ShardHandlerFactory factory = cc.getShardHandlerFactory(); assertTrue(factory instanceof MockShardHandlerFactory); NamedList args = ((MockShardHandlerFactory)factory).args; diff --git a/solr/core/src/test/org/apache/solr/core/TestSolrXml.java b/solr/core/src/test/org/apache/solr/core/TestSolrXml.java index 377389cf16c..bcf874e80a8 100644 --- a/solr/core/src/test/org/apache/solr/core/TestSolrXml.java +++ b/solr/core/src/test/org/apache/solr/core/TestSolrXml.java @@ -51,7 +51,7 @@ public class TestSolrXml extends SolrTestCaseJ4 { @BeforeClass public static void setupLoader() throws Exception { solrHome = createTempDir().toFile(); - loader = new SolrResourceLoader(solrHome.getAbsolutePath()); + loader = new SolrResourceLoader(solrHome.toPath()); } @AfterClass @@ -65,7 +65,7 @@ public class TestSolrXml extends SolrTestCaseJ4 { File testSrcRoot = new File(SolrTestCaseJ4.TEST_HOME()); FileUtils.copyFile(new File(testSrcRoot, "solr-50-all.xml"), new File(solrHome, "solr.xml")); - NodeConfig cfg = SolrXmlConfig.fromSolrHome(loader, solrHome.getAbsolutePath()); + NodeConfig cfg = SolrXmlConfig.fromSolrHome(loader, solrHome.toPath()); CloudConfig ccfg = cfg.getCloudConfig(); UpdateShardHandlerConfig ucfg = cfg.getUpdateShardHandlerConfig(); @@ -117,7 +117,7 @@ public class TestSolrXml extends SolrTestCaseJ4 { File testSrcRoot = new File(SolrTestCaseJ4.TEST_HOME()); FileUtils.copyFile(new File(testSrcRoot, "solr-50-all.xml"), new File(solrHome, "solr.xml")); - NodeConfig cfg = SolrXmlConfig.fromSolrHome(loader, solrHome.getAbsolutePath()); + NodeConfig cfg = SolrXmlConfig.fromSolrHome(loader, solrHome.toPath()); assertThat(cfg.getCoreRootDirectory().toString(), containsString("myCoreRoot")); assertEquals("solr host port", 8888, cfg.getCloudConfig().getSolrHostPort()); assertEquals("schema cache", false, cfg.hasSchemaCache()); diff --git a/solr/core/src/test/org/apache/solr/handler/admin/CoreAdminCreateDiscoverTest.java b/solr/core/src/test/org/apache/solr/handler/admin/CoreAdminCreateDiscoverTest.java index 1d686608d2e..e9e1938801b 100644 --- a/solr/core/src/test/org/apache/solr/handler/admin/CoreAdminCreateDiscoverTest.java +++ b/solr/core/src/test/org/apache/solr/handler/admin/CoreAdminCreateDiscoverTest.java @@ -21,6 +21,7 @@ import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; import java.util.Properties; import org.apache.commons.io.FileUtils; @@ -34,8 +35,6 @@ import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; -import java.nio.charset.StandardCharsets; - public class CoreAdminCreateDiscoverTest extends SolrTestCaseJ4 { private static File solrHomeDirectory = null; @@ -52,7 +51,7 @@ public class CoreAdminCreateDiscoverTest extends SolrTestCaseJ4 { solrHomeDirectory = createTempDir().toFile(); - setupNoCoreTest(solrHomeDirectory, null); + setupNoCoreTest(solrHomeDirectory.toPath(), null); admin = new CoreAdminHandler(h.getCoreContainer()); } diff --git a/solr/core/src/test/org/apache/solr/rest/TestManagedResource.java b/solr/core/src/test/org/apache/solr/rest/TestManagedResource.java index 92db0bb3d07..8f1860247d6 100644 --- a/solr/core/src/test/org/apache/solr/rest/TestManagedResource.java +++ b/solr/core/src/test/org/apache/solr/rest/TestManagedResource.java @@ -23,6 +23,7 @@ import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import java.io.StringReader; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; @@ -216,7 +217,7 @@ public class TestManagedResource extends SolrTestCaseJ4 { storageIO.storage.put(storedResourceId, new BytesRef(json(storedJson))); ManagedTestResource res = - new ManagedTestResource(resourceId, new SolrResourceLoader("./"), storageIO); + new ManagedTestResource(resourceId, new SolrResourceLoader(Paths.get("./")), storageIO); res.loadManagedDataAndNotify(observers); assertTrue("Observer was not notified by ManagedResource!", observer.wasNotified); @@ -282,7 +283,7 @@ public class TestManagedResource extends SolrTestCaseJ4 { storageIO.storage.put(storedResourceId, ser2bytes((Serializable)storedData)); CustomStorageFormatResource res = - new CustomStorageFormatResource(resourceId, new SolrResourceLoader("./"), storageIO); + new CustomStorageFormatResource(resourceId, new SolrResourceLoader(Paths.get("./")), storageIO); res.loadManagedDataAndNotify(observers); assertTrue("Observer was not notified by ManagedResource!", observer.wasNotified); diff --git a/solr/core/src/test/org/apache/solr/rest/TestManagedResourceStorage.java b/solr/core/src/test/org/apache/solr/rest/TestManagedResourceStorage.java index ffc203d6f36..de469512641 100644 --- a/solr/core/src/test/org/apache/solr/rest/TestManagedResourceStorage.java +++ b/solr/core/src/test/org/apache/solr/rest/TestManagedResourceStorage.java @@ -17,22 +17,23 @@ package org.apache.solr.rest; */ import java.io.File; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; -import org.apache.lucene.util.LuceneTestCase.Slow; import org.apache.lucene.util.LuceneTestCase.AwaitsFix; +import org.apache.lucene.util.LuceneTestCase.Slow; import org.apache.solr.cloud.AbstractZkTestCase; import org.apache.solr.common.cloud.SolrZkClient; import org.apache.solr.common.util.NamedList; import org.apache.solr.core.SolrResourceLoader; -import org.apache.solr.rest.ManagedResourceStorage.StorageIO; import org.apache.solr.rest.ManagedResourceStorage.FileStorageIO; -import org.apache.solr.rest.ManagedResourceStorage.ZooKeeperStorageIO; import org.apache.solr.rest.ManagedResourceStorage.JsonStorage; +import org.apache.solr.rest.ManagedResourceStorage.StorageIO; +import org.apache.solr.rest.ManagedResourceStorage.ZooKeeperStorageIO; import org.junit.Test; /** @@ -51,7 +52,7 @@ public class TestManagedResourceStorage extends AbstractZkTestCase { // test using ZooKeeper assertTrue("Not using ZooKeeper", h.getCoreContainer().isZooKeeperAware()); SolrZkClient zkClient = h.getCoreContainer().getZkController().getZkClient(); - SolrResourceLoader loader = new SolrResourceLoader("./"); + SolrResourceLoader loader = new SolrResourceLoader(Paths.get("./")); // Solr unit tests can only write to their working directory due to // a custom Java Security Manager installed in the test environment NamedList initArgs = new NamedList<>(); @@ -71,7 +72,7 @@ public class TestManagedResourceStorage extends AbstractZkTestCase { @Test public void testFileBasedJsonStorage() throws Exception { File instanceDir = createTempDir("json-storage").toFile(); - SolrResourceLoader loader = new SolrResourceLoader(instanceDir.getAbsolutePath()); + SolrResourceLoader loader = new SolrResourceLoader(instanceDir.toPath()); try { NamedList initArgs = new NamedList<>(); String managedDir = instanceDir.getAbsolutePath() + File.separator + "managed"; diff --git a/solr/core/src/test/org/apache/solr/rest/TestRestManager.java b/solr/core/src/test/org/apache/solr/rest/TestRestManager.java index d5321333091..c45c14f40e2 100644 --- a/solr/core/src/test/org/apache/solr/rest/TestRestManager.java +++ b/solr/core/src/test/org/apache/solr/rest/TestRestManager.java @@ -17,6 +17,7 @@ package org.apache.solr.rest; */ import java.io.File; +import java.nio.file.Paths; import java.util.Arrays; import java.util.Locale; import java.util.Set; @@ -79,7 +80,7 @@ public class TestRestManager extends SolrRestletTestBase { public void testManagedResourceRegistrationAndInitialization() throws Exception { // first, we need to register some ManagedResources, which is done with the registry // provided by the SolrResourceLoader - SolrResourceLoader loader = new SolrResourceLoader("./"); + SolrResourceLoader loader = new SolrResourceLoader(Paths.get("./")); RestManager.Registry registry = loader.getManagedResourceRegistry(); assertNotNull("Expected a non-null RestManager.Registry from the SolrResourceLoader!", registry); @@ -229,7 +230,7 @@ public class TestRestManager extends SolrRestletTestBase { @Test public void testReloadFromPersistentStorage() throws Exception { - SolrResourceLoader loader = new SolrResourceLoader("./"); + SolrResourceLoader loader = new SolrResourceLoader(Paths.get("./")); File unitTestStorageDir = createTempDir("testRestManager").toFile(); assertTrue(unitTestStorageDir.getAbsolutePath()+" is not a directory!", unitTestStorageDir.isDirectory()); diff --git a/solr/core/src/test/org/apache/solr/schema/DateFieldTest.java b/solr/core/src/test/org/apache/solr/schema/DateFieldTest.java index 725b638c22c..95d004cfe98 100644 --- a/solr/core/src/test/org/apache/solr/schema/DateFieldTest.java +++ b/solr/core/src/test/org/apache/solr/schema/DateFieldTest.java @@ -18,6 +18,7 @@ package org.apache.solr.schema; import java.io.File; +import java.nio.file.Paths; import java.text.ParseException; import java.util.Collections; import java.util.Date; @@ -44,7 +45,7 @@ public class DateFieldTest extends SolrTestCaseJ4 { System.setProperty("solr.test.sys.prop1", "propone"); System.setProperty("solr.test.sys.prop2", "proptwo"); SolrConfig config = new SolrConfig - (new SolrResourceLoader(testInstanceDir), testConfHome + "solrconfig.xml", null); + (new SolrResourceLoader(Paths.get(testInstanceDir)), testConfHome + "solrconfig.xml", null); IndexSchema schema = IndexSchemaFactory.buildIndexSchema(testConfHome + "schema.xml", config); f = new TrieDateField(); f.init(schema, Collections.emptyMap()); diff --git a/solr/core/src/test/org/apache/solr/schema/OpenExchangeRatesOrgProviderTest.java b/solr/core/src/test/org/apache/solr/schema/OpenExchangeRatesOrgProviderTest.java index c0fee2109ed..8bb00f7d819 100644 --- a/solr/core/src/test/org/apache/solr/schema/OpenExchangeRatesOrgProviderTest.java +++ b/solr/core/src/test/org/apache/solr/schema/OpenExchangeRatesOrgProviderTest.java @@ -16,17 +16,17 @@ package org.apache.solr.schema; * limitations under the License. */ -import org.apache.solr.SolrTestCaseJ4; +import java.util.HashMap; +import java.util.Map; + import org.apache.lucene.analysis.util.ResourceLoader; +import org.apache.solr.SolrTestCaseJ4; import org.apache.solr.common.SolrException; import org.apache.solr.common.util.SuppressForbidden; import org.apache.solr.core.SolrResourceLoader; import org.junit.Before; import org.junit.Test; -import java.util.HashMap; -import java.util.Map; - /** * Tests currency field type. */ @@ -48,7 +48,7 @@ public class OpenExchangeRatesOrgProviderTest extends SolrTestCaseJ4 { mockParams.put(OpenExchangeRatesOrgProvider.PARAM_RATES_FILE_LOCATION, "open-exchange-rates.json"); oerp = new OpenExchangeRatesOrgProvider(); - loader = new SolrResourceLoader("solr/collection1"); + loader = new SolrResourceLoader(TEST_PATH().resolve("collection1")); } @Test diff --git a/solr/core/src/test/org/apache/solr/schema/PrimitiveFieldTypeTest.java b/solr/core/src/test/org/apache/solr/schema/PrimitiveFieldTypeTest.java index 5cc6e326994..fc4db6f0472 100644 --- a/solr/core/src/test/org/apache/solr/schema/PrimitiveFieldTypeTest.java +++ b/solr/core/src/test/org/apache/solr/schema/PrimitiveFieldTypeTest.java @@ -17,15 +17,15 @@ package org.apache.solr.schema; +import java.io.File; +import java.util.HashMap; +import java.util.Map; + import org.apache.solr.SolrTestCaseJ4; import org.apache.solr.core.SolrConfig; import org.apache.solr.core.SolrResourceLoader; import org.junit.Test; -import java.io.File; -import java.util.HashMap; -import java.util.Map; - /** * Tests that defaults are set for Primitive (non-analyzed) fields */ @@ -45,7 +45,7 @@ public class PrimitiveFieldTypeTest extends SolrTestCaseJ4 { System.setProperty("solr.allow.unsafe.resourceloading", "true"); initMap = new HashMap<>(); - config = new SolrConfig(new SolrResourceLoader("solr/collection1"), testConfHome + "solrconfig.xml", null); + config = new SolrConfig(new SolrResourceLoader(TEST_PATH().resolve("collection1")), testConfHome + "solrconfig.xml", null); } @Override diff --git a/solr/core/src/test/org/apache/solr/update/SolrIndexConfigTest.java b/solr/core/src/test/org/apache/solr/update/SolrIndexConfigTest.java index 07cbd7e59a2..6c5dc52de04 100644 --- a/solr/core/src/test/org/apache/solr/update/SolrIndexConfigTest.java +++ b/solr/core/src/test/org/apache/solr/update/SolrIndexConfigTest.java @@ -17,12 +17,9 @@ package org.apache.solr.update; * limitations under the License. */ -import java.io.File; -import java.io.IOException; +import java.nio.file.Path; import java.util.Map; -import javax.xml.parsers.ParserConfigurationException; - import org.apache.lucene.index.ConcurrentMergeScheduler; import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.index.SimpleMergedSegmentWarmer; @@ -34,7 +31,6 @@ import org.apache.solr.schema.IndexSchema; import org.apache.solr.schema.IndexSchemaFactory; import org.junit.BeforeClass; import org.junit.Test; -import org.xml.sax.SAXException; /** * Testcase for {@link SolrIndexConfig} @@ -48,7 +44,7 @@ public class SolrIndexConfigTest extends SolrTestCaseJ4 { initCore("solrconfig.xml","schema.xml"); } - private final String instanceDir = new File("solr", "collection1").getPath(); + private final Path instanceDir = TEST_PATH().resolve("collection1"); private final String solrConfigFileNameWarmer = "solrconfig-warmer.xml"; private final String solrConfigFileNameTieredMergePolicy = "solrconfig-tieredmergepolicy.xml"; diff --git a/solr/core/src/test/org/apache/solr/util/TestSystemIdResolver.java b/solr/core/src/test/org/apache/solr/util/TestSystemIdResolver.java index 10a7ffb06a8..dd0a213998f 100644 --- a/solr/core/src/test/org/apache/solr/util/TestSystemIdResolver.java +++ b/solr/core/src/test/org/apache/solr/util/TestSystemIdResolver.java @@ -17,14 +17,15 @@ package org.apache.solr.util; * limitations under the License. */ -import org.apache.solr.core.SolrResourceLoader; +import java.io.File; +import java.nio.file.Path; + +import org.apache.commons.io.IOUtils; import org.apache.lucene.analysis.util.ResourceLoader; import org.apache.lucene.util.LuceneTestCase; import org.apache.solr.SolrTestCaseJ4; - -import java.io.File; +import org.apache.solr.core.SolrResourceLoader; import org.xml.sax.InputSource; -import org.apache.commons.io.IOUtils; public class TestSystemIdResolver extends LuceneTestCase { @@ -48,8 +49,8 @@ public class TestSystemIdResolver extends LuceneTestCase { } public void testResolving() throws Exception { - final String testHome = SolrTestCaseJ4.getFile("solr/collection1").getParent(); - final ResourceLoader loader = new SolrResourceLoader(testHome + "/collection1", this.getClass().getClassLoader()); + final Path testHome = SolrTestCaseJ4.getFile("solr/collection1").getParentFile().toPath(); + final ResourceLoader loader = new SolrResourceLoader(testHome.resolve("collection1"), this.getClass().getClassLoader()); final SystemIdResolver resolver = new SystemIdResolver(loader); final String fileUri = new File(testHome+"/crazy-path-to-config.xml").toURI().toASCIIString(); diff --git a/solr/solrj/src/java/org/apache/solr/common/util/JavaBinCodec.java b/solr/solrj/src/java/org/apache/solr/common/util/JavaBinCodec.java index 37a80a6ebd7..b90fb7ea1dd 100644 --- a/solr/solrj/src/java/org/apache/solr/common/util/JavaBinCodec.java +++ b/solr/solrj/src/java/org/apache/solr/common/util/JavaBinCodec.java @@ -16,19 +16,28 @@ */ package org.apache.solr.common.util; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.ByteBuffer; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Date; +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + import org.apache.solr.common.EnumFieldValue; -import org.noggit.CharArr; import org.apache.solr.common.SolrDocument; import org.apache.solr.common.SolrDocumentList; import org.apache.solr.common.SolrInputDocument; import org.apache.solr.common.SolrInputField; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.util.*; -import java.util.Map.Entry; -import java.nio.ByteBuffer; +import org.noggit.CharArr; /** * The class is designed to optimaly serialize/deserialize any supported types in Solr response. As we know there are only a limited type of @@ -297,6 +306,10 @@ public class JavaBinCodec { writeIterator((Iterator) val); return true; } + if (val instanceof Path) { + writeStr(((Path) val).toAbsolutePath().toString()); + return true; + } if (val instanceof Iterable) { writeIterator(((Iterable) val).iterator()); return true; diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/embedded/AbstractEmbeddedSolrServerTestCase.java b/solr/solrj/src/test/org/apache/solr/client/solrj/embedded/AbstractEmbeddedSolrServerTestCase.java index c76728c44cd..2b8c1ae53b2 100644 --- a/solr/solrj/src/test/org/apache/solr/client/solrj/embedded/AbstractEmbeddedSolrServerTestCase.java +++ b/solr/solrj/src/test/org/apache/solr/client/solrj/embedded/AbstractEmbeddedSolrServerTestCase.java @@ -18,6 +18,7 @@ package org.apache.solr.client.solrj.embedded; */ import java.io.File; +import java.nio.file.Path; import org.apache.solr.SolrTestCaseJ4; import org.apache.solr.client.solrj.SolrClient; @@ -31,7 +32,7 @@ public abstract class AbstractEmbeddedSolrServerTestCase extends SolrTestCaseJ4 protected static Logger log = LoggerFactory.getLogger(AbstractEmbeddedSolrServerTestCase.class); - protected static final File SOLR_HOME = SolrTestCaseJ4.getFile("solrj/solr/shared"); + protected static final Path SOLR_HOME = getFile("solrj/solr/shared").toPath().toAbsolutePath(); protected CoreContainer cores = null; protected File tempDir; @@ -41,9 +42,9 @@ public abstract class AbstractEmbeddedSolrServerTestCase extends SolrTestCaseJ4 public void setUp() throws Exception { super.setUp(); - System.setProperty("solr.solr.home", SOLR_HOME.getAbsolutePath()); - System.setProperty("configSetBase", SolrTestCaseJ4.getFile("solrj/solr/configsets").getAbsolutePath()); - System.out.println("Solr home: " + SOLR_HOME.getAbsolutePath()); + System.setProperty("solr.solr.home", SOLR_HOME.toString()); + System.setProperty("configSetBase", SOLR_HOME.resolve("../configsets").normalize().toString()); + System.out.println("Solr home: " + SOLR_HOME.toString()); //The index is always stored within a temporary directory tempDir = createTempDir().toFile(); @@ -54,11 +55,13 @@ public abstract class AbstractEmbeddedSolrServerTestCase extends SolrTestCaseJ4 System.setProperty("dataDir2", dataDir2.getAbsolutePath()); System.setProperty("tempDir", tempDir.getAbsolutePath()); System.setProperty("tests.shardhandler.randomSeed", Long.toString(random().nextLong())); - cores = CoreContainer.createAndLoad(SOLR_HOME.getAbsolutePath(), getSolrXml()); + cores = CoreContainer.createAndLoad(SOLR_HOME, getSolrXml()); //cores.setPersistent(false); } - protected abstract File getSolrXml() throws Exception; + protected Path getSolrXml() throws Exception { + return SOLR_HOME.resolve("solr.xml"); + } @Override @After diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/embedded/TestEmbeddedSolrServer.java b/solr/solrj/src/test/org/apache/solr/client/solrj/embedded/TestEmbeddedSolrServer.java index a9a5e970cfa..b825b66334a 100644 --- a/solr/solrj/src/test/org/apache/solr/client/solrj/embedded/TestEmbeddedSolrServer.java +++ b/solr/solrj/src/test/org/apache/solr/client/solrj/embedded/TestEmbeddedSolrServer.java @@ -17,6 +17,10 @@ package org.apache.solr.client.solrj.embedded; * limitations under the License. */ +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + import com.carrotsearch.randomizedtesting.rules.SystemPropertiesRestoreRule; import junit.framework.Assert; import org.apache.solr.core.SolrCore; @@ -26,11 +30,6 @@ import org.junit.rules.TestRule; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.File; -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; - public class TestEmbeddedSolrServer extends AbstractEmbeddedSolrServerTestCase { @Rule @@ -44,11 +43,6 @@ public class TestEmbeddedSolrServer extends AbstractEmbeddedSolrServerTestCase { return new EmbeddedSolrServer(cores, "core1"); } - @Override - protected File getSolrXml() throws Exception { - return new File(SOLR_HOME, "solr.xml"); - } - public void testGetCoreContainer() { Assert.assertEquals(cores, ((EmbeddedSolrServer)getSolrCore0()).getCoreContainer()); Assert.assertEquals(cores, ((EmbeddedSolrServer)getSolrCore1()).getCoreContainer()); diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/embedded/TestSolrProperties.java b/solr/solrj/src/test/org/apache/solr/client/solrj/embedded/TestSolrProperties.java index 3b94377b9e1..3eaebae9948 100644 --- a/solr/solrj/src/test/org/apache/solr/client/solrj/embedded/TestSolrProperties.java +++ b/solr/solrj/src/test/org/apache/solr/client/solrj/embedded/TestSolrProperties.java @@ -34,8 +34,6 @@ import org.junit.rules.TestRule; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.File; - /** * * @since solr 1.3 @@ -43,17 +41,10 @@ import java.io.File; public class TestSolrProperties extends AbstractEmbeddedSolrServerTestCase { protected static Logger log = LoggerFactory.getLogger(TestSolrProperties.class); - private static final String SOLR_XML = "solr.xml"; - @Rule public TestRule solrTestRules = RuleChain.outerRule(new SystemPropertiesRestoreRule()); - @Override - protected File getSolrXml() throws Exception { - return new File(SOLR_HOME, SOLR_XML); - } - protected SolrClient getSolrAdmin() { return new EmbeddedSolrServer(cores, "core0"); } diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/request/TestCoreAdmin.java b/solr/solrj/src/test/org/apache/solr/client/solrj/request/TestCoreAdmin.java index ac71b999aa7..20e06604da6 100644 --- a/solr/solrj/src/test/org/apache/solr/client/solrj/request/TestCoreAdmin.java +++ b/solr/solrj/src/test/org/apache/solr/client/solrj/request/TestCoreAdmin.java @@ -17,6 +17,8 @@ package org.apache.solr.client.solrj.request; +import java.io.File; + import com.carrotsearch.randomizedtesting.annotations.ThreadLeakFilters; import com.carrotsearch.randomizedtesting.rules.SystemPropertiesRestoreRule; import org.apache.commons.io.FileUtils; @@ -39,22 +41,19 @@ import org.junit.rules.TestRule; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.File; - import static org.hamcrest.CoreMatchers.notNullValue; import static org.hamcrest.core.Is.is; @ThreadLeakFilters(defaultFilters = true, filters = {SolrIgnoredThreadsFilter.class}) public class TestCoreAdmin extends AbstractEmbeddedSolrServerTestCase { protected static Logger log = LoggerFactory.getLogger(TestCoreAdmin.class); - - private static final String SOLR_XML = "solr.xml"; private static String tempDirProp; @Rule public TestRule testRule = RuleChain.outerRule(new SystemPropertiesRestoreRule()); - + + /* @Override protected File getSolrXml() throws Exception { // This test writes on the directory where the solr.xml is located. Better @@ -65,7 +64,8 @@ public class TestCoreAdmin extends AbstractEmbeddedSolrServerTestCase { FileUtils.copyFile(origSolrXml, solrXml); return solrXml; } - + */ + protected SolrClient getSolrAdmin() { return new EmbeddedSolrServer(cores, "core0"); } diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/response/NoOpResponseParserTest.java b/solr/solrj/src/test/org/apache/solr/client/solrj/response/NoOpResponseParserTest.java index 9b321f65917..ea7552a48c4 100644 --- a/solr/solrj/src/test/org/apache/solr/client/solrj/response/NoOpResponseParserTest.java +++ b/solr/solrj/src/test/org/apache/solr/client/solrj/response/NoOpResponseParserTest.java @@ -17,11 +17,18 @@ package org.apache.solr.client.solrj.response; * limitations under the License. */ +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; +import java.nio.charset.StandardCharsets; +import java.util.List; + import org.apache.commons.io.IOUtils; import org.apache.solr.SolrJettyTestBase; import org.apache.solr.client.solrj.ResponseParser; -import org.apache.solr.client.solrj.SolrQuery; import org.apache.solr.client.solrj.SolrClient; +import org.apache.solr.client.solrj.SolrQuery; import org.apache.solr.client.solrj.SolrServerException; import org.apache.solr.client.solrj.impl.HttpSolrClient; import org.apache.solr.client.solrj.impl.NoOpResponseParser; @@ -35,13 +42,6 @@ import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.Reader; -import java.nio.charset.StandardCharsets; -import java.util.List; - /** * A test for parsing Solr response from query by NoOpResponseParser. * @see org.apache.solr.client.solrj.impl.NoOpResponseParser @@ -50,7 +50,7 @@ import java.util.List; public class NoOpResponseParserTest extends SolrJettyTestBase { private static InputStream getResponse() throws IOException { - return new SolrResourceLoader(null, null).openResource("solrj/sampleDateFacetResponse.xml"); + return new SolrResourceLoader().openResource("solrj/sampleDateFacetResponse.xml"); } @BeforeClass diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/response/QueryResponseTest.java b/solr/solrj/src/test/org/apache/solr/client/solrj/response/QueryResponseTest.java index 17ae49f84d5..d9840b07945 100644 --- a/solr/solrj/src/test/org/apache/solr/client/solrj/response/QueryResponseTest.java +++ b/solr/solrj/src/test/org/apache/solr/client/solrj/response/QueryResponseTest.java @@ -17,8 +17,13 @@ package org.apache.solr.client.solrj.response; -import junit.framework.Assert; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; +import java.nio.charset.StandardCharsets; +import java.util.List; +import junit.framework.Assert; import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.util.TestRuleLimitSysouts.Limit; import org.apache.solr.client.solrj.impl.XMLResponseParser; @@ -28,12 +33,6 @@ import org.apache.solr.common.util.NamedList; import org.apache.solr.core.SolrResourceLoader; import org.junit.Test; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.Reader; -import java.nio.charset.StandardCharsets; -import java.util.List; - /** * A few tests for parsing Solr response in QueryResponse * @@ -44,7 +43,7 @@ public class QueryResponseTest extends LuceneTestCase { @Test public void testDateFacets() throws Exception { XMLResponseParser parser = new XMLResponseParser(); - InputStream is = new SolrResourceLoader(null, null).openResource("solrj/sampleDateFacetResponse.xml"); + InputStream is = new SolrResourceLoader().openResource("solrj/sampleDateFacetResponse.xml"); assertNotNull(is); Reader in = new InputStreamReader(is, StandardCharsets.UTF_8); NamedList response = parser.processResponse(in); @@ -68,7 +67,7 @@ public class QueryResponseTest extends LuceneTestCase { @Test public void testRangeFacets() throws Exception { XMLResponseParser parser = new XMLResponseParser(); - InputStream is = new SolrResourceLoader(null, null).openResource("solrj/sampleDateFacetResponse.xml"); + InputStream is = new SolrResourceLoader().openResource("solrj/sampleDateFacetResponse.xml"); assertNotNull(is); Reader in = new InputStreamReader(is, StandardCharsets.UTF_8); NamedList response = parser.processResponse(in); @@ -125,7 +124,7 @@ public class QueryResponseTest extends LuceneTestCase { @Test public void testGroupResponse() throws Exception { XMLResponseParser parser = new XMLResponseParser(); - InputStream is = new SolrResourceLoader(null, null).openResource("solrj/sampleGroupResponse.xml"); + InputStream is = new SolrResourceLoader().openResource("solrj/sampleGroupResponse.xml"); assertNotNull(is); Reader in = new InputStreamReader(is, StandardCharsets.UTF_8); NamedList response = parser.processResponse(in); @@ -227,7 +226,7 @@ public class QueryResponseTest extends LuceneTestCase { @Test public void testSimpleGroupResponse() throws Exception { XMLResponseParser parser = new XMLResponseParser(); - InputStream is = new SolrResourceLoader(null, null).openResource("solrj/sampleSimpleGroupResponse.xml"); + InputStream is = new SolrResourceLoader().openResource("solrj/sampleSimpleGroupResponse.xml"); assertNotNull(is); Reader in = new InputStreamReader(is, StandardCharsets.UTF_8); NamedList response = parser.processResponse(in); @@ -268,7 +267,7 @@ public class QueryResponseTest extends LuceneTestCase { public void testIntervalFacetsResponse() throws Exception { XMLResponseParser parser = new XMLResponseParser(); - try(SolrResourceLoader loader = new SolrResourceLoader(null, null)) { + try(SolrResourceLoader loader = new SolrResourceLoader()) { InputStream is = loader.openResource("solrj/sampleIntervalFacetsResponse.xml"); assertNotNull(is); Reader in = new InputStreamReader(is, StandardCharsets.UTF_8); diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/response/TestClusteringResponse.java b/solr/solrj/src/test/org/apache/solr/client/solrj/response/TestClusteringResponse.java index 057e2c2243b..454d4347875 100644 --- a/solr/solrj/src/test/org/apache/solr/client/solrj/response/TestClusteringResponse.java +++ b/solr/solrj/src/test/org/apache/solr/client/solrj/response/TestClusteringResponse.java @@ -39,7 +39,7 @@ public class TestClusteringResponse extends SolrJettyTestBase { public void testClusteringResponse() throws Exception { XMLResponseParser parser = new XMLResponseParser(); /*Load a simple XML with the clustering response encoded in an XML format*/ - InputStream is = new SolrResourceLoader(null, null).openResource("solrj/sampleClusteringResponse.xml"); + InputStream is = new SolrResourceLoader().openResource("solrj/sampleClusteringResponse.xml"); assertNotNull(is); Reader in = new InputStreamReader(is, StandardCharsets.UTF_8); NamedList response = parser.processResponse(in); diff --git a/solr/solrj/src/test/org/apache/solr/common/util/ContentStreamTest.java b/solr/solrj/src/test/org/apache/solr/common/util/ContentStreamTest.java index 537d60a9414..6bb86215301 100644 --- a/solr/solrj/src/test/org/apache/solr/common/util/ContentStreamTest.java +++ b/solr/solrj/src/test/org/apache/solr/common/util/ContentStreamTest.java @@ -29,7 +29,6 @@ import java.nio.charset.StandardCharsets; import org.apache.commons.io.IOUtils; import org.apache.solr.SolrTestCaseJ4; -import org.apache.lucene.util.TestUtil; import org.apache.solr.core.SolrResourceLoader; /** @@ -47,7 +46,7 @@ public class ContentStreamTest extends SolrTestCaseJ4 public void testFileStream() throws IOException { - InputStream is = new SolrResourceLoader(null, null).openResource( "solrj/README" ); + InputStream is = new SolrResourceLoader().openResource( "solrj/README" ); assertNotNull( is ); File file = new File(createTempDir().toFile(), "README"); FileOutputStream os = new FileOutputStream(file); @@ -76,7 +75,7 @@ public class ContentStreamTest extends SolrTestCaseJ4 public void testURLStream() throws IOException { - InputStream is = new SolrResourceLoader(null, null).openResource( "solrj/README" ); + InputStream is = new SolrResourceLoader().openResource( "solrj/README" ); assertNotNull( is ); File file = new File(createTempDir().toFile(), "README"); FileOutputStream os = new FileOutputStream(file); diff --git a/solr/test-framework/src/java/org/apache/solr/SolrTestCaseJ4.java b/solr/test-framework/src/java/org/apache/solr/SolrTestCaseJ4.java index eaf3ea40385..ca65fd559a3 100644 --- a/solr/test-framework/src/java/org/apache/solr/SolrTestCaseJ4.java +++ b/solr/test-framework/src/java/org/apache/solr/SolrTestCaseJ4.java @@ -17,8 +17,7 @@ package org.apache.solr; -import static com.google.common.base.Preconditions.checkNotNull; - +import javax.xml.xpath.XPathExpressionException; import java.io.File; import java.io.IOException; import java.io.OutputStreamWriter; @@ -34,31 +33,22 @@ import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.net.URL; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Comparator; -import java.util.Date; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Iterator; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Locale; -import java.util.Map; -import java.util.Properties; +import java.nio.file.Paths; +import java.util.*; import java.util.logging.Level; -import javax.xml.xpath.XPathExpressionException; - +import com.carrotsearch.randomizedtesting.RandomizedContext; +import com.carrotsearch.randomizedtesting.annotations.ThreadLeakFilters; +import com.carrotsearch.randomizedtesting.rules.SystemPropertiesRestoreRule; import org.apache.commons.codec.Charsets; import org.apache.commons.io.FileUtils; import org.apache.lucene.analysis.MockAnalyzer; import org.apache.lucene.analysis.MockTokenizer; import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.util.Constants; -import org.apache.lucene.util.IOUtils; import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.util.LuceneTestCase.SuppressFileSystems; import org.apache.lucene.util.LuceneTestCase.SuppressSysoutChecks; @@ -116,9 +106,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.xml.sax.SAXException; -import com.carrotsearch.randomizedtesting.RandomizedContext; -import com.carrotsearch.randomizedtesting.annotations.ThreadLeakFilters; -import com.carrotsearch.randomizedtesting.rules.SystemPropertiesRestoreRule; +import static com.google.common.base.Preconditions.checkNotNull; /** * A junit4 Solr test harness that extends LuceneTestCaseJ4. To change which core is used when loading the schema and solrconfig.xml, simply @@ -340,16 +328,12 @@ public abstract class SolrTestCaseJ4 extends LuceneTestCase { * @param xmlStr - the text of an XML file to use. If null, use the what's the absolute minimal file. * @throws Exception Lost of file-type things can go wrong. */ - public static void setupNoCoreTest(File solrHome, String xmlStr) throws Exception { + public static void setupNoCoreTest(Path solrHome, String xmlStr) throws Exception { - File tmpFile = new File(solrHome, SolrXmlConfig.SOLR_XML_FILE); - if (xmlStr == null) { + if (xmlStr == null) xmlStr = ""; - } - FileUtils.write(tmpFile, xmlStr, IOUtils.UTF_8); - - SolrResourceLoader loader = new SolrResourceLoader(solrHome.getAbsolutePath()); - h = new TestHarness(SolrXmlConfig.fromFile(loader, new File(solrHome, "solr.xml"))); + Files.write(solrHome.resolve(SolrXmlConfig.SOLR_XML_FILE), xmlStr.getBytes(StandardCharsets.UTF_8)); + h = new TestHarness(SolrXmlConfig.fromSolrHome(solrHome)); lrf = h.getRequestFactory("standard", 0, 20, CommonParams.VERSION, "2.2"); } @@ -412,7 +396,7 @@ public abstract class SolrTestCaseJ4 extends LuceneTestCase { assertNotNull(solrHome); configString = config; schemaString = schema; - testSolrHome = solrHome; + testSolrHome = Paths.get(solrHome); if (solrHome != null) { System.setProperty("solr.solr.home", solrHome); } @@ -497,7 +481,7 @@ public abstract class SolrTestCaseJ4 extends LuceneTestCase { protected static String configString; protected static String schemaString; - protected static String testSolrHome; + protected static Path testSolrHome; protected static SolrConfig solrConfig; @@ -591,7 +575,7 @@ public abstract class SolrTestCaseJ4 extends LuceneTestCase { ("standard",0,20,CommonParams.VERSION,"2.2"); } - public static CoreContainer createCoreContainer(String solrHome, String solrXML) { + public static CoreContainer createCoreContainer(Path solrHome, String solrXML) { testSolrHome = checkNotNull(solrHome); h = new TestHarness(solrHome, solrXML); lrf = h.getRequestFactory("standard", 0, 20, CommonParams.VERSION, "2.2"); @@ -599,21 +583,21 @@ public abstract class SolrTestCaseJ4 extends LuceneTestCase { } public static CoreContainer createCoreContainer(NodeConfig config, CoresLocator locator) { - testSolrHome = config.getSolrResourceLoader().getInstanceDir(); + testSolrHome = config.getSolrResourceLoader().getInstancePath(); h = new TestHarness(config, locator); lrf = h.getRequestFactory("standard", 0, 20, CommonParams.VERSION, "2.2"); return h.getCoreContainer(); } public static CoreContainer createCoreContainer(String coreName, String dataDir, String solrConfig, String schema) { - NodeConfig nodeConfig = TestHarness.buildTestNodeConfig(new SolrResourceLoader(SolrResourceLoader.locateSolrHome())); + NodeConfig nodeConfig = TestHarness.buildTestNodeConfig(new SolrResourceLoader(TEST_PATH())); CoresLocator locator = new TestHarness.TestCoresLocator(coreName, dataDir, solrConfig, schema); CoreContainer cc = createCoreContainer(nodeConfig, locator); h.coreName = coreName; return cc; } - public static CoreContainer createDefaultCoreContainer(String solrHome) { + public static CoreContainer createDefaultCoreContainer(Path solrHome) { testSolrHome = checkNotNull(solrHome); h = new TestHarness("collection1", initCoreDataDir.getAbsolutePath(), "solrconfig.xml", "schema.xml"); lrf = h.getRequestFactory("standard", 0, 20, CommonParams.VERSION, "2.2"); @@ -1763,6 +1747,8 @@ public abstract class SolrTestCaseJ4 extends LuceneTestCase { return getFile("solr/collection1").getParent(); } + public static Path TEST_PATH() { return getFile("solr/collection1").getParentFile().toPath(); } + public static Throwable getRootCause(Throwable t) { Throwable result = t; for (Throwable cause = t; null != cause; cause = cause.getCause()) { diff --git a/solr/test-framework/src/java/org/apache/solr/util/TestHarness.java b/solr/test-framework/src/java/org/apache/solr/util/TestHarness.java index 5c889778554..05a3af8ecbe 100644 --- a/solr/test-framework/src/java/org/apache/solr/util/TestHarness.java +++ b/solr/test-framework/src/java/org/apache/solr/util/TestHarness.java @@ -17,9 +17,9 @@ package org.apache.solr.util; -import java.io.File; import java.io.IOException; import java.io.StringWriter; +import java.nio.file.Path; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -66,21 +66,21 @@ public class TestHarness extends BaseTestHarness { * follows the basic conventions of being a relative path in the solrHome * dir. (ie: ${solrHome}/${coreName}/conf/${confFile} */ - public static SolrConfig createConfig(String solrHome, String coreName, String confFile) { + public static SolrConfig createConfig(Path solrHome, String coreName, String confFile) { // set some system properties for use by tests System.setProperty("solr.test.sys.prop1", "propone"); System.setProperty("solr.test.sys.prop2", "proptwo"); try { - return new SolrConfig(solrHome + File.separator + coreName, confFile, null); + return new SolrConfig(solrHome.resolve(coreName), confFile, null); } catch (Exception xany) { throw new RuntimeException(xany); } } /** - * Creates a SolrConfig object for the default test core using {@link #createConfig(String,String,String)} + * Creates a SolrConfig object for the default test core using {@link #createConfig(Path,String,String)} */ - public static SolrConfig createConfig(String solrHome, String confFile) { + public static SolrConfig createConfig(Path solrHome, String confFile) { return createConfig(solrHome, SolrTestCaseJ4.DEFAULT_TEST_CORENAME, confFile); } @@ -139,7 +139,7 @@ public class TestHarness extends BaseTestHarness { * @param solrHome the solr home directory * @param solrXml the text of a solrxml */ - public TestHarness(String solrHome, String solrXml) { + public TestHarness(Path solrHome, String solrXml) { this(new SolrResourceLoader(solrHome), solrXml); }