Search in sources :

Example 36 with BundleWiring

use of org.osgi.framework.wiring.BundleWiring in project knime-core by knime.

the class JavaSnippet method getAdditionalBundlesPaths.

/**
 * Get jar files required for compiling with the additional bundles.
 *
 * @param withDeps Whether to also resolve the dependencies of the bundles.
 * @return Array of .jar files.
 */
public File[] getAdditionalBundlesPaths(final boolean withDeps) {
    final Set<Bundle> bundles = new LinkedHashSet<>();
    final LinkedBlockingQueue<Bundle> pending = new LinkedBlockingQueue<Bundle>();
    if (m_settings != null) {
        // Resolve bundle names to bundles
        Stream.of(m_settings.getBundles()).map(bname -> Platform.getBundle(bname.split(" ")[0])).filter(b -> b != null).collect(Collectors.toCollection(() -> pending));
    }
    Bundle bundle = null;
    while ((bundle = pending.poll()) != null) {
        if (!bundles.add(bundle)) {
            continue;
        }
        if (withDeps) {
            // resolve dependencies
            final BundleWiring wiring = bundle.adapt(BundleWiring.class);
            final List<BundleWire> requiredWires = wiring.getRequiredWires(BundleNamespace.BUNDLE_NAMESPACE);
            for (final BundleWire w : requiredWires) {
                pending.add(w.getProviderWiring().getBundle());
            }
        }
    }
    final ArrayList<File> result = new ArrayList<>();
    for (final Bundle b : bundles) {
        try {
            result.add(FileLocator.getBundleFile(b));
        } catch (IOException e) {
            LOGGER.warn("Failed to get bundle file of \"" + b + "\"");
        }
    }
    return result.toArray(new File[result.size()]);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Arrays(java.util.Arrays) RowKey(org.knime.core.data.RowKey) Parser(org.fife.ui.rsyntaxtextarea.parser.Parser) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) JavaSnippetSettings(org.knime.base.node.jsnippet.util.JavaSnippetSettings) CollectionConverterFactory(org.knime.core.data.convert.java.CollectionConverterFactory) JavaSnippetDocument(org.knime.base.node.jsnippet.guarded.JavaSnippetDocument) URLClassLoader(java.net.URLClassLoader) ConverterUtil(org.knime.base.node.jsnippet.type.ConverterUtil) Map(java.util.Map) EclipseFileObject(org.eclipse.jdt.internal.compiler.tool.EclipseFileObject) JavaSnippetUtil(org.knime.base.node.jsnippet.util.JavaSnippetUtil) MultiParentClassLoader(org.knime.core.data.convert.util.MultiParentClassLoader) Set(java.util.Set) BadLocationException(javax.swing.text.BadLocationException) StandardCharsets(java.nio.charset.StandardCharsets) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) JavaFileObject(javax.tools.JavaFileObject) JavaFieldList(org.knime.base.node.jsnippet.util.JavaFieldList) Stream(java.util.stream.Stream) Type(org.knime.base.node.jsnippet.expression.Type) JavaToDataCellConverterRegistry(org.knime.core.data.convert.datacell.JavaToDataCellConverterRegistry) Kind(javax.tools.JavaFileObject.Kind) FlowVariable(org.knime.core.node.workflow.FlowVariable) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) BundleNamespace(org.osgi.framework.namespace.BundleNamespace) JavaSnippetFields(org.knime.base.node.jsnippet.util.JavaSnippetFields) DocumentEvent(javax.swing.event.DocumentEvent) DataCell(org.knime.core.data.DataCell) LinkedHashSet(java.util.LinkedHashSet) InCol(org.knime.base.node.jsnippet.util.field.InCol) ClassUtil(org.knime.core.data.convert.util.ClassUtil) DefaultRow(org.knime.core.data.def.DefaultRow) JavaSnippetTemplate(org.knime.base.node.jsnippet.template.JavaSnippetTemplate) ValidationReport(org.knime.base.node.jsnippet.util.ValidationReport) KNIMEConstants(org.knime.core.node.KNIMEConstants) BufferedWriter(java.io.BufferedWriter) StringWriter(java.io.StringWriter) GUARDED_FIELDS(org.knime.base.node.jsnippet.guarded.JavaSnippetDocument.GUARDED_FIELDS) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) Field(java.lang.reflect.Field) FlowVariableException(org.knime.base.node.jsnippet.expression.FlowVariableException) File(java.io.File) DataRow(org.knime.core.data.DataRow) DataCellToJavaConverterRegistry(org.knime.core.data.convert.java.DataCellToJavaConverterRegistry) OutCol(org.knime.base.node.jsnippet.util.field.OutCol) GuardedDocument(org.knime.base.node.jsnippet.guarded.GuardedDocument) Platform(org.eclipse.core.runtime.Platform) FileUtil(org.knime.core.util.FileUtil) DataType(org.knime.core.data.DataType) DocumentListener(javax.swing.event.DocumentListener) ModuleClassLoader(org.eclipse.osgi.internal.loader.ModuleClassLoader) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) URL(java.net.URL) Abort(org.knime.base.node.jsnippet.expression.Abort) GUARDED_BODY_END(org.knime.base.node.jsnippet.guarded.JavaSnippetDocument.GUARDED_BODY_END) ArrayToCollectionConverterFactory(org.knime.core.data.convert.datacell.ArrayToCollectionConverterFactory) DataCellToJavaConverterFactory(org.knime.core.data.convert.java.DataCellToJavaConverterFactory) JSnippet(org.knime.base.node.jsnippet.util.JSnippet) OutVar(org.knime.base.node.jsnippet.util.field.OutVar) CompilationTask(javax.tools.JavaCompiler.CompilationTask) AbstractJSnippet(org.knime.base.node.jsnippet.expression.AbstractJSnippet) DataColumnSpec(org.knime.core.data.DataColumnSpec) GUARDED_IMPORTS(org.knime.base.node.jsnippet.guarded.JavaSnippetDocument.GUARDED_IMPORTS) Locale(java.util.Locale) Diagnostic(javax.tools.Diagnostic) FlowVariableRepository(org.knime.base.node.jsnippet.util.FlowVariableRepository) Cell(org.knime.base.node.jsnippet.expression.Cell) Bundle(org.osgi.framework.Bundle) DiagnosticCollector(javax.tools.DiagnosticCollector) GUARDED_BODY_START(org.knime.base.node.jsnippet.guarded.JavaSnippetDocument.GUARDED_BODY_START) Collection(java.util.Collection) JavaField(org.knime.base.node.jsnippet.util.field.JavaField) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Collectors(java.util.stream.Collectors) ColumnException(org.knime.base.node.jsnippet.expression.ColumnException) Objects(java.util.Objects) List(java.util.List) BufferedDataTable(org.knime.core.node.BufferedDataTable) Writer(java.io.Writer) OutColList(org.knime.base.node.jsnippet.util.JavaFieldList.OutColList) Optional(java.util.Optional) Document(javax.swing.text.Document) DateAndTimeCell(org.knime.core.data.date.DateAndTimeCell) GuardedSection(org.knime.base.node.jsnippet.guarded.GuardedSection) DataTableSpec(org.knime.core.data.DataTableSpec) TypeException(org.knime.base.node.jsnippet.expression.TypeException) JavaSnippetCompiler(org.knime.base.node.jsnippet.util.JavaSnippetCompiler) JavaToDataCellConverterFactory(org.knime.core.data.convert.datacell.JavaToDataCellConverterFactory) ExecutionContext(org.knime.core.node.ExecutionContext) JarEntry(java.util.jar.JarEntry) BundleFile(org.eclipse.osgi.storage.bundlefile.BundleFile) CellFactory(org.knime.core.data.container.CellFactory) NodeLogger(org.knime.core.node.NodeLogger) OutputStreamWriter(java.io.OutputStreamWriter) JarOutputStream(java.util.jar.JarOutputStream) BundleWiring(org.osgi.framework.wiring.BundleWiring) MalformedURLException(java.net.MalformedURLException) InVar(org.knime.base.node.jsnippet.util.field.InVar) FileLocator(org.eclipse.core.runtime.FileLocator) JSnippetParser(org.knime.base.node.jsnippet.ui.JSnippetParser) Closeable(java.io.Closeable) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) BundleWire(org.osgi.framework.wiring.BundleWire) Collections(java.util.Collections) InputStream(java.io.InputStream) Bundle(org.osgi.framework.Bundle) BundleWiring(org.osgi.framework.wiring.BundleWiring) ArrayList(java.util.ArrayList) IOException(java.io.IOException) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) BundleWire(org.osgi.framework.wiring.BundleWire) File(java.io.File) BundleFile(org.eclipse.osgi.storage.bundlefile.BundleFile)

Example 37 with BundleWiring

use of org.osgi.framework.wiring.BundleWiring in project rt.equinox.framework by eclipse.

the class StateResolverTest method testResolveFragmentEE01.

public void testResolveFragmentEE01() throws BundleException {
    State state = buildEmptyState();
    Dictionary[] props = new Dictionary[] { new Hashtable() };
    // $NON-NLS-1$ //$NON-NLS-2$
    props[0].put("org.osgi.framework.executionenvironment", "test");
    state.setPlatformProperties(props);
    int bundleID = 0;
    Hashtable manifest = new Hashtable();
    manifest.clear();
    // $NON-NLS-1$
    manifest.put(Constants.BUNDLE_MANIFESTVERSION, "2");
    // $NON-NLS-1$
    manifest.put(Constants.BUNDLE_SYMBOLICNAME, "org.eclipse.osgi");
    // $NON-NLS-1$
    manifest.put(Constants.BUNDLE_VERSION, "1.0.0");
    manifest.put(Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT, "test");
    BundleDescription a = state.getFactory().createBundleDescription(state, manifest, (String) manifest.get(Constants.BUNDLE_SYMBOLICNAME) + manifest.get(Constants.BUNDLE_VERSION), bundleID++);
    manifest.clear();
    // $NON-NLS-1$
    manifest.put(Constants.BUNDLE_MANIFESTVERSION, "2");
    // $NON-NLS-1$
    manifest.put(Constants.BUNDLE_SYMBOLICNAME, "b");
    // $NON-NLS-1$
    manifest.put(Constants.BUNDLE_VERSION, "1.0.0");
    // $NON-NLS-1$
    manifest.put(Constants.FRAGMENT_HOST, "org.eclipse.osgi; bundle-version=\"[1.0, 1.1)\"");
    manifest.put(Constants.REQUIRE_CAPABILITY, "osgi.ee; filter:=\"(osgi.ee=test)\"");
    BundleDescription b = state.getFactory().createBundleDescription(state, manifest, (String) manifest.get(Constants.BUNDLE_SYMBOLICNAME) + manifest.get(Constants.BUNDLE_VERSION), bundleID++);
    manifest.clear();
    // $NON-NLS-1$
    manifest.put(Constants.BUNDLE_MANIFESTVERSION, "2");
    // $NON-NLS-1$
    manifest.put(Constants.BUNDLE_SYMBOLICNAME, "c");
    // $NON-NLS-1$
    manifest.put(Constants.BUNDLE_VERSION, "1.0.0");
    // $NON-NLS-1$
    manifest.put(Constants.REQUIRE_CAPABILITY, "osgi.identity; filter:=\"(osgi.identity=b)\"");
    BundleDescription c = state.getFactory().createBundleDescription(state, manifest, (String) manifest.get(Constants.BUNDLE_SYMBOLICNAME) + manifest.get(Constants.BUNDLE_VERSION), bundleID++);
    state.addBundle(b);
    state.addBundle(a);
    state.addBundle(c);
    state.resolve();
    // $NON-NLS-1$
    assertTrue("A is not resolved", a.isResolved());
    // $NON-NLS-1$
    assertTrue("B is not resolved", b.isResolved());
    // $NON-NLS-1$
    assertTrue("C is not resolved", c.isResolved());
    BundleWiring aWiring = a.getWiring();
    List aRequirements = a.getRequirements("osgi.ee");
    List aRequiredWires = aWiring.getRequiredWires("osgi.ee");
    assertEquals("Wrong number of osgi.ee requirements from system bundle", 1, aRequirements.size());
    assertEquals("Wrong number of wires from system bundle", 1, aRequiredWires.size());
    BundleWiring bWiring = b.getWiring();
    List bRequirements = b.getRequirements("osgi.ee");
    List bRequiredWires = bWiring.getRequiredWires("osgi.ee");
    assertEquals("Wrong number of osgi.ee requirements from fragment", 1, bRequirements.size());
    assertEquals("Wrong number of wires from fragment", 1, bRequiredWires.size());
    BundleWiring cWiring = c.getWiring();
    List cRequirements = c.getRequirements("osgi.identity");
    List cRequiredWires = cWiring.getRequiredWires("osgi.identity");
    assertEquals("Wrong number of osgi.identity requirements from c", 1, cRequirements.size());
    assertEquals("Wrong number of wires from c", 1, cRequiredWires.size());
    manifest.clear();
    // $NON-NLS-1$
    manifest.put(Constants.BUNDLE_MANIFESTVERSION, "2");
    // $NON-NLS-1$
    manifest.put(Constants.BUNDLE_SYMBOLICNAME, "b");
    // $NON-NLS-1$
    manifest.put(Constants.BUNDLE_VERSION, "1.0.0");
    // $NON-NLS-1$
    manifest.put(Constants.FRAGMENT_HOST, "org.eclipse.osgi; bundle-version=\"[1.0, 1.1)\"");
    manifest.put(Constants.REQUIRE_CAPABILITY, "osgi.ee; filter:=\"(osgi.ee=fail)\"");
    b = state.getFactory().createBundleDescription(state, manifest, (String) manifest.get(Constants.BUNDLE_SYMBOLICNAME) + manifest.get(Constants.BUNDLE_VERSION), b.getBundleId());
    state.updateBundle(b);
    state.resolve(false);
    // $NON-NLS-1$
    assertTrue("A is not resolved", a.isResolved());
    // $NON-NLS-1$
    assertFalse("B is not resolved", b.isResolved());
    // $NON-NLS-1$
    assertFalse("C is not resolved", c.isResolved());
}
Also used : Dictionary(java.util.Dictionary) State(org.eclipse.osgi.service.resolver.State) Hashtable(java.util.Hashtable) BundleDescription(org.eclipse.osgi.service.resolver.BundleDescription) BundleWiring(org.osgi.framework.wiring.BundleWiring) ArrayList(java.util.ArrayList) List(java.util.List)

Example 38 with BundleWiring

use of org.osgi.framework.wiring.BundleWiring in project rt.equinox.framework by eclipse.

the class OSGiCapabilityTest method testOSGiCardinality.

public void testOSGiCardinality() throws BundleException {
    State state = buildEmptyState();
    long bundleID = 0;
    Dictionary manifest;
    manifest = loadManifest("p1.osgi.MF");
    BundleDescription p1 = state.getFactory().createBundleDescription(state, manifest, (String) manifest.get(Constants.BUNDLE_SYMBOLICNAME), bundleID++);
    manifest = loadManifest("p2.osgi.MF");
    BundleDescription p2 = state.getFactory().createBundleDescription(state, manifest, (String) manifest.get(Constants.BUNDLE_SYMBOLICNAME), bundleID++);
    manifest = loadManifest("p3.osgi.MF");
    BundleDescription p3 = state.getFactory().createBundleDescription(state, manifest, (String) manifest.get(Constants.BUNDLE_SYMBOLICNAME), bundleID++);
    manifest = loadManifest("c5.osgi.MF");
    BundleDescription c5 = state.getFactory().createBundleDescription(state, manifest, (String) manifest.get(Constants.BUNDLE_SYMBOLICNAME), bundleID++);
    state.addBundle(p1);
    state.addBundle(p2);
    state.addBundle(p3);
    state.addBundle(c5);
    state.resolve();
    assertTrue("p1", p1.isResolved());
    assertTrue("p2", p2.isResolved());
    assertTrue("p3", p3.isResolved());
    assertTrue("c5", c5.isResolved());
    BundleWiring c5Wiring = c5.getWiring();
    List requiredWires = c5Wiring.getRequiredWires(null);
    assertEquals("Wrong number of required wires.", 3, requiredWires.size());
    List expectedCapabilities = new ArrayList();
    expectedCapabilities.addAll(p1.getCapabilities("namespace.1"));
    expectedCapabilities.addAll(p2.getCapabilities("namespace.1"));
    expectedCapabilities.addAll(p3.getCapabilities("namespace.1"));
    for (Iterator iWires = requiredWires.iterator(); iWires.hasNext(); ) {
        BundleWire wire = (BundleWire) iWires.next();
        expectedCapabilities.remove(wire.getCapability());
    }
    assertTrue("Unexpected capability wire: " + requiredWires, expectedCapabilities.isEmpty());
}
Also used : BundleWiring(org.osgi.framework.wiring.BundleWiring) BundleWire(org.osgi.framework.wiring.BundleWire)

Example 39 with BundleWiring

use of org.osgi.framework.wiring.BundleWiring in project rt.equinox.framework by eclipse.

the class ClassLoadingBundleTests method testBootGetResources02.

public void testBootGetResources02() throws Exception {
    // This will fail on the IBM VM (see bug 409314)
    if (System.getProperty(Constants.FRAMEWORK_BOOTDELEGATION) != null)
        // cannot really test this if this property is set
        return;
    // $NON-NLS-1$
    Bundle test = installer.installBundle("test");
    installer.resolveBundles(new Bundle[] { test });
    BundleWiring wiring = test.adapt(BundleWiring.class);
    ClassLoader bcl = wiring.getClassLoader();
    URLClassLoader cl = new URLClassLoader(new URL[0], bcl);
    // $NON-NLS-1$
    Enumeration manifests = cl.getResources("META-INF/MANIFEST.MF");
    // $NON-NLS-1$
    assertNotNull("manifests", manifests);
    ArrayList manifestURLs = new ArrayList();
    while (manifests.hasMoreElements()) manifestURLs.add(manifests.nextElement());
    // $NON-NLS-1$
    assertEquals("manifest number", 1, manifestURLs.size());
    URL manifest = (URL) manifestURLs.get(0);
    assertEquals("wrong protocol", "bundleresource", manifest.getProtocol());
    int dotIndex = manifest.getHost().indexOf('.');
    long bundleId = dotIndex >= 0 && dotIndex < manifest.getHost().length() - 1 ? Long.parseLong(manifest.getHost().substring(0, dotIndex)) : Long.parseLong(manifest.getHost());
    // $NON-NLS-1$
    assertEquals("host id", test.getBundleId(), bundleId);
    cl.close();
}
Also used : Enumeration(java.util.Enumeration) Bundle(org.osgi.framework.Bundle) BundleWiring(org.osgi.framework.wiring.BundleWiring) URLClassLoader(java.net.URLClassLoader) ArrayList(java.util.ArrayList) ModuleClassLoader(org.eclipse.osgi.internal.loader.ModuleClassLoader) URLClassLoader(java.net.URLClassLoader) URL(java.net.URL) Endpoint(javax.xml.ws.Endpoint)

Example 40 with BundleWiring

use of org.osgi.framework.wiring.BundleWiring in project rt.equinox.framework by eclipse.

the class ClassLoadingBundleTests method testUninstallInUse01.

public void testUninstallInUse01() throws BundleException {
    // $NON-NLS-1$
    Bundle exporter1 = installer.installBundle("exporter.importer1");
    BundleRevision iExporter1 = exporter1.adapt(BundleRevision.class);
    // $NON-NLS-1$
    Bundle exporter2 = installer.installBundle("exporter.importer2");
    installer.resolveBundles(new Bundle[] { exporter1, exporter2 });
    exporter1.uninstall();
    // $NON-NLS-1$
    Bundle importer = installer.installBundle("exporter.importer4");
    installer.resolveBundles(new Bundle[] { importer });
    BundleWiring importerWiring = importer.adapt(BundleWiring.class);
    assertNotNull("Bundle b has no wiring.", importerWiring);
    List<BundleWire> bImports = importerWiring.getRequiredWires(PackageNamespace.PACKAGE_NAMESPACE);
    assertEquals("Wrong number of imported packages.", 1, bImports.size());
    assertEquals("Wrong exporter.", iExporter1, bImports.get(0).getProvider());
}
Also used : Bundle(org.osgi.framework.Bundle) BundleWiring(org.osgi.framework.wiring.BundleWiring) BundleRevision(org.osgi.framework.wiring.BundleRevision) BundleWire(org.osgi.framework.wiring.BundleWire)

Aggregations

BundleWiring (org.osgi.framework.wiring.BundleWiring)82 Bundle (org.osgi.framework.Bundle)48 BundleWire (org.osgi.framework.wiring.BundleWire)27 ArrayList (java.util.ArrayList)20 Test (org.junit.Test)14 BundleCapability (org.osgi.framework.wiring.BundleCapability)13 BundleRevision (org.osgi.framework.wiring.BundleRevision)13 Hashtable (java.util.Hashtable)12 Dictionary (java.util.Dictionary)10 List (java.util.List)10 HashMap (java.util.HashMap)9 URL (java.net.URL)8 BundleContext (org.osgi.framework.BundleContext)8 LinkedHashMap (java.util.LinkedHashMap)6 AbstractIntegrationTest (org.apache.aries.jmx.AbstractIntegrationTest)6 IAnswer (org.easymock.IAnswer)6 ServiceReference (org.osgi.framework.ServiceReference)6 ServiceRegistration (org.osgi.framework.ServiceRegistration)6 Version (org.osgi.framework.Version)6 File (java.io.File)5