/**
   * Set a property of a resource to a value.
   *
   * @param name the property name
   * @param value the property value
   * @exception com.ibm.webdav.WebDAVException
   */
  public void setProperty(String name, Element value) throws WebDAVException {
    // load the properties
    Document propertiesDocument = resource.loadProperties();
    Element properties = propertiesDocument.getDocumentElement();
    String ns = value.getNamespaceURI();

    Element property = null;
    if (ns == null) {
      property = (Element) ((Element) properties).getElementsByTagName(value.getTagName()).item(0);
    } else {
      property = (Element) properties.getElementsByTagNameNS(ns, value.getLocalName()).item(0);
    }

    if (property != null) {
      try {
        properties.removeChild(property);
      } catch (DOMException exc) {
      }
    }

    properties.appendChild(propertiesDocument.importNode(value, true));

    // write out the properties
    resource.saveProperties(propertiesDocument);
  }
예제 #2
0
  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);
    }
  }
예제 #3
0
 public Resource getResource(String id) {
   for (Iterator i = getResourceList().iterator(); i.hasNext(); ) {
     ResourceImpl resource = (ResourceImpl) i.next();
     if (resource.getId().equals(id)) return resource;
   }
   return null;
 }
예제 #4
0
  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);
  }
  /**
   * Get the named properties for this resource and (potentially) its children.
   *
   * @param names an arrary of property names to retrieve
   * @return a MultiStatus of PropertyResponses
   * @exception com.ibm.webdav.WebDAVException
   */
  public MultiStatus getProperties(PropertyName[] names) throws WebDAVException {
    MultiStatus multiStatus = resource.getProperties(resource.getContext());
    MultiStatus newMultiStatus = new MultiStatus();

    Enumeration responses = multiStatus.getResponses();

    while (responses.hasMoreElements()) {
      PropertyResponse response = (PropertyResponse) responses.nextElement();
      PropertyResponse newResponse = new PropertyResponse(response.getResource());
      newResponse.setDescription(response.getDescription());
      newMultiStatus.addResponse(newResponse);

      Hashtable properties = (Hashtable) response.getPropertiesByPropName();

      // Hashtable newProperties = (Hashtable) newResponse.getProperties();
      for (int i = 0; i < names.length; i++) {
        if (properties.containsKey(names[i])) {
          PropertyValue srcval = response.getProperty(names[i]);
          newResponse.setProperty(names[i], srcval);

          // newProperties.put(names[i], properties.get(names[i]));
        } else {
          Document factory = null;

          try {
            factory = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
          } catch (Exception e) {
            throw new WebDAVException(WebDAVStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
          }

          // we'll create an xml element with no value because that's
          //    what webdav will need to return for most methods... even if the
          //    property doesn't exist.   That's because the
          //    distinction between a propertyname and propertyvalue
          //    is fuzzy in WebDAV xml. A property name is
          //    essentially an empty property value because
          //    all property values have their property
          //    name stuck on.
          // if we decide to set reviewStatus to null instead (as
          //    we did previously, then the code for MultiStatus.asXML()
          //    needs to be updated to expect null values.
          // (jlc 990520)
          Element elTm = factory.createElementNS("X", "X:" + names[i].getLocal());

          elTm.setAttribute("xmlns:X", names[i].getNamespace());

          newResponse.addProperty(names[i], elTm, WebDAVStatus.SC_NOT_FOUND);
        }
      }
    }

    return newMultiStatus;
  }
  /**
   * Get all the properties of this resource. This implementation stores properties in an XML
   * document containing a properties root element. The properties file name is derived from the URI
   * by adding the extension PropertiesManager.propertiesSuffix. This applies to collections as well
   * as other resources.
   *
   * <p>Since the properties are stored in a file, and all methods that query and update the
   * properties must read the XML file into memory and parse it, many of the other property methods
   * are implemented by calling this method. Subclasses of ResourceImpl may want to use other
   * techniques depending on how the properties are stored.
   *
   * @return a MultiStatus containing response elements with prop elements containing the properties
   *     and their statuses.
   * @exception com.ibm.webdav.WebDAVException
   */
  public MultiStatus getProperties() throws WebDAVException {
    Document propertiesDocument = resource.loadProperties();

    // create a MultiStatus to hold the results
    MultiStatus results = new MultiStatus();

    // create a response element to hold the properties for this resource
    PropertyResponse response = null;
    String urlstring;

    if (false) {
      // I consider this to be the more correct way and the
      //    way used in the examples in the spec... but it is
      //    redundant and creates the possibility of the two
      //    redundant parts being out of synch.
      urlstring = resource.getURL().toString();
    } else {
      // this is the way that mod_dav and a few others do it. This
      //    way also makes it easier to debug clients even if
      //    redirecting through a dedicated proxy.  Without this
      //    it's inconvenient to debug IE5.  It gets confused if
      //    the host:port (if provided) don't match who it thinks
      //    it's connecting to.
      urlstring = resource.getURL().getFile();
    }

    response = new PropertyResponse(urlstring);

    // add the properties to the response
    NodeList properties = propertiesDocument.getDocumentElement().getChildNodes();
    Node temp = null;

    for (int i = 0; i < properties.getLength(); i++) {
      temp = properties.item(i);

      // Skip ignorable TXText elements
      if (!(temp.getNodeType() == Node.ELEMENT_NODE)) {
        continue;
      }

      Element property = (Element) temp;
      PropertyName pn = new PropertyName(property);
      response.addProperty(pn, property, WebDAVStatus.SC_OK);
    }

    results.addResponse(response);

    return results;
  }
예제 #7
0
 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);
   }
 }
예제 #8
0
 private static void doBundle(ResourceImpl resource, Headers headers) {
   CapabilityImpl capability = new CapabilityImpl(Capability.BUNDLE);
   capability.addProperty(Resource.SYMBOLIC_NAME, getSymbolicName(headers));
   if (headers.getHeader(Constants.BUNDLE_NAME) != null) {
     capability.addProperty(Resource.PRESENTATION_NAME, headers.getHeader(Constants.BUNDLE_NAME));
   }
   capability.addProperty(Resource.VERSION, Property.VERSION, getVersion(headers));
   capability.addProperty(Resource.MANIFEST_VERSION, getManifestVersion(headers));
   resource.addCapability(capability);
 }
예제 #9
0
  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);
    }
  }
예제 #10
0
  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);
    }
  }
  /**
   * Get the names of all properties for this resource. This implementation reads all the properties
   * and then extracts their names.
   *
   * @return a MultiStatus of PropertyResponses (PropertyValue.value is always null,
   *     PropertyValue.status contains the status)
   * @exception com.ibm.webdav.WebDAVException
   */
  public MultiStatus getPropertyNames() throws WebDAVException {
    MultiStatus multiStatus = resource.getProperties(resource.getContext());
    Enumeration responses = multiStatus.getResponses();

    // we have the result, but all of the properties in our structure contain
    //    values.  We don't want to include values.  Just names.  The following
    //    code strips out the content of these elements.
    while (responses.hasMoreElements()) {
      PropertyResponse response = (PropertyResponse) responses.nextElement();
      Dictionary properties = response.getPropertiesByPropName();
      Enumeration keys = properties.keys();

      while (keys.hasMoreElements()) {
        PropertyName key = (PropertyName) keys.nextElement();
        Element value = (Element) response.getProperty(key).getValue();
        response.setProperty(
            key, new PropertyValue((Element) value.cloneNode(false), WebDAVStatus.SC_OK));
      }
    }

    return multiStatus;
  }
예제 #12
0
 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);
   }
 }
예제 #13
0
  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);
    }
  }
예제 #14
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");
  }
  /**
   * Edit the properties of a resource. The updates must refer to a Document containing a WebDAV
   * propertyupdates element as the document root.
   *
   * @param updates an XML Document containing propertyupdate elements
   * @return the result of making the updates describing the edits to be made.
   * @exception com.ibm.webdav.WebDAVException
   */
  public MultiStatus setProperties(Document propertyUpdates) throws WebDAVException {
    // create a MultiStatus to hold the results. It will hold a MethodResponse
    // for each update, and one for the method as a whole
    MultiStatus multiStatus = new MultiStatus();
    boolean errorsOccurred = false;

    // first, load the properties so they can be edited
    Document propertiesDocument = resource.loadProperties();
    Element properties = (Element) propertiesDocument.getDocumentElement();

    // be sure the updates have at least one update
    Element propertyupdate = (Element) propertyUpdates.getDocumentElement();
    String tagName = propertyupdate.getNamespaceURI() + propertyupdate.getLocalName();

    if (!tagName.equals("DAV:propertyupdate")) {
      throw new WebDAVException(
          WebDAVStatus.SC_UNPROCESSABLE_ENTITY, "missing propertyupdate element");
    }

    NodeList updates = propertyupdate.getChildNodes();

    if (updates.getLength() == 0) {
      throw new WebDAVException(WebDAVStatus.SC_UNPROCESSABLE_ENTITY, "no updates in request");
    }

    Vector propsGood = new Vector(); // a list of properties that

    // were patched correctly or would have been if another
    // property hadn't gone bad.
    // apply the updates
    Node temp = null;

    for (int i = 0; i < updates.getLength(); i++) {
      temp = updates.item(i);

      // skip any ignorable TXText elements
      if (!(temp.getNodeType() == Node.ELEMENT_NODE)) {
        continue;
      }

      Element update = (Element) temp;
      int updateCommand = -1;
      tagName = update.getNamespaceURI() + update.getLocalName();

      if (tagName.equals("DAV:set")) {
        updateCommand = set;
      } else if (tagName.equals("DAV:remove")) {
        updateCommand = remove;
      } else {
        throw new WebDAVException(
            WebDAVStatus.SC_UNPROCESSABLE_ENTITY,
            update.getTagName() + " is not a valid property update request");
      }

      // iterate through the props in the set or remove element and update the
      // properties as directed
      Element prop = (Element) update.getElementsByTagNameNS("DAV:", "prop").item(0);

      if (prop == null) {
        throw new WebDAVException(
            WebDAVStatus.SC_UNPROCESSABLE_ENTITY, "no propeprties in update request");
      }

      NodeList propsToUpdate = prop.getChildNodes();

      for (int j = 0; j < propsToUpdate.getLength(); j++) {
        temp = propsToUpdate.item(j);

        // skip any TXText elements??
        if (!(temp.getNodeType() == Node.ELEMENT_NODE)) {
          continue;
        }

        Element propToUpdate = (Element) temp;

        // find the property in the properties element
        Element property = null;
        PropertyName propertyName = new PropertyName(propToUpdate);

        if (((Element) propToUpdate).getNamespaceURI() != null) {
          property =
              (Element)
                  properties
                      .getElementsByTagNameNS(
                          propToUpdate.getNamespaceURI(), propToUpdate.getLocalName())
                      .item(0);
        } else {
          property = (Element) properties.getElementsByTagName(propToUpdate.getTagName()).item(0);
        }

        boolean liveone = isLive(propertyName.asExpandedString());

        if (liveone) {
          errorsOccurred = true;

          PropertyResponse response = new PropertyResponse(resource.getURL().toString());
          response.addProperty(propertyName, propToUpdate, WebDAVStatus.SC_FORBIDDEN);
          multiStatus.addResponse(response);
        }

        // do the update
        if (updateCommand == set) {
          if (property != null) {
            try {
              properties.removeChild(property);
            } catch (DOMException exc) {
            }
          }

          if (!liveone) {
            // I don't think we're allowed to update live properties
            //    here.  Doing so effects the cache.  A case in
            //    point is the lockdiscoveryproperty.  properties
            //    is actually the properites cache "document" of this
            //    resource.  Even though we don't "save" the request
            //    if it includes live properties, we don't remove
            //    it from the cache after we'd set it here, so it
            //    can affect other queries. (jlc 991002)
            properties.appendChild(propertiesDocument.importNode(propToUpdate, true));

            propsGood.addElement(propToUpdate);
          }
        } else if (updateCommand == remove) {
          try {
            if (property != null) {
              properties.removeChild(property);
              propsGood.addElement(propToUpdate);
            }
          } catch (DOMException exc) {
          }
        }
      }
    }

    {
      Enumeration els = propsGood.elements();

      for (; els.hasMoreElements(); ) {
        Object ob1 = els.nextElement();
        Element elProp = (Element) ob1;
        PropertyName pn = new PropertyName(elProp);
        PropertyResponse response = new PropertyResponse(resource.getURL().toString());
        response.addProperty(
            pn,
            (Element) elProp.cloneNode(false),
            (errorsOccurred ? WebDAVStatus.SC_FAILED_DEPENDENCY : WebDAVStatus.SC_OK));

        // todo: add code for responsedescription
        multiStatus.addResponse(response);
      }
    }

    // write out the properties
    if (!errorsOccurred) {
      resource.saveProperties(propertiesDocument);
    }

    return multiStatus;
  }
예제 #16
0
 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());
   }
 }
예제 #17
0
  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;
  }