private static Set doImportPackageAttributes(
     RequirementImpl requirement, StringBuffer filter, Attribute[] attributes) {
   HashSet set = new HashSet();
   for (int i = 0; attributes != null && i < attributes.length; i++) {
     String name = attributes[i].getName();
     String value = attributes[i].getValue();
     if (name.equalsIgnoreCase(Constants.PACKAGE_SPECIFICATION_VERSION)
         || name.equalsIgnoreCase(Constants.VERSION_ATTRIBUTE)) {
       continue;
     } else if (name.equalsIgnoreCase(Constants.RESOLUTION_DIRECTIVE + ":")) {
       requirement.setOptional(Constants.RESOLUTION_OPTIONAL.equalsIgnoreCase(value));
     }
     if (name.endsWith(":")) {
       // Ignore
     } else {
       filter.append("(");
       filter.append(name);
       filter.append("=");
       filter.append(value);
       filter.append(")");
       set.add(name);
     }
   }
   return set;
 }
  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);
    }
  }
  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 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 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 #6
0
  /**
   * Parse an old style OBR repository.
   *
   * <p><dtd-version>1.0</dtd-version> <repository> <name>Oscar Bundle Repository</name>
   * <url>http://oscar-osgi.sourceforge.net/</url> <date>Fri May 07 16:45:07 CEST 2004</date>
   * <extern-repositories>
   * <!--
   * Stefano Lenzi ([email protected]) -->
   * <url>http://domoware.isti.cnr.it/osgi-obr/niche-osgi-obr.xml</url>
   * <!--Manuel Palencia ([email protected]) -->
   * <!--
   * <url>http://jmood.forge.os4os.org/repository.xml</url> -->
   * <!-- Enrique
   * Rodriguez ([email protected]) -->
   * <url>http://update.cainenable.org/repository.xml</url> </extern-repositories> </repository>
   * <bundle> <bundle-name>Bundle Repository</bundle-name> <bundle-description> A bundle repository
   * service for Oscar. </bundle-description> <bundle-updatelocation>
   * http://oscar-osgi.sf.net/repo/bundlerepository/bundlerepository.jar </bundle-updatelocation>
   * <bundle-sourceurl> http://oscar-osgi.sf.net/repo/bundlerepository/bundlerepository-src.jar
   * </bundle-sourceurl> <bundle-version>1.1.3</bundle-version> <bundle-docurl>
   * http://oscar-osgi.sf.net/repo/bundlerepository/ </bundle-docurl>
   * <bundle-category>General</bundle-category> <import-package package="org.osgi.framework"/>
   * <export-package package="org.ungoverned.osgi.service.bundlerepository"
   * specification-version="1.1.0"/> </bundle> *
   */
  private void parseOscar(XmlPullParser parser) throws Exception {
    parser.require(XmlPullParser.START_TAG, null, "bundles");
    while (true) {
      int event = parser.next();

      // Error ..
      if (event == XmlPullParser.TEXT) event = parser.next();

      if (event != XmlPullParser.START_TAG) break;

      ResourceImpl resource = new ResourceImpl(this);

      if (parser.getName().equals("bundle")) {
        while (parser.nextTag() == XmlPullParser.START_TAG) {
          String key = parser.getName();
          if (key.equals("import-package")) {
            RequirementImpl requirement = new RequirementImpl("package");

            requirement.setOptional(false);
            requirement.setMultiple(false);

            String p = parser.getAttributeValue(null, "package");
            StringBuffer sb = new StringBuffer();
            sb.append("(&(package=");
            sb.append(p);
            sb.append(")");
            String version = parser.getAttributeValue(null, "specification-version");
            VersionRange v = new VersionRange("0");
            if (version != null) {
              sb.append("(version=");
              sb.append(v = new VersionRange(version));
              sb.append(")");
            }
            sb.append(")");
            requirement.setFilter(sb.toString());
            requirement.setComment("Import-Package: " + p + ";" + v);
            resource.addRequirement(requirement);

            parser.nextTag();
          } else if (key.equals("export-package")) {
            CapabilityImpl capability = new CapabilityImpl("package");
            capability.addProperty("package", parser.getAttributeValue(null, "package"));
            String version = parser.getAttributeValue(null, "specification-version");
            if (version != null) {
              capability.addProperty("version", new VersionRange(version));
            }
            resource.addCapability(capability);
            parser.nextTag();
          } else {
            String value = parser.nextText().trim();
            if (key.equals("bundle-sourceurl")) resource.setSource(new URL(value));
            else if (key.equals("bundle-docurl")) resource.setDocumentation(new URL(value));
            else if (key.equals("bundle-updatelocation")) resource.setURL(new URL(value));
            else if (key.equals("bundle-description")) resource.setDescription(value);
            else if (key.equals("bundle-category")) resource.addCategory(value);
            else if (key.equals("bundle-name")) {
              resource.setName(value);
              resource.setPresentationName(value);
            } else if (key.equals("bundle-version")) resource.setVersion(new VersionRange(value));
            else {
              resource.put(key, value);
            }
          }
        }
        resources.add(resource);
        parser.require(XmlPullParser.END_TAG, null, "bundle");
      } else if (parser.getName().equals("repository")) {
        parser.require(XmlPullParser.START_TAG, null, "repository");
        while (parser.nextTag() == XmlPullParser.START_TAG) {
          String tag = parser.getName();
          if (tag.equals("name")) {
            String name = parser.nextText();
            if (this.name == null) this.name = name.trim();
          } else if (tag.equals("url")) parser.nextText().trim();
          else if (tag.equals("date")) parser.nextText().trim();
          else if (tag.equals("extern-repositories")) {
            parser.require(XmlPullParser.START_TAG, null, "extern-repositories");
            while (parser.nextTag() == XmlPullParser.START_TAG) {
              if (parser.getName().equals("url")) parseDocument(new URL(parser.nextText().trim()));
              else
                throw new IllegalArgumentException(
                    "Invalid tag in repository while parsing extern repositories: "
                        + url
                        + " "
                        + parser.getName());
            }
            parser.require(XmlPullParser.END_TAG, null, "extern-repositories");
          } else
            throw new IllegalArgumentException(
                "Invalid tag in repository: " + url + " " + parser.getName());
        }
        parser.require(XmlPullParser.END_TAG, null, "repository");
      } else if (parser.getName().equals("dtd-version")) {
        parser.nextText();
      } else
        throw new IllegalArgumentException(
            "Invalid tag in repository: " + url + " " + parser.getName());
    }
    parser.require(XmlPullParser.END_TAG, null, "bundles");
  }