This is an automated email from the ASF dual-hosted git repository. cziegeler pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-commons-mime.git
commit 46de3a484792dc23f055b50fea38b3eb03e79789 Author: Carsten Ziegeler <cziege...@apache.org> AuthorDate: Wed Dec 6 11:40:54 2023 +0100 SLING-12183 : Avoid exception on startup if Apache Tika is not available --- bnd.bnd | 2 + pom.xml | 3 +- .../commons/mime/internal/MimeBundleActivator.java | 47 ++++++++++++++++++++++ .../mime/internal/TikaMimeTypeProvider.java | 9 ----- .../mime/internal/MimeTypeServiceImplTest.java | 3 +- 5 files changed, 51 insertions(+), 13 deletions(-) diff --git a/bnd.bnd b/bnd.bnd index 25a5e88..c56a8c9 100644 --- a/bnd.bnd +++ b/bnd.bnd @@ -6,6 +6,8 @@ Import-Package:\ org.apache.tika.*;resolution:=optional,\ * +Bundle-Activator: org.apache.sling.commons.mime.internal.MimeBundleActivator + -removeheaders:\ Include-Resource,\ Private-Package diff --git a/pom.xml b/pom.xml index 8e57244..1f374ce 100644 --- a/pom.xml +++ b/pom.xml @@ -39,8 +39,7 @@ <connection>scm:git:https://gitbox.apache.org/repos/asf/sling-org-apache-sling-commons-mime.git</connection> <developerConnection>scm:git:https://gitbox.apache.org/repos/asf/sling-org-apache-sling-commons-mime.git</developerConnection> <url>https://github.com/apache/sling-org-apache-sling-commons-mime.git</url> - <tag>org.apache.sling.commons.mime-2.2.4</tag> - </scm> + </scm> <build> <plugins> diff --git a/src/main/java/org/apache/sling/commons/mime/internal/MimeBundleActivator.java b/src/main/java/org/apache/sling/commons/mime/internal/MimeBundleActivator.java new file mode 100644 index 0000000..a36229d --- /dev/null +++ b/src/main/java/org/apache/sling/commons/mime/internal/MimeBundleActivator.java @@ -0,0 +1,47 @@ +/* + * 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.sling.commons.mime.internal; + +import org.apache.sling.commons.mime.MimeTypeProvider; +import org.osgi.framework.BundleActivator; +import org.osgi.framework.BundleContext; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class MimeBundleActivator implements BundleActivator { + + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + + @Override + public void start(final BundleContext context) throws Exception { + try { + context.registerService(MimeTypeProvider.class, + new TikaMimeTypeProvider() {}, + null); + logger.info("Apache Tika detected, using it for MIME type detection"); + } catch ( final Throwable t ) { + // don't care + } + } + + @Override + public void stop(final BundleContext context) throws Exception { + // nothing to do + } +} diff --git a/src/main/java/org/apache/sling/commons/mime/internal/TikaMimeTypeProvider.java b/src/main/java/org/apache/sling/commons/mime/internal/TikaMimeTypeProvider.java index ed7fda7..55ee458 100644 --- a/src/main/java/org/apache/sling/commons/mime/internal/TikaMimeTypeProvider.java +++ b/src/main/java/org/apache/sling/commons/mime/internal/TikaMimeTypeProvider.java @@ -21,18 +21,10 @@ import org.apache.tika.Tika; import org.apache.tika.mime.MimeType; import org.apache.tika.mime.MimeTypeException; import org.apache.tika.mime.MimeTypes; -import org.osgi.framework.Constants; -import org.osgi.service.component.annotations.Component; /** * MIME type provider based on Apache Tika. */ -@Component( - property = { // - Constants.SERVICE_DESCRIPTION + "=" + "Apache Tika MIME Type Provider", - Constants.SERVICE_VENDOR + "=" + "The Apache Software Foundation" - } - ) public class TikaMimeTypeProvider implements MimeTypeProvider { private final Tika tika = new Tika(); @@ -62,5 +54,4 @@ public class TikaMimeTypeProvider implements MimeTypeProvider { // fall back return null; } - } diff --git a/src/test/java/org/apache/sling/commons/mime/internal/MimeTypeServiceImplTest.java b/src/test/java/org/apache/sling/commons/mime/internal/MimeTypeServiceImplTest.java index de2c227..097ab57 100644 --- a/src/test/java/org/apache/sling/commons/mime/internal/MimeTypeServiceImplTest.java +++ b/src/test/java/org/apache/sling/commons/mime/internal/MimeTypeServiceImplTest.java @@ -20,12 +20,11 @@ import java.io.IOException; import java.io.InputStream; import org.apache.sling.commons.mime.MimeTypeProvider; -import org.apache.sling.commons.mime.internal.MimeTypeServiceImpl; import junit.framework.TestCase; /** - * The <code>MimeTypeServiceImplTest</code> TODO + * The <code>MimeTypeServiceImplTest</code> */ public class MimeTypeServiceImplTest extends TestCase {