Repository: tika Updated Branches: refs/heads/2.x a0f365524 -> b73cd8ce8
TIKA-2074 - ServiceLoader can use Class files loaded via dynamic loading Project: http://git-wip-us.apache.org/repos/asf/tika/repo Commit: http://git-wip-us.apache.org/repos/asf/tika/commit/b73cd8ce Tree: http://git-wip-us.apache.org/repos/asf/tika/tree/b73cd8ce Diff: http://git-wip-us.apache.org/repos/asf/tika/diff/b73cd8ce Branch: refs/heads/2.x Commit: b73cd8ce867402491d73f7289422eb08de62b565 Parents: a0f3655 Author: Bob Paulin <[email protected]> Authored: Fri Sep 9 22:34:54 2016 -0500 Committer: Bob Paulin <[email protected]> Committed: Fri Sep 9 22:34:54 2016 -0500 ---------------------------------------------------------------------- .../org/apache/tika/config/ServiceLoader.java | 25 ++++++- .../apache/tika/config/ServiceLoaderTest.java | 73 ++++++++++++++++++++ 2 files changed, 97 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tika/blob/b73cd8ce/tika-core/src/main/java/org/apache/tika/config/ServiceLoader.java ---------------------------------------------------------------------- diff --git a/tika-core/src/main/java/org/apache/tika/config/ServiceLoader.java b/tika-core/src/main/java/org/apache/tika/config/ServiceLoader.java index 37d61e2..28084d0 100644 --- a/tika-core/src/main/java/org/apache/tika/config/ServiceLoader.java +++ b/tika-core/src/main/java/org/apache/tika/config/ServiceLoader.java @@ -116,6 +116,18 @@ public class ServiceLoader { return services.remove(reference); } } + + static Class getDynamicServiceClass(String klass) throws ClassNotFoundException { + synchronized (services) { + for(RankedService currentRankedService: services.values()) + { + Class serviceClass = currentRankedService.service.getClass(); + if(serviceClass.getName().equals(klass)) + return serviceClass; + } + } + throw new ClassNotFoundException("Class " + klass + " could not be found in dynamic loader."); + } private final ClassLoader loader; @@ -204,7 +216,18 @@ public class ServiceLoader { throw new ClassNotFoundException( "Service class " + name + " is not available"); } - Class<?> klass = Class.forName(name, true, loader); + Class<?> klass = null; + try{ + klass = Class.forName(name, true, loader); + }catch(ClassNotFoundException e) + { + if(this.dynamic) { + klass = getDynamicServiceClass(name); + } + else{ + throw e; + } + } if (klass.isInterface()) { throw new ClassNotFoundException( "Service class " + name + " is an interface"); http://git-wip-us.apache.org/repos/asf/tika/blob/b73cd8ce/tika-core/src/test/java/org/apache/tika/config/ServiceLoaderTest.java ---------------------------------------------------------------------- diff --git a/tika-core/src/test/java/org/apache/tika/config/ServiceLoaderTest.java b/tika-core/src/test/java/org/apache/tika/config/ServiceLoaderTest.java new file mode 100644 index 0000000..2df5d67 --- /dev/null +++ b/tika-core/src/test/java/org/apache/tika/config/ServiceLoaderTest.java @@ -0,0 +1,73 @@ +/* + * 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.config; + +import static org.junit.Assert.*; + +import org.apache.tika.detect.Detector; +import org.apache.tika.parser.DummyProxyParser; +import org.apache.tika.parser.Parser; +import org.junit.Before; +import org.junit.Test; + +public class ServiceLoaderTest { + + + @Before + public void setUp(){ + ServiceLoader.addService(new Object(), new DummyProxyParser(), 1); + } + @Test + public void testDynamicClass() throws ClassNotFoundException { + ServiceLoader.getDynamicServiceClass("org.apache.tika.parser.DummyProxyParser"); + } + + @Test + public void testServiceClass() throws ClassNotFoundException { + ClassLoader fakeClassLoader = new ClassLoader(null) { + }; + ServiceLoader loader = new ServiceLoader(fakeClassLoader, LoadErrorHandler.IGNORE, true); + loader.getServiceClass(Parser.class, "org.apache.tika.parser.DummyProxyParser"); + } + + @Test + public void testNonDynamicServiceLoader() { + ClassLoader fakeClassLoader = new ClassLoader(null) { + }; + ServiceLoader loader = new ServiceLoader(fakeClassLoader); + try { + loader.getServiceClass(Parser.class, "org.apache.tika.parser.DummyProxyParser"); + fail("Non Dynamic Classloading. Should throw Exception"); + } catch (ClassNotFoundException e) { + //Should throw Exception + } + } + + @Test + public void testNonMatchingInterface() { + ClassLoader fakeClassLoader = new ClassLoader() { + }; + ServiceLoader loader = new ServiceLoader(fakeClassLoader, LoadErrorHandler.IGNORE, true); + try { + loader.getServiceClass(Detector.class, "org.apache.tika.parser.DummyProxyParser"); + fail("Interface does not match Implementation. Should throw Exception."); + } catch (ClassNotFoundException e) { + //Should throw Exception + } + } + +}
