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/09/10 03:59:41 UTC

tika git commit: TIKA-2072 - Create TikaServiceFactory for creating TikaService

Repository: tika
Updated Branches:
  refs/heads/2.x d57a85274 -> 587dcb772


TIKA-2072 - Create TikaServiceFactory for creating TikaService

Project: http://git-wip-us.apache.org/repos/asf/tika/repo
Commit: http://git-wip-us.apache.org/repos/asf/tika/commit/587dcb77
Tree: http://git-wip-us.apache.org/repos/asf/tika/tree/587dcb77
Diff: http://git-wip-us.apache.org/repos/asf/tika/diff/587dcb77

Branch: refs/heads/2.x
Commit: 587dcb7725a6556a08618a41c7c1b5ff893e3721
Parents: d57a852
Author: Bob Paulin <bo...@apache.org>
Authored: Fri Sep 9 22:59:20 2016 -0500
Committer: Bob Paulin <bo...@apache.org>
Committed: Fri Sep 9 22:59:20 2016 -0500

----------------------------------------------------------------------
 .../org/apache/tika/config/TikaActivator.java   |  9 ++-
 .../apache/tika/osgi/TikaServiceFactory.java    | 45 ++++++++++++++
 .../osgi/internal/TikaServiceFactoryImpl.java   | 65 ++++++++++++++++++++
 3 files changed, 117 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tika/blob/587dcb77/tika-core/src/main/java/org/apache/tika/config/TikaActivator.java
----------------------------------------------------------------------
diff --git a/tika-core/src/main/java/org/apache/tika/config/TikaActivator.java b/tika-core/src/main/java/org/apache/tika/config/TikaActivator.java
index a87ac8e..ffab128 100644
--- a/tika-core/src/main/java/org/apache/tika/config/TikaActivator.java
+++ b/tika-core/src/main/java/org/apache/tika/config/TikaActivator.java
@@ -20,7 +20,8 @@ import org.apache.tika.detect.Detector;
 import org.apache.tika.detect.EncodingDetector;
 import org.apache.tika.language.detect.LanguageDetector;
 import org.apache.tika.osgi.TikaService;
-import org.apache.tika.osgi.internal.TikaServiceImpl;
+import org.apache.tika.osgi.TikaServiceFactory;
+import org.apache.tika.osgi.internal.TikaServiceFactoryImpl;
 import org.apache.tika.parser.Parser;
 import org.osgi.framework.BundleActivator;
 import org.osgi.framework.BundleContext;
@@ -66,7 +67,11 @@ public class TikaActivator implements BundleActivator, ServiceTrackerCustomizer
         encodingDetectorTracker.open();
         languageDetectorTracker.open();
         
-        context.registerService(TikaService.class, new TikaServiceImpl(TikaConfig.getDefaultConfig()), null);
+        TikaServiceFactory tikaServiceFactory = new TikaServiceFactoryImpl(context);
+        
+        context.registerService(TikaServiceFactory.class, tikaServiceFactory, null);
+        
+        context.registerService(TikaService.class, tikaServiceFactory.createTikaService(), null);
     }
 
     public void stop(BundleContext context) throws Exception {

http://git-wip-us.apache.org/repos/asf/tika/blob/587dcb77/tika-core/src/main/java/org/apache/tika/osgi/TikaServiceFactory.java
----------------------------------------------------------------------
diff --git a/tika-core/src/main/java/org/apache/tika/osgi/TikaServiceFactory.java b/tika-core/src/main/java/org/apache/tika/osgi/TikaServiceFactory.java
new file mode 100644
index 0000000..940db9b
--- /dev/null
+++ b/tika-core/src/main/java/org/apache/tika/osgi/TikaServiceFactory.java
@@ -0,0 +1,45 @@
+/*
+ * 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.osgi;
+
+import org.apache.tika.config.TikaConfig;
+
+/**
+ * 
+ * Factory for creating and configuring TikaService.
+ * 
+ * @since Apache Tika 2.0
+ *
+ */
+public interface TikaServiceFactory {
+    
+    /**
+     * Create TikaService with default TikaConfig or configured TikaConfig
+     * OSGi Property.
+     * 
+     * @return TikaService
+     */
+    public TikaService createTikaService();
+    
+    /**
+     * Create TikaService based on provided TikaConfig
+     * 
+     * @param config TikaConfig object
+     * @return TikaService
+     */
+    public TikaService createTikaService(TikaConfig config);
+}

http://git-wip-us.apache.org/repos/asf/tika/blob/587dcb77/tika-core/src/main/java/org/apache/tika/osgi/internal/TikaServiceFactoryImpl.java
----------------------------------------------------------------------
diff --git a/tika-core/src/main/java/org/apache/tika/osgi/internal/TikaServiceFactoryImpl.java b/tika-core/src/main/java/org/apache/tika/osgi/internal/TikaServiceFactoryImpl.java
new file mode 100644
index 0000000..24d759c
--- /dev/null
+++ b/tika-core/src/main/java/org/apache/tika/osgi/internal/TikaServiceFactoryImpl.java
@@ -0,0 +1,65 @@
+/*
+ * 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.osgi.internal;
+
+import java.io.File;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import org.apache.tika.config.TikaConfig;
+import org.apache.tika.osgi.TikaService;
+import org.apache.tika.osgi.TikaServiceFactory;
+import org.osgi.framework.BundleContext;
+
+public class TikaServiceFactoryImpl implements TikaServiceFactory {
+    
+    private static Logger LOG = Logger.getLogger(TikaServiceFactoryImpl.class.getName());
+    
+    private final BundleContext context;
+    
+    public TikaServiceFactoryImpl(BundleContext context) {
+        this.context = context;
+    }
+    
+    @Override
+    public TikaService createTikaService() {
+        TikaConfig config = null;
+        String configFilePath = context.getProperty("org.apache.tika.osgi.internal.TikaServiceImpl.tikaConfigPath");
+        
+        if(configFilePath != null)
+        {
+            File configFile = new File(configFilePath);
+            try {
+                config = new TikaConfig(configFile);
+            } catch (Exception e ) {
+                config = TikaConfig.getDefaultConfig();
+                LOG.log(Level.WARNING, "Error Creating TikaConfig. Using Default Config", e);
+            }
+        }
+        else
+        {
+            config = TikaConfig.getDefaultConfig();
+        }
+        return createTikaService(config);
+    }
+
+    @Override
+    public TikaService createTikaService(TikaConfig config) {
+        return new TikaServiceImpl(config);
+    }
+
+}