-
Notifications
You must be signed in to change notification settings - Fork 586
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
HannesWell
wants to merge
5
commits into
bytedeco:master
Choose a base branch
from
HannesWell:osgiAwareLoader
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5305155
Support directory extraction in modular OSGi runtimes (issue #506)
HannesWell e5399d1
[SQUASH] extract bundle from URL and move OSGi related code to own class
HannesWell caed160
[FIXUP] apply javacpp formatting
HannesWell 10f2020
[SQUASH] use BundleTracker
HannesWell 7f428b1
[SQUASH] store URL and targeted bundle in a WeakHashMap
HannesWell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
176 changes: 176 additions & 0 deletions
176
src/main/java/org/bytedeco/javacpp/tools/OSGiBundleResourceLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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).