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 3 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
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@
<version>7.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>osgi.core</artifactId>
<version>7.0.0</version>
<scope>provided</scope>
</dependency>

</dependencies>

<repositories>
Expand Down
52 changes: 50 additions & 2 deletions src/main/java/org/bytedeco/javacpp/Loader.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,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 +524,21 @@ public static File cacheResource(URL resourceURL, String target) throws IOExcept
if (!noSubdir) {
cacheSubdir = new File(cacheSubdir, urlFile.getParentFile().getName());
}
} else if (OSGiBundleResourceLoader.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.getContainerBundleName(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,9 +762,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();
JarFile jarFile = ((JarURLConnection) urlConnection).getJarFile();
JarEntry jarEntry = ((JarURLConnection) urlConnection).getJarEntry();
String jarFileName = jarFile.getName();
String jarEntryName = jarEntry.getName();
if (!jarEntryName.endsWith("/")) {
Expand All @@ -771,14 +789,43 @@ public static File extractResource(URL resourceURL, File directoryOrFile,
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 (OSGiBundleResourceLoader.isOSGiRuntime()) {
Enumeration<URL> directoryEntries = OSGiBundleResourceLoader.getBundleDirectoryContent(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 || !file.exists() || file.length() != entrySize
|| file.lastModified() != entryTimestamp || !file.equals(file.getCanonicalFile())) {
// ... 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 +857,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 Down
176 changes: 176 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,176 @@
/*
* 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.net.URL;
import java.util.Enumeration;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleEvent;
import org.osgi.framework.BundleException;
import org.osgi.framework.BundleListener;
import org.osgi.framework.FrameworkEvent;
import org.osgi.framework.FrameworkListener;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.Version;

public class OSGiBundleResourceLoader {

private OSGiBundleResourceLoader() { // static use only
}

public static boolean isOSGiRuntime() {
return IS_OSGI_RUNTIME;
}

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

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

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

private static final Map<String, Long> HOST_2_BUNDLE = new ConcurrentHashMap<String, Long>();
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;
if (IS_OSGI_RUNTIME) {
OSGiEnvironmentLoader.initialize();
}
}

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

private static void initialize() {
BundleContext context = getBundleContext();
if (context != null) {
indexAllBundles(context);
context.addBundleListener(new BundleListener() {
@Override
public void bundleChanged(BundleEvent event) {
Bundle bundle = event.getBundle();
switch (event.getType()) {
case BundleEvent.RESOLVED:
HOST_2_BUNDLE.put(getBundleURLHost(bundle), bundle.getBundleId());
break;
case BundleEvent.UNRESOLVED:
HOST_2_BUNDLE.remove(getBundleURLHost(bundle));
break;
default:
break;
}
}
});
context.addFrameworkListener(new FrameworkListener() {
@Override
public void frameworkEvent(FrameworkEvent event) {
if (event.getType() == FrameworkEvent.PACKAGES_REFRESHED) {
HOST_2_BUNDLE.clear();
indexAllBundles(getBundleContext());
// don't keep a reference on the BundleContext
}
}
});
}
}

private static BundleContext getBundleContext() {
Bundle bundle = FrameworkUtil.getBundle(OSGiEnvironmentLoader.class);
if (bundle != null) {
int state = bundle.getState();
if (state != Bundle.ACTIVE && (state == Bundle.INSTALLED || state == Bundle.RESOLVED || state == Bundle.STARTING)) {
try {
bundle.start();
} catch (BundleException e) { // ignore
}
}
if (bundle.getState() == Bundle.ACTIVE) {
return bundle.getBundleContext();
}
}
return null;
}

private static void indexAllBundles(BundleContext context) {
if (context != null) {
for (Bundle bundle : context.getBundles()) {
HOST_2_BUNDLE.put(getBundleURLHost(bundle), bundle.getBundleId());
}
}
}

private static String getBundleURLHost(Bundle bundle) {
return bundle.getEntry("/").getHost();
}

private static Bundle getContainerBundle(URL url) {
Long bundleId = HOST_2_BUNDLE.get(url.getHost());
if (bundleId != null) {
BundleContext context = getBundleContext();
if (context != null) {
return context.getBundle(bundleId);
}
}
return null;
}

public 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;
}

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