private static void doExports(ResourceImpl resource, Headers headers) {
   Clause[] clauses = Parser.parseHeader(headers.getHeader(Constants.EXPORT_PACKAGE));
   for (int i = 0; clauses != null && i < clauses.length; i++) {
     CapabilityImpl capability = createCapability(Capability.PACKAGE, clauses[i]);
     resource.addCapability(capability);
   }
 }
Exemple #2
0
 protected void findBundlesWithFramentsToRefresh(Set<Bundle> toRefresh) {
   for (Bundle b : toRefresh) {
     if (b.getState() != Bundle.UNINSTALLED) {
       String hostHeader = (String) b.getHeaders().get(Constants.FRAGMENT_HOST);
       if (hostHeader != null) {
         Clause[] clauses = Parser.parseHeader(hostHeader);
         if (clauses != null && clauses.length > 0) {
           Clause path = clauses[0];
           for (Bundle hostBundle : bundleContext.getBundles()) {
             if (hostBundle.getSymbolicName().equals(path.getName())) {
               String ver = path.getAttribute(Constants.BUNDLE_VERSION_ATTRIBUTE);
               if (ver != null) {
                 VersionRange v = VersionRange.parseVersionRange(ver);
                 if (v.contains(hostBundle.getVersion())) {
                   toRefresh.add(hostBundle);
                 }
               } else {
                 toRefresh.add(hostBundle);
               }
             }
           }
         }
       }
     }
   }
 }
  private static void doFragment(ResourceImpl resource, Headers headers) {
    // Check if we are a fragment
    Clause[] clauses = Parser.parseHeader(headers.getHeader(Constants.FRAGMENT_HOST));
    if (clauses != null && clauses.length == 1) {
      // We are a fragment, create a requirement
      // to our host.
      RequirementImpl r = new RequirementImpl(Capability.BUNDLE);
      StringBuffer sb = new StringBuffer();
      sb.append("(&(symbolicname=");
      sb.append(clauses[0].getName());
      sb.append(")");
      appendVersion(
          sb,
          VersionRange.parseVersionRange(
              clauses[0].getAttribute(Constants.BUNDLE_VERSION_ATTRIBUTE)));
      sb.append(")");
      r.setFilter(sb.toString());
      r.addText("Required Host " + clauses[0].getName());
      r.setExtend(true);
      r.setOptional(false);
      r.setMultiple(false);
      resource.addRequire(r);

      // And insert a capability that we are available
      // as a fragment. ### Do we need that with extend?
      CapabilityImpl capability = new CapabilityImpl(Capability.FRAGMENT);
      capability.addProperty("host", clauses[0].getName());
      capability.addProperty("version", Property.VERSION, getVersion(clauses[0]));
      resource.addCapability(capability);
    }
  }
Exemple #4
0
 protected List<Clause> getOptionalImports(String importsStr) {
   Clause[] imports = Parser.parseHeader(importsStr);
   List<Clause> result = new LinkedList<Clause>();
   for (int i = 0; i < imports.length; i++) {
     String resolution = imports[i].getDirective(Constants.RESOLUTION_DIRECTIVE);
     if (Constants.RESOLUTION_OPTIONAL.equals(resolution)) {
       result.add(imports[i]);
     }
   }
   return result;
 }
 private static String getSymbolicName(Headers headers) {
   String bsn = headers.getHeader(Constants.BUNDLE_SYMBOLICNAME);
   if (bsn == null) {
     bsn = headers.getHeader(Constants.BUNDLE_NAME);
     if (bsn == null) {
       bsn = "Untitled-" + headers.hashCode();
     }
   }
   Clause[] clauses = Parser.parseHeader(bsn);
   return clauses[0].getName();
 }
  private static void doImportExportServices(ResourceImpl resource, Headers headers) {
    Clause[] imports = Parser.parseHeader(headers.getHeader(Constants.IMPORT_SERVICE));
    for (int i = 0; imports != null && i < imports.length; i++) {
      RequirementImpl ri = new RequirementImpl(Capability.SERVICE);
      ri.setFilter(createServiceFilter(imports[i]));
      ri.addText("Import Service " + imports[i].getName());

      String avail = imports[i].getDirective("availability");
      String mult = imports[i].getDirective("multiple");
      ri.setOptional("optional".equalsIgnoreCase(avail));
      ri.setMultiple(!"false".equalsIgnoreCase(mult));
      resource.addRequire(ri);
    }

    Clause[] exports = Parser.parseHeader(headers.getHeader(Constants.EXPORT_SERVICE));
    for (int i = 0; exports != null && i < exports.length; i++) {
      CapabilityImpl cap = createServiceCapability(exports[i]);
      resource.addCapability(cap);
    }
  }
  private static void doImports(ResourceImpl resource, Headers headers) {
    Clause[] clauses = Parser.parseHeader(headers.getHeader(Constants.IMPORT_PACKAGE));
    for (int i = 0; clauses != null && i < clauses.length; i++) {
      RequirementImpl requirement = new RequirementImpl(Capability.PACKAGE);

      createImportFilter(requirement, Capability.PACKAGE, clauses[i]);
      requirement.addText("Import package " + clauses[i]);
      requirement.setOptional(
          Constants.RESOLUTION_OPTIONAL.equalsIgnoreCase(
              clauses[i].getDirective(Constants.RESOLUTION_DIRECTIVE)));
      resource.addRequire(requirement);
    }
  }
 private static void doExecutionEnvironment(ResourceImpl resource, Headers headers) {
   Clause[] clauses =
       Parser.parseHeader(headers.getHeader(Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT));
   if (clauses != null && clauses.length > 0) {
     StringBuffer sb = new StringBuffer();
     sb.append("(|");
     for (int i = 0; i < clauses.length; i++) {
       sb.append("(");
       sb.append(Capability.EXECUTIONENVIRONMENT);
       sb.append("=");
       sb.append(clauses[i].getName());
       sb.append(")");
     }
     sb.append(")");
     RequirementImpl req = new RequirementImpl(Capability.EXECUTIONENVIRONMENT);
     req.setFilter(sb.toString());
     req.addText("Execution Environment " + sb.toString());
     resource.addRequire(req);
   }
 }
  private static void doRequires(ResourceImpl resource, Headers headers) {
    Clause[] clauses = Parser.parseHeader(headers.getHeader(Constants.REQUIRE_BUNDLE));
    for (int i = 0; clauses != null && i < clauses.length; i++) {
      RequirementImpl r = new RequirementImpl(Capability.BUNDLE);

      VersionRange v =
          VersionRange.parseVersionRange(
              clauses[i].getAttribute(Constants.BUNDLE_VERSION_ATTRIBUTE));

      StringBuffer sb = new StringBuffer();
      sb.append("(&(symbolicname=");
      sb.append(clauses[i].getName());
      sb.append(")");
      appendVersion(sb, v);
      sb.append(")");
      r.setFilter(sb.toString());

      r.addText("Require Bundle " + clauses[i].getName() + "; " + v);
      r.setOptional(
          Constants.RESOLUTION_OPTIONAL.equalsIgnoreCase(
              clauses[i].getDirective(Constants.RESOLUTION_DIRECTIVE)));
      resource.addRequire(r);
    }
  }
Exemple #10
0
 protected void formatHeader(
     String header, ClauseFormatter formatter, StringBuilder builder, int indent) {
   Clause[] clauses = Parser.parseHeader(header);
   formatClauses(clauses, formatter, builder, indent);
 }
Exemple #11
0
 protected void findBundlesWithOptionalPackagesToRefresh(Set<Bundle> toRefresh) {
   // First pass: include all bundles contained in these features
   Set<Bundle> bundles = new HashSet<Bundle>(Arrays.asList(bundleContext.getBundles()));
   bundles.removeAll(toRefresh);
   if (bundles.isEmpty()) {
     return;
   }
   // Second pass: for each bundle, check if there is any unresolved optional package that could be
   // resolved
   Map<Bundle, List<Clause>> imports = new HashMap<Bundle, List<Clause>>();
   for (Iterator<Bundle> it = bundles.iterator(); it.hasNext(); ) {
     Bundle b = it.next();
     String importsStr = (String) b.getHeaders().get(Constants.IMPORT_PACKAGE);
     List<Clause> importsList = getOptionalImports(importsStr);
     if (importsList.isEmpty()) {
       it.remove();
     } else {
       imports.put(b, importsList);
     }
   }
   if (bundles.isEmpty()) {
     return;
   }
   // Third pass: compute a list of packages that are exported by our bundles and see if
   //             some exported packages can be wired to the optional imports
   List<Clause> exports = new ArrayList<Clause>();
   for (Bundle b : toRefresh) {
     if (b.getState() != Bundle.UNINSTALLED) {
       String exportsStr = (String) b.getHeaders().get(Constants.EXPORT_PACKAGE);
       if (exportsStr != null) {
         Clause[] exportsList = Parser.parseHeader(exportsStr);
         exports.addAll(Arrays.asList(exportsList));
       }
     }
   }
   for (Iterator<Bundle> it = bundles.iterator(); it.hasNext(); ) {
     Bundle b = it.next();
     List<Clause> importsList = imports.get(b);
     for (Iterator<Clause> itpi = importsList.iterator(); itpi.hasNext(); ) {
       Clause pi = itpi.next();
       boolean matching = false;
       for (Clause pe : exports) {
         if (pi.getName().equals(pe.getName())) {
           String evStr = pe.getAttribute(Constants.VERSION_ATTRIBUTE);
           String ivStr = pi.getAttribute(Constants.VERSION_ATTRIBUTE);
           Version exported = evStr != null ? Version.parseVersion(evStr) : Version.emptyVersion;
           VersionRange imported =
               ivStr != null ? VersionRange.parseVersionRange(ivStr) : VersionRange.ANY_VERSION;
           if (imported.contains(exported)) {
             matching = true;
             break;
           }
         }
       }
       if (!matching) {
         itpi.remove();
       }
     }
     if (importsList.isEmpty()) {
       it.remove();
     }
   }
   toRefresh.addAll(bundles);
 }
 private static void doCategories(ResourceImpl resource, Headers headers) {
   Clause[] clauses = Parser.parseHeader(headers.getHeader(Constants.BUNDLE_CATEGORY));
   for (int i = 0; clauses != null && i < clauses.length; i++) {
     resource.addCategory(clauses[i].getName());
   }
 }