Search in sources :

Example 1 with ResourceOnFileSystem

use of org.apache.sis.internal.storage.ResourceOnFileSystem in project sis by apache.

the class WritableStore method remove.

/**
 * Removes a {@code Resource} from this store. The resource must be a part of this {@code Aggregate}.
 * For a folder store, this means that the resource must be a direct children of the directory managed
 * by this store.
 *
 * This operation is destructive: the {@link Resource} and it's related files will be deleted.
 */
@Override
public synchronized void remove(final Resource resource) throws DataStoreException {
    if (resource instanceof DataStore)
        try {
            if (resource instanceof Store) {
                final Path path = ((Store) resource).location;
                if (Files.isSameFile(path.getParent(), location)) {
                    ((Store) resource).close();
                    deleteRecursively(path, true);
                    children.remove(path);
                    return;
                }
            } else if (resource instanceof ResourceOnFileSystem) {
                final Path[] componentPaths = ((ResourceOnFileSystem) resource).getComponentFiles().clone();
                for (Path root : componentPaths) {
                    root = root.getParent();
                    if (Files.isSameFile(root, location)) {
                        /*
                         * If we enter in this block, we have determined that at least one file is located in the
                         * directory managed by this store - NOT in a subdirectory since they could be managed by
                         * different folder stores. We assume that this root file is the "main" file. Other files
                         * could be in subdirectories, but we need to verify - we do not delete files outside.
                         */
                        for (final Path path : componentPaths) {
                            if (path.startsWith(root)) {
                                Files.delete(path);
                            }
                        }
                        children.values().removeIf((e) -> e == resource);
                        // Clear cache. TODO: we should do something more efficient.
                        components = null;
                        return;
                    }
                }
            }
        } catch (IOException e) {
            throw new DataStoreException(messages().getString(Resources.Keys.CanNotRemoveResource_2, getDisplayName(), ((DataStore) resource).getDisplayName()), e);
        }
    throw new DataStoreException(messages().getString(Resources.Keys.NoSuchResourceInAggregate_2, getDisplayName(), StoreUtilities.getLabel(resource)));
}
Also used : Path(java.nio.file.Path) ResourceOnFileSystem(org.apache.sis.internal.storage.ResourceOnFileSystem) DataStoreException(org.apache.sis.storage.DataStoreException) DataStore(org.apache.sis.storage.DataStore) DataStore(org.apache.sis.storage.DataStore) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)1 Path (java.nio.file.Path)1 ResourceOnFileSystem (org.apache.sis.internal.storage.ResourceOnFileSystem)1 DataStore (org.apache.sis.storage.DataStore)1 DataStoreException (org.apache.sis.storage.DataStoreException)1