You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tika.apache.org by bo...@apache.org on 2016/03/26 05:18:39 UTC
tika git commit: TIKA-1909 - Allow Proxy Parser and Detectors to
accept Classloaders
Repository: tika
Updated Branches:
refs/heads/2.x 5993a9649 -> f403a0832
TIKA-1909 - Allow Proxy Parser and Detectors to accept Classloaders
Project: http://git-wip-us.apache.org/repos/asf/tika/repo
Commit: http://git-wip-us.apache.org/repos/asf/tika/commit/f403a083
Tree: http://git-wip-us.apache.org/repos/asf/tika/tree/f403a083
Diff: http://git-wip-us.apache.org/repos/asf/tika/diff/f403a083
Branch: refs/heads/2.x
Commit: f403a083289a1d2fc0aee469cca6d8c2f5ae9f19
Parents: 5993a96
Author: Bob Paulin <bo...@apache.org>
Authored: Fri Mar 25 23:15:34 2016 -0500
Committer: Bob Paulin <bo...@apache.org>
Committed: Fri Mar 25 23:18:08 2016 -0500
----------------------------------------------------------------------
.../apache/tika/detect/AbstractDetector.java | 43 +++++++++++++++
.../org/apache/tika/detect/DetectorProxy.java | 8 +--
.../org/apache/tika/parser/AbstractParser.java | 11 ++++
.../org/apache/tika/parser/ParserProxy.java | 55 ++++++++------------
.../apache/tika/detect/DetectorProxyTest.java | 4 +-
.../org/apache/tika/parser/ParserProxyTest.java | 2 +
6 files changed, 85 insertions(+), 38 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/tika/blob/f403a083/tika-core/src/main/java/org/apache/tika/detect/AbstractDetector.java
----------------------------------------------------------------------
diff --git a/tika-core/src/main/java/org/apache/tika/detect/AbstractDetector.java b/tika-core/src/main/java/org/apache/tika/detect/AbstractDetector.java
new file mode 100644
index 0000000..f0d6129
--- /dev/null
+++ b/tika-core/src/main/java/org/apache/tika/detect/AbstractDetector.java
@@ -0,0 +1,43 @@
+/*
+ * 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.
+ */
+package org.apache.tika.detect;
+
+/**
+ * Abstract base class for new detectors. This class has a convenience method for
+ * creating a DetectorProxy
+ *
+ * @since Apache Tika 2.0
+ */
+public abstract class AbstractDetector implements Detector {
+
+ /**
+ * Serial version UID.
+ */
+ private static final long serialVersionUID = -5869078281784941763L;
+
+ /**
+ * Convenience method for creating DetectorProxy instances
+ * with the current class' ClassLoader
+ *
+ * @param detectorClassName
+ * @return
+ */
+ public Detector createDetectorProxy(String detectorClassName){
+ return new DetectorProxy(detectorClassName, getClass().getClassLoader());
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/tika/blob/f403a083/tika-core/src/main/java/org/apache/tika/detect/DetectorProxy.java
----------------------------------------------------------------------
diff --git a/tika-core/src/main/java/org/apache/tika/detect/DetectorProxy.java b/tika-core/src/main/java/org/apache/tika/detect/DetectorProxy.java
index 5714cd3..d222612 100644
--- a/tika-core/src/main/java/org/apache/tika/detect/DetectorProxy.java
+++ b/tika-core/src/main/java/org/apache/tika/detect/DetectorProxy.java
@@ -37,16 +37,16 @@ public class DetectorProxy implements Detector
private Detector detector;
- public DetectorProxy(String detectorClassName)
+ public DetectorProxy(String detectorClassName, ClassLoader loader)
{
- this(detectorClassName, LoadErrorHandler.WARN);
+ this(detectorClassName, loader, LoadErrorHandler.IGNORE);
}
- public DetectorProxy(String detectorClassName, LoadErrorHandler handler)
+ public DetectorProxy(String detectorClassName, ClassLoader loader, LoadErrorHandler handler)
{
try
{
- this.detector = (Detector)Class.forName(detectorClassName).newInstance();
+ this.detector = (Detector)Class.forName(detectorClassName, true, loader).newInstance();
}
catch (InstantiationException | IllegalAccessException | ClassNotFoundException e)
{
http://git-wip-us.apache.org/repos/asf/tika/blob/f403a083/tika-core/src/main/java/org/apache/tika/parser/AbstractParser.java
----------------------------------------------------------------------
diff --git a/tika-core/src/main/java/org/apache/tika/parser/AbstractParser.java b/tika-core/src/main/java/org/apache/tika/parser/AbstractParser.java
index 2411f05..a4c7719 100644
--- a/tika-core/src/main/java/org/apache/tika/parser/AbstractParser.java
+++ b/tika-core/src/main/java/org/apache/tika/parser/AbstractParser.java
@@ -52,5 +52,16 @@ public abstract class AbstractParser implements Parser {
throws IOException, SAXException, TikaException {
parse(stream, handler, metadata, new ParseContext());
}
+
+ /**
+ * Convenience method for creating ParserProxy instances
+ * with the current class' ClassLoader
+ *
+ * @param parserClassName
+ * @return
+ */
+ public Parser createParserProxy(String parserClassName){
+ return new ParserProxy(parserClassName, getClass().getClassLoader());
+ }
}
http://git-wip-us.apache.org/repos/asf/tika/blob/f403a083/tika-core/src/main/java/org/apache/tika/parser/ParserProxy.java
----------------------------------------------------------------------
diff --git a/tika-core/src/main/java/org/apache/tika/parser/ParserProxy.java b/tika-core/src/main/java/org/apache/tika/parser/ParserProxy.java
index b664c0a..47790e9 100644
--- a/tika-core/src/main/java/org/apache/tika/parser/ParserProxy.java
+++ b/tika-core/src/main/java/org/apache/tika/parser/ParserProxy.java
@@ -29,42 +29,33 @@ import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
/**
- * This parser is a proxy for another detector
- * this allows modules to use parsers from other modules
- * as optional dependencies since not including the classes
- * simply does nothing rather than throwing a ClassNotFoundException.
+ * This parser is a proxy for another detector this allows modules to use
+ * parsers from other modules as optional dependencies since not including the
+ * classes simply does nothing rather than throwing a ClassNotFoundException.
*
* @since Apache Tika 2.0
*/
-public class ParserProxy extends AbstractParser
-{
-
+public class ParserProxy extends AbstractParser {
+
private static final long serialVersionUID = -4838436708916910179L;
private Parser parser;
-
- public ParserProxy(String parserClassName)
- {
- this(parserClassName, LoadErrorHandler.WARN);
+
+ public ParserProxy(String parserClassName, ClassLoader loader) {
+ this(parserClassName, loader, LoadErrorHandler.IGNORE);
}
-
- public ParserProxy(String parserClassName, LoadErrorHandler handler)
- {
- try
- {
- this.parser = (Parser)Class.forName(parserClassName).newInstance();
- }
- catch (InstantiationException | IllegalAccessException | ClassNotFoundException e)
- {
- handler.handleLoadError(parserClassName, e);
- }
-
+
+ public ParserProxy(String parserClassName, ClassLoader loader, LoadErrorHandler handler) {
+ try {
+ this.parser = (Parser) Class.forName(parserClassName, true, loader).newInstance();
+ } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
+ handler.handleLoadError(parserClassName, e);
+ }
+
}
-
+
@Override
- public Set<MediaType> getSupportedTypes(ParseContext context)
- {
- if (parser == null)
- {
+ public Set<MediaType> getSupportedTypes(ParseContext context) {
+ if (parser == null) {
return Collections.emptySet();
}
return parser.getSupportedTypes(context);
@@ -72,12 +63,10 @@ public class ParserProxy extends AbstractParser
@Override
public void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context)
- throws IOException, SAXException, TikaException
- {
- if(parser != null)
- {
+ throws IOException, SAXException, TikaException {
+ if (parser != null) {
parser.parse(stream, handler, metadata, context);
}
- //Otherwise do nothing
+ // Otherwise do nothing
}
}
http://git-wip-us.apache.org/repos/asf/tika/blob/f403a083/tika-core/src/test/java/org/apache/tika/detect/DetectorProxyTest.java
----------------------------------------------------------------------
diff --git a/tika-core/src/test/java/org/apache/tika/detect/DetectorProxyTest.java b/tika-core/src/test/java/org/apache/tika/detect/DetectorProxyTest.java
index 800413d..060f3d9 100644
--- a/tika-core/src/test/java/org/apache/tika/detect/DetectorProxyTest.java
+++ b/tika-core/src/test/java/org/apache/tika/detect/DetectorProxyTest.java
@@ -29,7 +29,8 @@ public class DetectorProxyTest
@Test
public void testDetectorProxyExists() throws IOException
{
- Detector dummyDetector = new DetectorProxy("org.apache.tika.detect.DummyProxyDetector",
+ Detector dummyDetector = new DetectorProxy("org.apache.tika.detect.DummyProxyDetector",
+ getClass().getClassLoader(),
LoadErrorHandler.IGNORE);
MediaType result = dummyDetector.detect(null, null);
@@ -43,6 +44,7 @@ public class DetectorProxyTest
public void testParserProxyNotExists() throws IOException
{
Detector dummyDetector = new DetectorProxy("org.apache.tika.detect.DoesNotExist",
+ getClass().getClassLoader(),
LoadErrorHandler.IGNORE);
MediaType result = dummyDetector.detect(null, null);
http://git-wip-us.apache.org/repos/asf/tika/blob/f403a083/tika-core/src/test/java/org/apache/tika/parser/ParserProxyTest.java
----------------------------------------------------------------------
diff --git a/tika-core/src/test/java/org/apache/tika/parser/ParserProxyTest.java b/tika-core/src/test/java/org/apache/tika/parser/ParserProxyTest.java
index 13a8665..20c6247 100644
--- a/tika-core/src/test/java/org/apache/tika/parser/ParserProxyTest.java
+++ b/tika-core/src/test/java/org/apache/tika/parser/ParserProxyTest.java
@@ -33,6 +33,7 @@ public class ParserProxyTest
public void testParserProxyExists() throws IOException, SAXException, TikaException
{
Parser dummyParser = new ParserProxy("org.apache.tika.parser.DummyProxyParser",
+ getClass().getClassLoader(),
LoadErrorHandler.IGNORE);
Metadata metadata = new Metadata();
@@ -48,6 +49,7 @@ public class ParserProxyTest
public void testParserProxyNotExists() throws IOException, SAXException, TikaException
{
Parser dummyParser = new ParserProxy("org.apache.tika.parser.NotExists",
+ getClass().getClassLoader(),
LoadErrorHandler.IGNORE);
Metadata metadata = new Metadata();