Example #1
0
  public void testOptionalResolution() throws Exception {
    RepositoryAdminImpl repoAdmin = createRepositoryAdmin();
    repoAdmin.addRepository(getClass().getResource("/repo_for_optional_resources.xml"));

    Resolver resolver = repoAdmin.resolver();
    resolver.add(repoAdmin.getHelper().requirement("bundle", "(symbolicname=res1)"));

    assertTrue(resolver.resolve());
    assertEquals(1, resolver.getRequiredResources().length);
    assertEquals(2, resolver.getOptionalResources().length);
  }
Example #2
0
  public void run(IProgressMonitor monitor) {
    SubMonitor progress = SubMonitor.convert(monitor, "Resolving...", 0);

    MultiStatus status =
        new MultiStatus(Plugin.PLUGIN_ID, 0, "Problems during OBR resolution", null);

    // Get the repositories
    List<OBRIndexProvider> indexProviders;
    try {
      indexProviders = loadIndexProviders();
    } catch (Exception e) {
      status.add(new Status(IStatus.ERROR, Plugin.PLUGIN_ID, 0, "Error loading OBR indexes.", e));
      result = createErrorResult(status);
      return;
    }

    // Create the dummy system bundle and repository admin
    File frameworkFile = findFramework(status);
    if (frameworkFile == null) {
      result = createErrorResult(status);
      return;
    }
    DummyBundleContext bundleContext;
    try {
      bundleContext = new DummyBundleContext(frameworkFile);
    } catch (IOException e) {
      status.add(
          new Status(
              IStatus.ERROR, Plugin.PLUGIN_ID, 0, "Error reading system bundle manifest.", e));
      result = createErrorResult(status);
      return;
    }

    // Load repository indexes
    List<Repository> repos = new LinkedList<Repository>();
    for (OBRIndexProvider prov : indexProviders) {
      String repoName;
      if (prov instanceof RepositoryPlugin) {
        RepositoryPlugin repo = (RepositoryPlugin) prov;
        repoName = repo.getName();
      } else {
        repoName = prov.toString();
      }

      File cacheDir;
      if (prov instanceof RemoteRepositoryPlugin) {
        cacheDir = ((RemoteRepositoryPlugin) prov).getCacheDirectory();
      } else {
        cacheDir = Plugin.getDefault().getStateLocation().toFile();
      }

      try {
        for (URL indexUrl : prov.getOBRIndexes()) {
          addRepository(indexUrl, repos, cacheDir);
        }
      } catch (Exception e) {
        status.add(
            new Status(
                IStatus.ERROR,
                Plugin.PLUGIN_ID,
                0,
                "Error processing index for repository " + repoName,
                e));
      }
    }

    RepositoryAdminImpl repoAdmin =
        new RepositoryAdminImpl(bundleContext, new Logger(Plugin.getDefault().getBundleContext()));

    repos.add(
        0,
        repoAdmin
            .getLocalRepository()); // BUG? Calling `resolver(Repository[])` excludes the local and
                                    // system repos!
    repos.add(0, repoAdmin.getSystemRepository());
    Resolver resolver = repoAdmin.resolver(repos.toArray(new Repository[repos.size()]));

    // Add project builders
    Set<Resource> projectBuildResources = addProjectBuildBundles(resolver);

    // Add EE capabilities
    EE ee = model.getEE();
    if (ee == null) ee = EE.J2SE_1_5; // TODO: read default from the workbench
    resolver.addGlobalCapability(createEeCapability(ee));
    for (EE compat : ee.getCompatible()) {
      resolver.addGlobalCapability(createEeCapability(compat));
    }

    // Add JRE package capabilities
    try {
      addJREPackageCapabilities(resolver, ee);
    } catch (IOException e) {
      status.add(
          new Status(
              IStatus.ERROR, Plugin.PLUGIN_ID, 0, "Error adding JRE package capabilities", e));
      result = createErrorResult(status);
      return;
    }

    // HACK: add capabilities for usual framework services (not all frameworks declare these
    // statically)
    String[] frameworkServices =
        new String[] {
          "org.osgi.service.packageadmin.PackageAdmin",
          "org.osgi.service.startlevel.StartLevel",
          "org.osgi.service.permissionadmin.PermissionAdmin"
        };
    for (String frameworkService : frameworkServices) {
      Map<String, String> props = new HashMap<String, String>();
      props.put(ObrConstants.FILTER_SERVICE, frameworkService);
      resolver.addGlobalCapability(helper.capability(ObrConstants.REQUIREMENT_SERVICE, props));
    }

    // Add system packages-extra capabilities (from -runsystempackages)
    List<ExportedPackage> systemPackages = model.getSystemPackages();
    if (systemPackages != null) addSystemPackagesExtraCapabilities(resolver, systemPackages);

    // Add requirements
    List<bndtools.api.Requirement> requirements = model.getRunRequire();
    if (requirements != null)
      for (bndtools.api.Requirement req : requirements) {
        resolver.add(helper.requirement(req.getName(), req.getFilter()));
      }

    boolean resolved = resolver.resolve();

    result =
        new ObrResolutionResult(
            resolved,
            Status.OK_STATUS,
            filterGlobalResource(resolver.getRequiredResources()),
            filterGlobalResource(resolver.getOptionalResources()),
            Arrays.asList(resolver.getUnsatisfiedRequirements()));
  }