Search in sources :

Example 81 with Bundle

use of org.osgi.framework.Bundle in project karaf by apache.

the class ObrCommandSupport method searchRepository.

protected Resource[] searchRepository(RepositoryAdmin admin, String targetId, String targetVersion) throws InvalidSyntaxException {
    // Try to see if the targetId is a bundle ID.
    try {
        Bundle bundle = bundleContext.getBundle(Long.parseLong(targetId));
        targetId = bundle.getSymbolicName();
    } catch (NumberFormatException ex) {
    // It was not a number, so ignore.
    }
    // The targetId may be a bundle name or a bundle symbolic name,
    // so create the appropriate LDAP query.
    StringBuffer sb = new StringBuffer("(|(presentationname=");
    sb.append(targetId);
    sb.append(")(symbolicname=");
    sb.append(targetId);
    sb.append("))");
    if (targetVersion != null) {
        sb.insert(0, "(&");
        sb.append("(version=");
        sb.append(targetVersion);
        sb.append("))");
    }
    return admin.discoverResources(sb.toString());
}
Also used : Bundle(org.osgi.framework.Bundle)

Example 82 with Bundle

use of org.osgi.framework.Bundle in project karaf by apache.

the class Converters method convertBundle.

private Object convertBundle(Object in) {
    String s = in.toString();
    try {
        long id = Long.parseLong(s);
        return context.getBundle(id);
    } catch (NumberFormatException nfe) {
    // Ignore
    }
    Bundle[] bundles = context.getBundles();
    for (Bundle b : bundles) {
        if (b.getLocation().equals(s)) {
            return b;
        }
        if (b.getSymbolicName().equals(s)) {
            return b;
        }
    }
    return null;
}
Also used : Bundle(org.osgi.framework.Bundle)

Example 83 with Bundle

use of org.osgi.framework.Bundle in project karaf by apache.

the class BundleHelpProvider method getHelp.

@Override
public String getHelp(Session session, String path) {
    if (path.indexOf('|') > 0) {
        if (path.startsWith("bundle|")) {
            path = path.substring("bundle|".length());
        } else {
            return null;
        }
    }
    if (path.matches("[0-9]*")) {
        long id = Long.parseLong(path);
        BundleContext bundleContext = FrameworkUtil.getBundle(getClass()).getBundleContext();
        Bundle bundle = bundleContext.getBundle(id);
        if (bundle != null) {
            String title = ShellUtil.getBundleName(bundle);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            PrintStream ps = new PrintStream(baos);
            ps.println("\n" + title);
            ps.println(ShellUtil.getUnderlineString(title));
            URL bundleInfo = bundle.getEntry("OSGI-INF/bundle.info");
            if (bundleInfo != null) {
                try (BufferedReader reader = new BufferedReader(new InputStreamReader(bundleInfo.openStream()))) {
                    int maxSize = 80;
                    Terminal terminal = session.getTerminal();
                    if (terminal != null) {
                        maxSize = terminal.getWidth();
                    }
                    WikiVisitor visitor = new AnsiPrintingWikiVisitor(ps, maxSize);
                    WikiParser parser = new WikiParser(visitor);
                    parser.parse(reader);
                } catch (Exception e) {
                // ignore
                }
            }
            ps.close();
            return baos.toString();
        }
    }
    return null;
}
Also used : PrintStream(java.io.PrintStream) InputStreamReader(java.io.InputStreamReader) Bundle(org.osgi.framework.Bundle) AnsiPrintingWikiVisitor(org.apache.karaf.shell.impl.console.commands.help.wikidoc.AnsiPrintingWikiVisitor) WikiVisitor(org.apache.karaf.shell.impl.console.commands.help.wikidoc.WikiVisitor) ByteArrayOutputStream(java.io.ByteArrayOutputStream) WikiParser(org.apache.karaf.shell.impl.console.commands.help.wikidoc.WikiParser) Terminal(org.apache.karaf.shell.api.console.Terminal) URL(java.net.URL) AnsiPrintingWikiVisitor(org.apache.karaf.shell.impl.console.commands.help.wikidoc.AnsiPrintingWikiVisitor) BufferedReader(java.io.BufferedReader) BundleContext(org.osgi.framework.BundleContext)

Example 84 with Bundle

use of org.osgi.framework.Bundle in project karaf by apache.

the class ShowBundleTree method createNodeForImport.

/*
     * Create a child node for a given import (by finding a matching export in the currently installed bundles)
     */
private void createNodeForImport(Node<Bundle> node, Bundle bundle, Clause i) {
    VersionRange range = VersionRange.parseVersionRange(i.getAttribute(Constants.VERSION_ATTRIBUTE));
    boolean foundMatch = false;
    for (Bundle b : bundleContext.getBundles()) {
        BundleWiring wiring = b.adapt(BundleWiring.class);
        if (wiring != null) {
            List<BundleCapability> caps = wiring.getCapabilities(BundleRevision.PACKAGE_NAMESPACE);
            if (caps != null) {
                for (BundleCapability cap : caps) {
                    String n = getAttribute(cap, BundleRevision.PACKAGE_NAMESPACE);
                    String v = getAttribute(cap, Constants.VERSION_ATTRIBUTE);
                    if (i.getName().equals(n) && range.contains(VersionTable.getVersion(v))) {
                        boolean existing = tree.flatten().contains(b);
                        System.out.printf("- import %s: resolved using %s%n", i, b);
                        foundMatch = true;
                        if (!node.hasChild(b)) {
                            Node<Bundle> child = node.addChild(b);
                            if (!existing) {
                                createNode(child);
                            }
                        }
                    }
                }
            }
        }
    }
    if (!foundMatch) {
        System.out.printf("- import %s: WARNING - unable to find matching export%n", i);
    }
}
Also used : Bundle(org.osgi.framework.Bundle) BundleWiring(org.osgi.framework.wiring.BundleWiring) VersionRange(org.apache.felix.utils.version.VersionRange) BundleCapability(org.osgi.framework.wiring.BundleCapability)

Example 85 with Bundle

use of org.osgi.framework.Bundle in project karaf by apache.

the class ShowBundleTree method createNode.

/*
    * Creates a node in the bundle tree
    */
private void createNode(Node<Bundle> node) {
    Bundle bundle = node.getValue();
    Collection<Bundle> exporters = new HashSet<>();
    exporters.addAll(bundleService.getWiredBundles(bundle).values());
    for (Bundle exporter : exporters) {
        if (node.hasAncestor(exporter)) {
            LOGGER.debug(format("Skipping %s (already exists in the current branch)", exporter));
        } else {
            boolean existing = tree.flatten().contains(exporter);
            LOGGER.debug(format("Adding %s as a dependency for %s", exporter, bundle));
            Node<Bundle> child = node.addChild(exporter);
            if (existing) {
                LOGGER.debug(format("Skipping children of %s (already exists in another branch)", exporter));
            } else {
                createNode(child);
            }
        }
    }
}
Also used : Bundle(org.osgi.framework.Bundle) HashSet(java.util.HashSet)

Aggregations

Bundle (org.osgi.framework.Bundle)2490 Test (org.junit.Test)659 URL (java.net.URL)388 BundleContext (org.osgi.framework.BundleContext)311 File (java.io.File)298 ArrayList (java.util.ArrayList)292 IOException (java.io.IOException)278 BundleException (org.osgi.framework.BundleException)270 HashMap (java.util.HashMap)149 ServiceReference (org.osgi.framework.ServiceReference)145 Path (org.eclipse.core.runtime.Path)126 Hashtable (java.util.Hashtable)124 HashSet (java.util.HashSet)95 InputStream (java.io.InputStream)94 IStatus (org.eclipse.core.runtime.IStatus)86 Status (org.eclipse.core.runtime.Status)82 List (java.util.List)80 Map (java.util.Map)74 BundleWiring (org.osgi.framework.wiring.BundleWiring)74 Version (org.osgi.framework.Version)73