示例#1
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;
  }
示例#2
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);
    }
  }
示例#3
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);
    }
  }
示例#4
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()));
  }
示例#5
0
  private Capability createEeCapability(EE ee) {
    Map<String, String> props = new HashMap<String, String>();
    props.put(ObrConstants.FILTER_EE, ee.getEEName());

    return helper.capability(ObrConstants.REQUIREMENT_EE, props);
  }