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); } } }
/** * 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(); }
/** * Throws a runtime exception indicating that the given attribute cannot be set. */ protected static RuntimeException unsettable(String view, String attribute, boolean create) { // This matches the behavior of the real file system implementations: if the attempt to set the // attribute is being made during file creation, throw UOE even though the attribute is one // that cannot be set under any circumstances checkNotCreate(view, attribute, create); throw new IllegalArgumentException("cannot set attribute '" + view + ":" + attribute + "'"); }
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 UnsupportedOperationException( "cannot set attribute '" + view + ":" + attribute + "'"); }
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); } } }
@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; }
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); }
/** * Returns the set of attributes supported by this view that are present in the given file. For * most providers, this will be a fixed set of attributes. */ public ImmutableSet<String> attributes(File file) { return fixedAttributes(); }
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); } }
/** * Checks that the given value is of the given type, returning the value if so and throwing an * exception if not. */ protected static <T> T checkType(String view, String attribute, Object value, Class<T> type) { checkNotNull(value); if (type.isInstance(value)) { return type.cast(value); } throw invalidType(view, attribute, value, type); }
/** * 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); }
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 + "'"); }
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); } } }
@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; }
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); }
/** * Returns whether or not this provider supports the given attribute directly. */ public boolean supports(String attribute) { return fixedAttributes().contains(attribute); }
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()); }
/** * Checks that the given value is of the given type, returning the value if so and throwing an * exception if not. */ protected static <T> T checkType(String view, String attribute, Object value, Class<T> type) { checkNotNull(value); if (type.isInstance(value)) { return type.cast(value); } throw invalidType(view, attribute, value, type); }
/** * 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); }
/** * 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(); }