All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.smallmind.file.ephemeral.EphemeralFileSystemProvider Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2007 through 2024 David Berkman
 *
 * This file is part of the SmallMind Code Project.
 *
 * The SmallMind Code Project is free software, you can redistribute
 * it and/or modify it under either, at your discretion...
 *
 * 1) The terms of GNU Affero General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or (at
 * your option) any later version.
 *
 * ...or...
 *
 * 2) The terms of the Apache License, Version 2.0.
 *
 * The SmallMind Code Project is distributed in the hope that it will
 * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License or Apache License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * and the Apache License along with the SmallMind Code Project. If not, see
 *  or .
 *
 * Additional permission under the GNU Affero GPL version 3 section 7
 * ------------------------------------------------------------------
 * If you modify this Program, or any covered work, by linking or
 * combining it with other code, such other code is not for that reason
 * alone subject to any of the requirements of the GNU Affero GPL
 * version 3.
 */
package org.smallmind.file.ephemeral;

import java.io.IOException;
import java.net.URI;
import java.nio.channels.SeekableByteChannel;
import java.nio.file.AccessMode;
import java.nio.file.CopyOption;
import java.nio.file.DirectoryStream;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.FileStore;
import java.nio.file.FileSystem;
import java.nio.file.FileSystemAlreadyExistsException;
import java.nio.file.LinkOption;
import java.nio.file.NoSuchFileException;
import java.nio.file.NotDirectoryException;
import java.nio.file.OpenOption;
import java.nio.file.Path;
import java.nio.file.ProviderMismatchException;
import java.nio.file.SecureDirectoryStream;
import java.nio.file.StandardOpenOption;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileAttribute;
import java.nio.file.attribute.FileAttributeView;
import java.nio.file.spi.FileSystemProvider;
import java.util.Map;
import java.util.Set;

/*
 -Djava.nio.file.spi.DefaultFileSystemProvider=org.smallmind.file.ephemeral.EphemeralFileSystemProvider

  -Dorg.smallmind.file.ephemeral.configuration.capacity=
  -Dorg.smallmind.file.ephemeral.configuration.blockSize=<1024>
  -Dorg.smallmind.file.ephemeral.configuration.roots=/overlay/file/system,/any/other/path
*/

public class EphemeralFileSystemProvider extends FileSystemProvider {

  private final EphemeralFileSystem ephemeralFileSystem;
  private final String scheme;
  private FileSystem nativeFileSystem;

  public EphemeralFileSystemProvider () {

    this("ephemeral");
  }

  public EphemeralFileSystemProvider (FileSystemProvider fileSystemProvider) {

    this(fileSystemProvider.getScheme());

    nativeFileSystem = fileSystemProvider.getFileSystem(URI.create(fileSystemProvider.getScheme() + ":///"));
  }

  public EphemeralFileSystemProvider (String scheme) {

    this.scheme = scheme;

    ephemeralFileSystem = new EphemeralFileSystem(this, new EphemeralFileSystemConfiguration());
  }

  public boolean isDefault () {

    return "file".equals(scheme);
  }

  @Override
  public String getScheme () {

    return scheme;
  }

  public FileSystem getNativeFileSystem () {

    return nativeFileSystem;
  }

  @Override
  public FileSystem newFileSystem (URI uri, Map env) {

    EphemeralURIUtility.checkUri(scheme, uri);
    throw new FileSystemAlreadyExistsException();
  }

  @Override
  public FileSystem getFileSystem (URI uri) {

    EphemeralURIUtility.checkUri(scheme, uri);

    return ephemeralFileSystem;
  }

  @Override
  public Path getPath (URI uri) {

    return EphemeralURIUtility.fromUri(ephemeralFileSystem, uri);
  }

  @Override
  public synchronized SeekableByteChannel newByteChannel (Path path, Set options, FileAttribute... attrs)
    throws IOException {

    if (!EphemeralFileSystem.class.isAssignableFrom(path.getFileSystem().getClass())) {
      throw new ProviderMismatchException("The path(" + path + ") is not associated with the " + EphemeralFileSystem.class.getSimpleName());
    } else {

      EphemeralPath normalizedPath = (EphemeralPath)path.normalize();

      if (options.contains(StandardOpenOption.WRITE) || options.contains(StandardOpenOption.APPEND)) {
        try {
          internalCheckAccess(normalizedPath, AccessMode.WRITE);
        } catch (NoSuchFileException noSuchFileException) {
          if (normalizedPath.getNames().length > 0) {
            internalCheckAccess(normalizedPath.getParent(), AccessMode.WRITE);
          }
        }
      } else {
        internalCheckAccess(normalizedPath, AccessMode.READ);
      }

      return ((EphemeralFileSystem)normalizedPath.getFileSystem()).getFileStore().newByteChannel(normalizedPath, options, attrs);
    }
  }

  @Override
  public synchronized DirectoryStream newDirectoryStream (Path dir, DirectoryStream.Filter filter)
    throws NoSuchFileException, NotDirectoryException {

    return newDirectoryStream(dir, filter, new LinkOption[0]);
  }

  public synchronized SecureDirectoryStream newDirectoryStream (Path dir, DirectoryStream.Filter filter, LinkOption... options)
    throws NoSuchFileException, NotDirectoryException {

    if (!EphemeralFileSystem.class.isAssignableFrom(dir.getFileSystem().getClass())) {
      throw new ProviderMismatchException("The path(" + dir + ") is not associated with the " + EphemeralFileSystem.class.getSimpleName());
    } else {

      EphemeralPath normalizedDir = (EphemeralPath)dir.normalize();

      internalCheckAccess(normalizedDir, AccessMode.READ);

      return ((EphemeralFileSystem)normalizedDir.getFileSystem()).getFileStore().newDirectoryStream(normalizedDir, filter);
    }
  }

  @Override
  public synchronized void createDirectory (Path dir, FileAttribute... attrs)
    throws NoSuchFileException, FileAlreadyExistsException {

    if (!EphemeralFileSystem.class.isAssignableFrom(dir.getFileSystem().getClass())) {
      throw new ProviderMismatchException("The path(" + dir + ") is not associated with the " + EphemeralFileSystem.class.getSimpleName());
    } else {

      EphemeralPath normalizedDir = (EphemeralPath)dir.normalize();

      if (normalizedDir.getNames().length > 0) {
        internalCheckAccess(normalizedDir.getParent(), AccessMode.WRITE);
      }

      ((EphemeralFileSystem)normalizedDir.getFileSystem()).getFileStore().createDirectory(normalizedDir, attrs);
    }
  }

  @Override
  public synchronized void delete (Path path)
    throws IOException {

    if (!EphemeralFileSystem.class.isAssignableFrom(path.getFileSystem().getClass())) {
      throw new ProviderMismatchException("The path(" + path + ") is not associated with the " + EphemeralFileSystem.class.getSimpleName());
    } else {

      EphemeralPath normalizedPath = (EphemeralPath)path.normalize();

      internalCheckAccess(normalizedPath, AccessMode.WRITE);

      ((EphemeralFileSystem)normalizedPath.getFileSystem()).getFileStore().delete(normalizedPath);
    }
  }

  @Override
  public synchronized void copy (Path source, Path target, CopyOption... options)
    throws IOException {

    if (!EphemeralFileSystem.class.isAssignableFrom(source.getFileSystem().getClass())) {
      throw new ProviderMismatchException("The path(" + source + ") is not associated with the " + EphemeralFileSystem.class.getSimpleName());
    } else if (!EphemeralFileSystem.class.isAssignableFrom(target.getFileSystem().getClass())) {
      throw new ProviderMismatchException("The path(" + target + ") is not associated with the " + EphemeralFileSystem.class.getSimpleName());
    } else {

      EphemeralPath normalizedSource = (EphemeralPath)source.normalize();
      EphemeralPath normalizedTarget = (EphemeralPath)target.normalize();

      internalCheckAccess(normalizedSource, AccessMode.READ);
      internalCheckAccess(normalizedTarget, AccessMode.WRITE);

      ((EphemeralFileSystem)normalizedSource.getFileSystem()).getFileStore().copy(normalizedSource, normalizedTarget, options);
    }
  }

  @Override
  public synchronized void move (Path source, Path target, CopyOption... options)
    throws IOException {

    if (!EphemeralFileSystem.class.isAssignableFrom(source.getFileSystem().getClass())) {
      throw new ProviderMismatchException("The path(" + source + ") is not associated with the " + EphemeralFileSystem.class.getSimpleName());
    } else if (!EphemeralFileSystem.class.isAssignableFrom(target.getFileSystem().getClass())) {
      throw new ProviderMismatchException("The path(" + target + ") is not associated with the " + EphemeralFileSystem.class.getSimpleName());
    } else {

      EphemeralPath normalizedSource = (EphemeralPath)source.normalize();
      EphemeralPath normalizedTarget = (EphemeralPath)target.normalize();

      internalCheckAccess(normalizedSource, AccessMode.READ);
      internalCheckAccess(normalizedTarget, AccessMode.WRITE);

      ((EphemeralFileSystem)source.getFileSystem()).getFileStore().move(normalizedSource, normalizedTarget, options);
    }
  }

  @Override
  public synchronized boolean isSameFile (Path source, Path target)
    throws NoSuchFileException {

    if (!EphemeralFileSystem.class.isAssignableFrom(source.getFileSystem().getClass())) {
      throw new ProviderMismatchException("The path(" + source + ") is not associated with the " + EphemeralFileSystem.class.getSimpleName());
    } else if (!EphemeralFileSystem.class.isAssignableFrom(target.getFileSystem().getClass())) {
      throw new ProviderMismatchException("The path(" + target + ") is not associated with the " + EphemeralFileSystem.class.getSimpleName());
    } else {

      EphemeralPath normalizedSource = (EphemeralPath)source.normalize();
      EphemeralPath normalizedTarget = (EphemeralPath)target.normalize();

      internalCheckAccess(normalizedSource, AccessMode.READ);
      internalCheckAccess(normalizedTarget, AccessMode.READ);

      return normalizedSource.equals(normalizedTarget);
    }
  }

  @Override
  public boolean isHidden (Path path) {

    return false;
  }

  @Override
  public FileStore getFileStore (Path path) {

    return path.getFileSystem().getFileStores().iterator().next();
  }

  @Override
  public synchronized void checkAccess (Path path, AccessMode... modes)
    throws NoSuchFileException {

    if (!EphemeralFileSystem.class.isAssignableFrom(path.getFileSystem().getClass())) {
      throw new ProviderMismatchException("The path(" + path + ") is not associated with the " + EphemeralFileSystem.class.getSimpleName());
    } else {

      EphemeralPath normalizedPath = (EphemeralPath)path.normalize();

      internalCheckAccess(normalizedPath, modes);
    }
  }

  private void internalCheckAccess (EphemeralPath path, AccessMode... modes)
    throws NoSuchFileException {

    ((EphemeralFileSystem)path.getFileSystem()).getFileStore().checkAccess(path);
  }

  @Override
  public synchronized  V getFileAttributeView (Path path, Class type, LinkOption... options) {

    if (!EphemeralFileSystem.class.isAssignableFrom(path.getFileSystem().getClass())) {
      throw new ProviderMismatchException("The path(" + path + ") is not associated with the " + EphemeralFileSystem.class.getSimpleName());
    } else {

      EphemeralPath normalizedPath = (EphemeralPath)path.normalize();

      try {
        internalCheckAccess(normalizedPath, AccessMode.READ);

        return ((EphemeralFileSystem)normalizedPath.getFileSystem()).getFileStore().getFileAttributeView(normalizedPath, type, options);
      } catch (NoSuchFileException noSuchFileException) {

        return null;
      }
    }
  }

  @Override
  public synchronized  A readAttributes (Path path, Class type, LinkOption... options)
    throws NoSuchFileException {

    if (!EphemeralFileSystem.class.isAssignableFrom(path.getFileSystem().getClass())) {
      throw new ProviderMismatchException("The path(" + path + ") is not associated with the " + EphemeralFileSystem.class.getSimpleName());
    } else {

      EphemeralPath normalizedPath = (EphemeralPath)path.normalize();

      internalCheckAccess(normalizedPath, AccessMode.READ);

      return ((EphemeralFileSystem)normalizedPath.getFileSystem()).getFileStore().readAttributes(normalizedPath, type, options);
    }
  }

  @Override
  public synchronized Map readAttributes (Path path, String attributes, LinkOption... options)
    throws NoSuchFileException {

    if (!EphemeralFileSystem.class.isAssignableFrom(path.getFileSystem().getClass())) {
      throw new ProviderMismatchException("The path(" + path + ") is not associated with the " + EphemeralFileSystem.class.getSimpleName());
    } else {

      EphemeralPath normalizedPath = (EphemeralPath)path.normalize();

      internalCheckAccess(normalizedPath, AccessMode.READ);

      return ((EphemeralFileSystem)normalizedPath.getFileSystem()).getFileStore().readAttributes(normalizedPath, attributes, options);
    }
  }

  @Override
  public synchronized void setAttribute (Path path, String attribute, Object value, LinkOption... options)
    throws NoSuchFileException {

    if (!EphemeralFileSystem.class.isAssignableFrom(path.getFileSystem().getClass())) {
      throw new ProviderMismatchException("The path(" + path + ") is not associated with the " + EphemeralFileSystem.class.getSimpleName());
    } else {

      EphemeralPath normalizedPath = (EphemeralPath)path.normalize();

      internalCheckAccess(normalizedPath, AccessMode.WRITE);

      ((EphemeralFileSystem)normalizedPath.getFileSystem()).getFileStore().setAttribute(normalizedPath, attribute, value, options);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy