public ResourceKey deserialize(final ResourceKey bundleKey, final String stringKey)
      throws ResourceKeyCreationException {
    // Parse the data
    final ResourceKeyData keyData = ResourceKeyUtils.parse(stringKey);

    // Validate the data
    if (SCHEMA_NAME.equals(keyData.getSchema()) == false) {
      throw new ResourceKeyCreationException(
          "Serialized version of key does not contain correct schema");
    }

    // Create a new file based on the path provided
    final File file = new File(keyData.getIdentifier());
    return new ResourceKey(SCHEMA_NAME, file, keyData.getFactoryParameters());
  }
  public String serialize(final ResourceKey bundleKey, final ResourceKey key)
      throws ResourceException {
    // Validate the parameter
    if (key == null) {
      throw new NullPointerException("The ResourceKey can not be null");
    }
    if (isSupportedKey(key) == false) {
      throw new IllegalArgumentException("Key format is not recognized.");
    }
    if (!(key.getIdentifier() instanceof File)) {
      throw new IllegalArgumentException(
          "ResourceKey is invalid - identifier is not a File object");
    }

    // Log information
    logger.debug("Serializing a File Resource Key...");
    if (key.getParent() != null) {
      throw new ResourceException(
          "Unable to serialize a File-ResourceKey with a parent. This type is not expected to have a parent.");
    }

    // Create a string version of the identifier
    try {
      final File file = (File) key.getIdentifier();
      final String strIdentifier = file.getCanonicalPath();
      final String result =
          ResourceKeyUtils.createStringResourceKey(
              key.getSchema().toString(), strIdentifier, key.getFactoryParameters());
      logger.debug("Serialized File Resource Key: [" + result + "]");
      return result;
    } catch (IOException ioe) {
      throw new IllegalArgumentException(
          "Could not determine cononical path to file specified in ResourceKey: "
              + ioe.getMessage());
    }
  }
 public boolean isSupportedDeserializer(final String data) {
   return SCHEMA_NAME.equals(ResourceKeyUtils.readSchemaFromString(data));
 }
Пример #4
0
  /**
   * A helper method that serializes the element object.
   *
   * @param stream the stream to which the element should be serialized.
   * @throws IOException if an IO error occured or a property was not serializable.
   */
  private void writeObject(final ObjectOutputStream stream) throws IOException {
    stream.defaultWriteObject();
    final ReportAttributeMap attributes = this.attributes;
    stream.writeLong(attributes.getChangeTracker());
    final String[] nameSpaces = attributes.getNameSpaces();
    stream.writeObject(nameSpaces);
    for (int i = 0; i < nameSpaces.length; i++) {
      final String nameSpace = nameSpaces[i];
      final String[] names = attributes.getNames(nameSpace);
      stream.writeObject(names);
      for (int j = 0; j < names.length; j++) {
        final String name = names[j];
        final Object attribute = attributes.getAttribute(nameSpace, name);

        final AttributeMetaData data = getMetaData().getAttributeDescription(nameSpace, name);
        if (data != null) {
          if (data.isTransient()) {
            stream.writeByte(1);
            continue;
          }

          if (attribute instanceof ResourceKey) {
            final ResourceKey key = (ResourceKey) attribute;
            final ResourceKey parent = key.getParent();
            if (AttributeNames.Core.NAMESPACE.equals(nameSpace)
                && (AttributeNames.Core.CONTENT_BASE.equals(name)
                    || AttributeNames.Core.SOURCE.equals(name))) {
              if (parent != null) {
                // unwrap the content base attribute. After deserialization, the report assumes the
                // bundle-location
                // as content base, as the bundle will be gone.
                if (isKeySerializable(parent)) {
                  stream.writeByte(0);
                  SerializerHelper.getInstance().writeObject(parent, stream);
                } else {
                  stream.writeByte(1);
                }
              } else {
                // great, the report was never part of a bundle. That makes life easier and the key
                // should be
                // safely serializable too.

                if (isKeySerializable(key)) {
                  stream.writeByte(0);
                  SerializerHelper.getInstance().writeObject(key, stream);
                } else {
                  stream.writeByte(1);
                }
              }
            } else {
              if ("Resource".equals(data.getValueRole()) || parent != null) {
                stream.writeByte(0);
                try {
                  final ResourceKey resourceKey =
                      ResourceKeyUtils.embedResourceInKey(
                          locateResourceManager(), key, key.getFactoryParameters());
                  SerializerHelper.getInstance().writeObject(resourceKey, stream);
                } catch (ResourceException e) {
                  throw new IOException("Failed to convert resource-key into byte-array key: " + e);
                }
              } else {
                stream.writeByte(0);
                SerializerHelper.getInstance().writeObject(attribute, stream);
              }
            }
          } else if (SerializerHelper.getInstance().isSerializable(attribute)) {
            stream.writeByte(0);
            SerializerHelper.getInstance().writeObject(attribute, stream);
          } else {
            stream.writeByte(1);
          }
        } else if (attribute instanceof String) {
          stream.writeByte(0);
          SerializerHelper.getInstance().writeObject(attribute, stream);
        } else {
          stream.writeByte(1);
        }
      }
    }
  }