Author: nick
Date: Sat Aug  1 22:09:55 2015
New Revision: 1693773

URL: http://svn.apache.org/r1693773
Log:
Patch from Bob Paulin from TIKA-1700 - Allow setting the Service Loader dynamic 
flag and load error handler from the tika config xml

Added:
    tika/trunk/tika-core/src/test/java/org/apache/tika/config/DummyParser.java
    
tika/trunk/tika-core/src/test/resources/org/apache/tika/config/TIKA-1700-dynamic.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/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=1693773&r1=1693772&r2=1693773&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 22:09:55 2015
@@ -142,6 +142,16 @@ public class ServiceLoader {
        this(getContextClassLoader(), 
Boolean.getBoolean("org.apache.tika.service.error.warn") 
                        ? LoadErrorHandler.WARN:LoadErrorHandler.IGNORE, true);
     }
+    
+    /**
+     * Returns if the service loader is static or dynamic
+     * 
+     * @return dynamic or static loading
+     * @since Apache Tika 1.10
+     */
+    public boolean isDynamic() {
+        return dynamic;
+    }
 
     /**
      * Returns the load error handler used by this loader.

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=1693773&r1=1693772&r2=1693773&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 22:09:55 2015
@@ -125,12 +125,12 @@ public class TikaConfig {
     }
 
     public TikaConfig(Element element) throws TikaException, IOException {
-        this(element, new ServiceLoader());
+        this(element, serviceLoaderFromDomElement(element, null));
     }
 
     public TikaConfig(Element element, ClassLoader loader)
             throws TikaException, IOException {
-        this(element, new ServiceLoader(loader));
+        this(element, serviceLoaderFromDomElement(element, loader));
     }
 
     private TikaConfig(Element element, ServiceLoader loader)
@@ -405,6 +405,28 @@ public class TikaConfig {
         return Collections.emptySet();
     }
     
+    private static ServiceLoader serviceLoaderFromDomElement(Element element, 
ClassLoader loader) {
+        Element serviceLoaderElement = getChild(element, "service-loader");
+        ServiceLoader serviceLoader = null;
+        if(serviceLoaderElement != null) {
+            boolean dynamic = 
Boolean.parseBoolean(serviceLoaderElement.getAttribute("dynamic"));
+            LoadErrorHandler loadErrorHandler = LoadErrorHandler.IGNORE;
+            String loadErrorHandleConfig = 
serviceLoaderElement.getAttribute("loadErrorHandler");
+            if("WARN".equalsIgnoreCase(loadErrorHandleConfig)) {
+                loadErrorHandler = LoadErrorHandler.WARN;
+            } else if("THROW".equalsIgnoreCase(loadErrorHandleConfig)) {
+                loadErrorHandler = LoadErrorHandler.THROW;
+            }
+            
+            serviceLoader = new ServiceLoader(loader, loadErrorHandler, 
dynamic);
+        } else if(loader != null) {
+            serviceLoader = new ServiceLoader(loader);
+        } else {
+            serviceLoader = new ServiceLoader();
+        }
+        return serviceLoader;
+    }
+    
     private static abstract class XmlLoader<CT,T> {
         abstract boolean supportsComposite();
         abstract String getParentTagName(); // eg parsers

Added: 
tika/trunk/tika-core/src/test/java/org/apache/tika/config/DummyParser.java
URL: 
http://svn.apache.org/viewvc/tika/trunk/tika-core/src/test/java/org/apache/tika/config/DummyParser.java?rev=1693773&view=auto
==============================================================================
--- tika/trunk/tika-core/src/test/java/org/apache/tika/config/DummyParser.java 
(added)
+++ tika/trunk/tika-core/src/test/java/org/apache/tika/config/DummyParser.java 
Sat Aug  1 22:09:55 2015
@@ -0,0 +1,38 @@
+/*
+ * 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 java.util.Collection;
+
+import org.apache.tika.mime.MediaTypeRegistry;
+import org.apache.tika.parser.CompositeParser;
+import org.apache.tika.parser.Parser;
+
+public class DummyParser extends CompositeParser implements Parser {
+    private static final long serialVersionUID = 7179782154785528555L;
+    
+    private ServiceLoader loader;
+
+    public DummyParser(MediaTypeRegistry registry, ServiceLoader loader,
+            Collection<Class<? extends Parser>> excludeParsers) {
+        this.loader = loader;
+    }
+
+    public ServiceLoader getLoader() {
+        return loader;
+    }
+}
\ No newline at end of file

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=1693773&r1=1693772&r2=1693773&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 22:09:55 2015
@@ -230,4 +230,17 @@ public class TikaConfigTest extends Abst
             fail("Unexpected TikaException: " + e);
         }
     }
+    
+    @Test
+    public void testDynamicServiceLoaderFromConfig() throws Exception {
+        URL url = TikaConfigTest.class.getResource("TIKA-1700-dynamic.xml");
+        TikaConfig config = new TikaConfig(url);
+        
+        DummyParser parser = (DummyParser)config.getParser();
+
+        ServiceLoader loader = parser.getLoader();
+        boolean dynamicValue = loader.isDynamic();
+        
+        assertTrue("Dynamic Service Loading Should be true", dynamicValue);
+    }
 }
\ No newline at end of file

Added: 
tika/trunk/tika-core/src/test/resources/org/apache/tika/config/TIKA-1700-dynamic.xml
URL: 
http://svn.apache.org/viewvc/tika/trunk/tika-core/src/test/resources/org/apache/tika/config/TIKA-1700-dynamic.xml?rev=1693773&view=auto
==============================================================================
--- 
tika/trunk/tika-core/src/test/resources/org/apache/tika/config/TIKA-1700-dynamic.xml
 (added)
+++ 
tika/trunk/tika-core/src/test/resources/org/apache/tika/config/TIKA-1700-dynamic.xml
 Sat Aug  1 22:09:55 2015
@@ -0,0 +1,24 @@
+<?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>
+  <service-loader dynamic="true"/>
+  <parsers>
+    <parser class="org.apache.tika.config.DummyParser"/>
+  </parsers>
+</properties>


Reply via email to