diff --git a/lucene/analysis/common/src/java/org/apache/lucene/analysis/util/ClasspathResourceLoader.java b/lucene/analysis/common/src/java/org/apache/lucene/analysis/util/ClasspathResourceLoader.java new file mode 100644 index 00000000000..e0097b4601b --- /dev/null +++ b/lucene/analysis/common/src/java/org/apache/lucene/analysis/util/ClasspathResourceLoader.java @@ -0,0 +1,80 @@ +package org.apache.lucene.analysis.util; + +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import java.io.IOException; +import java.io.InputStream; + +/** + * Simple {@link ResourceLoader} that uses {@link ClassLoader#getResourceAsStream(String)} + * and {@link Class#forName(String,boolean,ClassLoader)} to open resources and + * classes, respectively. + */ +public final class ClasspathResourceLoader implements ResourceLoader { + private final Class clazz; + private final ClassLoader loader; + + /** + * Creates an instance using the context classloader to load Resources and classes. + * Resource paths must be absolute. + */ + public ClasspathResourceLoader() { + this(Thread.currentThread().getContextClassLoader()); + } + + /** + * Creates an instance using the given classloader to load Resources and classes. + * Resource paths must be absolute. + */ + public ClasspathResourceLoader(ClassLoader loader) { + this(null, loader); + } + + /** + * Creates an instance using the context classloader to load Resources and classes + * Resources are resolved relative to the given class, if path is not absolute. + */ + public ClasspathResourceLoader(Class clazz) { + this(clazz, clazz.getClassLoader()); + } + + private ClasspathResourceLoader(Class clazz, ClassLoader loader) { + this.clazz = clazz; + this.loader = loader; + } + + @Override + public InputStream openResource(String resource) throws IOException { + final InputStream stream = (clazz != null) ? + clazz.getResourceAsStream(resource) : + loader.getResourceAsStream(resource); + if (stream == null) + throw new IOException("Resource not found: " + resource); + return stream; + } + + @Override + public T newInstance(String cname, Class expectedType) { + try { + final Class clazz = Class.forName(cname, true, loader).asSubclass(expectedType); + return clazz.newInstance(); + } catch (Exception e) { + throw new RuntimeException("Cannot instantiate class: " + cname, e); + } + } +} diff --git a/lucene/analysis/common/src/java/org/apache/lucene/analysis/util/FilesystemResourceLoader.java b/lucene/analysis/common/src/java/org/apache/lucene/analysis/util/FilesystemResourceLoader.java new file mode 100644 index 00000000000..744b1b70ee3 --- /dev/null +++ b/lucene/analysis/common/src/java/org/apache/lucene/analysis/util/FilesystemResourceLoader.java @@ -0,0 +1,94 @@ +package org.apache.lucene.analysis.util; + +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; + +/** + * Simple {@link ResourceLoader} that opens resource files + * from the local file system, optionally resolving against + * a base directory. + * + *

This loader wraps a delegate {@link ResourceLoader} + * that is used to resolve all files, the current base directory + * does not contain. {@link #newInstance} is always resolved + * against the delegate, as a {@link ClassLoader} is needed. + * + *

You can chain several {@code FilesystemResourceLoader}s + * to allow lookup of files in more than one base directory. + */ +public final class FilesystemResourceLoader implements ResourceLoader { + private final File baseDirectory; + private final ResourceLoader delegate; + + /** + * Creates a resource loader that requires absolute filenames or relative to CWD + * to resolve resources. Files not found in file system and class lookups + * are delegated to context classloader. + */ + public FilesystemResourceLoader() { + this((File) null); + } + + /** + * Creates a resource loader that resolves resources against the given + * base directory (may be {@code null} to refer to CWD). + * Files not found in file system and class lookups are delegated to context + * classloader. + */ + public FilesystemResourceLoader(File baseDirectory) { + this(baseDirectory, new ClasspathResourceLoader()); + } + + /** + * Creates a resource loader that resolves resources against the given + * base directory (may be {@code null} to refer to CWD). + * Files not found in file system and class lookups are delegated + * to the given delegate {@link ResourceLoader}. + */ + public FilesystemResourceLoader(File baseDirectory, ResourceLoader delegate) { + if (baseDirectory != null && !baseDirectory.isDirectory()) + throw new IllegalArgumentException("baseDirectory is not a directory or null"); + if (delegate == null) + throw new IllegalArgumentException("delegate ResourceLoader may not be null"); + this.baseDirectory = baseDirectory; + this.delegate = delegate; + } + + @Override + public InputStream openResource(String resource) throws IOException { + try { + File file = new File (resource); + if (baseDirectory != null && !file.isAbsolute()) { + file = new File(baseDirectory, resource); + } + return new FileInputStream(file); + } catch (FileNotFoundException fnfe) { + return delegate.openResource(resource); + } + } + + @Override + public T newInstance(String cname, Class expectedType) { + return delegate.newInstance(cname, expectedType); + } +} diff --git a/lucene/analysis/common/src/java/org/apache/lucene/analysis/util/ResourceAsStreamResourceLoader.java b/lucene/analysis/common/src/java/org/apache/lucene/analysis/util/ResourceAsStreamResourceLoader.java deleted file mode 100644 index d53944b6df8..00000000000 --- a/lucene/analysis/common/src/java/org/apache/lucene/analysis/util/ResourceAsStreamResourceLoader.java +++ /dev/null @@ -1,48 +0,0 @@ -package org.apache.lucene.analysis.util; - -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import java.io.IOException; -import java.io.InputStream; - -/** - * Simple ResourceLoader that uses Class.getResourceAsStream - * and Class.forName to open resources and classes, respectively. - */ -public class ResourceAsStreamResourceLoader implements ResourceLoader { - private final Class clazz; - - public ResourceAsStreamResourceLoader(Class clazz) { - this.clazz = clazz; - } - - @Override - public InputStream openResource(String resource) throws IOException { - return clazz.getResourceAsStream(resource); - } - - @Override - public T newInstance(String cname, Class expectedType) { - try { - Class clazz = Class.forName(cname).asSubclass(expectedType); - return clazz.newInstance(); - } catch (Exception e) { - throw new RuntimeException(e); - } - } -} diff --git a/lucene/analysis/common/src/test/org/apache/lucene/analysis/commongrams/TestCommonGramsFilterFactory.java b/lucene/analysis/common/src/test/org/apache/lucene/analysis/commongrams/TestCommonGramsFilterFactory.java index 3739f0812a6..c7167682575 100644 --- a/lucene/analysis/common/src/test/org/apache/lucene/analysis/commongrams/TestCommonGramsFilterFactory.java +++ b/lucene/analysis/common/src/test/org/apache/lucene/analysis/commongrams/TestCommonGramsFilterFactory.java @@ -23,7 +23,7 @@ import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.analysis.Tokenizer; import org.apache.lucene.analysis.core.TestStopFilter; import org.apache.lucene.analysis.util.CharArraySet; -import org.apache.lucene.analysis.util.ResourceAsStreamResourceLoader; +import org.apache.lucene.analysis.util.ClasspathResourceLoader; import org.apache.lucene.analysis.util.ResourceLoader; import java.io.StringReader; @@ -39,7 +39,7 @@ import java.util.HashMap; public class TestCommonGramsFilterFactory extends BaseTokenStreamTestCase { public void testInform() throws Exception { - ResourceLoader loader = new ResourceAsStreamResourceLoader(TestStopFilter.class); + ResourceLoader loader = new ClasspathResourceLoader(TestStopFilter.class); assertTrue("loader is null and it shouldn't be", loader != null); CommonGramsFilterFactory factory = new CommonGramsFilterFactory(); Map args = new HashMap(); @@ -89,7 +89,7 @@ public class TestCommonGramsFilterFactory extends BaseTokenStreamTestCase { * If no words are provided, then a set of english default stopwords is used. */ public void testDefaults() throws Exception { - ResourceLoader loader = new ResourceAsStreamResourceLoader(TestStopFilter.class); + ResourceLoader loader = new ClasspathResourceLoader(TestStopFilter.class); assertTrue("loader is null and it shouldn't be", loader != null); CommonGramsFilterFactory factory = new CommonGramsFilterFactory(); factory.setLuceneMatchVersion(TEST_VERSION_CURRENT); diff --git a/lucene/analysis/common/src/test/org/apache/lucene/analysis/commongrams/TestCommonGramsQueryFilterFactory.java b/lucene/analysis/common/src/test/org/apache/lucene/analysis/commongrams/TestCommonGramsQueryFilterFactory.java index 95de4d413c1..070228ac871 100644 --- a/lucene/analysis/common/src/test/org/apache/lucene/analysis/commongrams/TestCommonGramsQueryFilterFactory.java +++ b/lucene/analysis/common/src/test/org/apache/lucene/analysis/commongrams/TestCommonGramsQueryFilterFactory.java @@ -23,7 +23,7 @@ import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.analysis.Tokenizer; import org.apache.lucene.analysis.core.TestStopFilter; import org.apache.lucene.analysis.util.CharArraySet; -import org.apache.lucene.analysis.util.ResourceAsStreamResourceLoader; +import org.apache.lucene.analysis.util.ClasspathResourceLoader; import org.apache.lucene.analysis.util.ResourceLoader; import java.io.StringReader; @@ -39,7 +39,7 @@ import java.util.HashMap; public class TestCommonGramsQueryFilterFactory extends BaseTokenStreamTestCase { public void testInform() throws Exception { - ResourceLoader loader = new ResourceAsStreamResourceLoader(TestStopFilter.class); + ResourceLoader loader = new ClasspathResourceLoader(TestStopFilter.class); assertTrue("loader is null and it shouldn't be", loader != null); CommonGramsQueryFilterFactory factory = new CommonGramsQueryFilterFactory(); Map args = new HashMap(); @@ -89,7 +89,7 @@ public class TestCommonGramsQueryFilterFactory extends BaseTokenStreamTestCase { * If no words are provided, then a set of english default stopwords is used. */ public void testDefaults() throws Exception { - ResourceLoader loader = new ResourceAsStreamResourceLoader(TestStopFilter.class); + ResourceLoader loader = new ClasspathResourceLoader(TestStopFilter.class); assertTrue("loader is null and it shouldn't be", loader != null); CommonGramsQueryFilterFactory factory = new CommonGramsQueryFilterFactory(); factory.setLuceneMatchVersion(TEST_VERSION_CURRENT); diff --git a/lucene/analysis/common/src/test/org/apache/lucene/analysis/compound/TestDictionaryCompoundWordTokenFilterFactory.java b/lucene/analysis/common/src/test/org/apache/lucene/analysis/compound/TestDictionaryCompoundWordTokenFilterFactory.java index c3b656a287f..e466257941c 100644 --- a/lucene/analysis/common/src/test/org/apache/lucene/analysis/compound/TestDictionaryCompoundWordTokenFilterFactory.java +++ b/lucene/analysis/common/src/test/org/apache/lucene/analysis/compound/TestDictionaryCompoundWordTokenFilterFactory.java @@ -26,7 +26,7 @@ import org.apache.lucene.analysis.BaseTokenStreamTestCase; import org.apache.lucene.analysis.MockTokenizer; import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.analysis.Tokenizer; -import org.apache.lucene.analysis.util.ResourceAsStreamResourceLoader; +import org.apache.lucene.analysis.util.ClasspathResourceLoader; import org.apache.lucene.analysis.util.ResourceLoader; /** @@ -40,7 +40,7 @@ public class TestDictionaryCompoundWordTokenFilterFactory extends BaseTokenStrea Reader reader = new StringReader("I like to play softball"); Tokenizer tokenizer = new MockTokenizer(reader, MockTokenizer.WHITESPACE, false); DictionaryCompoundWordTokenFilterFactory factory = new DictionaryCompoundWordTokenFilterFactory(); - ResourceLoader loader = new ResourceAsStreamResourceLoader(getClass()); + ResourceLoader loader = new ClasspathResourceLoader(getClass()); Map args = new HashMap(); args.put("dictionary", "compoundDictionary.txt"); factory.setLuceneMatchVersion(TEST_VERSION_CURRENT); diff --git a/lucene/analysis/common/src/test/org/apache/lucene/analysis/compound/TestHyphenationCompoundWordTokenFilterFactory.java b/lucene/analysis/common/src/test/org/apache/lucene/analysis/compound/TestHyphenationCompoundWordTokenFilterFactory.java index c5a22f9904f..bf1f8d40f99 100644 --- a/lucene/analysis/common/src/test/org/apache/lucene/analysis/compound/TestHyphenationCompoundWordTokenFilterFactory.java +++ b/lucene/analysis/common/src/test/org/apache/lucene/analysis/compound/TestHyphenationCompoundWordTokenFilterFactory.java @@ -26,7 +26,7 @@ import org.apache.lucene.analysis.BaseTokenStreamTestCase; import org.apache.lucene.analysis.MockTokenizer; import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.analysis.Tokenizer; -import org.apache.lucene.analysis.util.ResourceAsStreamResourceLoader; +import org.apache.lucene.analysis.util.ClasspathResourceLoader; import org.apache.lucene.analysis.util.ResourceLoader; /** @@ -40,7 +40,7 @@ public class TestHyphenationCompoundWordTokenFilterFactory extends BaseTokenStre Reader reader = new StringReader("min veninde som er lidt af en læsehest"); Tokenizer tokenizer = new MockTokenizer(reader, MockTokenizer.WHITESPACE, false); HyphenationCompoundWordTokenFilterFactory factory = new HyphenationCompoundWordTokenFilterFactory(); - ResourceLoader loader = new ResourceAsStreamResourceLoader(getClass()); + ResourceLoader loader = new ClasspathResourceLoader(getClass()); Map args = new HashMap(); args.put("hyphenator", "da_UTF8.xml"); args.put("dictionary", "da_compoundDictionary.txt"); @@ -64,7 +64,7 @@ public class TestHyphenationCompoundWordTokenFilterFactory extends BaseTokenStre Reader reader = new StringReader("basketballkurv"); Tokenizer tokenizer = new MockTokenizer(reader, MockTokenizer.WHITESPACE, false); HyphenationCompoundWordTokenFilterFactory factory = new HyphenationCompoundWordTokenFilterFactory(); - ResourceLoader loader = new ResourceAsStreamResourceLoader(getClass()); + ResourceLoader loader = new ClasspathResourceLoader(getClass()); Map args = new HashMap(); args.put("hyphenator", "da_UTF8.xml"); args.put("minSubwordSize", "2"); diff --git a/lucene/analysis/common/src/test/org/apache/lucene/analysis/core/TestStopFilterFactory.java b/lucene/analysis/common/src/test/org/apache/lucene/analysis/core/TestStopFilterFactory.java index dc977c4f441..3e6c519d1a6 100644 --- a/lucene/analysis/common/src/test/org/apache/lucene/analysis/core/TestStopFilterFactory.java +++ b/lucene/analysis/common/src/test/org/apache/lucene/analysis/core/TestStopFilterFactory.java @@ -19,7 +19,7 @@ package org.apache.lucene.analysis.core; import org.apache.lucene.analysis.BaseTokenStreamTestCase; import org.apache.lucene.analysis.util.CharArraySet; -import org.apache.lucene.analysis.util.ResourceAsStreamResourceLoader; +import org.apache.lucene.analysis.util.ClasspathResourceLoader; import org.apache.lucene.analysis.util.ResourceLoader; import java.util.Map; @@ -32,7 +32,7 @@ import java.util.HashMap; public class TestStopFilterFactory extends BaseTokenStreamTestCase { public void testInform() throws Exception { - ResourceLoader loader = new ResourceAsStreamResourceLoader(getClass()); + ResourceLoader loader = new ClasspathResourceLoader(getClass()); assertTrue("loader is null and it shouldn't be", loader != null); StopFilterFactory factory = new StopFilterFactory(); Map args = new HashMap(); diff --git a/lucene/analysis/common/src/test/org/apache/lucene/analysis/core/TestTypeTokenFilterFactory.java b/lucene/analysis/common/src/test/org/apache/lucene/analysis/core/TestTypeTokenFilterFactory.java index a69b24a8689..39cd5e4cca9 100644 --- a/lucene/analysis/common/src/test/org/apache/lucene/analysis/core/TestTypeTokenFilterFactory.java +++ b/lucene/analysis/common/src/test/org/apache/lucene/analysis/core/TestTypeTokenFilterFactory.java @@ -19,7 +19,7 @@ package org.apache.lucene.analysis.core; import org.apache.lucene.analysis.BaseTokenStreamTestCase; import org.apache.lucene.analysis.NumericTokenStream; -import org.apache.lucene.analysis.util.ResourceAsStreamResourceLoader; +import org.apache.lucene.analysis.util.ClasspathResourceLoader; import org.apache.lucene.analysis.util.ResourceLoader; import org.junit.Test; @@ -34,7 +34,7 @@ public class TestTypeTokenFilterFactory extends BaseTokenStreamTestCase { @Test public void testInform() throws Exception { - ResourceLoader loader = new ResourceAsStreamResourceLoader(getClass()); + ResourceLoader loader = new ClasspathResourceLoader(getClass()); TypeTokenFilterFactory factory = new TypeTokenFilterFactory(); Map args = new HashMap(); args.put("types", "stoptypes-1.txt"); @@ -94,7 +94,7 @@ public class TestTypeTokenFilterFactory extends BaseTokenStreamTestCase { args.put("enablePositionIncrements", "false"); typeTokenFilterFactory.setLuceneMatchVersion(TEST_VERSION_CURRENT); typeTokenFilterFactory.init(args); - typeTokenFilterFactory.inform(new ResourceAsStreamResourceLoader(getClass())); + typeTokenFilterFactory.inform(new ClasspathResourceLoader(getClass())); fail("not supplying 'types' parameter should cause an IllegalArgumentException"); } catch (IllegalArgumentException e) { // everything ok diff --git a/lucene/analysis/common/src/test/org/apache/lucene/analysis/fr/TestElisionFilterFactory.java b/lucene/analysis/common/src/test/org/apache/lucene/analysis/fr/TestElisionFilterFactory.java index 2c48d8e5eda..daef999b67e 100644 --- a/lucene/analysis/common/src/test/org/apache/lucene/analysis/fr/TestElisionFilterFactory.java +++ b/lucene/analysis/common/src/test/org/apache/lucene/analysis/fr/TestElisionFilterFactory.java @@ -27,7 +27,7 @@ import org.apache.lucene.analysis.BaseTokenStreamTestCase; import org.apache.lucene.analysis.MockTokenizer; import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.analysis.Tokenizer; -import org.apache.lucene.analysis.util.ResourceAsStreamResourceLoader; +import org.apache.lucene.analysis.util.ClasspathResourceLoader; import org.apache.lucene.analysis.util.ResourceLoader; /** @@ -42,7 +42,7 @@ public class TestElisionFilterFactory extends BaseTokenStreamTestCase { Tokenizer tokenizer = new MockTokenizer(reader, MockTokenizer.WHITESPACE, false); ElisionFilterFactory factory = new ElisionFilterFactory(); factory.setLuceneMatchVersion(TEST_VERSION_CURRENT); - ResourceLoader loader = new ResourceAsStreamResourceLoader(getClass()); + ResourceLoader loader = new ClasspathResourceLoader(getClass()); Map args = new HashMap(); args.put("articles", "frenchArticles.txt"); factory.init(args); @@ -61,7 +61,7 @@ public class TestElisionFilterFactory extends BaseTokenStreamTestCase { factory.setLuceneMatchVersion(TEST_VERSION_CURRENT); Map args = Collections.emptyMap(); factory.init(args); - ResourceLoader loader = new ResourceAsStreamResourceLoader(getClass()); + ResourceLoader loader = new ClasspathResourceLoader(getClass()); factory.inform(loader); TokenStream stream = factory.create(tokenizer); assertTokenStreamContents(stream, new String[] { "avion" }); @@ -75,7 +75,7 @@ public class TestElisionFilterFactory extends BaseTokenStreamTestCase { Tokenizer tokenizer = new MockTokenizer(reader, MockTokenizer.WHITESPACE, false); ElisionFilterFactory factory = new ElisionFilterFactory(); factory.setLuceneMatchVersion(TEST_VERSION_CURRENT); - ResourceLoader loader = new ResourceAsStreamResourceLoader(getClass()); + ResourceLoader loader = new ClasspathResourceLoader(getClass()); Map args = new HashMap(); args.put("articles", "frenchArticles.txt"); args.put("ignoreCase", "true"); diff --git a/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/TestHunspellStemFilterFactory.java b/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/TestHunspellStemFilterFactory.java index 32c056a2d3c..8caac4a578e 100644 --- a/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/TestHunspellStemFilterFactory.java +++ b/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/TestHunspellStemFilterFactory.java @@ -25,7 +25,7 @@ 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.analysis.util.ResourceAsStreamResourceLoader; +import org.apache.lucene.analysis.util.ClasspathResourceLoader; /** * Simple tests to ensure the Hunspell stemmer loads from factory @@ -38,7 +38,7 @@ public class TestHunspellStemFilterFactory extends BaseTokenStreamTestCase { args.put("affix", "test.aff"); factory.setLuceneMatchVersion(TEST_VERSION_CURRENT); factory.init(args); - factory.inform(new ResourceAsStreamResourceLoader(getClass())); + factory.inform(new ClasspathResourceLoader(getClass())); Reader reader = new StringReader("abc"); TokenStream stream = factory.create(new MockTokenizer(reader, MockTokenizer.WHITESPACE, false)); diff --git a/lucene/analysis/common/src/test/org/apache/lucene/analysis/miscellaneous/TestKeepFilterFactory.java b/lucene/analysis/common/src/test/org/apache/lucene/analysis/miscellaneous/TestKeepFilterFactory.java index 12da04d0ec3..f17ca59f8ad 100644 --- a/lucene/analysis/common/src/test/org/apache/lucene/analysis/miscellaneous/TestKeepFilterFactory.java +++ b/lucene/analysis/common/src/test/org/apache/lucene/analysis/miscellaneous/TestKeepFilterFactory.java @@ -19,7 +19,7 @@ package org.apache.lucene.analysis.miscellaneous; import org.apache.lucene.analysis.BaseTokenStreamTestCase; import org.apache.lucene.analysis.util.CharArraySet; -import org.apache.lucene.analysis.util.ResourceAsStreamResourceLoader; +import org.apache.lucene.analysis.util.ClasspathResourceLoader; import org.apache.lucene.analysis.util.ResourceLoader; import java.util.Map; @@ -32,7 +32,7 @@ import java.util.HashMap; public class TestKeepFilterFactory extends BaseTokenStreamTestCase { public void testInform() throws Exception { - ResourceLoader loader = new ResourceAsStreamResourceLoader(getClass()); + ResourceLoader loader = new ClasspathResourceLoader(getClass()); assertTrue("loader is null and it shouldn't be", loader != null); KeepWordFilterFactory factory = new KeepWordFilterFactory(); Map args = new HashMap(); diff --git a/lucene/analysis/common/src/test/org/apache/lucene/analysis/synonym/TestSynonymFilterFactory.java b/lucene/analysis/common/src/test/org/apache/lucene/analysis/synonym/TestSynonymFilterFactory.java index 9e9196f0cb8..18fec95673f 100644 --- a/lucene/analysis/common/src/test/org/apache/lucene/analysis/synonym/TestSynonymFilterFactory.java +++ b/lucene/analysis/common/src/test/org/apache/lucene/analysis/synonym/TestSynonymFilterFactory.java @@ -25,7 +25,7 @@ import org.apache.lucene.analysis.BaseTokenStreamTestCase; import org.apache.lucene.analysis.MockTokenizer; import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.analysis.synonym.SynonymFilter; -import org.apache.lucene.analysis.util.ResourceAsStreamResourceLoader; +import org.apache.lucene.analysis.util.ClasspathResourceLoader; import org.apache.lucene.analysis.util.StringMockResourceLoader; public class TestSynonymFilterFactory extends BaseTokenStreamTestCase { @@ -36,7 +36,7 @@ public class TestSynonymFilterFactory extends BaseTokenStreamTestCase { args.put("synonyms", "synonyms.txt"); factory.setLuceneMatchVersion(TEST_VERSION_CURRENT); factory.init(args); - factory.inform(new ResourceAsStreamResourceLoader(getClass())); + factory.inform(new ClasspathResourceLoader(getClass())); TokenStream ts = factory.create(new MockTokenizer(new StringReader("GB"), MockTokenizer.WHITESPACE, false)); assertTrue(ts instanceof SynonymFilter); assertTokenStreamContents(ts, diff --git a/lucene/analysis/common/src/test/org/apache/lucene/analysis/util/TestFilesystemResourceLoader.java b/lucene/analysis/common/src/test/org/apache/lucene/analysis/util/TestFilesystemResourceLoader.java new file mode 100644 index 00000000000..96248a2e8db --- /dev/null +++ b/lucene/analysis/common/src/test/org/apache/lucene/analysis/util/TestFilesystemResourceLoader.java @@ -0,0 +1,98 @@ +package org.apache.lucene.analysis.util; + +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.io.Writer; + +import org.apache.lucene.util.IOUtils; +import org.apache.lucene.util.LuceneTestCase; +import org.apache.lucene.util._TestUtil; + +public class TestFilesystemResourceLoader extends LuceneTestCase { + + private void assertNotFound(ResourceLoader rl) throws Exception { + try { + IOUtils.closeWhileHandlingException(rl.openResource("/this-directory-really-really-really-should-not-exist/foo/bar.txt")); + fail("The resource does not exist, should fail!"); + } catch (IOException ioe) { + // pass + } + try { + rl.newInstance("org.apache.lucene.analysis.FooBarFilterFactory", TokenFilterFactory.class); + fail("The class does not exist, should fail!"); + } catch (RuntimeException iae) { + // pass + } + } + + private void assertClasspathDelegation(ResourceLoader rl) throws Exception { + // try a stopwords file from classpath + CharArraySet set = WordlistLoader.getSnowballWordSet( + new InputStreamReader(rl.openResource("org/apache/lucene/analysis/snowball/english_stop.txt"), IOUtils.CHARSET_UTF_8), + TEST_VERSION_CURRENT + ); + assertTrue(set.contains("you")); + // try to load a class; we use string comparison because classloader may be different... + assertEquals("org.apache.lucene.analysis.en.KStemFilterFactory", + rl.newInstance("org.apache.lucene.analysis.en.KStemFilterFactory", TokenFilterFactory.class).getClass().getName()); + // theoretically classes should also be loadable: + IOUtils.closeWhileHandlingException(rl.openResource("java/lang/String.class")); + } + + public void testBaseDir() throws Exception { + final File base = _TestUtil.getTempDir("fsResourceLoaderBase"); + try { + base.mkdirs(); + Writer os = new OutputStreamWriter(new FileOutputStream(new File(base, "template.txt")), IOUtils.CHARSET_UTF_8); + try { + os.write("foobar\n"); + } finally { + IOUtils.closeWhileHandlingException(os); + } + + ResourceLoader rl = new FilesystemResourceLoader(base); + assertEquals("foobar", WordlistLoader.getLines(rl.openResource("template.txt"), IOUtils.CHARSET_UTF_8).get(0)); + // Same with full path name: + String fullPath = new File(base, "template.txt").toString(); + assertEquals("foobar", + WordlistLoader.getLines(rl.openResource(fullPath), IOUtils.CHARSET_UTF_8).get(0)); + assertClasspathDelegation(rl); + assertNotFound(rl); + + // now use RL without base dir: + rl = new FilesystemResourceLoader(); + assertEquals("foobar", + WordlistLoader.getLines(rl.openResource(new File(base, "template.txt").toString()), IOUtils.CHARSET_UTF_8).get(0)); + assertClasspathDelegation(rl); + assertNotFound(rl); + } finally { + _TestUtil.rmDir(base); + } + } + + public void testDelegation() throws Exception { + ResourceLoader rl = new FilesystemResourceLoader(null, new StringMockResourceLoader("foobar\n")); + assertEquals("foobar", WordlistLoader.getLines(rl.openResource("template.txt"), IOUtils.CHARSET_UTF_8).get(0)); + } + +} diff --git a/lucene/analysis/stempel/src/test/org/apache/lucene/analysis/stempel/TestStempelPolishStemFilterFactory.java b/lucene/analysis/stempel/src/test/org/apache/lucene/analysis/stempel/TestStempelPolishStemFilterFactory.java index a3cc40b77e7..8c57d4ab1cc 100644 --- a/lucene/analysis/stempel/src/test/org/apache/lucene/analysis/stempel/TestStempelPolishStemFilterFactory.java +++ b/lucene/analysis/stempel/src/test/org/apache/lucene/analysis/stempel/TestStempelPolishStemFilterFactory.java @@ -22,7 +22,7 @@ import java.io.StringReader; import org.apache.lucene.analysis.BaseTokenStreamTestCase; import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.analysis.core.WhitespaceTokenizer; -import org.apache.lucene.analysis.util.ResourceAsStreamResourceLoader; +import org.apache.lucene.analysis.util.ClasspathResourceLoader; /** * Tests for {@link StempelPolishStemFilterFactory} @@ -31,7 +31,7 @@ public class TestStempelPolishStemFilterFactory extends BaseTokenStreamTestCase public void testBasics() throws Exception { StringReader document = new StringReader("studenta studenci"); StempelPolishStemFilterFactory factory = new StempelPolishStemFilterFactory(); - factory.inform(new ResourceAsStreamResourceLoader(getClass())); + factory.inform(new ClasspathResourceLoader(getClass())); TokenStream ts = factory.create(new WhitespaceTokenizer(TEST_VERSION_CURRENT, document)); assertTokenStreamContents(ts, new String[] { "student", "student" });