You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tika.apache.org by ni...@apache.org on 2015/08/01 22:40:39 UTC
svn commit: r1693759 - in /tika/trunk/tika-core/src:
main/java/org/apache/tika/config/ test/java/org/apache/tika/config/
test/resources/org/apache/tika/config/
Author: nick
Date: Sat Aug 1 20:40:39 2015
New Revision: 1693759
URL: http://svn.apache.org/r1693759
Log:
TIKA-1700 Add TikaConfig constructors that take a ServiceLoader, and add a unit test that shows we (now) use the LoadErrorHandler on that properly for reporting problems with listed class names
Added:
tika/trunk/tika-core/src/test/resources/org/apache/tika/config/TIKA-1700-unknown-parser.xml
Modified:
tika/trunk/tika-core/src/main/java/org/apache/tika/config/ServiceLoader.java
tika/trunk/tika-core/src/main/java/org/apache/tika/config/TikaConfig.java
tika/trunk/tika-core/src/test/java/org/apache/tika/config/AbstractTikaConfigTest.java
tika/trunk/tika-core/src/test/java/org/apache/tika/config/TikaConfigTest.java
Modified: tika/trunk/tika-core/src/main/java/org/apache/tika/config/ServiceLoader.java
URL: http://svn.apache.org/viewvc/tika/trunk/tika-core/src/main/java/org/apache/tika/config/ServiceLoader.java?rev=1693759&r1=1693758&r2=1693759&view=diff
==============================================================================
--- tika/trunk/tika-core/src/main/java/org/apache/tika/config/ServiceLoader.java (original)
+++ tika/trunk/tika-core/src/main/java/org/apache/tika/config/ServiceLoader.java Sat Aug 1 20:40:39 2015
@@ -173,6 +173,10 @@ public class ServiceLoader {
/**
* Loads and returns the named service class that's expected to implement
* the given interface.
+ *
+ * Note that this class does not use the {@link LoadErrorHandler}, a
+ * {@link ClassNotFoundException} is always returned for unknown
+ * classes or classes of the wrong type
*
* @param iface service interface
* @param name service class name
Modified: tika/trunk/tika-core/src/main/java/org/apache/tika/config/TikaConfig.java
URL: http://svn.apache.org/viewvc/tika/trunk/tika-core/src/main/java/org/apache/tika/config/TikaConfig.java?rev=1693759&r1=1693758&r2=1693759&view=diff
==============================================================================
--- tika/trunk/tika-core/src/main/java/org/apache/tika/config/TikaConfig.java (original)
+++ tika/trunk/tika-core/src/main/java/org/apache/tika/config/TikaConfig.java Sat Aug 1 20:40:39 2015
@@ -92,18 +92,25 @@ public class TikaConfig {
public TikaConfig(File file)
throws TikaException, IOException, SAXException {
- this(getBuilder().parse(file));
+ this(file, new ServiceLoader());
+ }
+ public TikaConfig(File file, ServiceLoader loader)
+ throws TikaException, IOException, SAXException {
+ this(getBuilder().parse(file), loader);
}
public TikaConfig(URL url)
throws TikaException, IOException, SAXException {
this(url, ServiceLoader.getContextClassLoader());
}
-
public TikaConfig(URL url, ClassLoader loader)
throws TikaException, IOException, SAXException {
this(getBuilder().parse(url.toString()).getDocumentElement(), loader);
}
+ public TikaConfig(URL url, ServiceLoader loader)
+ throws TikaException, IOException, SAXException {
+ this(getBuilder().parse(url.toString()).getDocumentElement(), loader);
+ }
public TikaConfig(InputStream stream)
throws TikaException, IOException, SAXException {
@@ -113,6 +120,9 @@ public class TikaConfig {
public TikaConfig(Document document) throws TikaException, IOException {
this(document.getDocumentElement());
}
+ public TikaConfig(Document document, ServiceLoader loader) throws TikaException, IOException {
+ this(document.getDocumentElement(), loader);
+ }
public TikaConfig(Element element) throws TikaException, IOException {
this(element, new ServiceLoader());
@@ -418,7 +428,8 @@ public class TikaConfig {
// Find the children of the parent tag, if any
for (Element le : getTopLevelElementChildren(element, getParentTagName(), getLoaderTagName())) {
- loaded.add(loadOne(le, mimeTypes, loader));
+ T loadedChild = loadOne(le, mimeTypes, loader);
+ if (loadedChild != null) loaded.add(loadedChild);
}
// Build the classes, and wrap as needed
@@ -462,9 +473,9 @@ public class TikaConfig {
NodeList childNodes = element.getElementsByTagName(getLoaderTagName());
if (childNodes.getLength() > 0) {
for (int i = 0; i < childNodes.getLength(); i++) {
- children.add(loadOne(
- (Element)childNodes.item(i), mimeTypes, loader
- ));
+ T loadedChild = loadOne((Element)childNodes.item(i),
+ mimeTypes, loader);
+ if (loadedChild != null) children.add(loadedChild);
}
}
@@ -499,8 +510,14 @@ public class TikaConfig {
// All done with setup
return loaded;
} catch (ClassNotFoundException e) {
- throw new TikaException(
+ if (loader.getLoadErrorHandler() == LoadErrorHandler.THROW) {
+ // Use a different exception signature here
+ throw new TikaException(
"Unable to find a "+getLoaderTagName()+" class: " + name, e);
+ }
+ // Report the problem
+ loader.getLoadErrorHandler().handleLoadError(name, e);
+ return null;
} catch (IllegalAccessException e) {
throw new TikaException(
"Unable to access a "+getLoaderTagName()+" class: " + name, e);
@@ -510,7 +527,8 @@ public class TikaConfig {
} catch (InstantiationException e) {
throw new TikaException(
"Unable to instantiate a "+getLoaderTagName()+" class: " + name, e);
- } }
+ }
+ }
}
private static class ParserXmlLoader extends XmlLoader<CompositeParser,Parser> {
boolean supportsComposite() { return true; }
Modified: tika/trunk/tika-core/src/test/java/org/apache/tika/config/AbstractTikaConfigTest.java
URL: http://svn.apache.org/viewvc/tika/trunk/tika-core/src/test/java/org/apache/tika/config/AbstractTikaConfigTest.java?rev=1693759&r1=1693758&r2=1693759&view=diff
==============================================================================
--- tika/trunk/tika-core/src/test/java/org/apache/tika/config/AbstractTikaConfigTest.java (original)
+++ tika/trunk/tika-core/src/test/java/org/apache/tika/config/AbstractTikaConfigTest.java Sat Aug 1 20:40:39 2015
@@ -16,6 +16,8 @@
*/
package org.apache.tika.config;
+import static org.junit.Assert.assertNotNull;
+
import java.net.URL;
import org.apache.tika.parser.ParseContext;
@@ -30,9 +32,13 @@ import org.junit.After;
public abstract class AbstractTikaConfigTest {
protected static ParseContext context = new ParseContext();
- protected static TikaConfig getConfig(String config) throws Exception {
+ protected static String getConfigPath(String config) throws Exception {
URL url = TikaConfig.class.getResource(config);
- System.setProperty("tika.config", url.toExternalForm());
+ assertNotNull("Test Tika Config not found: " + config, url);
+ return url.toExternalForm();
+ }
+ protected static TikaConfig getConfig(String config) throws Exception {
+ System.setProperty("tika.config", getConfigPath(config));
return new TikaConfig();
}
Modified: tika/trunk/tika-core/src/test/java/org/apache/tika/config/TikaConfigTest.java
URL: http://svn.apache.org/viewvc/tika/trunk/tika-core/src/test/java/org/apache/tika/config/TikaConfigTest.java?rev=1693759&r1=1693758&r2=1693759&view=diff
==============================================================================
--- tika/trunk/tika-core/src/test/java/org/apache/tika/config/TikaConfigTest.java (original)
+++ tika/trunk/tika-core/src/test/java/org/apache/tika/config/TikaConfigTest.java Sat Aug 1 20:40:39 2015
@@ -16,6 +16,8 @@
*/
package org.apache.tika.config;
+import java.io.File;
+import java.net.URI;
import java.net.URL;
import java.util.List;
import java.util.Map;
@@ -65,7 +67,28 @@ public class TikaConfigTest extends Abst
*/
@Test
public void testUnknownParser() throws Exception {
- // TODO
+ ServiceLoader ignoreLoader = new ServiceLoader(
+ getClass().getClassLoader(), LoadErrorHandler.IGNORE);
+ ServiceLoader warnLoader = new ServiceLoader(
+ getClass().getClassLoader(), LoadErrorHandler.WARN);
+ ServiceLoader throwLoader = new ServiceLoader(
+ getClass().getClassLoader(), LoadErrorHandler.THROW);
+ File configPath = new File(new URI(getConfigPath("TIKA-1700-unknown-parser.xml")));
+
+ TikaConfig ignore = new TikaConfig(configPath, ignoreLoader);
+ assertNotNull(ignore);
+ assertNotNull(ignore.getParser());
+ assertEquals(1, ((CompositeParser)ignore.getParser()).getAllComponentParsers().size());
+
+ TikaConfig warn = new TikaConfig(configPath, warnLoader);
+ assertNotNull(warn);
+ assertNotNull(warn.getParser());
+ assertEquals(1, ((CompositeParser)warn.getParser()).getAllComponentParsers().size());
+
+ try {
+ new TikaConfig(configPath, throwLoader);
+ fail("Shouldn't get here, invalid parser class");
+ } catch (TikaException expected) {}
}
/**
Added: tika/trunk/tika-core/src/test/resources/org/apache/tika/config/TIKA-1700-unknown-parser.xml
URL: http://svn.apache.org/viewvc/tika/trunk/tika-core/src/test/resources/org/apache/tika/config/TIKA-1700-unknown-parser.xml?rev=1693759&view=auto
==============================================================================
--- tika/trunk/tika-core/src/test/resources/org/apache/tika/config/TIKA-1700-unknown-parser.xml (added)
+++ tika/trunk/tika-core/src/test/resources/org/apache/tika/config/TIKA-1700-unknown-parser.xml Sat Aug 1 20:40:39 2015
@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ 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.
+-->
+<properties>
+ <parsers>
+ <parser class="org.apache.tika.parser.EmptyParser">
+ <mime>hello/world</mime>
+ </parser>
+ <parser class="made.up.invalid" />
+ </parsers>
+</properties>