Author: bob
Date: Sun Dec 13 19:34:17 2015
New Revision: 1719820

URL: http://svn.apache.org/viewvc?rev=1719820&view=rev
Log:
TIKA-1809 Enhanced Tika OSGi Service with test for core bundle.

Added:
    tika/branches/2.x/tika-core/src/main/java/org/apache/tika/osgi/
    
tika/branches/2.x/tika-core/src/main/java/org/apache/tika/osgi/TikaAbstractBundleActivator.java
    
tika/branches/2.x/tika-core/src/main/java/org/apache/tika/osgi/TikaService.java
    tika/branches/2.x/tika-core/src/main/java/org/apache/tika/osgi/internal/
    
tika/branches/2.x/tika-core/src/main/java/org/apache/tika/osgi/internal/TikaServiceImpl.java
    tika/branches/2.x/tika-core/src/test/java/org/apache/tika/osgi/
    tika/branches/2.x/tika-core/src/test/java/org/apache/tika/osgi/BundleIT.java
Modified:
    tika/branches/2.x/tika-core/pom.xml
    
tika/branches/2.x/tika-core/src/main/java/org/apache/tika/config/TikaActivator.java
    tika/branches/2.x/tika-parent/pom.xml

Modified: tika/branches/2.x/tika-core/pom.xml
URL: 
http://svn.apache.org/viewvc/tika/branches/2.x/tika-core/pom.xml?rev=1719820&r1=1719819&r2=1719820&view=diff
==============================================================================
--- tika/branches/2.x/tika-core/pom.xml (original)
+++ tika/branches/2.x/tika-core/pom.xml Sun Dec 13 19:34:17 2015
@@ -39,14 +39,12 @@
     <dependency>
       <groupId>org.osgi</groupId>
       <artifactId>org.osgi.core</artifactId>
-      <version>4.0.0</version>
       <scope>provided</scope>
       <optional>true</optional>
     </dependency>
     <dependency>
       <groupId>org.osgi</groupId>
       <artifactId>org.osgi.compendium</artifactId>
-      <version>4.0.0</version>
       <scope>provided</scope>
       <optional>true</optional>
     </dependency>
@@ -60,6 +58,42 @@
     <dependency>
       <groupId>junit</groupId>
       <artifactId>junit</artifactId>
+      <scope>test</scope>
+    </dependency>
+     <dependency>
+      <groupId>org.ops4j.pax.exam</groupId>
+      <artifactId>pax-exam-junit4</artifactId>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.ops4j.pax.exam</groupId>
+      <artifactId>pax-exam-container-native</artifactId>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.felix</groupId>
+      <artifactId>org.apache.felix.framework</artifactId>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.ops4j.pax.exam</groupId>
+      <artifactId>pax-exam-link-assembly</artifactId>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.ops4j.pax.url</groupId>
+      <artifactId>pax-url-aether</artifactId>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.slf4j</groupId>
+      <artifactId>slf4j-simple</artifactId>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>javax.inject</groupId>
+      <artifactId>javax.inject</artifactId>
+      <scope>test</scope>
     </dependency>
   </dependencies>
 
@@ -72,9 +106,7 @@
         <configuration>
           <instructions>
             <Bundle-DocURL>${project.url}</Bundle-DocURL>
-            <Bundle-Activator>
-              org.apache.tika.config.TikaActivator
-            </Bundle-Activator>
+            
<Bundle-Activator>org.apache.tika.config.TikaActivator</Bundle-Activator>
             <Bundle-ActivationPolicy>lazy</Bundle-ActivationPolicy>
           </instructions>
         </configuration>
@@ -129,24 +161,34 @@
         </executions>
       </plugin>
       <plugin>
-        <artifactId>maven-failsafe-plugin</artifactId>
-        <version>2.10</version>
-        <configuration>
-          <additionalClasspathElements>
-            <additionalClasspathElement>
-              ${project.build.directory}/${project.build.finalName}.jar
-            </additionalClasspathElement>
-          </additionalClasspathElements>
-        </configuration>
-        <executions>
-          <execution>
-            <goals>
-              <goal>integration-test</goal>
-              <goal>verify</goal>
-            </goals>
-          </execution>
-        </executions>
-      </plugin>
+            <artifactId>maven-failsafe-plugin</artifactId>
+            <executions>
+              <execution>
+                <goals>
+                  <goal>integration-test</goal>
+                  <goal>verify</goal>
+                </goals>
+              </execution>
+            </executions>
+            <configuration>
+              <additionalClasspathElements>
+                   <additionalClasspathElement>
+                     ${project.build.directory}/${project.build.finalName}.jar
+                   </additionalClasspathElement>
+              </additionalClasspathElements>
+              <systemPropertyVariables>
+                <org.ops4j.pax.logging.DefaultServiceLog.level>
+                  WARN
+                </org.ops4j.pax.logging.DefaultServiceLog.level>
+              </systemPropertyVariables>
+              <systemProperties>
+                <property>
+                    <name>project.bundle.file</name>
+                    <value>target/${project.build.finalName}.jar</value>
+                </property>
+              </systemProperties>
+            </configuration>
+          </plugin>
     </plugins>
   </build>
 

Modified: 
tika/branches/2.x/tika-core/src/main/java/org/apache/tika/config/TikaActivator.java
URL: 
http://svn.apache.org/viewvc/tika/branches/2.x/tika-core/src/main/java/org/apache/tika/config/TikaActivator.java?rev=1719820&r1=1719819&r2=1719820&view=diff
==============================================================================
--- 
tika/branches/2.x/tika-core/src/main/java/org/apache/tika/config/TikaActivator.java
 (original)
+++ 
tika/branches/2.x/tika-core/src/main/java/org/apache/tika/config/TikaActivator.java
 Sun Dec 13 19:34:17 2015
@@ -17,6 +17,8 @@
 package org.apache.tika.config;
 
 import org.apache.tika.detect.Detector;
+import org.apache.tika.osgi.TikaService;
+import org.apache.tika.osgi.internal.TikaServiceImpl;
 import org.apache.tika.parser.Parser;
 import org.osgi.framework.BundleActivator;
 import org.osgi.framework.BundleContext;
@@ -53,6 +55,7 @@ public class TikaActivator implements Bu
 
         detectorTracker.open();
         parserTracker.open();
+        context.registerService(TikaService.class, new TikaServiceImpl(), 
null);
     }
 
     public void stop(BundleContext context) throws Exception {

Added: 
tika/branches/2.x/tika-core/src/main/java/org/apache/tika/osgi/TikaAbstractBundleActivator.java
URL: 
http://svn.apache.org/viewvc/tika/branches/2.x/tika-core/src/main/java/org/apache/tika/osgi/TikaAbstractBundleActivator.java?rev=1719820&view=auto
==============================================================================
--- 
tika/branches/2.x/tika-core/src/main/java/org/apache/tika/osgi/TikaAbstractBundleActivator.java
 (added)
+++ 
tika/branches/2.x/tika-core/src/main/java/org/apache/tika/osgi/TikaAbstractBundleActivator.java
 Sun Dec 13 19:34:17 2015
@@ -0,0 +1,61 @@
+/*
+ * 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 java.util.Dictionary;
+import java.util.Enumeration;
+import java.util.Locale;
+import java.util.Properties;
+
+import org.apache.tika.parser.Parser;
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+
+public abstract class TikaAbstractBundleActivator implements BundleActivator {
+
+    Dictionary createServiceRankProperties(String configName, BundleContext 
context) {
+        Dictionary serviceProps = new Properties();
+        String serviceRank = context.getProperty(configName);
+        if (serviceRank != null) {
+            serviceProps.put(Constants.SERVICE_RANKING, 
Integer.parseInt(serviceRank));
+        }
+        return serviceProps;
+
+    }
+
+    public void registerTikaService(BundleContext context, Parser 
parserService,
+            Dictionary additionalServiceProperties) {
+        String parserFullyClassifiedName = 
parserService.getClass().getCanonicalName().toLowerCase(Locale.US);
+
+        String serviceRankingPropName = parserFullyClassifiedName + 
".serviceRanking";
+
+        Dictionary serviceProperties = 
createServiceRankProperties(serviceRankingPropName, context);
+
+        if (additionalServiceProperties != null) {
+            Enumeration keys = additionalServiceProperties.keys();
+            while (keys.hasMoreElements()) {
+                String currentKey = (String) keys.nextElement();
+                serviceProperties.put(currentKey, 
additionalServiceProperties.get(currentKey));
+            }
+
+        }
+
+        context.registerService(Parser.class, parserService, 
serviceProperties);
+    }
+
+}

Added: 
tika/branches/2.x/tika-core/src/main/java/org/apache/tika/osgi/TikaService.java
URL: 
http://svn.apache.org/viewvc/tika/branches/2.x/tika-core/src/main/java/org/apache/tika/osgi/TikaService.java?rev=1719820&view=auto
==============================================================================
--- 
tika/branches/2.x/tika-core/src/main/java/org/apache/tika/osgi/TikaService.java 
(added)
+++ 
tika/branches/2.x/tika-core/src/main/java/org/apache/tika/osgi/TikaService.java 
Sun Dec 13 19:34:17 2015
@@ -0,0 +1,25 @@
+/*
+ * 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.detect.Detector;
+import org.apache.tika.language.translate.Translator;
+import org.apache.tika.parser.Parser;
+
+public interface TikaService extends Parser, Detector, Translator {
+
+}

Added: 
tika/branches/2.x/tika-core/src/main/java/org/apache/tika/osgi/internal/TikaServiceImpl.java
URL: 
http://svn.apache.org/viewvc/tika/branches/2.x/tika-core/src/main/java/org/apache/tika/osgi/internal/TikaServiceImpl.java?rev=1719820&view=auto
==============================================================================
--- 
tika/branches/2.x/tika-core/src/main/java/org/apache/tika/osgi/internal/TikaServiceImpl.java
 (added)
+++ 
tika/branches/2.x/tika-core/src/main/java/org/apache/tika/osgi/internal/TikaServiceImpl.java
 Sun Dec 13 19:34:17 2015
@@ -0,0 +1,81 @@
+/*
+ * 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.IOException;
+import java.io.InputStream;
+import java.util.Set;
+
+import org.apache.tika.Tika;
+import org.apache.tika.config.TikaConfig;
+import org.apache.tika.exception.TikaException;
+import org.apache.tika.metadata.Metadata;
+import org.apache.tika.mime.MediaType;
+import org.apache.tika.osgi.TikaService;
+import org.apache.tika.parser.ParseContext;
+import org.xml.sax.ContentHandler;
+import org.xml.sax.SAXException;
+
+public class TikaServiceImpl implements TikaService {
+
+    private static final long serialVersionUID = 1L;
+    
+    private final Tika tika;
+
+    public TikaServiceImpl() {
+        this.tika = new Tika();
+    }
+    
+    public TikaServiceImpl(TikaConfig config)
+    {
+        this.tika = new Tika(config);
+    }
+
+    @Override
+    public Set<MediaType> getSupportedTypes(ParseContext context) {
+        return this.tika.getParser().getSupportedTypes(context);
+    }
+
+    @Override
+    public void parse(InputStream stream, ContentHandler handler, Metadata 
metadata, ParseContext context)
+            throws IOException, SAXException, TikaException {
+        tika.getParser().parse(stream, handler, metadata, context);
+
+    }
+
+    @Override
+    public MediaType detect(InputStream input, Metadata metadata) throws 
IOException {
+        return tika.getDetector().detect(input, metadata);
+    }
+    
+    @Override
+    public String translate(String text, String sourceLanguage, String 
targetLanguage)
+            throws TikaException, IOException {
+        return tika.getTranslator().translate(text, sourceLanguage, 
targetLanguage);
+    }
+    
+    @Override
+    public String translate(String text, String targetLanguage) throws 
TikaException, IOException {
+        return tika.getTranslator().translate(text, targetLanguage);
+    }
+    
+    @Override
+    public boolean isAvailable() {
+        return tika.getTranslator().isAvailable();
+    }
+
+}

Added: 
tika/branches/2.x/tika-core/src/test/java/org/apache/tika/osgi/BundleIT.java
URL: 
http://svn.apache.org/viewvc/tika/branches/2.x/tika-core/src/test/java/org/apache/tika/osgi/BundleIT.java?rev=1719820&view=auto
==============================================================================
--- 
tika/branches/2.x/tika-core/src/test/java/org/apache/tika/osgi/BundleIT.java 
(added)
+++ 
tika/branches/2.x/tika-core/src/test/java/org/apache/tika/osgi/BundleIT.java 
Sun Dec 13 19:34:17 2015
@@ -0,0 +1,93 @@
+/*
+ * 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 static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertNotNull;
+import static org.ops4j.pax.exam.CoreOptions.bundle;
+import static org.ops4j.pax.exam.CoreOptions.junitBundles;
+import static org.ops4j.pax.exam.CoreOptions.options;
+import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
+
+import javax.inject.Inject;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.StringWriter;
+import java.io.Writer;
+import java.net.URISyntaxException;
+import java.util.Set;
+
+import org.apache.tika.metadata.Metadata;
+import org.apache.tika.metadata.TikaCoreProperties;
+import org.apache.tika.osgi.TikaService;
+import org.apache.tika.parser.ParseContext;
+import org.apache.tika.sax.BodyContentHandler;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.ops4j.pax.exam.Configuration;
+import org.ops4j.pax.exam.Option;
+import org.ops4j.pax.exam.junit.PaxExam;
+import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
+import org.ops4j.pax.exam.spi.reactors.PerMethod;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.xml.sax.ContentHandler;
+
+@RunWith(PaxExam.class)
+@ExamReactorStrategy(PerMethod.class)
+public class BundleIT {
+
+    private static final String BUNDLE_JAR_SYS_PROP = "project.bundle.file";
+
+    @Inject
+    private BundleContext bc;
+
+    @Configuration
+    public Option[] configuration() throws IOException, URISyntaxException {
+        String bundleFileName = System.getProperty(BUNDLE_JAR_SYS_PROP);
+        return options(junitBundles(),
+                bundle(new File(bundleFileName).toURI().toString()));
+    }
+
+    @Test
+    public void testBundleLoaded() throws Exception {
+        boolean hasBundle = false;
+        for (Bundle b : bc.getBundles()) {
+            if ("org.apache.tika.core".equals(b.getSymbolicName())) {
+                hasBundle = true;
+                assertEquals("Bundle not activated", Bundle.ACTIVE, 
b.getState());
+            }
+        }
+        assertTrue("Bundle not found", hasBundle);
+    }
+
+    @Test
+    public void testEmptyParser() throws Exception {
+
+        TikaService tikaService = (TikaService)
+                       
bc.getService(bc.getServiceReference(TikaService.class));
+
+        ParseContext context = new ParseContext();
+        Set types = tikaService.getSupportedTypes(context);
+
+        assertEquals("Types should be Empty", 0, types.size());
+    }
+
+}

Modified: tika/branches/2.x/tika-parent/pom.xml
URL: 
http://svn.apache.org/viewvc/tika/branches/2.x/tika-parent/pom.xml?rev=1719820&r1=1719819&r2=1719820&view=diff
==============================================================================
--- tika/branches/2.x/tika-parent/pom.xml (original)
+++ tika/branches/2.x/tika-parent/pom.xml Sun Dec 13 19:34:17 2015
@@ -303,6 +303,20 @@
         <version>${slf4j.version}</version>
       </dependency>
       <dependency>
+        <groupId>org.osgi</groupId>
+        <artifactId>org.osgi.core</artifactId>
+        <version>5.0.0</version>
+        <scope>provided</scope>
+        <optional>true</optional>
+      </dependency>
+      <dependency>
+        <groupId>org.osgi</groupId>
+        <artifactId>org.osgi.compendium</artifactId>
+        <version>5.0.0</version>
+        <scope>provided</scope>
+        <optional>true</optional>
+      </dependency>
+      <dependency>
         <groupId>org.ops4j.pax.exam</groupId>
         <artifactId>pax-exam-junit4</artifactId>
         <version>${pax.exam.version}</version>


Reply via email to