This repository has been archived by the owner on Dec 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[auxiliary] Allow bnd to load plugins from OSGi
This patch allows bndlib to load plugins from OSGi. Any exported package that specifies ‘bnd-plugins=true|range’ and that matches the optional range for the bnd version is added to the Dynamic Imports of bndlib Signed-off-by: Peter Kriens <peter.kriens@aqute.biz>
- Loading branch information
Showing
5 changed files
with
199 additions
and
2 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
package bndtools.central; | ||
|
||
import java.io.Closeable; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.Map.Entry; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
import org.osgi.framework.Bundle; | ||
import org.osgi.framework.BundleContext; | ||
import org.osgi.framework.BundleEvent; | ||
import org.osgi.framework.ServiceRegistration; | ||
import org.osgi.framework.hooks.weaving.WeavingHook; | ||
import org.osgi.framework.hooks.weaving.WovenClass; | ||
import org.osgi.framework.wiring.BundleWiring; | ||
import org.osgi.util.tracker.BundleTracker; | ||
|
||
import aQute.bnd.header.Attrs; | ||
import aQute.bnd.header.Parameters; | ||
import aQute.bnd.osgi.About; | ||
import aQute.bnd.osgi.Constants; | ||
import aQute.bnd.osgi.Verifier; | ||
import aQute.bnd.version.Version; | ||
import aQute.bnd.version.VersionRange; | ||
|
||
/** | ||
* This class extends the dynamic imports of bndlib with any exported package from OSGi that specifies a 'bnd-plugins' | ||
* attribute. Its value is either true or a version range on the bnd version. | ||
*/ | ||
class Auxiliary implements Closeable, WeavingHook { | ||
private final BundleContext context; | ||
private final AtomicBoolean closed = new AtomicBoolean(false); | ||
private final ServiceRegistration<WeavingHook> hook; | ||
private final BundleTracker<Bundle> tracker; | ||
private Bundle bndlib; | ||
private List<String> delta = new ArrayList<>(); | ||
|
||
Auxiliary(BundleContext context, Bundle bndlib) { | ||
this.bndlib = bndlib; | ||
this.context = context; | ||
this.tracker = new BundleTracker<Bundle>(context, Bundle.RESOLVED + Bundle.ACTIVE + Bundle.STARTING, null) { | ||
@Override | ||
public Bundle addingBundle(Bundle bundle, BundleEvent event) { | ||
if (!doImport(bundle.getHeaders().get(Constants.EXPORT_PACKAGE))) | ||
return null; | ||
|
||
return super.addingBundle(bundle, event); | ||
} | ||
|
||
}; | ||
this.tracker.open(); | ||
this.hook = this.context.registerService(WeavingHook.class, this, null); | ||
} | ||
|
||
/* | ||
* Parse the exports and see | ||
* | ||
*/ | ||
private boolean doImport(String exports) { | ||
if (closed.get() || exports == null || exports.isEmpty()) | ||
return false; | ||
|
||
Parameters out = new Parameters(); | ||
|
||
Parameters p = new Parameters(exports); | ||
for (Entry<String,Attrs> e : p.entrySet()) { | ||
Attrs attrs = e.getValue(); | ||
if (attrs == null) | ||
continue; | ||
|
||
String plugins = attrs.get("bnd-plugins"); | ||
if (plugins == null) | ||
continue; | ||
|
||
if (!(plugins.isEmpty() || "true".equalsIgnoreCase(plugins))) { | ||
if (Verifier.isVersionRange(plugins)) { | ||
VersionRange range = new VersionRange(plugins); | ||
if (!range.includes(About._3_0)) // TODO | ||
continue; | ||
} | ||
} | ||
|
||
// | ||
// Ok, matched | ||
// | ||
|
||
String v = attrs.getVersion(); | ||
if (v == null) | ||
v = "0"; | ||
|
||
for (Iterator<String> i = attrs.keySet().iterator(); i.hasNext();) { | ||
String key = i.next(); | ||
if (key.endsWith(":")) | ||
i.remove(); | ||
} | ||
|
||
if (Verifier.isVersion(v)) { | ||
Version version = new Version(v); | ||
attrs.put("version", new VersionRange(true, version, version, true).toString()); | ||
} | ||
out.put(e.getKey(), attrs); | ||
} | ||
if (out.isEmpty()) | ||
return false; | ||
|
||
String imports = out.toString(); | ||
synchronized (this) { | ||
|
||
if (delta == null) | ||
delta = new ArrayList<>(); | ||
|
||
delta.add(imports); | ||
} | ||
return true; | ||
} | ||
|
||
@Override | ||
public void weave(WovenClass wovenClass) { | ||
if (delta == null || delta.isEmpty()) | ||
return; | ||
|
||
BundleWiring wiring = wovenClass.getBundleWiring(); | ||
if (wiring == null) | ||
return; | ||
|
||
if (wiring.getBundle() != bndlib) | ||
return; | ||
|
||
List<String> extra; | ||
synchronized (this) { | ||
extra = delta; | ||
delta = null; | ||
} | ||
if (extra != null) | ||
wovenClass.getDynamicImports().addAll(extra); | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
if (closed.getAndSet(true) == false) { | ||
hook.unregister(); | ||
tracker.close(); | ||
} | ||
} | ||
|
||
} |
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
21 changes: 21 additions & 0 deletions
21
bndtools.jareditor/src/bndtools/test/plugin/TestPlugin.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,21 @@ | ||
package bndtools.test.plugin; | ||
|
||
import java.util.Map; | ||
|
||
import aQute.bnd.service.Plugin; | ||
import aQute.service.reporter.Reporter; | ||
|
||
public class TestPlugin implements Plugin { | ||
|
||
@Override | ||
public void setProperties(Map<String,String> map) throws Exception { | ||
System.out.println("Yes, worked!"); | ||
} | ||
|
||
@Override | ||
public void setReporter(Reporter processor) { | ||
// TODO Auto-generated method stub | ||
|
||
} | ||
|
||
} |
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 |
---|---|---|
@@ -1,2 +1,4 @@ | ||
Foo foo | ||
-include ~${workspace}/cnf/bar.bnd | ||
-include ~${workspace}/cnf/bar.bnd | ||
|
||
-plugin.foo = bndtools.test.plugin.TestPlugin |