static void populate(Headers headers, ResourceImpl resource) {
    String bsn = getSymbolicName(headers);
    String v = getVersion(headers);

    resource.put(Resource.ID, bsn + "/" + v);
    resource.put(Resource.SYMBOLIC_NAME, bsn);
    resource.put(Resource.VERSION, v);
    if (headers.getHeader(Constants.BUNDLE_NAME) != null) {
      resource.put(Resource.PRESENTATION_NAME, headers.getHeader(Constants.BUNDLE_NAME));
    }
    if (headers.getHeader(Constants.BUNDLE_DESCRIPTION) != null) {
      resource.put(Resource.DESCRIPTION, headers.getHeader(Constants.BUNDLE_DESCRIPTION));
    }
    if (headers.getHeader(BUNDLE_LICENSE) != null) {
      resource.put(Resource.LICENSE_URI, headers.getHeader(BUNDLE_LICENSE));
    }
    if (headers.getHeader(Constants.BUNDLE_COPYRIGHT) != null) {
      resource.put(Resource.COPYRIGHT, headers.getHeader(Constants.BUNDLE_COPYRIGHT));
    }
    if (headers.getHeader(Constants.BUNDLE_DOCURL) != null) {
      resource.put(Resource.DOCUMENTATION_URI, headers.getHeader(Constants.BUNDLE_DOCURL));
    }
    if (headers.getHeader(BUNDLE_SOURCE) != null) {
      resource.put(Resource.SOURCE_URI, headers.getHeader(BUNDLE_SOURCE));
    }

    doCategories(resource, headers);
    doBundle(resource, headers);
    doImportExportServices(resource, headers);
    doFragment(resource, headers);
    doRequires(resource, headers);
    doExports(resource, headers);
    doImports(resource, headers);
    doExecutionEnvironment(resource, headers);
  }
Beispiel #2
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");
  }
  public Resource createResource(final URL bundleUrl) throws IOException {
    ResourceImpl resource =
        createResource(
            new Headers() {
              private final Manifest manifest;
              private Properties localization;

              {
                // Do not use a JarInputStream so that we can read the manifest even if it's not
                // the first entry in the JAR.
                byte[] man = loadEntry(JarFile.MANIFEST_NAME);
                if (man == null) {
                  throw new IllegalArgumentException(
                      "The specified url is not a valid jar (can't read manifest): " + bundleUrl);
                }
                manifest = new Manifest(new ByteArrayInputStream(man));
              }

              public String getHeader(String name) {
                String value = manifest.getMainAttributes().getValue(name);
                if (value != null && value.startsWith("%")) {
                  if (localization == null) {
                    try {
                      localization = new Properties();
                      String path =
                          manifest.getMainAttributes().getValue(Constants.BUNDLE_LOCALIZATION);
                      if (path == null) {
                        path = Constants.BUNDLE_LOCALIZATION_DEFAULT_BASENAME;
                      }
                      path += ".properties";
                      byte[] loc = loadEntry(path);
                      if (loc != null) {
                        localization.load(new ByteArrayInputStream(loc));
                      }
                    } catch (IOException e) {
                      // TODO: ?
                    }
                  }
                  value = value.substring(1);
                  value = localization.getProperty(value, value);
                }
                return value;
              }

              private byte[] loadEntry(String name) throws IOException {
                InputStream is = FileUtil.openURL(bundleUrl);
                try {
                  ZipInputStream jis = new ZipInputStream(is);
                  for (ZipEntry e = jis.getNextEntry(); e != null; e = jis.getNextEntry()) {
                    if (name.equalsIgnoreCase(e.getName())) {
                      ByteArrayOutputStream baos = new ByteArrayOutputStream();
                      byte[] buf = new byte[1024];
                      int n;
                      while ((n = jis.read(buf, 0, buf.length)) > 0) {
                        baos.write(buf, 0, n);
                      }
                      return baos.toByteArray();
                    }
                  }
                } finally {
                  is.close();
                }
                return null;
              }
            });
    if (resource != null) {
      if ("file".equals(bundleUrl.getProtocol())) {
        try {
          File f = new File(bundleUrl.toURI());
          resource.put(Resource.SIZE, Long.toString(f.length()), null);
        } catch (URISyntaxException e) {
          throw new RuntimeException(e);
        }
      }
      resource.put(Resource.URI, bundleUrl.toExternalForm(), null);
    }
    return resource;
  }