Esempio n. 1
0
  /**
   * @param systemContext
   * @throws MalformedURLException
   * @throws FileNotFoundException
   * @throws IOException
   */
  private void update() throws Exception {
    trace("Updating framework with %s", parms.runbundles);

    // Turn the bundle location paths into files
    List<File> desired = new ArrayList<File>();
    for (Object o : parms.runbundles) {
      File file = new File((String) o).getAbsoluteFile();
      if (!file.exists()) error("Bundle files does not exist: " + file);
      else desired.add(file);
    }

    // deleted = old - new
    List<File> tobedeleted = new ArrayList<File>(installedBundles.keySet());
    tobedeleted.removeAll(desired);

    // updated = old /\ new
    List<File> tobeupdated = new ArrayList<File>(installedBundles.keySet());
    tobeupdated.retainAll(desired);

    // install = new - old
    List<File> tobeinstalled = new ArrayList<File>(desired);
    tobeinstalled.removeAll(installedBundles.keySet());

    List<Bundle> tobestarted = new ArrayList<Bundle>();

    for (File f : tobedeleted)
      try {
        trace("uninstalling %s", f);
        installedBundles.get(f).uninstall();
        installedBundles.remove(f);
      } catch (Exception e) {
        error("Failed to uninstall bundle %s, exception %s", f, e);
      }

    for (File f : tobeinstalled)
      try {
        trace("installing %s", f);
        Bundle b = install(f);
        installedBundles.put(f, b);
        tobestarted.add(b);
      } catch (Exception e) {
        error("Failed to uninstall bundle %s, exception %s", f, e);
      }

    for (File f : tobeupdated)
      try {
        Bundle b = installedBundles.get(f);
        if (b.getLastModified() < f.lastModified()) {
          trace("updating %s", f);
          if (b.getState() == Bundle.ACTIVE) {
            tobestarted.add(b);
            b.stop();
          }
          b.update();
        } else trace("bundle is still current according to timestamp %s", f);
      } catch (Exception e) {
        error("Failed to update bundle %s, exception %s", f, e);
      }

    if (padmin != null) padmin.refreshPackages(null);

    trace("bundles administered %s", installedBundles.keySet());

    // From now on, the bundles are on their own. They have
    // by default AllPermission, but if they install bundles
    // they will not automatically get AllPermission anymore

    // Get the resolved status
    if (padmin != null && padmin.resolveBundles(null) == false) {
      error("could not resolve the bundles");
      // 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);
    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) {
        error("Failed to start bundle %s-%s, exception %s", b.getSymbolicName(), b.getVersion(), e);
      }
    }
  }
Esempio n. 2
0
  /**
   * Install all bundles contained in the specified crates from the given crate location.
   *
   * @param context The bundle context to use for interacting with OSGi.
   * @param location The location containing the crates.
   * @param crates The set of crates to activate.
   * @return an ordered list of bundles whose 'start' flag is set to true in their containing crate
   * @throws CrateException on read error.
   */
  public static List<Bundle> installBundles(
      BundleContext context, InstallLocation location, Crate... crates) throws CrateException {
    Logger logger = Logger.getLogger(CrateStarterElf.class);

    List<Crate> toLoad = expandDependencies(location, crates);

    Map<String, Bundle> installed = new HashMap<String, Bundle>();
    for (Bundle bundle : context.getBundles()) {
      installed.put(bundle.getSymbolicName(), bundle);
    }

    // prevent osgi from trying to be installed.
    installed.put("org.eclipse.osgi", null);

    ServiceReference reference = context.getServiceReference(StartLevel.class.getName());
    StartLevel start = (StartLevel) context.getService(reference);

    List<Bundle> bundles = new ArrayList<Bundle>();
    List<Bundle> toStart = new LinkedList<Bundle>();

    for (Crate crate : toLoad) {
      for (CrateBundle bundle : crate.getBundles()) {
        if (installed.containsKey(bundle.getId())) {
          if (bundle.isStart()) {
            Bundle alreadyInstalledBundle = installed.get(bundle.getId());
            if (alreadyInstalledBundle != null) {
              int state = alreadyInstalledBundle.getState();
              if (state != Bundle.ACTIVE && state != Bundle.STARTING) {
                toStart.add(alreadyInstalledBundle);
              }
            }
          }

          continue;
        }

        installed.put(bundle.getId(), null);

        // exclude fragments not applicable for the platform.
        if (!location.isApplicable(bundle)) {
          continue;
        }

        File file = getFileForBundle(location.getRoot(), bundle);

        try {
          Bundle b = context.installBundle("reference:file:" + file.toString());
          bundles.add(b);

          if (bundle.getStartLevel() > 0) {
            start.setBundleStartLevel(b, bundle.getStartLevel());
          }

          if (bundle.isStart()) {
            toStart.add(b);
          }

          logger.debug("Installed bundle: " + bundle.getId());
        } catch (BundleException e) {
          logger.warn("Error installing bundle: " + bundle.getId(), e);
        }
      }
    }
    context.ungetService(reference);

    reference = context.getServiceReference(PackageAdmin.class.getName());

    PackageAdmin packageAdmin = (PackageAdmin) context.getService(reference);
    packageAdmin.resolveBundles(bundles.toArray(new Bundle[bundles.size()]));

    context.ungetService(reference);

    return toStart;
  }