Search in sources :

Example 96 with BundleException

use of org.osgi.framework.BundleException in project bnd by bndtools.

the class Launcher method install.

Bundle install(File f) throws Exception {
    BundleContext context = systemBundle.getBundleContext();
    try {
        String reference;
        if (isWindows() || parms.noreferences) {
            trace("no reference: url %s", parms.noreferences);
            reference = f.toURI().toURL().toExternalForm();
        } else
            reference = "reference:" + f.toURI().toURL().toExternalForm();
        Bundle b = context.installBundle(reference);
        if (b.getLastModified() < f.lastModified()) {
            b.update();
        }
        return b;
    } catch (BundleException e) {
        trace("failed reference, will try to install %s with input stream", f.getAbsolutePath());
        String reference = f.toURI().toURL().toExternalForm();
        try (InputStream in = IO.stream(f)) {
            return context.installBundle(reference, in);
        }
    }
}
Also used : Bundle(org.osgi.framework.Bundle) InputStream(java.io.InputStream) BundleException(org.osgi.framework.BundleException) BundleContext(org.osgi.framework.BundleContext)

Example 97 with BundleException

use of org.osgi.framework.BundleException in project bnd by bndtools.

the class Launcher method update.

/**
	 * Ensure that all the bundles in the parameters are actually started. We
	 * can start in embedded mode (bundles are inside our main jar) or in file
	 * system mode.
	 * 
	 * @param begin
	 */
void update(long before) throws Exception {
    trace("Updating framework with %s", parms.runbundles);
    List<Bundle> tobestarted = new ArrayList<Bundle>();
    if (parms.embedded)
        installEmbedded(tobestarted);
    else
        synchronizeFiles(tobestarted, before);
    if (padmin != null) {
        inrefresh = true;
        padmin.refreshPackages(null);
        trace("Waiting for refresh to finish");
        // when we created the framework.
        while (inrefresh) Thread.sleep(100);
    } else
        trace("cannot refresh the bundles because there is no Package Admin");
    trace("bundles administered %s", installedBundles.keySet());
    // they will not automatically get AllPermission anymore
    if (security)
        policy.setDefaultPermissions(null);
    // Get the resolved status
    if (padmin != null && padmin.resolveBundles(null) == false) {
        List<String> failed = new ArrayList<String>();
        for (Bundle b : installedBundles.values()) {
            try {
                if (b.getState() == Bundle.INSTALLED) {
                    b.start();
                }
            } catch (Exception e) {
                failed.add(b.getSymbolicName() + "-" + b.getVersion() + " " + e + "\n");
            }
        }
        error("could not resolve the bundles: " + failed);
    // return LauncherConstants.RESOLVE_ERROR;
    }
    // Now start all the installed bundles in the same order
    // (unless they're a fragment)
    trace("Will start bundles: %s", tobestarted);
    List<Bundle> all = new ArrayList<Bundle>(tobestarted);
    // Add all bundles that we've tried to start but failed
    all.addAll(wantsToBeStarted);
    for (Bundle b : tobestarted) {
        try {
            trace("starting %s", b.getSymbolicName());
            if (!isFragment(b))
                b.start(Bundle.START_ACTIVATION_POLICY);
            trace("started  %s", b.getSymbolicName());
        } catch (BundleException e) {
            wantsToBeStarted.add(b);
            error("Failed to start bundle %s-%s, exception %s", b.getSymbolicName(), b.getVersion(), e);
        }
    }
}
Also used : Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) BundleException(org.osgi.framework.BundleException) BundleException(org.osgi.framework.BundleException) InvocationTargetException(java.lang.reflect.InvocationTargetException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException)

Example 98 with BundleException

use of org.osgi.framework.BundleException in project bnd by bndtools.

the class MiniFramework method installBundle.

@Override
public Bundle installBundle(String location) throws BundleException {
    try {
        if (location.startsWith("reference:"))
            location = new File(new URL(location.substring("reference:".length())).toURI()).getPath();
        else if (location.startsWith("file:"))
            location = new File(location.substring("file:".length())).getPath();
        while (location.startsWith("//")) location = location.substring(1);
        Context c = new Context(this, last, ++ID, location);
        bundles.put(Long.valueOf(c.id), c);
        last = c;
        return c;
    } catch (Exception e) {
        throw new BundleException("Failed to install", e);
    }
}
Also used : BundleContext(org.osgi.framework.BundleContext) BundleException(org.osgi.framework.BundleException) File(java.io.File) URL(java.net.URL) BundleException(org.osgi.framework.BundleException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException)

Example 99 with BundleException

use of org.osgi.framework.BundleException in project bnd by bndtools.

the class UnresolvedTester method testAllResolved.

@SuppressWarnings("deprecation")
public void testAllResolved() {
    assertNotNull("Expected a Bundle Context", context);
    StringBuilder sb = new StringBuilder();
    for (Bundle b : context.getBundles()) {
        if (b.getState() == Bundle.INSTALLED && b.getHeaders().get(aQute.bnd.osgi.Constants.FRAGMENT_HOST) == null) {
            try {
                b.start();
            } catch (BundleException e) {
                sb.append(b.getSymbolicName()).append(" [").append(b.getBundleId()).append("];").append(b.getVersion()).append("\n");
                sb.append("    ").append(e.getMessage()).append("\n\n");
                System.err.println(e.getMessage());
            }
        }
    }
    Matcher matcher = IP_P.matcher(sb);
    String out = matcher.replaceAll("\n\n         " + aQute.bnd.osgi.Constants.IMPORT_PACKAGE + ": $1;version=[$2,$3)\n");
    assertTrue("Unresolved bundles\n" + out, sb.length() == 0);
}
Also used : Matcher(java.util.regex.Matcher) Bundle(org.osgi.framework.Bundle) BundleException(org.osgi.framework.BundleException)

Aggregations

BundleException (org.osgi.framework.BundleException)99 Bundle (org.osgi.framework.Bundle)54 IOException (java.io.IOException)31 Test (org.junit.Test)19 File (java.io.File)15 ArrayList (java.util.ArrayList)13 InputStream (java.io.InputStream)10 FileInputStream (java.io.FileInputStream)9 BundleContext (org.osgi.framework.BundleContext)9 HashMap (java.util.HashMap)8 Map (java.util.Map)7 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)7 Hashtable (java.util.Hashtable)5 Manifest (java.util.jar.Manifest)5 ServiceReference (org.osgi.framework.ServiceReference)5 Version (org.osgi.framework.Version)5 BundleStartLevel (org.osgi.framework.startlevel.BundleStartLevel)5 LowDiskException (android.taobao.atlas.runtime.LowDiskException)4 TimeoutException (java.util.concurrent.TimeoutException)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4