From 6dcfda99e81e1f03ed83da69250bb9a4b7fc938a Mon Sep 17 00:00:00 2001 From: Ryan Ernst Date: Thu, 13 Aug 2015 19:43:03 -0700 Subject: [PATCH] Internal: Remove ClassLoader from Settings Settings currently has a classloader member, which any user (plugin or core ES code) can access to load classes/resources. This is extremely error prone as setting the classloder on the Settings instance is a public method. Furthermore, it is not really necessary. Classes that need resources should load resources using normal means (getClass().getResourceAsStream). Those that need classes should use Class.forName, which will load the class with the same classloader as the calling class. This means, in the few places where classes are loaded by string name, they will use the appropriate loader: either the default classloader which loads core ES code, or a child classloader for each plugin. This change removes the classloader member from Settings, as well as other classloader related uses (except for a handful of cases which must use a classloader, at least for now). --- .../elasticsearch/bootstrap/Bootstrap.java | 2 +- .../cluster/metadata/IndexMetaData.java | 8 ++- .../metadata/MetaDataIndexUpgradeService.java | 7 ++- .../org/elasticsearch/common/Classes.java | 46 --------------- .../common/geo/ShapesAvailability.java | 4 +- .../elasticsearch/common/inject/Modules.java | 4 -- .../common/settings/Settings.java | 56 +------------------ .../org/elasticsearch/env/Environment.java | 5 +- .../elasticsearch/indices/IndicesService.java | 1 - .../internal/InternalSettingsPreparer.java | 2 +- .../elasticsearch/plugins/PluginsService.java | 17 ++---- .../elasticsearch/script/ScriptModule.java | 6 +- .../groovy/GroovyScriptEngineService.java | 2 +- .../ExceptionSerializationTests.java | 2 +- .../loader/JsonSettingsLoaderTests.java | 3 +- .../loader/YamlSettingsLoaderTests.java | 24 +++----- .../index/analysis/AnalysisModuleTests.java | 9 +-- .../index/analysis/AnalysisTestsHelper.java | 2 +- .../index/analysis/CJKFilterFactoryTests.java | 2 +- .../index/analysis/CompoundAnalysisTests.java | 6 +- .../analysis/KeepFilterFactoryTests.java | 2 +- .../PatternCaptureTokenFilterTests.java | 3 +- .../ShingleTokenFilterFactoryTests.java | 2 +- .../index/analysis/StopAnalyzerTests.java | 3 +- .../CommonGramsTokenFilterFactoryTests.java | 6 +- .../synonyms/SynonymsAnalysisTest.java | 3 +- .../InternalSettingsPreparerTests.java | 15 ----- .../index/analysis/KuromojiAnalysisTests.java | 3 +- .../analysis/SimplePhoneticAnalysisTests.java | 3 +- 29 files changed, 70 insertions(+), 178 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/bootstrap/Bootstrap.java b/core/src/main/java/org/elasticsearch/bootstrap/Bootstrap.java index cc3aa5068db..d1143a54542 100644 --- a/core/src/main/java/org/elasticsearch/bootstrap/Bootstrap.java +++ b/core/src/main/java/org/elasticsearch/bootstrap/Bootstrap.java @@ -192,7 +192,7 @@ public class Bootstrap { @SuppressForbidden(reason = "Exception#printStackTrace()") private static void setupLogging(Settings settings, Environment environment) { try { - settings.getClassLoader().loadClass("org.apache.log4j.Logger"); + Class.forName("org.apache.log4j.Logger"); LogConfigurator.configure(settings); } catch (ClassNotFoundException e) { // no log4j diff --git a/core/src/main/java/org/elasticsearch/cluster/metadata/IndexMetaData.java b/core/src/main/java/org/elasticsearch/cluster/metadata/IndexMetaData.java index aa2fc96a9b5..943bca9442b 100644 --- a/core/src/main/java/org/elasticsearch/cluster/metadata/IndexMetaData.java +++ b/core/src/main/java/org/elasticsearch/cluster/metadata/IndexMetaData.java @@ -23,6 +23,7 @@ import com.carrotsearch.hppc.cursors.ObjectCursor; import com.carrotsearch.hppc.cursors.ObjectObjectCursor; import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableMap; +import org.elasticsearch.ElasticsearchException; import org.elasticsearch.Version; import org.elasticsearch.cluster.Diff; import org.elasticsearch.cluster.Diffable; @@ -251,7 +252,12 @@ public class IndexMetaData implements Diffable, FromXContentBuild if (hashFunction == null) { routingHashFunction = MURMUR3_HASH_FUNCTION; } else { - final Class hashFunctionClass = Classes.loadClass(getClass().getClassLoader(), hashFunction); + final Class hashFunctionClass; + try { + hashFunctionClass = Class.forName(hashFunction).asSubclass(HashFunction.class); + } catch (ClassNotFoundException|NoClassDefFoundError e) { + throw new ElasticsearchException("failed to load custom hash function [" + hashFunction + "]", e); + } try { routingHashFunction = hashFunctionClass.newInstance(); } catch (InstantiationException | IllegalAccessException e) { diff --git a/core/src/main/java/org/elasticsearch/cluster/metadata/MetaDataIndexUpgradeService.java b/core/src/main/java/org/elasticsearch/cluster/metadata/MetaDataIndexUpgradeService.java index 08f5dbe7e53..2d26ebfd941 100644 --- a/core/src/main/java/org/elasticsearch/cluster/metadata/MetaDataIndexUpgradeService.java +++ b/core/src/main/java/org/elasticsearch/cluster/metadata/MetaDataIndexUpgradeService.java @@ -20,6 +20,7 @@ package org.elasticsearch.cluster.metadata; import com.carrotsearch.hppc.cursors.ObjectCursor; import org.apache.lucene.analysis.Analyzer; +import org.elasticsearch.ElasticsearchException; import org.elasticsearch.Version; import org.elasticsearch.cluster.routing.DjbHashFunction; import org.elasticsearch.cluster.routing.HashFunction; @@ -78,7 +79,11 @@ public class MetaDataIndexUpgradeService extends AbstractComponent { pre20HashFunction = DjbHashFunction.class; break; default: - pre20HashFunction = Classes.loadClass(getClass().getClassLoader(), pre20HashFunctionName); + try { + pre20HashFunction = Class.forName(pre20HashFunctionName).asSubclass(HashFunction.class); + } catch (ClassNotFoundException|NoClassDefFoundError e) { + throw new ElasticsearchException("failed to load custom hash function [" + pre20HashFunctionName + "]", e); + } } } else { pre20HashFunction = DjbHashFunction.class; diff --git a/core/src/main/java/org/elasticsearch/common/Classes.java b/core/src/main/java/org/elasticsearch/common/Classes.java index b9a508e94bd..4a73c0d8ae4 100644 --- a/core/src/main/java/org/elasticsearch/common/Classes.java +++ b/core/src/main/java/org/elasticsearch/common/Classes.java @@ -19,17 +19,7 @@ package org.elasticsearch.common; -import org.elasticsearch.ElasticsearchException; -import org.elasticsearch.bootstrap.Elasticsearch; -import org.elasticsearch.common.inject.Module; -import org.elasticsearch.common.settings.NoClassSettingsException; - import java.lang.reflect.Modifier; -import java.util.Arrays; -import java.util.Iterator; -import java.util.Locale; - -import static org.elasticsearch.common.Strings.toCamelCase; /** * @@ -41,34 +31,6 @@ public class Classes { */ private static final char PACKAGE_SEPARATOR = '.'; - /** - * Return the default ClassLoader to use: typically the thread context - * ClassLoader, if available; the ClassLoader that loaded the ClassUtils - * class will be used as fallback. - *

- *

Call this method if you intend to use the thread context ClassLoader - * in a scenario where you absolutely need a non-null ClassLoader reference: - * for example, for class path resource loading (but not necessarily for - * Class.forName, which accepts a null ClassLoader - * reference as well). - * - * @return the default ClassLoader (never null) - * @see java.lang.Thread#getContextClassLoader() - */ - public static ClassLoader getDefaultClassLoader() { - ClassLoader cl = null; - try { - cl = Thread.currentThread().getContextClassLoader(); - } catch (Throwable ex) { - // Cannot access thread context ClassLoader - falling back to system class loader... - } - if (cl == null) { - // No thread context class loader -> use class loader of this class. - cl = Classes.class.getClassLoader(); - } - return cl; - } - /** * Determine the name of the package of the given class: * e.g. "java.lang" for the java.lang.String class. @@ -93,13 +55,5 @@ public class Classes { return !clazz.isInterface() && !Modifier.isAbstract(modifiers); } - public static Class loadClass(ClassLoader classLoader, String className) { - try { - return (Class) classLoader.loadClass(className); - } catch (ClassNotFoundException|NoClassDefFoundError e) { - throw new ElasticsearchException("failed to load class [" + className + "]", e); - } - } - private Classes() {} } diff --git a/core/src/main/java/org/elasticsearch/common/geo/ShapesAvailability.java b/core/src/main/java/org/elasticsearch/common/geo/ShapesAvailability.java index 882bfcbf718..fce18337728 100644 --- a/core/src/main/java/org/elasticsearch/common/geo/ShapesAvailability.java +++ b/core/src/main/java/org/elasticsearch/common/geo/ShapesAvailability.java @@ -31,7 +31,7 @@ public class ShapesAvailability { static { boolean xSPATIAL4J_AVAILABLE; try { - Classes.getDefaultClassLoader().loadClass("com.spatial4j.core.shape.impl.PointImpl"); + Class.forName("com.spatial4j.core.shape.impl.PointImpl"); xSPATIAL4J_AVAILABLE = true; } catch (Throwable t) { xSPATIAL4J_AVAILABLE = false; @@ -40,7 +40,7 @@ public class ShapesAvailability { boolean xJTS_AVAILABLE; try { - Classes.getDefaultClassLoader().loadClass("com.vividsolutions.jts.geom.GeometryFactory"); + Class.forName("com.vividsolutions.jts.geom.GeometryFactory"); xJTS_AVAILABLE = true; } catch (Throwable t) { xJTS_AVAILABLE = false; diff --git a/core/src/main/java/org/elasticsearch/common/inject/Modules.java b/core/src/main/java/org/elasticsearch/common/inject/Modules.java index 4e5ae23e05a..edb08dd183a 100644 --- a/core/src/main/java/org/elasticsearch/common/inject/Modules.java +++ b/core/src/main/java/org/elasticsearch/common/inject/Modules.java @@ -30,10 +30,6 @@ import java.lang.reflect.Constructor; */ public class Modules { - public static Module createModule(String moduleClass, Settings settings) throws ClassNotFoundException { - return createModule((Class) settings.getClassLoader().loadClass(moduleClass), settings); - } - public static Module createModule(Class moduleClass, @Nullable Settings settings) { Constructor constructor; try { diff --git a/core/src/main/java/org/elasticsearch/common/settings/Settings.java b/core/src/main/java/org/elasticsearch/common/settings/Settings.java index 4d422575480..16eca5c8dfc 100644 --- a/core/src/main/java/org/elasticsearch/common/settings/Settings.java +++ b/core/src/main/java/org/elasticsearch/common/settings/Settings.java @@ -79,9 +79,8 @@ public final class Settings implements ToXContent { private ImmutableMap settings; private final ImmutableMap forcedUnderscoreSettings; - private transient ClassLoader classLoader; - Settings(Map settings, ClassLoader classLoader) { + Settings(Map settings) { // we use a sorted map for consistent serialization when using getAsMap() // TODO: use Collections.unmodifiableMap with a TreeMap this.settings = ImmutableSortedMap.copyOf(settings); @@ -96,22 +95,6 @@ public final class Settings implements ToXContent { } } this.forcedUnderscoreSettings = forcedUnderscoreSettings == null ? ImmutableMap.of() : ImmutableMap.copyOf(forcedUnderscoreSettings); - this.classLoader = classLoader; - } - - /** - * The class loader associated with this settings, or {@link org.elasticsearch.common.Classes#getDefaultClassLoader()} - * if not set. - */ - public ClassLoader getClassLoader() { - return this.classLoader == null ? Classes.getDefaultClassLoader() : classLoader; - } - - /** - * The class loader associated with this settings, but only if explicitly set, otherwise null. - */ - public ClassLoader getClassLoaderIfSet() { - return this.classLoader; } /** @@ -227,7 +210,6 @@ public final class Settings implements ToXContent { builder.put(entry.getKey().substring(prefix.length()), entry.getValue()); } } - builder.classLoader(classLoader); return builder.build(); } @@ -648,7 +630,7 @@ public final class Settings implements ToXContent { } Map retVal = new LinkedHashMap<>(); for (Map.Entry> entry : map.entrySet()) { - retVal.put(entry.getKey(), new Settings(Collections.unmodifiableMap(entry.getValue()), classLoader)); + retVal.put(entry.getKey(), new Settings(Collections.unmodifiableMap(entry.getValue()))); } return Collections.unmodifiableMap(retVal); } @@ -701,17 +683,13 @@ public final class Settings implements ToXContent { if (o == null || getClass() != o.getClass()) return false; Settings that = (Settings) o; - - if (classLoader != null ? !classLoader.equals(that.classLoader) : that.classLoader != null) return false; if (settings != null ? !settings.equals(that.settings) : that.settings != null) return false; - return true; } @Override public int hashCode() { int result = settings != null ? settings.hashCode() : 0; - result = 31 * result + (classLoader != null ? classLoader.hashCode() : 0); return result; } @@ -769,8 +747,6 @@ public final class Settings implements ToXContent { private final Map map = new LinkedHashMap<>(); - private ClassLoader classLoader; - private Builder() { } @@ -998,7 +974,6 @@ public final class Settings implements ToXContent { public Builder put(Settings settings) { removeNonArraysFieldsIfNewSettingsContainsFieldAsArray(settings.getAsMap()); map.putAll(settings.getAsMap()); - classLoader = settings.getClassLoaderIfSet(); return this; } @@ -1118,31 +1093,6 @@ public final class Settings implements ToXContent { return this; } - /** - * Loads settings from classpath that represents them using the - * {@link SettingsLoaderFactory#loaderFromSource(String)}. - */ - public Builder loadFromClasspath(String resourceName) throws SettingsException { - ClassLoader classLoader = this.classLoader; - if (classLoader == null) { - classLoader = Classes.getDefaultClassLoader(); - } - InputStream is = classLoader.getResourceAsStream(resourceName); - if (is == null) { - throw new SettingsException("Failed to load settings from [" + resourceName + "]"); - } - - return loadFromStream(resourceName, is); - } - - /** - * Sets the class loader associated with the settings built. - */ - public Builder classLoader(ClassLoader classLoader) { - this.classLoader = classLoader; - return this; - } - /** * Puts all the properties with keys starting with the provided prefix. * @@ -1270,7 +1220,7 @@ public final class Settings implements ToXContent { * set on this builder. */ public Settings build() { - return new Settings(Collections.unmodifiableMap(map), classLoader); + return new Settings(Collections.unmodifiableMap(map)); } } diff --git a/core/src/main/java/org/elasticsearch/env/Environment.java b/core/src/main/java/org/elasticsearch/env/Environment.java index c0e3bf4be7d..8ec9a7e802b 100644 --- a/core/src/main/java/org/elasticsearch/env/Environment.java +++ b/core/src/main/java/org/elasticsearch/env/Environment.java @@ -319,13 +319,14 @@ public class Environment { } } // try and load it from the classpath directly - URL resource = settings.getClassLoader().getResource(path); + // TODO: remove this, callers can look up their own config on classpath + URL resource = getClass().getClassLoader().getResource(path); if (resource != null) { return resource; } // try and load it from the classpath with config/ prefix if (!path.startsWith("config/")) { - resource = settings.getClassLoader().getResource("config/" + path); + resource = getClass().getClassLoader().getResource("config/" + path); if (resource != null) { return resource; } diff --git a/core/src/main/java/org/elasticsearch/indices/IndicesService.java b/core/src/main/java/org/elasticsearch/indices/IndicesService.java index d207e4b6618..022fbd997b6 100644 --- a/core/src/main/java/org/elasticsearch/indices/IndicesService.java +++ b/core/src/main/java/org/elasticsearch/indices/IndicesService.java @@ -300,7 +300,6 @@ public class IndicesService extends AbstractLifecycleComponent i Settings indexSettings = settingsBuilder() .put(this.settings) .put(settings) - .classLoader(settings.getClassLoader()) .build(); ModulesBuilder modules = new ModulesBuilder(); diff --git a/core/src/main/java/org/elasticsearch/node/internal/InternalSettingsPreparer.java b/core/src/main/java/org/elasticsearch/node/internal/InternalSettingsPreparer.java index 09de3a118aa..7a824cd1ed0 100644 --- a/core/src/main/java/org/elasticsearch/node/internal/InternalSettingsPreparer.java +++ b/core/src/main/java/org/elasticsearch/node/internal/InternalSettingsPreparer.java @@ -180,7 +180,7 @@ public class InternalSettingsPreparer { static Settings replacePromptPlaceholders(Settings settings, Terminal terminal) { UnmodifiableIterator> iter = settings.getAsMap().entrySet().iterator(); - Settings.Builder builder = Settings.builder().classLoader(settings.getClassLoaderIfSet()); + Settings.Builder builder = Settings.builder(); while (iter.hasNext()) { Map.Entry entry = iter.next(); diff --git a/core/src/main/java/org/elasticsearch/plugins/PluginsService.java b/core/src/main/java/org/elasticsearch/plugins/PluginsService.java index c895bf96e18..762d497e832 100644 --- a/core/src/main/java/org/elasticsearch/plugins/PluginsService.java +++ b/core/src/main/java/org/elasticsearch/plugins/PluginsService.java @@ -95,7 +95,7 @@ public class PluginsService extends AbstractComponent { // this is a hack for what is between unit and integration tests... String[] defaultPluginsClasses = settings.getAsArray("plugin.types"); for (String pluginClass : defaultPluginsClasses) { - Plugin plugin = loadPlugin(pluginClass, settings); + Plugin plugin = loadPlugin(pluginClass, settings, getClass().getClassLoader()); PluginInfo pluginInfo = new PluginInfo(plugin.name(), plugin.description(), false, "NA", true, pluginClass, false); if (logger.isTraceEnabled()) { logger.trace("plugin loaded from settings [{}]", pluginInfo); @@ -347,7 +347,7 @@ public class PluginsService extends AbstractComponent { // pluginmanager does it, but we do it again, in case lusers mess with jar files manually try { final List jars = new ArrayList<>(); - ClassLoader parentLoader = settings.getClassLoader(); + ClassLoader parentLoader = getClass().getClassLoader(); if (parentLoader instanceof URLClassLoader) { for (URL url : ((URLClassLoader) parentLoader).getURLs()) { jars.add(url); @@ -360,16 +360,11 @@ public class PluginsService extends AbstractComponent { } // create a child to load the plugins in this bundle - ClassLoader loader = URLClassLoader.newInstance(bundle.urls.toArray(new URL[0]), settings.getClassLoader()); - Settings settings = Settings.builder() - .put(this.settings) - .classLoader(loader) - .build(); - + ClassLoader loader = URLClassLoader.newInstance(bundle.urls.toArray(new URL[0]), getClass().getClassLoader()); for (PluginInfo pluginInfo : bundle.plugins) { final Plugin plugin; if (pluginInfo.isJvm()) { - plugin = loadPlugin(pluginInfo.getClassname(), settings); + plugin = loadPlugin(pluginInfo.getClassname(), settings, loader); } else { plugin = new SitePlugin(pluginInfo.getName(), pluginInfo.getDescription()); } @@ -380,9 +375,9 @@ public class PluginsService extends AbstractComponent { return plugins.build(); } - private Plugin loadPlugin(String className, Settings settings) { + private Plugin loadPlugin(String className, Settings settings, ClassLoader loader) { try { - Class pluginClass = settings.getClassLoader().loadClass(className).asSubclass(Plugin.class); + Class pluginClass = loader.loadClass(className).asSubclass(Plugin.class); try { return pluginClass.getConstructor(Settings.class).newInstance(settings); diff --git a/core/src/main/java/org/elasticsearch/script/ScriptModule.java b/core/src/main/java/org/elasticsearch/script/ScriptModule.java index d6c52a6b130..f5de92f67d7 100644 --- a/core/src/main/java/org/elasticsearch/script/ScriptModule.java +++ b/core/src/main/java/org/elasticsearch/script/ScriptModule.java @@ -79,21 +79,21 @@ public class ScriptModule extends AbstractModule { multibinder.addBinding().to(NativeScriptEngineService.class); try { - settings.getClassLoader().loadClass("groovy.lang.GroovyClassLoader"); + Class.forName("groovy.lang.GroovyClassLoader"); multibinder.addBinding().to(GroovyScriptEngineService.class).asEagerSingleton(); } catch (Throwable t) { Loggers.getLogger(ScriptService.class, settings).debug("failed to load groovy", t); } try { - settings.getClassLoader().loadClass("com.github.mustachejava.Mustache"); + Class.forName("com.github.mustachejava.Mustache"); multibinder.addBinding().to(MustacheScriptEngineService.class).asEagerSingleton(); } catch (Throwable t) { Loggers.getLogger(ScriptService.class, settings).debug("failed to load mustache", t); } try { - settings.getClassLoader().loadClass("org.apache.lucene.expressions.Expression"); + Class.forName("org.apache.lucene.expressions.Expression"); multibinder.addBinding().to(ExpressionScriptEngineService.class).asEagerSingleton(); } catch (Throwable t) { Loggers.getLogger(ScriptService.class, settings).debug("failed to load lucene expressions", t); diff --git a/core/src/main/java/org/elasticsearch/script/groovy/GroovyScriptEngineService.java b/core/src/main/java/org/elasticsearch/script/groovy/GroovyScriptEngineService.java index 93f196f448e..a74cdf94946 100644 --- a/core/src/main/java/org/elasticsearch/script/groovy/GroovyScriptEngineService.java +++ b/core/src/main/java/org/elasticsearch/script/groovy/GroovyScriptEngineService.java @@ -70,7 +70,7 @@ public class GroovyScriptEngineService extends AbstractComponent implements Scri config.addCompilationCustomizers(imports); // Add BigDecimal -> Double transformer config.addCompilationCustomizers(new GroovyBigDecimalTransformer(CompilePhase.CONVERSION)); - this.loader = new GroovyClassLoader(settings.getClassLoader(), config); + this.loader = new GroovyClassLoader(getClass().getClassLoader(), config); } @Override diff --git a/core/src/test/java/org/elasticsearch/ExceptionSerializationTests.java b/core/src/test/java/org/elasticsearch/ExceptionSerializationTests.java index b76dac80075..034d31c23f4 100644 --- a/core/src/test/java/org/elasticsearch/ExceptionSerializationTests.java +++ b/core/src/test/java/org/elasticsearch/ExceptionSerializationTests.java @@ -152,7 +152,7 @@ public class ExceptionSerializationTests extends ESTestCase { pkg.append(p.getFileName().toString()).append("."); } pkg.append(filename.substring(0, filename.length() - 6)); - return Thread.currentThread().getContextClassLoader().loadClass(pkg.toString()); + return getClass().getClassLoader().loadClass(pkg.toString()); } @Override diff --git a/core/src/test/java/org/elasticsearch/common/settings/loader/JsonSettingsLoaderTests.java b/core/src/test/java/org/elasticsearch/common/settings/loader/JsonSettingsLoaderTests.java index f4604d28d1a..142d60871aa 100644 --- a/core/src/test/java/org/elasticsearch/common/settings/loader/JsonSettingsLoaderTests.java +++ b/core/src/test/java/org/elasticsearch/common/settings/loader/JsonSettingsLoaderTests.java @@ -34,8 +34,9 @@ public class JsonSettingsLoaderTests extends ESTestCase { @Test public void testSimpleJsonSettings() throws Exception { + String json = "/org/elasticsearch/common/settings/loader/test-settings.json"; Settings settings = settingsBuilder() - .loadFromClasspath("org/elasticsearch/common/settings/loader/test-settings.json") + .loadFromStream(json, getClass().getResourceAsStream(json)) .build(); assertThat(settings.get("test1.value1"), equalTo("value1")); diff --git a/core/src/test/java/org/elasticsearch/common/settings/loader/YamlSettingsLoaderTests.java b/core/src/test/java/org/elasticsearch/common/settings/loader/YamlSettingsLoaderTests.java index 362188ea03d..49b5444a52b 100644 --- a/core/src/test/java/org/elasticsearch/common/settings/loader/YamlSettingsLoaderTests.java +++ b/core/src/test/java/org/elasticsearch/common/settings/loader/YamlSettingsLoaderTests.java @@ -34,8 +34,9 @@ public class YamlSettingsLoaderTests extends ESTestCase { @Test public void testSimpleYamlSettings() throws Exception { + String yaml = "/org/elasticsearch/common/settings/loader/test-settings.yml"; Settings settings = settingsBuilder() - .loadFromClasspath("org/elasticsearch/common/settings/loader/test-settings.yml") + .loadFromStream(yaml, getClass().getResourceAsStream(yaml)) .build(); assertThat(settings.get("test1.value1"), equalTo("value1")); @@ -52,28 +53,17 @@ public class YamlSettingsLoaderTests extends ESTestCase { @Test(expected = SettingsException.class) public void testIndentation() { + String yaml = "/org/elasticsearch/common/settings/loader/indentation-settings.yml"; settingsBuilder() - .loadFromClasspath("org/elasticsearch/common/settings/loader/indentation-settings.yml") - .build(); + .loadFromStream(yaml, getClass().getResourceAsStream(yaml)) + .build(); } @Test(expected = SettingsException.class) public void testIndentationWithExplicitDocumentStart() { + String yaml = "/org/elasticsearch/common/settings/loader/indentation-with-explicit-document-start-settings.yml"; settingsBuilder() - .loadFromClasspath("org/elasticsearch/common/settings/loader/indentation-with-explicit-document-start-settings.yml") + .loadFromStream(yaml, getClass().getResourceAsStream(yaml)) .build(); } - - - @Test - public void testYamlSettingsNoFile() throws Exception { - String invalidResourceName = "org/elasticsearch/common/settings/loader/no-test-settings.yml"; - try { - Settings defaultSettings = settingsBuilder().loadFromClasspath(invalidResourceName).build(); - fail("For a not exiting file an exception should be thrown."); - } catch (Exception e) { - assertTrue(e instanceof SettingsException); - assertThat(e.getMessage(), equalTo("Failed to load settings from [" + invalidResourceName + "]")); - } - } } \ No newline at end of file diff --git a/core/src/test/java/org/elasticsearch/index/analysis/AnalysisModuleTests.java b/core/src/test/java/org/elasticsearch/index/analysis/AnalysisModuleTests.java index e667afaf5a3..8a81705684d 100644 --- a/core/src/test/java/org/elasticsearch/index/analysis/AnalysisModuleTests.java +++ b/core/src/test/java/org/elasticsearch/index/analysis/AnalysisModuleTests.java @@ -79,7 +79,7 @@ public class AnalysisModuleTests extends ESTestCase { } private Settings loadFromClasspath(String path) { - return settingsBuilder().loadFromClasspath(path) + return settingsBuilder().loadFromStream(path, getClass().getResourceAsStream(path)) .put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT) .put("path.home", createTempDir().toString()) .build(); @@ -88,13 +88,13 @@ public class AnalysisModuleTests extends ESTestCase { @Test public void testSimpleConfigurationJson() { - Settings settings = loadFromClasspath("org/elasticsearch/index/analysis/test1.json"); + Settings settings = loadFromClasspath("/org/elasticsearch/index/analysis/test1.json"); testSimpleConfiguration(settings); } @Test public void testSimpleConfigurationYaml() { - Settings settings = loadFromClasspath("org/elasticsearch/index/analysis/test1.yml"); + Settings settings = loadFromClasspath("/org/elasticsearch/index/analysis/test1.yml"); testSimpleConfiguration(settings); } @@ -107,8 +107,9 @@ public class AnalysisModuleTests extends ESTestCase { @Test public void testVersionedAnalyzers() throws Exception { + String yaml = "/org/elasticsearch/index/analysis/test1.yml"; Settings settings2 = settingsBuilder() - .loadFromClasspath("org/elasticsearch/index/analysis/test1.yml") + .loadFromStream(yaml, getClass().getResourceAsStream(yaml)) .put("path.home", createTempDir().toString()) .put(IndexMetaData.SETTING_VERSION_CREATED, Version.V_0_90_0) .build(); diff --git a/core/src/test/java/org/elasticsearch/index/analysis/AnalysisTestsHelper.java b/core/src/test/java/org/elasticsearch/index/analysis/AnalysisTestsHelper.java index 6f7581768f3..74ff95d4a14 100644 --- a/core/src/test/java/org/elasticsearch/index/analysis/AnalysisTestsHelper.java +++ b/core/src/test/java/org/elasticsearch/index/analysis/AnalysisTestsHelper.java @@ -39,7 +39,7 @@ public class AnalysisTestsHelper { public static AnalysisService createAnalysisServiceFromClassPath(Path baseDir, String resource) { Settings settings = Settings.settingsBuilder() - .loadFromClasspath(resource) + .loadFromStream(resource, AnalysisTestsHelper.class.getResourceAsStream(resource)) .put("path.home", baseDir.toString()) .build(); diff --git a/core/src/test/java/org/elasticsearch/index/analysis/CJKFilterFactoryTests.java b/core/src/test/java/org/elasticsearch/index/analysis/CJKFilterFactoryTests.java index 418cd2b30a2..98ed9d28703 100644 --- a/core/src/test/java/org/elasticsearch/index/analysis/CJKFilterFactoryTests.java +++ b/core/src/test/java/org/elasticsearch/index/analysis/CJKFilterFactoryTests.java @@ -29,7 +29,7 @@ import java.io.StringReader; public class CJKFilterFactoryTests extends ESTokenStreamTestCase { - private static final String RESOURCE = "org/elasticsearch/index/analysis/cjk_analysis.json"; + private static final String RESOURCE = "/org/elasticsearch/index/analysis/cjk_analysis.json"; @Test public void testDefault() throws IOException { diff --git a/core/src/test/java/org/elasticsearch/index/analysis/CompoundAnalysisTests.java b/core/src/test/java/org/elasticsearch/index/analysis/CompoundAnalysisTests.java index a259dc0d19e..ad81450c336 100644 --- a/core/src/test/java/org/elasticsearch/index/analysis/CompoundAnalysisTests.java +++ b/core/src/test/java/org/elasticsearch/index/analysis/CompoundAnalysisTests.java @@ -115,16 +115,18 @@ public class CompoundAnalysisTests extends ESTestCase { } private Settings getJsonSettings() { + String json = "/org/elasticsearch/index/analysis/test1.json"; return settingsBuilder() - .loadFromClasspath("org/elasticsearch/index/analysis/test1.json") + .loadFromStream(json, getClass().getResourceAsStream(json)) .put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT) .put("path.home", createTempDir().toString()) .build(); } private Settings getYamlSettings() { + String yaml = "/org/elasticsearch/index/analysis/test1.yml"; return settingsBuilder() - .loadFromClasspath("org/elasticsearch/index/analysis/test1.yml") + .loadFromStream(yaml, getClass().getResourceAsStream(yaml)) .put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT) .put("path.home", createTempDir().toString()) .build(); diff --git a/core/src/test/java/org/elasticsearch/index/analysis/KeepFilterFactoryTests.java b/core/src/test/java/org/elasticsearch/index/analysis/KeepFilterFactoryTests.java index 14f3b6fe0d3..33cb31f751e 100644 --- a/core/src/test/java/org/elasticsearch/index/analysis/KeepFilterFactoryTests.java +++ b/core/src/test/java/org/elasticsearch/index/analysis/KeepFilterFactoryTests.java @@ -34,7 +34,7 @@ import static org.hamcrest.Matchers.instanceOf; public class KeepFilterFactoryTests extends ESTokenStreamTestCase { - private static final String RESOURCE = "org/elasticsearch/index/analysis/keep_analysis.json"; + private static final String RESOURCE = "/org/elasticsearch/index/analysis/keep_analysis.json"; @Test diff --git a/core/src/test/java/org/elasticsearch/index/analysis/PatternCaptureTokenFilterTests.java b/core/src/test/java/org/elasticsearch/index/analysis/PatternCaptureTokenFilterTests.java index 0a1d7e5e196..2796367f07f 100644 --- a/core/src/test/java/org/elasticsearch/index/analysis/PatternCaptureTokenFilterTests.java +++ b/core/src/test/java/org/elasticsearch/index/analysis/PatternCaptureTokenFilterTests.java @@ -41,10 +41,11 @@ public class PatternCaptureTokenFilterTests extends ESTokenStreamTestCase { @Test public void testPatternCaptureTokenFilter() throws Exception { + String json = "/org/elasticsearch/index/analysis/pattern_capture.json"; Index index = new Index("test"); Settings settings = settingsBuilder() .put("path.home", createTempDir()) - .loadFromClasspath("org/elasticsearch/index/analysis/pattern_capture.json") + .loadFromStream(json, getClass().getResourceAsStream(json)) .put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT) .build(); Injector parentInjector = new ModulesBuilder().add(new SettingsModule(settings), new EnvironmentModule(new Environment(settings)), new IndicesAnalysisModule()).createInjector(); diff --git a/core/src/test/java/org/elasticsearch/index/analysis/ShingleTokenFilterFactoryTests.java b/core/src/test/java/org/elasticsearch/index/analysis/ShingleTokenFilterFactoryTests.java index d8b6905d777..26883f562f5 100644 --- a/core/src/test/java/org/elasticsearch/index/analysis/ShingleTokenFilterFactoryTests.java +++ b/core/src/test/java/org/elasticsearch/index/analysis/ShingleTokenFilterFactoryTests.java @@ -36,7 +36,7 @@ import static org.hamcrest.Matchers.instanceOf; @ThreadLeakScope(Scope.NONE) public class ShingleTokenFilterFactoryTests extends ESTokenStreamTestCase { - private static final String RESOURCE = "org/elasticsearch/index/analysis/shingle_analysis.json"; + private static final String RESOURCE = "/org/elasticsearch/index/analysis/shingle_analysis.json"; @Test public void testDefault() throws IOException { diff --git a/core/src/test/java/org/elasticsearch/index/analysis/StopAnalyzerTests.java b/core/src/test/java/org/elasticsearch/index/analysis/StopAnalyzerTests.java index e520fd1c557..5ec0178cea0 100644 --- a/core/src/test/java/org/elasticsearch/index/analysis/StopAnalyzerTests.java +++ b/core/src/test/java/org/elasticsearch/index/analysis/StopAnalyzerTests.java @@ -41,9 +41,10 @@ public class StopAnalyzerTests extends ESTokenStreamTestCase { @Test public void testDefaultsCompoundAnalysis() throws Exception { + String json = "/org/elasticsearch/index/analysis/stop.json"; Index index = new Index("test"); Settings settings = settingsBuilder() - .loadFromClasspath("org/elasticsearch/index/analysis/stop.json") + .loadFromStream(json, getClass().getResourceAsStream(json)) .put("path.home", createTempDir().toString()) .put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT) .build(); diff --git a/core/src/test/java/org/elasticsearch/index/analysis/commongrams/CommonGramsTokenFilterFactoryTests.java b/core/src/test/java/org/elasticsearch/index/analysis/commongrams/CommonGramsTokenFilterFactoryTests.java index 6aadf4d1377..7391b045f6d 100644 --- a/core/src/test/java/org/elasticsearch/index/analysis/commongrams/CommonGramsTokenFilterFactoryTests.java +++ b/core/src/test/java/org/elasticsearch/index/analysis/commongrams/CommonGramsTokenFilterFactoryTests.java @@ -134,8 +134,9 @@ public class CommonGramsTokenFilterFactoryTests extends ESTokenStreamTestCase { @Test public void testCommonGramsAnalysis() throws IOException { + String json = "/org/elasticsearch/index/analysis/commongrams/commongrams.json"; Settings settings = Settings.settingsBuilder() - .loadFromClasspath("org/elasticsearch/index/analysis/commongrams/commongrams.json") + .loadFromStream(json, getClass().getResourceAsStream(json)) .put("path.home", createTempDir().toString()) .build(); { @@ -218,8 +219,9 @@ public class CommonGramsTokenFilterFactoryTests extends ESTokenStreamTestCase { @Test public void testQueryModeCommonGramsAnalysis() throws IOException { + String json = "/org/elasticsearch/index/analysis/commongrams/commongrams_query_mode.json"; Settings settings = Settings.settingsBuilder() - .loadFromClasspath("org/elasticsearch/index/analysis/commongrams/commongrams_query_mode.json") + .loadFromStream(json, getClass().getResourceAsStream(json)) .put("path.home", createTempDir().toString()) .build(); { diff --git a/core/src/test/java/org/elasticsearch/index/analysis/synonyms/SynonymsAnalysisTest.java b/core/src/test/java/org/elasticsearch/index/analysis/synonyms/SynonymsAnalysisTest.java index beb5ae3b51a..78349a012bb 100644 --- a/core/src/test/java/org/elasticsearch/index/analysis/synonyms/SynonymsAnalysisTest.java +++ b/core/src/test/java/org/elasticsearch/index/analysis/synonyms/SynonymsAnalysisTest.java @@ -59,8 +59,9 @@ public class SynonymsAnalysisTest extends ESTestCase { @Test public void testSynonymsAnalysis() throws IOException { + String json = "/org/elasticsearch/index/analysis/synonyms/synonyms.json"; Settings settings = settingsBuilder(). - loadFromClasspath("org/elasticsearch/index/analysis/synonyms/synonyms.json") + loadFromStream(json, getClass().getResourceAsStream(json)) .put("path.home", createTempDir().toString()) .put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT).build(); diff --git a/core/src/test/java/org/elasticsearch/node/internal/InternalSettingsPreparerTests.java b/core/src/test/java/org/elasticsearch/node/internal/InternalSettingsPreparerTests.java index a299be41093..e77a1410a91 100644 --- a/core/src/test/java/org/elasticsearch/node/internal/InternalSettingsPreparerTests.java +++ b/core/src/test/java/org/elasticsearch/node/internal/InternalSettingsPreparerTests.java @@ -222,19 +222,4 @@ public class InternalSettingsPreparerTests extends ESTestCase { assertThat(settings.get("name"), is("prompted name 0")); assertThat(settings.get("node.name"), is("prompted name 0")); } - - @Test - public void testPreserveSettingsClassloader() { - final ClassLoader classLoader = URLClassLoader.newInstance(new URL[0]); - Settings settings = settingsBuilder() - .put("foo", "bar") - .put("path.home", createTempDir()) - .classLoader(classLoader) - .build(); - - Tuple tuple = InternalSettingsPreparer.prepareSettings(settings, randomBoolean()); - - Settings preparedSettings = tuple.v1(); - assertThat(preparedSettings.getClassLoaderIfSet(), is(classLoader)); - } } diff --git a/plugins/analysis-kuromoji/src/test/java/org/elasticsearch/index/analysis/KuromojiAnalysisTests.java b/plugins/analysis-kuromoji/src/test/java/org/elasticsearch/index/analysis/KuromojiAnalysisTests.java index 3468a8e2bdc..be9f00768d8 100644 --- a/plugins/analysis-kuromoji/src/test/java/org/elasticsearch/index/analysis/KuromojiAnalysisTests.java +++ b/plugins/analysis-kuromoji/src/test/java/org/elasticsearch/index/analysis/KuromojiAnalysisTests.java @@ -191,9 +191,10 @@ public class KuromojiAnalysisTests extends ESTestCase { public AnalysisService createAnalysisService() { + String json = "/org/elasticsearch/index/analysis/kuromoji_analysis.json"; Settings settings = Settings.settingsBuilder() .put("path.home", createTempDir()) - .loadFromClasspath("org/elasticsearch/index/analysis/kuromoji_analysis.json") + .loadFromStream(json, getClass().getResourceAsStream(json)) .put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT) .build(); diff --git a/plugins/analysis-phonetic/src/test/java/org/elasticsearch/index/analysis/SimplePhoneticAnalysisTests.java b/plugins/analysis-phonetic/src/test/java/org/elasticsearch/index/analysis/SimplePhoneticAnalysisTests.java index ed3da957571..e501d06cf3e 100644 --- a/plugins/analysis-phonetic/src/test/java/org/elasticsearch/index/analysis/SimplePhoneticAnalysisTests.java +++ b/plugins/analysis-phonetic/src/test/java/org/elasticsearch/index/analysis/SimplePhoneticAnalysisTests.java @@ -45,7 +45,8 @@ public class SimplePhoneticAnalysisTests extends ESTestCase { @Test public void testPhoneticTokenFilterFactory() { - Settings settings = settingsBuilder().loadFromClasspath("org/elasticsearch/index/analysis/phonetic-1.yml") + String yaml = "/org/elasticsearch/index/analysis/phonetic-1.yml"; + Settings settings = settingsBuilder().loadFromStream(yaml, getClass().getResourceAsStream(yaml)) .put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT) .put("path.home", createTempDir()) .build();