Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support directory extraction in modular OSGi runtimes (issue #506) #511

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,22 @@
<optional>true</optional>
</dependency>
<!--
The OSGi R7 Annotations are build time only and not
The OSGi R8 Annotations are build time only and not
needed at runtime, therefore they can be provided scope
-->
<dependency>
<groupId>org.osgi</groupId>
<artifactId>osgi.annotation</artifactId>
<version>7.0.0</version>
<version>8.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>osgi.core</artifactId>
<version>8.0.0</version>
<scope>provided</scope>
</dependency>

</dependencies>

<repositories>
Expand Down
70 changes: 62 additions & 8 deletions src/main/java/org/bytedeco/javacpp/Loader.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@

package org.bytedeco.javacpp;

import static org.bytedeco.javacpp.tools.OSGiBundleResourceLoader.getOSGiClassLoaderResources;
import static org.bytedeco.javacpp.tools.OSGiBundleResourceLoader.getOSGiClassResource;
import static org.bytedeco.javacpp.tools.OSGiBundleResourceLoader.isOSGiRuntime;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's try to keep the imports clean of OSGi stuff. Android in particular has a tendency to trip on those things since it tries to load all the classes there, and fails. This also means that we can't put anything that loads those classes by default, so you'll need to revert those changes too.


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
Expand Down Expand Up @@ -55,12 +59,14 @@
import java.util.WeakHashMap;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

import org.bytedeco.javacpp.annotation.Cast;
import org.bytedeco.javacpp.annotation.Name;
import org.bytedeco.javacpp.annotation.Platform;
import org.bytedeco.javacpp.annotation.Raw;
import org.bytedeco.javacpp.tools.Builder;
import org.bytedeco.javacpp.tools.Logger;
import org.bytedeco.javacpp.tools.OSGiBundleResourceLoader;

/**
* The Loader contains functionality to load native libraries, but also has a bit
Expand Down Expand Up @@ -522,6 +528,21 @@ public static File cacheResource(URL resourceURL, String target) throws IOExcept
if (!noSubdir) {
cacheSubdir = new File(cacheSubdir, urlFile.getParentFile().getName());
}
} else if (isOSGiRuntime()) {
// TODO: what happens if this is another URL in a OSGi environment?
// I think it is unlikely that is called with URL-schema?!
if (!noSubdir) {
String subdirName = OSGiBundleResourceLoader.getOSGiContainerBundleName(resourceURL);
if (subdirName != null) {
String parentName = urlFile.getParentFile().toString();
if (parentName != null) {
subdirName = subdirName + File.separator + parentName;
}
cacheSubdir = new File(cacheSubdir, subdirName);
}
size = urlConnection.getContentLengthLong();
timestamp = urlConnection.getLastModified();
}
} else {
if (urlFile.exists()) {
size = urlFile.length();
Expand Down Expand Up @@ -745,10 +766,10 @@ public static File extractResource(URL resourceURL, File directoryOrFile,
public static File extractResource(URL resourceURL, File directoryOrFile,
String prefix, String suffix, boolean cacheDirectory) throws IOException {
URLConnection urlConnection = resourceURL != null ? resourceURL.openConnection() : null;
long start = System.currentTimeMillis();
if (urlConnection instanceof JarURLConnection) {
JarFile jarFile = ((JarURLConnection)urlConnection).getJarFile();
JarEntry jarEntry = ((JarURLConnection)urlConnection).getJarEntry();
String jarFileName = jarFile.getName();
JarFile jarFile = ((JarURLConnection) urlConnection).getJarFile();
JarEntry jarEntry = ((JarURLConnection) urlConnection).getJarEntry();
String jarEntryName = jarEntry.getName();
if (!jarEntryName.endsWith("/")) {
jarEntryName += "/";
Expand All @@ -765,20 +786,47 @@ public static File extractResource(URL resourceURL, File directoryOrFile,
File file = new File(directoryOrFile, entryName.substring(jarEntryName.length()));
if (entry.isDirectory()) {
file.mkdirs();
} else if (!cacheDirectory || !file.exists() || file.length() != entrySize
|| file.lastModified() != entryTimestamp || !file.equals(file.getCanonicalFile())) {
} else if (!cacheDirectory || isCacheFileCurrent(file, entrySize, entryTimestamp)) {
// ... extract it from our resources ...
file.delete();
String s = resourceURL.toString();
URL u = new URL(s.substring(0, s.indexOf("!/") + 2) + entryName);
// FIXME: check if directories have to be extracted again?!
file = extractResource(u, file, prefix, suffix);
}
file.setLastModified(entryTimestamp);
}
}
System.out.println("Extract took " + (System.currentTimeMillis() - start) + "ms, for:" + resourceURL);
return directoryOrFile;
}
}
if (isOSGiRuntime()) {
Enumeration<URL> directoryEntries = OSGiBundleResourceLoader.getOSGiBundleDirectoryContent(resourceURL);
if (directoryEntries != null && directoryEntries.hasMoreElements()) { // a not empty directory
String directoryName = resourceURL.getPath();
while (directoryEntries.hasMoreElements()) {
Copy link
Member

@saudet saudet Aug 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This while loop looks pretty similar to the one above. Is the idea that some OSGi implementations may support other things than JAR files?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is intentionally. Actually I would have liked to move the extraction part into an own method. But because in case of a JarURLConnection the URL is created just before extractResource is called again this is not possible without creating the URL before (and therefore also in cases of a directory.

And yes a bundle can be a jar but also can be extracted into a directory (which is for example very common if you develop Eclipse plug-ins and use them in a debugged application started from the IDE).

URL entry = directoryEntries.nextElement();
String entryName = entry.getPath();
URLConnection entryConnection = entry.openConnection();
long entrySize = entryConnection.getContentLengthLong();
long entryTimestamp = entryConnection.getLastModified();
File file = new File(directoryOrFile, entryName.substring(directoryName.length()));
if (entryName.endsWith("/")) { // is directory
file.mkdirs();
} else if (!cacheDirectory || isCacheFileCurrent(file, entrySize, entryTimestamp)) {
// ... extract it from our resources ...
file.delete();
// FIXME: check if directories have to be extracted again?!
file = extractResource(entry, file, prefix, suffix);
}
file.setLastModified(entryTimestamp);
}
System.out.println("Extract took " + (System.currentTimeMillis() - start) + "ms, for:" + resourceURL);
return directoryOrFile;
}
}

InputStream is = urlConnection != null ? urlConnection.getInputStream() : null;
OutputStream os = null;
if (is == null) {
Expand Down Expand Up @@ -810,6 +858,7 @@ public static File extractResource(URL resourceURL, File directoryOrFile,
} else {
file = File.createTempFile(prefix, suffix, directoryOrFile);
}
System.out.println("Extract resource (" + resourceURL + ") to " + file);
file.delete();
os = new FileOutputStream(file);
byte[] buffer = new byte[64 * 1024];
Expand All @@ -831,6 +880,11 @@ public static File extractResource(URL resourceURL, File directoryOrFile,
return file;
}

private static boolean isCacheFileCurrent(File file, long entrySize, long entryTimestamp) throws IOException {
return !file.exists() || file.length() != entrySize || file.lastModified() != entryTimestamp
|| !file.equals(file.getCanonicalFile());
}

/** Returns {@code findResources(cls, name, 1)[0]} or null if none. */
public static URL findResource(Class cls, String name) throws IOException {
URL[] url = findResources(cls, name, 1);
Expand Down Expand Up @@ -862,7 +916,7 @@ public static URL[] findResources(Class cls, String name, int maxLength) throws
}

// Under JPMS, Class.getResource() and ClassLoader.getResources() do not return the same URLs
URL url = cls.getResource(name);
URL url = !isOSGiRuntime() ? cls.getResource(name) : getOSGiClassResource(cls, name);
if (url != null && maxLength == 1) {
return new URL[] {url};
}
Expand All @@ -882,7 +936,7 @@ public static URL[] findResources(Class cls, String name, int maxLength) throws
// This is the bootstrap class loader, let's try the system class loader instead
classLoader = ClassLoader.getSystemClassLoader();
}
Enumeration<URL> urls = classLoader.getResources(path + name);
Enumeration<URL> urls = !isOSGiRuntime() ? classLoader.getResources(path + name) : getOSGiClassLoaderResources(classLoader, path + name);
ArrayList<URL> array = new ArrayList<URL>();
if (url != null) {
array.add(url);
Expand All @@ -894,7 +948,7 @@ public static URL[] findResources(Class cls, String name, int maxLength) throws
} else {
path = "";
}
urls = classLoader.getResources(path + name);
urls = !isOSGiRuntime() ? classLoader.getResources(path + name) : getOSGiClassLoaderResources(classLoader, path + name);
}
while (urls.hasMoreElements() && (maxLength < 0 || array.size() < maxLength)) {
url = urls.nextElement();
Expand Down
162 changes: 162 additions & 0 deletions src/main/java/org/bytedeco/javacpp/tools/OSGiBundleResourceLoader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/*
* Copyright (C) 2021-2021 Hannes Wellmann
*
* Licensed either under the Apache License, Version 2.0, or (at your option)
* under the terms of the GNU General Public License as published by
* the Free Software Foundation (subject to the "Classpath" exception),
* either version 2, or any later version (collectively, 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
* http://www.gnu.org/licenses/
* http://www.gnu.org/software/classpath/license.html
*
* or as provided in the LICENSE.txt file that accompanied this code.
* 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.bytedeco.javacpp.tools;

import java.io.IOException;
import java.lang.ref.WeakReference;
import java.net.URL;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.Optional;
import java.util.WeakHashMap;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import org.osgi.framework.Bundle;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.Version;

public class OSGiBundleResourceLoader {

private OSGiBundleResourceLoader() { // static use only
}

private static final boolean IS_OSGI_RUNTIME;

static {
boolean isOSGI;
try {
Bundle.class.getName();
isOSGI = true;
} catch (NoClassDefFoundError e) {
isOSGI = false;
}
IS_OSGI_RUNTIME = isOSGI;
}

public static boolean isOSGiRuntime() {
return IS_OSGI_RUNTIME;
}

public static String getOSGiContainerBundleName(URL resourceURL) {
requireOSGi();
return OSGiEnvironmentLoader.getContainerBundleName(resourceURL);
}

public static Enumeration<URL> getOSGiBundleDirectoryContent(URL resourceURL) {
requireOSGi();
return OSGiEnvironmentLoader.getBundleDirectoryContent(resourceURL);
}

public static URL getOSGiClassResource(Class<?> c, String name) {
requireOSGi();
return OSGiEnvironmentLoader.getClassResource(c, name);
}

public static Enumeration<URL> getOSGiClassLoaderResources(ClassLoader cl, String name) throws IOException {
requireOSGi();
return OSGiEnvironmentLoader.getClassLoaderResources(cl, name);
}

private static void requireOSGi() {
if (!IS_OSGI_RUNTIME) {
throw new IllegalStateException(OSGiBundleResourceLoader.class.getSimpleName() + " must only be used within a OSGi runtime");
}
}

private static class OSGiEnvironmentLoader {
// Code using OSGi APIs has to be encapsulated into own class
// to prevent NoClassDefFoundErrors in OSGi environments

private static final ReadWriteLock LOCK = new ReentrantReadWriteLock();
private static final WeakHashMap<URL, WeakReference<Bundle>> URL_TO_BUNDLE = new WeakHashMap<>();

private static void associateURLtoBundle(URL resource, Bundle bundle) {
URL_TO_BUNDLE.put(resource, new WeakReference<>(bundle));
}

private static Bundle getContainerBundle(URL url) {
WeakReference<Bundle> bundle;
LOCK.readLock().lock();
try {
bundle = URL_TO_BUNDLE.get(url);
} finally {
LOCK.readLock().unlock();
}
return bundle != null ? bundle.get() : null;
}

private static URL getClassResource(Class<?> c, String name) {
URL resource = c.getResource(name);
if (resource != null) {
Bundle bundle = FrameworkUtil.getBundle(c);
LOCK.writeLock().lock();
try {
associateURLtoBundle(resource, bundle);
} finally {
LOCK.writeLock().unlock();
}
}
return resource;
}

private static Enumeration<URL> getClassLoaderResources(ClassLoader cl, String name) throws IOException {
Enumeration<URL> resources = cl.getResources(name);
if (resources != null && resources.hasMoreElements()) {
Optional<Bundle> bundleOpt = FrameworkUtil.getBundle(cl);
if (bundleOpt.isPresent()) {
Bundle bundle = bundleOpt.get();
List<URL> resourcesList = Collections.list(resources);
LOCK.writeLock().lock();
try {
for (URL url : resourcesList) {
associateURLtoBundle(url, bundle);
}
} finally {
LOCK.writeLock().unlock();
}
return Collections.enumeration(resourcesList);
}
}
return resources;
}

private static String getContainerBundleName(URL resourceURL) {
Bundle bundle = getContainerBundle(resourceURL);
if (bundle != null) {
Version v = bundle.getVersion();
String version = v.getMajor() + "." + v.getMinor() + "." + v.getMicro(); // skip qualifier
return bundle.getSymbolicName() + "_" + version;
}
return null;
}

private static Enumeration<URL> getBundleDirectoryContent(URL resourceURL) {
Bundle bundle = getContainerBundle(resourceURL);
if (bundle != null) {
return bundle.findEntries(resourceURL.getPath(), null, true);
}
return null;
}
}
}