You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by bo...@apache.org on 2021/03/07 10:56:08 UTC
[commons-compress] branch master updated: COMPRESS-552 use a more
complex and more reliable way to detect OSGi
This is an automated email from the ASF dual-hosted git repository.
bodewig pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-compress.git
The following commit(s) were added to refs/heads/master by this push:
new 30a7750 COMPRESS-552 use a more complex and more reliable way to detect OSGi
30a7750 is described below
commit 30a775050eb63d0572db762776397e45a61cd034
Author: Stefan Bodewig <bo...@apache.org>
AuthorDate: Sun Mar 7 11:55:30 2021 +0100
COMPRESS-552 use a more complex and more reliable way to detect OSGi
---
src/changes/changes.xml | 5 ++
.../compress/compressors/brotli/BrotliUtils.java | 8 ++-
.../compress/compressors/lzma/LZMAUtils.java | 7 +--
.../commons/compress/compressors/xz/XZUtils.java | 7 +--
.../compress/compressors/zstandard/ZstdUtils.java | 8 ++-
.../apache/commons/compress/utils/OsgiUtils.java | 58 ++++++++++++++++++++++
.../org/apache/commons/compress/OsgiITest.java | 32 ++++++++----
.../commons/compress/utils/OsgiUtilsTest.java | 31 ++++++++++++
8 files changed, 125 insertions(+), 31 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 06b3cff..6337843 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -324,6 +324,11 @@ The <action> type attribute can be add,update,fix,remove.
Fixes an infinite loop in the new TarFile class but affects
several formats.
</action>
+ <action issue="COMPRESS-552" type="fix" date="2021-03-07">
+ OSGi detection was broken and so we always cached whether
+ xz/brotli/zstd/lzma have been available even in OSGi
+ environments.
+ </action>
</release>
<release version="1.20" date="2020-02-08"
description="Release 1.20 (Java 7)">
diff --git a/src/main/java/org/apache/commons/compress/compressors/brotli/BrotliUtils.java b/src/main/java/org/apache/commons/compress/compressors/brotli/BrotliUtils.java
index 0278801..7ecbef1 100644
--- a/src/main/java/org/apache/commons/compress/compressors/brotli/BrotliUtils.java
+++ b/src/main/java/org/apache/commons/compress/compressors/brotli/BrotliUtils.java
@@ -18,6 +18,8 @@
*/
package org.apache.commons.compress.compressors.brotli;
+import org.apache.commons.compress.utils.OsgiUtils;
+
/**
* Utility code for the Brotli compression format.
* @ThreadSafe
@@ -33,11 +35,7 @@ public class BrotliUtils {
static {
cachedBrotliAvailability = CachedAvailability.DONT_CACHE;
- try {
- Class.forName("org.osgi.framework.BundleEvent");
- } catch (final Exception ex) { // NOSONAR
- setCacheBrotliAvailablity(true);
- }
+ setCacheBrotliAvailablity(!OsgiUtils.isRunningInOsgiEnvironment());
}
/** Private constructor to prevent instantiation of this utility class. */
diff --git a/src/main/java/org/apache/commons/compress/compressors/lzma/LZMAUtils.java b/src/main/java/org/apache/commons/compress/compressors/lzma/LZMAUtils.java
index 2ebb0ce..4f07d41 100644
--- a/src/main/java/org/apache/commons/compress/compressors/lzma/LZMAUtils.java
+++ b/src/main/java/org/apache/commons/compress/compressors/lzma/LZMAUtils.java
@@ -21,6 +21,7 @@ package org.apache.commons.compress.compressors.lzma;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.compress.compressors.FileNameUtil;
+import org.apache.commons.compress.utils.OsgiUtils;
/**
* Utility code for the lzma compression format.
@@ -50,11 +51,7 @@ public class LZMAUtils {
uncompressSuffix.put("-lzma", "");
fileNameUtil = new FileNameUtil(uncompressSuffix, ".lzma");
cachedLZMAAvailability = CachedAvailability.DONT_CACHE;
- try {
- Class.forName("org.osgi.framework.BundleEvent");
- } catch (final Exception ex) { // NOSONAR
- setCacheLZMAAvailablity(true);
- }
+ setCacheLZMAAvailablity(!OsgiUtils.isRunningInOsgiEnvironment());
}
/** Private constructor to prevent instantiation of this utility class. */
diff --git a/src/main/java/org/apache/commons/compress/compressors/xz/XZUtils.java b/src/main/java/org/apache/commons/compress/compressors/xz/XZUtils.java
index 619a385..b85411d 100644
--- a/src/main/java/org/apache/commons/compress/compressors/xz/XZUtils.java
+++ b/src/main/java/org/apache/commons/compress/compressors/xz/XZUtils.java
@@ -21,6 +21,7 @@ package org.apache.commons.compress.compressors.xz;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.compress.compressors.FileNameUtil;
+import org.apache.commons.compress.utils.OsgiUtils;
/**
* Utility code for the xz compression format.
@@ -54,11 +55,7 @@ public class XZUtils {
uncompressSuffix.put("-xz", "");
fileNameUtil = new FileNameUtil(uncompressSuffix, ".xz");
cachedXZAvailability = CachedAvailability.DONT_CACHE;
- try {
- Class.forName("org.osgi.framework.BundleEvent");
- } catch (final Exception ex) { // NOSONAR
- setCacheXZAvailablity(true);
- }
+ setCacheXZAvailablity(!OsgiUtils.isRunningInOsgiEnvironment());
}
/** Private constructor to prevent instantiation of this utility class. */
diff --git a/src/main/java/org/apache/commons/compress/compressors/zstandard/ZstdUtils.java b/src/main/java/org/apache/commons/compress/compressors/zstandard/ZstdUtils.java
index a89b5f5..2eaf04a 100644
--- a/src/main/java/org/apache/commons/compress/compressors/zstandard/ZstdUtils.java
+++ b/src/main/java/org/apache/commons/compress/compressors/zstandard/ZstdUtils.java
@@ -18,6 +18,8 @@
*/
package org.apache.commons.compress.compressors.zstandard;
+import org.apache.commons.compress.utils.OsgiUtils;
+
/**
* Utility code for the Zstandard compression format.
* @ThreadSafe
@@ -47,11 +49,7 @@ public class ZstdUtils {
static {
cachedZstdAvailability = CachedAvailability.DONT_CACHE;
- try {
- Class.forName("org.osgi.framework.BundleEvent");
- } catch (final Exception ex) { // NOSONAR
- setCacheZstdAvailablity(true);
- }
+ setCacheZstdAvailablity(!OsgiUtils.isRunningInOsgiEnvironment());
}
/** Private constructor to prevent instantiation of this utility class. */
diff --git a/src/main/java/org/apache/commons/compress/utils/OsgiUtils.java b/src/main/java/org/apache/commons/compress/utils/OsgiUtils.java
new file mode 100644
index 0000000..3ec7c4e
--- /dev/null
+++ b/src/main/java/org/apache/commons/compress/utils/OsgiUtils.java
@@ -0,0 +1,58 @@
+/*
+ * 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.commons.compress.utils;
+
+/**
+ * Utilities for dealing with OSGi environments.
+ *
+ * @since 1.21
+ */
+public class OsgiUtils {
+
+ private static final boolean inOsgiEnvironment;
+
+ static {
+ final Class<?> classloaderClass = OsgiUtils.class.getClassLoader().getClass();
+ inOsgiEnvironment = isBundleReference(classloaderClass);
+ }
+
+ private static boolean isBundleReference(final Class<?> clazz) {
+ Class<?> c = clazz;
+ while (c != null) {
+ if (c.getName().equals("org.osgi.framework.BundleReference")) {
+ return true;
+ }
+ c = c.getSuperclass();
+ }
+ for (Class<?> ifc : clazz.getInterfaces()) {
+ if (isBundleReference(ifc)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Is Compress running as an OSGi bundle?
+ */
+ public static boolean isRunningInOsgiEnvironment() {
+ return inOsgiEnvironment;
+ }
+
+}
diff --git a/src/test/java/org/apache/commons/compress/OsgiITest.java b/src/test/java/org/apache/commons/compress/OsgiITest.java
index ab81cb2..a89618a 100644
--- a/src/test/java/org/apache/commons/compress/OsgiITest.java
+++ b/src/test/java/org/apache/commons/compress/OsgiITest.java
@@ -18,6 +18,7 @@
*/
package org.apache.commons.compress;
+import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.ops4j.pax.exam.CoreOptions.bundle;
import static org.ops4j.pax.exam.CoreOptions.composite;
@@ -32,6 +33,7 @@ import org.ops4j.pax.exam.junit.PaxExam;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
+import java.lang.reflect.Method;
import javax.inject.Inject;
@RunWith(PaxExam.class)
@@ -62,19 +64,27 @@ public class OsgiITest {
}
@Test
- public void loadBundle() {
- final StringBuilder bundles = new StringBuilder();
- boolean foundCompressBundle = false, first = true;
+ public void canLoadBundle() {
+ assertNotNull("Expected to find bundle " + EXPECTED_BUNDLE_NAME, loadBundle());
+ }
+
+ @Test
+ public void properlyDetectsRunningInsideOsgiEnv() throws Exception {
+ final Class<?> osgiUtils = loadBundle().loadClass("org.apache.commons.compress.utils.OsgiUtils");
+ assertNotNull("Can load OsgiUtils via bundle", osgiUtils);
+
+ final Method m = osgiUtils.getMethod("isRunningInOsgiEnvironment");
+ assertNotNull("Can access isRunningInOsgiEnvironment method", m);
+
+ assertTrue("Compress detects OSGi environment", (Boolean) m.invoke(null));
+ }
+
+ private Bundle loadBundle() {
for (final Bundle b : ctx.getBundles()) {
- final String symbolicName = b.getSymbolicName();
- foundCompressBundle |= EXPECTED_BUNDLE_NAME.equals(symbolicName);
- if (!first) {
- bundles.append(", ");
+ if (EXPECTED_BUNDLE_NAME.equals(b.getSymbolicName())) {
+ return b;
}
- first = false;
- bundles.append(symbolicName);
}
- assertTrue("Expected to find bundle " + EXPECTED_BUNDLE_NAME + " in " + bundles,
- foundCompressBundle);
+ return null;
}
}
diff --git a/src/test/java/org/apache/commons/compress/utils/OsgiUtilsTest.java b/src/test/java/org/apache/commons/compress/utils/OsgiUtilsTest.java
new file mode 100644
index 0000000..fd67de1
--- /dev/null
+++ b/src/test/java/org/apache/commons/compress/utils/OsgiUtilsTest.java
@@ -0,0 +1,31 @@
+/*
+ * 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.commons.compress.utils;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class OsgiUtilsTest {
+
+ @Test
+ public void detectsItIsNotRunningInsideOsgiEnv() {
+ Assert.assertFalse(OsgiUtils.isRunningInOsgiEnvironment());
+ }
+
+}