Exemple #1
0
  public void testResolveReq() throws Exception {
    RepositoryAdminImpl repoAdmin = createRepositoryAdmin();
    repoAdmin.addRepository(getClass().getResource("/repo_for_resolvertest.xml"));

    Resolver resolver = repoAdmin.resolver();
    resolver.add(
        repoAdmin.getHelper().requirement("package", "(package=org.apache.felix.test.osgi)"));
    assertTrue(resolver.resolve());
  }
Exemple #2
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);
  }
Exemple #3
0
  private Set<Resource> addProjectBuildBundles(Resolver resolver) {
    if (!Project.BNDFILE.equals(runFile.getName())) return Collections.emptySet();

    Set<Resource> result = new HashSet<Resource>();
    try {

      Project model = Workspace.getProject(runFile.getProject().getLocation().toFile());
      for (Builder builder : model.getSubBuilders()) {
        File file = new File(model.getTarget(), builder.getBsn() + ".jar");
        if (file.isFile()) {
          JarInputStream stream = null;
          try {
            stream = new JarInputStream(new FileInputStream(file));
            Manifest manifest = stream.getManifest();

            Resource resource = helper.createResource(manifest.getMainAttributes());
            result.add(resource);
            resolver.add(resource);
          } catch (IOException e) {
            Plugin.logError("Error reading project bundle " + file, e);
          } finally {
            if (stream != null) stream.close();
          }
        }
      }
    } catch (Exception e) {
      Plugin.logError("Error getting builders for project: " + runFile.getProject(), e);
    }
    return result;
  }
Exemple #4
0
  private void addJREPackageCapabilities(Resolver resolver, EE ee) throws IOException {
    // EE Package Capabilities
    Properties pkgProps = new Properties();
    URL pkgsResource = ResolveOperation.class.getResource(ee.name() + ".properties");
    if (pkgsResource == null)
      throw new IOException(
          String.format(
              "No JRE package definition available for Execution Env %s.", ee.getEEName()));

    InputStream stream = null;
    try {
      stream = pkgsResource.openStream();
      pkgProps.load(stream);
    } finally {
      if (stream != null) IO.close(stream);
    }
    String pkgsStr = pkgProps.getProperty(Constants.FRAMEWORK_SYSTEMPACKAGES);

    Map<String, Map<String, String>> header = OSGiHeader.parseHeader(pkgsStr);
    for (Entry<String, Map<String, String>> entry : header.entrySet()) {
      String pkgName = Processor.removeDuplicateMarker(entry.getKey());
      String version = entry.getValue().get(Constants.VERSION_ATTRIBUTE);

      Map<String, String> capabilityProps = new HashMap<String, String>();
      capabilityProps.put(ObrConstants.FILTER_PACKAGE, pkgName);
      if (version != null) capabilityProps.put(ObrConstants.FILTER_VERSION, version);

      Capability capability = helper.capability(ObrConstants.REQUIREMENT_PACKAGE, capabilityProps);
      resolver.addGlobalCapability(capability);
    }
  }
Exemple #5
0
  public void testResolveInterrupt() throws Exception {
    RepositoryAdminImpl repoAdmin = createRepositoryAdmin();
    repoAdmin.addRepository(getClass().getResource("/repo_for_resolvertest.xml"));

    Resolver resolver = repoAdmin.resolver();
    resolver.add(
        repoAdmin.getHelper().requirement("package", "(package=org.apache.felix.test.osgi)"));

    Thread.currentThread().interrupt();
    try {
      resolver.resolve();
      fail("An excepiton should have been thrown");
    } catch (org.apache.felix.bundlerepository.InterruptedResolutionException e) {
      // ok
    }
  }
Exemple #6
0
  public void testReferral1() throws Exception {

    URL url = getClass().getResource("/repo_for_resolvertest.xml");
    RepositoryAdminImpl repoAdmin = createRepositoryAdmin();
    RepositoryImpl repo = (RepositoryImpl) repoAdmin.addRepository(url);

    Resolver resolver = repoAdmin.resolver();

    Resource[] discoverResources =
        repoAdmin.discoverResources("(symbolicname=org.apache.felix.test*)");
    assertNotNull(discoverResources);
    assertEquals(1, discoverResources.length);

    resolver.add(discoverResources[0]);
    assertTrue(resolver.resolve());
  }
Exemple #7
0
  private void addSystemPackagesExtraCapabilities(
      Resolver resolver, Collection<? extends ExportedPackage> systemPackages) {
    for (ExportedPackage clause : systemPackages) {
      String pkgName = clause.getName();
      String version = clause.getVersionString();

      Map<String, String> capabilityProps = new HashMap<String, String>();
      capabilityProps.put(ObrConstants.FILTER_PACKAGE, pkgName);
      if (version != null) capabilityProps.put(ObrConstants.FILTER_VERSION, version);
      Capability capability = helper.capability(ObrConstants.REQUIREMENT_PACKAGE, capabilityProps);
      resolver.addGlobalCapability(capability);
    }
  }
  public Reason[] getReason(Resource resource) {
    if (resolver == null) return new Reason[0];

    Reason[] reasons = resolver.getReason(resource);

    if (reasons == null) reasons = new Reason[0];

    // De-dupe. The resolver returns two copies of everything...?!
    Set<Reason> set =
        new TreeSet<Reason>(
            new ReasonComparator(new ResourceComparator(), new RequirementComparator()));
    for (Reason reason : reasons) {
      set.add(reason);
    }
    return set.toArray(new Reason[set.size()]);
  }
Exemple #9
0
  public void testMandatoryPackages() throws Exception {
    RepositoryAdminImpl repoAdmin = createRepositoryAdmin();
    repoAdmin.addRepository(getClass().getResource("/repo_for_mandatory.xml"));

    Resolver resolver = repoAdmin.resolver();
    resolver.add(repoAdmin.getHelper().requirement("bundle", "(symbolicname=res2)"));
    assertFalse(resolver.resolve());

    resolver = repoAdmin.resolver();
    resolver.add(repoAdmin.getHelper().requirement("bundle", "(symbolicname=res3)"));
    assertTrue(resolver.resolve());

    resolver = repoAdmin.resolver();
    resolver.add(repoAdmin.getHelper().requirement("bundle", "(symbolicname=res4)"));
    assertFalse(resolver.resolve());
  }
Exemple #10
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()));
  }