Java Code Examples for org.osgi.framework.BundleContext#installBundle()

The following examples show how to use org.osgi.framework.BundleContext#installBundle() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Util.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Install a bundle from a resource file.
 *
 * @param bc context owning both resources and to install bundle from
 * @param resource resource name of bundle jar file
 * @return the installed bundle
 * @throws BundleException if no such resource is found or if
 *                         installation fails.
 */
public static Bundle installBundle(BundleContext bc, String resource) throws BundleException {
  try {
    URL url = bc.getBundle().getResource(resource);
    if(url == null) {
      throw new BundleException("No resource " + resource);
    }
    InputStream in = url.openStream();
    if(in == null) {
      throw new BundleException("No resource " + resource);
    }
    return bc.installBundle("internal:" + resource, in);
  } catch (IOException e) {
    throw new BundleException("Failed to get input stream for " + resource + ": " + e);
  }
}
 
Example 2
Source File: Util.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Install a bundle from a resource file.
 *
 * @param bc context owning both resources and to install bundle from
 * @param resource resource name of bundle jar file
 * @return the installed bundle
 * @throws BundleException if no such resource is found or if
 *                         installation fails.
 */
public static Bundle installBundle(BundleContext bc, String resource)
  throws BundleException
{
  try {
    System.out.println("installBundle(" + resource + ")");
    URL url = bc.getBundle().getResource(resource);
    if(url == null) {
      throw new BundleException("No resource " + resource);
    }
    InputStream in = url.openStream();
    if(in == null) {
      throw new BundleException("No resource " + resource);
    }
    return bc.installBundle("internal:" + resource, in);
  } catch (IOException e) {
    throw new BundleException
      ("Failed to get input stream for " + resource + ": " + e);
  }
}
 
Example 3
Source File: Util.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static Bundle installBundle(BundleContext context, String resource){
  try{
    /* get the class loader */
    //ClassLoader classLoader = ClassLoader.getSystemClassLoader();
    /* get the resource */
    //URL url=classLoader.getResource(resource);
    URL url = context.getBundle().getResource(resource);
    /* print the url */
    System.out.println("URL:" + url);
    /* install the bundle */
    return context.installBundle(LOC_PROT + resource, url.openStream());
    //Bundle bundle = context.installBundle(url.toString());
    //return bundle;

  }catch(Exception e){
    /* print the error */
    System.out.println("ERROR in Util installBundle()" + e);
    e.printStackTrace();
    return null;
  }

}
 
Example 4
Source File: Util.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Install a bundle from a resource file.
 *
 * @param bc context owning both resources and to install bundle from
 * @param resource resource name of bundle jar file
 * @return the installed bundle
 * @throws BundleException if no such resource is found or if
 *                         installation fails.
 */
public static Bundle installBundle(BundleContext bc, String resource) throws BundleException {
  try {
    URL url = bc.getBundle().getResource(resource);
    if(url == null) {
      throw new BundleException("No resource " + resource);
    }
    InputStream in = url.openStream();
    if(in == null) {
      throw new BundleException("No resource " + resource);
    }
    return bc.installBundle("internal:" + resource, in);
  } catch (IOException e) {
    throw new BundleException("Failed to get input stream for " + resource + ": " + e);
  }
}
 
Example 5
Source File: CarbonServer.java    From carbon-kernel with Apache License 2.0 6 votes vote down vote up
/**
 * Installs a bundle from the specified locations.
 *
 * @param bundleContext bundle's execution context within the Framework
 * @throws BundleException
 */
private void loadInitialBundles(BundleContext bundleContext) throws BundleException {
    //Setting this property due to an issue with equinox simple configurator where it tries to uninstall bundles
    //which are loaded from initial bundle list.
    System.setProperty(Constants.EQUINOX_SIMPLE_CONFIGURATOR_EXCLUSIVE_INSTALLATION, "false");

    for (CarbonInitialBundle initialBundleInfo : config.getInitialBundles()) {
        if (logger.isLoggable(Level.FINE)) {
            logger.log(Level.FINE, "Loading initial bundle: " + initialBundleInfo.getLocation().toExternalForm() +
                    " with startlevel " + initialBundleInfo.getLevel());
        }

        Bundle bundle = bundleContext.installBundle(initialBundleInfo.getLocation().toString());
        if (initialBundleInfo.shouldStart()) {
            bundle.start();
        }
    }
}
 
Example 6
Source File: SyntheticBundleInstaller.java    From openhab-core with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Install synthetic bundle, denoted by its name, into the test runtime (by using the given bundle context).
 *
 * @param bundleContext the bundle context of the test runtime
 * @param testBundleNamethe symbolic name of the sub-directory of {@value #BUNDLE_POOL_PATH}, which contains the
 *            files
 *            for the synthetic bundle
 * @param extensionsToInclude a list of extension to be included into the synthetic bundle. In order to use the list
 *            of default extensions ({@link #DEFAULT_EXTENSIONS})
 * @return the synthetic bundle representation
 * @throws Exception thrown when error occurs while installing or starting the synthetic bundle
 */
public static Bundle install(BundleContext bundleContext, String testBundleName, Set<String> extensionsToInclude)
        throws Exception {
    String bundlePath = BUNDLE_POOL_PATH + "/" + testBundleName + "/";
    byte[] syntheticBundleBytes = createSyntheticBundle(bundleContext.getBundle(), bundlePath, testBundleName,
            extensionsToInclude);

    Bundle syntheticBundle = bundleContext.installBundle(testBundleName,
            new ByteArrayInputStream(syntheticBundleBytes));
    syntheticBundle.start(Bundle.ACTIVE);
    waitUntilLoadingFinished(bundleContext, syntheticBundle);
    return syntheticBundle;
}
 
Example 7
Source File: SyntheticBundleInstaller.java    From openhab-core with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Updates given bundle into the test runtime (the content is changed, but the symbolic name of the bundles remains
 * the same) with a new content, prepared in another resources directory.
 *
 * @param bundleContext the bundle context of the test runtime
 * @param bundleToUpdateName the symbolic name of the bundle to be updated
 * @param updateDirName the location of the new content, that the target bundle will be updated with
 * @param extensionsToInclude a list of extension to be included into the synthetic bundle
 * @return the Bundle representation of the updated bundle
 * @throws Exception thrown when error occurs while installing or starting the synthetic bundle
 */
public static Bundle update(BundleContext bundleContext, String bundleToUpdateName, String updateDirName,
        Set<String> extensionsToInclude) throws Exception {
    // Stop the bundle to update first
    Bundle[] bundles = bundleContext.getBundles();
    for (Bundle bundle : bundles) {
        if (bundleToUpdateName.equals(bundle.getSymbolicName())) {
            // we have to uninstall the bundle to update its contents
            bundle.uninstall();
            break;
        }
    }

    // New bytes are taken from the update path
    String updatePath = BUNDLE_POOL_PATH + "/" + updateDirName + "/";
    byte[] updatedBundleBytes = createSyntheticBundle(bundleContext.getBundle(), updatePath, bundleToUpdateName,
            extensionsToInclude);

    // The updated bytes are installed with the same name
    Bundle syntheticBundle = bundleContext.installBundle(bundleToUpdateName,
            new ByteArrayInputStream(updatedBundleBytes));

    // Starting the bundle
    syntheticBundle.start(Bundle.ACTIVE);
    waitUntilLoadingFinished(bundleContext, syntheticBundle);
    return syntheticBundle;
}
 
Example 8
Source File: EmbeddedFelixFramework.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
/** Wraps the bundle if successful or already installed, wraps TRUE if it's the system entry,
 * wraps null if the bundle is already installed from somewhere else;
 * in all these cases <i>masking</i> an explanatory error if already installed or it's the system entry.
 * <p>
 * Returns an instance wrapping null and <i>throwing</i> an error if the bundle could not be installed.
 */
private static ReferenceWithError<?> installExtensionBundle(BundleContext bundleContext, URL manifestUrl, Map<String, Bundle> installedBundles, String frameworkVersionedId) {
    //ignore http://felix.extensions:9/ system entry
    if("felix.extensions".equals(manifestUrl.getHost()))
        return ReferenceWithError.newInstanceMaskingError(null, new IllegalArgumentException("Skipping install of internal extension bundle from "+manifestUrl));

    try {
        Manifest manifest = readManifest(manifestUrl);
        if (!isValidBundle(manifest))
            return ReferenceWithError.newInstanceMaskingError(null, new IllegalArgumentException("Resource at "+manifestUrl+" is not an OSGi bundle: no valid manifest"));

        String versionedId = OsgiUtils.getVersionedId(manifest);
        URL bundleUrl = OsgiUtils.getContainerUrl(manifestUrl, MANIFEST_PATH);

        Bundle existingBundle = installedBundles.get(versionedId);
        if (existingBundle != null) {
            if (!bundleUrl.equals(existingBundle.getLocation()) &&
                    //the framework bundle is always pre-installed, don't display duplicate info
                    !versionedId.equals(frameworkVersionedId)) {
                return ReferenceWithError.newInstanceMaskingError(null, new IllegalArgumentException("Bundle "+versionedId+" (from manifest " + manifestUrl + ") is already installed, from " + existingBundle.getLocation()));
            }
            return ReferenceWithError.newInstanceMaskingError(existingBundle, new IllegalArgumentException("Bundle "+versionedId+" from manifest " + manifestUrl + " is already installed"));
        }

        byte[] jar = buildExtensionBundle(manifest);
        LOG.debug("Installing boot bundle " + bundleUrl);
        //mark the bundle as extension so we can detect it later using the "system:" protocol
        //(since we cannot access BundleImpl.isExtension)
        Bundle newBundle = bundleContext.installBundle(EXTENSION_PROTOCOL + ":" + bundleUrl.toString(), new ByteArrayInputStream(jar));
        installedBundles.put(versionedId, newBundle);
        return ReferenceWithError.newInstanceWithoutError(newBundle);
    } catch (Exception e) {
        Exceptions.propagateIfFatal(e);
        return ReferenceWithError.newInstanceThrowingError(null,
            new IllegalStateException("Problem installing extension bundle " + manifestUrl + ": "+e, e));
    }
}
 
Example 9
Source File: SyntheticBundleInstaller.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Updates given bundle into the test runtime (the content is changed, but the symbolic name of the bundles remains
 * the same) with a new content, prepared in another resources directory.
 *
 * @param bundleContext the bundle context of the test runtime
 * @param bundleToUpdateName the symbolic name of the bundle to be updated
 * @param updateDirName the location of the new content, that the target bundle will be updated with
 * @param extensionsToInclude a list of extension to be included into the synthetic bundle
 * @return the Bundle representation of the updated bundle
 * @throws Exception thrown when error occurs while installing or starting the synthetic bundle
 */
public static Bundle update(BundleContext bundleContext, String bundleToUpdateName, String updateDirName,
        Set<String> extensionsToInclude) throws Exception {
    // Stop the bundle to update first
    Bundle[] bundles = bundleContext.getBundles();
    for (Bundle bundle : bundles) {
        if (bundleToUpdateName.equals(bundle.getSymbolicName())) {
            // we have to uninstall the bundle to update its contents
            bundle.uninstall();
            break;
        }
    }

    // New bytes are taken from the update path
    String updatePath = BUNDLE_POOL_PATH + "/" + updateDirName + "/";
    byte[] updatedBundleBytes = createSyntheticBundle(bundleContext.getBundle(), updatePath, bundleToUpdateName,
            extensionsToInclude);

    // The updated bytes are installed with the same name
    Bundle syntheticBundle = bundleContext.installBundle(bundleToUpdateName,
            new ByteArrayInputStream(updatedBundleBytes));

    // Starting the bundle
    syntheticBundle.start(Bundle.ACTIVE);
    waitUntilLoadingFinished(bundleContext, syntheticBundle);
    return syntheticBundle;
}
 
Example 10
Source File: BundleGenerator.java    From concierge with Eclipse Public License 1.0 4 votes vote down vote up
public Bundle install(final BundleContext context) throws BundleException,
		IOException {
	return context.installBundle(symbolicName, getInputStream());
}
 
Example 11
Source File: OsgiComponentServiceTest.java    From components with Apache License 2.0 4 votes vote down vote up
private void installNewComponentBundle(BundleContext bundleContext)
        throws BundleException, MalformedURLException, URISyntaxException {
    String exampleUrl = new URL("link:classpath:org.talend.components-osgi-test-component.link").toURI().toString();
    Bundle bundle = bundleContext.installBundle(exampleUrl);
    bundle.start();
}
 
Example 12
Source File: SyntheticBundleInstaller.java    From openhab-core with Eclipse Public License 2.0 3 votes vote down vote up
/**
 * Install synthetic bundle fragment, denoted by its name, into the test runtime (by using the given bundle
 * context). Only the default extensions set ({@link #DEFAULT_EXTENSIONS}) will be included into the synthetic
 * bundle fragment.
 *
 * @param bundleContext the bundle context of the test runtime
 * @param testBundleName the name of the sub-directory of {@value #BUNDLE_POOL_PATH}, which contains the files for
 *            the synthetic bundle
 * @return the synthetic bundle representation
 * @throws Exception thrown when error occurs while installing or starting the synthetic bundle fragment
 */
public static Bundle installFragment(BundleContext bundleContext, String testBundleName,
        Set<String> extensionsToInclude) throws Exception {
    String bundlePath = BUNDLE_POOL_PATH + "/" + testBundleName + "/";
    byte[] syntheticBundleBytes = createSyntheticBundle(bundleContext.getBundle(), bundlePath, testBundleName,
            extensionsToInclude);

    Bundle syntheticBundle = bundleContext.installBundle(testBundleName,
            new ByteArrayInputStream(syntheticBundleBytes));
    return syntheticBundle;
}
 
Example 13
Source File: SyntheticBundleInstaller.java    From smarthome with Eclipse Public License 2.0 3 votes vote down vote up
/**
 * Install synthetic bundle, denoted by its name, into the test runtime (by using the given bundle context).
 *
 * @param bundleContext the bundle context of the test runtime
 * @param testBundleNamethe symbolic name of the sub-directory of {@value #BUNDLE_POOL_PATH}, which contains the
 *            files
 *            for the synthetic bundle
 * @param extensionsToInclude a list of extension to be included into the synthetic bundle. In order to use the list
 *            of default extensions ({@link #DEFAULT_EXTENSIONS})
 *
 * @return the synthetic bundle representation
 * @throws Exception thrown when error occurs while installing or starting the synthetic bundle
 */
public static Bundle install(BundleContext bundleContext, String testBundleName, Set<String> extensionsToInclude)
        throws Exception {
    String bundlePath = BUNDLE_POOL_PATH + "/" + testBundleName + "/";
    byte[] syntheticBundleBytes = createSyntheticBundle(bundleContext.getBundle(), bundlePath, testBundleName,
            extensionsToInclude);

    Bundle syntheticBundle = bundleContext.installBundle(testBundleName,
            new ByteArrayInputStream(syntheticBundleBytes));
    syntheticBundle.start(Bundle.ACTIVE);
    waitUntilLoadingFinished(bundleContext, syntheticBundle);
    return syntheticBundle;
}
 
Example 14
Source File: SyntheticBundleInstaller.java    From smarthome with Eclipse Public License 2.0 3 votes vote down vote up
/**
 * Install synthetic bundle fragment, denoted by its name, into the test runtime (by using the given bundle
 * context). Only the default extensions set ({@link #DEFAULT_EXTENSIONS}) will be included into the synthetic
 * bundle fragment.
 *
 * @param bundleContext the bundle context of the test runtime
 * @param testBundleName the name of the sub-directory of {@value #BUNDLE_POOL_PATH}, which contains the files for
 *            the
 *            synthetic bundle
 * @return the synthetic bundle representation
 * @throws Exception thrown when error occurs while installing or starting the synthetic bundle fragment
 */
public static Bundle installFragment(BundleContext bundleContext, String testBundleName,
        Set<String> extensionsToInclude) throws Exception {
    String bundlePath = BUNDLE_POOL_PATH + "/" + testBundleName + "/";
    byte[] syntheticBundleBytes = createSyntheticBundle(bundleContext.getBundle(), bundlePath, testBundleName,
            extensionsToInclude);

    Bundle syntheticBundle = bundleContext.installBundle(testBundleName,
            new ByteArrayInputStream(syntheticBundleBytes));
    return syntheticBundle;
}
 
Example 15
Source File: BundleUtils.java    From eclipse.jdt.ls with Eclipse Public License 2.0 2 votes vote down vote up
/**
 * Install the bundle within the given location.
 *
 * @param context
 *            Bundle context
 * @param bundlesToStart
 *            The set containing bundles which need to start
 * @param location
 *            The location of the bundle which needs to be installed
 * @throws BundleException
 */
private static void installBundle(BundleContext context, Set<Bundle> bundlesToStart, String location) throws BundleException {
	Bundle newlyInstalledBundle = context.installBundle(location);
	JavaLanguageServerPlugin.logInfo("Installed " + newlyInstalledBundle.getLocation());
	bundlesToStart.add(newlyInstalledBundle);
}