Esempio n. 1
0
  /** Implements {@link Files#readAttributes(Path, String, LinkOption...)}. */
  public ImmutableMap<String, Object> readAttributes(File file, String attributes) {
    String view = getViewName(attributes);
    List<String> attrs = getAttributeNames(attributes);

    if (attrs.size() > 1 && attrs.contains(ALL_ATTRIBUTES)) {
      // attrs contains * and other attributes
      throw new IllegalArgumentException("invalid attributes: " + attributes);
    }

    Map<String, Object> result = new HashMap<>();
    if (attrs.size() == 1 && attrs.contains(ALL_ATTRIBUTES)) {
      // for 'view:*' format, get all keys for all providers for the view
      AttributeProvider provider = providersByName.get(view);
      readAll(file, provider, result);

      for (String inheritedView : provider.inherits()) {
        AttributeProvider inheritedProvider = providersByName.get(inheritedView);
        readAll(file, inheritedProvider, result);
      }
    } else {
      // for 'view:attr1,attr2,etc'
      for (String attr : attrs) {
        result.put(attr, getAttribute(file, view, attr));
      }
    }

    return ImmutableMap.copyOf(result);
  }
Esempio n. 2
0
 private FileAttributeView getFileAttributeView(
     FileLookup lookup,
     Class<? extends FileAttributeView> viewType,
     Map<String, FileAttributeView> inheritedViews) {
   AttributeProvider provider = providersByViewType.get(viewType);
   createInheritedViews(lookup, provider, inheritedViews);
   return provider.view(lookup, ImmutableMap.copyOf(inheritedViews));
 }
Esempio n. 3
0
  /**
   * Returns attributes of the given file as an object of the given type.
   *
   * @throws UnsupportedOperationException if the given attributes type is not supported
   */
  @SuppressWarnings("unchecked")
  public <A extends BasicFileAttributes> A readAttributes(File file, Class<A> type) {
    AttributeProvider provider = providersByAttributesType.get(type);
    if (provider != null) {
      return (A) provider.readAttributes(file);
    }

    throw new UnsupportedOperationException("unsupported attributes type: " + type);
  }
Esempio n. 4
0
  private static void readAll(File file, AttributeProvider provider, Map<String, Object> map) {
    for (String attribute : provider.attributes(file)) {
      Object value = provider.get(file, attribute);

      // check for null to protect against race condition when an attribute present when
      // attributes(file) was called is deleted before get() is called for that attribute
      if (value != null) {
        map.put(attribute, value);
      }
    }
  }
Esempio n. 5
0
  /**
   * Returns an attribute view of the given type for the given file lookup callback, or {@code null}
   * if the view type is not supported.
   */
  @SuppressWarnings("unchecked")
  @Nullable
  public <V extends FileAttributeView> V getFileAttributeView(FileLookup lookup, Class<V> type) {
    AttributeProvider provider = providersByViewType.get(type);

    if (provider != null) {
      return (V) provider.view(lookup, createInheritedViews(lookup, provider));
    }

    return null;
  }
Esempio n. 6
0
  private static void addStandardProvider(Map<String, AttributeProvider> result, String view) {
    AttributeProvider provider = StandardAttributeProviders.get(view);

    if (provider == null) {
      if (!result.containsKey(view)) {
        throw new IllegalStateException("no provider found for attribute view '" + view + "'");
      }
    } else {
      result.put(provider.name(), provider);
    }
  }
Esempio n. 7
0
  private static Iterable<AttributeProvider> getProviders(Configuration configuration) {
    Map<String, AttributeProvider> result = new HashMap<>();

    for (AttributeProvider provider : configuration.attributeProviders) {
      result.put(provider.name(), provider);
    }

    for (String view : configuration.attributeViews) {
      addStandardProvider(result, view);
    }

    addMissingProviders(result);

    return Collections.unmodifiableCollection(result.values());
  }
Esempio n. 8
0
  private void createInheritedViews(
      FileLookup lookup,
      AttributeProvider provider,
      Map<String, FileAttributeView> inheritedViews) {

    for (String inherited : provider.inherits()) {
      if (!inheritedViews.containsKey(inherited)) {
        AttributeProvider inheritedProvider = providersByName.get(inherited);
        FileAttributeView inheritedView =
            getFileAttributeView(lookup, inheritedProvider.viewType(), inheritedViews);

        inheritedViews.put(inherited, inheritedView);
      }
    }
  }
Esempio n. 9
0
  private ImmutableMap<String, FileAttributeView> createInheritedViews(
      FileLookup lookup, AttributeProvider provider) {
    if (provider.inherits().isEmpty()) {
      return ImmutableMap.of();
    }

    Map<String, FileAttributeView> inheritedViews = new HashMap<>();
    createInheritedViews(lookup, provider, inheritedViews);
    return ImmutableMap.copyOf(inheritedViews);
  }
Esempio n. 10
0
  @Nullable
  private Object getAttributeInternal(File file, String view, String attribute) {
    AttributeProvider provider = providersByName.get(view);
    if (provider == null) {
      return null;
    }

    Object value = provider.get(file, attribute);
    if (value == null) {
      for (String inheritedView : provider.inherits()) {
        value = getAttributeInternal(file, inheritedView, attribute);
        if (value != null) {
          break;
        }
      }
    }

    return value;
  }
Esempio n. 11
0
  /**
   * Creates a new attribute service using the given providers and user provided default attribute
   * values.
   */
  public AttributeService(
      Iterable<? extends AttributeProvider> providers, Map<String, ?> userProvidedDefaults) {
    ImmutableMap.Builder<String, AttributeProvider> byViewNameBuilder = ImmutableMap.builder();
    ImmutableMap.Builder<Class<?>, AttributeProvider> byViewTypeBuilder = ImmutableMap.builder();
    ImmutableMap.Builder<Class<?>, AttributeProvider> byAttributesTypeBuilder =
        ImmutableMap.builder();

    ImmutableList.Builder<FileAttribute<?>> defaultAttributesBuilder = ImmutableList.builder();

    for (AttributeProvider provider : providers) {
      byViewNameBuilder.put(provider.name(), provider);
      byViewTypeBuilder.put(provider.viewType(), provider);
      if (provider.attributesType() != null) {
        byAttributesTypeBuilder.put(provider.attributesType(), provider);
      }

      for (Map.Entry<String, ?> entry : provider.defaultValues(userProvidedDefaults).entrySet()) {
        defaultAttributesBuilder.add(new SimpleFileAttribute<>(entry.getKey(), entry.getValue()));
      }
    }

    this.providersByName = byViewNameBuilder.build();
    this.providersByViewType = byViewTypeBuilder.build();
    this.providersByAttributesType = byAttributesTypeBuilder.build();
    this.defaultValues = defaultAttributesBuilder.build();
  }
Esempio n. 12
0
  private static void addMissingProviders(Map<String, AttributeProvider> providers) {
    Set<String> missingViews = new HashSet<>();
    for (AttributeProvider provider : providers.values()) {
      for (String inheritedView : provider.inherits()) {
        if (!providers.containsKey(inheritedView)) {
          missingViews.add(inheritedView);
        }
      }
    }

    if (missingViews.isEmpty()) {
      return;
    }

    // add any inherited views that were not listed directly
    for (String view : missingViews) {
      addStandardProvider(providers, view);
    }

    // in case any of the providers that were added themselves have missing views they inherit
    addMissingProviders(providers);
  }
Esempio n. 13
0
  private void setAttributeInternal(
      File file, String view, String attribute, Object value, boolean create) {
    AttributeProvider provider = providersByName.get(view);

    if (provider != null) {
      if (provider.supports(attribute)) {
        provider.set(file, view, attribute, value, create);
        return;
      }

      for (String inheritedView : provider.inherits()) {
        AttributeProvider inheritedProvider = providersByName.get(inheritedView);
        if (inheritedProvider.supports(attribute)) {
          inheritedProvider.set(file, view, attribute, value, create);
          return;
        }
      }
    }

    throw new IllegalArgumentException("cannot set attribute '" + view + ":" + attribute + "'");
  }