Search in sources :

Example 26 with MCRPath

use of org.mycore.datamodel.niofs.MCRPath in project mycore by MyCoRe-Org.

the class MCRFileSystemProvider method move.

/* (non-Javadoc)
     * @see java.nio.file.spi.FileSystemProvider#move(java.nio.file.Path, java.nio.file.Path, java.nio.file.CopyOption[])
     */
@Override
public void move(Path source, Path target, CopyOption... options) throws IOException {
    HashSet<CopyOption> copyOptions = Sets.newHashSet(options);
    if (copyOptions.contains(StandardCopyOption.ATOMIC_MOVE)) {
        throw new AtomicMoveNotSupportedException(source.toString(), target.toString(), "ATOMIC_MOVE not supported yet");
    }
    if (Files.isDirectory(source)) {
        MCRPath src = MCRFileSystemUtils.checkPathAbsolute(source);
        MCRDirectory srcRootDirectory = getRootDirectory(src);
        if (srcRootDirectory.hasChildren()) {
            throw new IOException("Directory is not empty");
        }
    }
    copy(source, target, options);
    delete(source);
}
Also used : CopyOption(java.nio.file.CopyOption) StandardCopyOption(java.nio.file.StandardCopyOption) MCRDirectory(org.mycore.datamodel.ifs.MCRDirectory) AtomicMoveNotSupportedException(java.nio.file.AtomicMoveNotSupportedException) IOException(java.io.IOException) MCRPath(org.mycore.datamodel.niofs.MCRPath)

Example 27 with MCRPath

use of org.mycore.datamodel.niofs.MCRPath in project mycore by MyCoRe-Org.

the class MCRFileSystemProvider method getFileStore.

/* (non-Javadoc)
     * @see java.nio.file.spi.FileSystemProvider#getFileStore(java.nio.file.Path)
     */
@Override
public FileStore getFileStore(Path path) throws IOException {
    MCRPath mcrPath = MCRFileSystemUtils.checkPathAbsolute(path);
    if (mcrPath.getNameCount() > 0) {
        MCRFilesystemNode node = resolvePath(mcrPath);
        if (node instanceof MCRFile) {
            MCRFile file = (MCRFile) node;
            String storeID = file.getStoreID();
            return MCRFileStore.getInstance(storeID);
        }
    }
    return MCRFileStore.getInstance(MCRContentStoreFactory.getDefaultStore().getID());
}
Also used : MCRFile(org.mycore.datamodel.ifs.MCRFile) MCRFilesystemNode(org.mycore.datamodel.ifs.MCRFilesystemNode) MCRPath(org.mycore.datamodel.niofs.MCRPath)

Example 28 with MCRPath

use of org.mycore.datamodel.niofs.MCRPath in project mycore by MyCoRe-Org.

the class MCRFileSystemUtils method getMCRFile.

static MCRFile getMCRFile(MCRPath ifsPath, boolean create, boolean createNew) throws IOException {
    if (!ifsPath.isAbsolute()) {
        throw new IllegalArgumentException("'path' needs to be absolute.");
    }
    MCRFile file;
    MCRDirectory root = null;
    boolean rootCreated = false;
    try {
        try {
            root = MCRFileSystemProvider.getRootDirectory(ifsPath);
        } catch (NoSuchFileException e) {
            if (create || createNew) {
                root = new MCRDirectory(ifsPath.getOwner());
                rootCreated = true;
            } else {
                throw e;
            }
        }
        MCRPath relativePath = root.toPath().relativize(ifsPath);
        file = getMCRFile(root, relativePath, create, createNew);
    } catch (Exception e) {
        if (rootCreated) {
            LOGGER.error("Exception while getting MCRFile {}. Removing created filesystem nodes.", ifsPath);
            try {
                root.delete();
            } catch (Exception de) {
                LOGGER.fatal("Error while deleting file system node: {}", root.getName(), de);
            }
        }
        throw e;
    }
    return file;
}
Also used : MCRFile(org.mycore.datamodel.ifs.MCRFile) MCRDirectory(org.mycore.datamodel.ifs.MCRDirectory) NoSuchFileException(java.nio.file.NoSuchFileException) MCRPath(org.mycore.datamodel.niofs.MCRPath) NoSuchFileException(java.nio.file.NoSuchFileException) ProviderMismatchException(java.nio.file.ProviderMismatchException) IOException(java.io.IOException) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) InvalidPathException(java.nio.file.InvalidPathException)

Example 29 with MCRPath

use of org.mycore.datamodel.niofs.MCRPath in project mycore by MyCoRe-Org.

the class MCRFileSystemUtils method getMCRFile.

static MCRFile getMCRFile(MCRDirectory baseDir, MCRPath relativePath, boolean create, boolean createNew) throws IOException {
    MCRPath ifsPath = relativePath;
    if (relativePath.isAbsolute()) {
        if (baseDir.getOwnerID().equals(relativePath.getOwner())) {
            ifsPath = baseDir.toPath().relativize(relativePath);
        } else
            throw new IOException(relativePath + " is absolute does not fit to " + baseDir.toPath());
    }
    Deque<MCRFilesystemNode> created = new LinkedList<>();
    MCRFile file;
    try {
        file = (MCRFile) baseDir.getChildByPath(ifsPath.toString());
        if (file != null && createNew) {
            throw new FileAlreadyExistsException(baseDir.toPath().resolve(ifsPath).toString());
        }
        if (file == null & (create || createNew)) {
            Path normalized = ifsPath.normalize();
            MCRDirectory parent = baseDir;
            int nameCount = normalized.getNameCount();
            int directoryCount = nameCount - 1;
            int i = 0;
            while (i < directoryCount) {
                String curName = normalized.getName(i).toString();
                MCRDirectory curDir = (MCRDirectory) parent.getChild(curName);
                if (curDir == null) {
                    curDir = new MCRDirectory(curName, parent);
                    created.addFirst(curDir);
                }
                i++;
                parent = curDir;
            }
            String fileName = normalized.getFileName().toString();
            file = new MCRFile(fileName, parent);
            // false -> no event handler
            file.setContentFrom(new ByteArrayInputStream(new byte[0]), false);
            created.addFirst(file);
        }
    } catch (Exception e) {
        if (create || createNew) {
            LOGGER.error("Exception while getting MCRFile {}. Removing created filesystem nodes.", ifsPath);
            while (created.peekFirst() != null) {
                MCRFilesystemNode node = created.pollFirst();
                try {
                    node.delete();
                } catch (Exception de) {
                    LOGGER.fatal("Error while deleting file system node: {}", node.getName(), de);
                }
            }
        }
        throw e;
    }
    return file;
}
Also used : MCRPath(org.mycore.datamodel.niofs.MCRPath) Path(java.nio.file.Path) MCRFile(org.mycore.datamodel.ifs.MCRFile) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) MCRFilesystemNode(org.mycore.datamodel.ifs.MCRFilesystemNode) IOException(java.io.IOException) LinkedList(java.util.LinkedList) NoSuchFileException(java.nio.file.NoSuchFileException) ProviderMismatchException(java.nio.file.ProviderMismatchException) IOException(java.io.IOException) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) InvalidPathException(java.nio.file.InvalidPathException) MCRDirectory(org.mycore.datamodel.ifs.MCRDirectory) ByteArrayInputStream(java.io.ByteArrayInputStream) MCRPath(org.mycore.datamodel.niofs.MCRPath)

Example 30 with MCRPath

use of org.mycore.datamodel.niofs.MCRPath in project mycore by MyCoRe-Org.

the class MCRIFSFileSystem method createRoot.

@Override
public void createRoot(String owner) throws FileSystemException {
    MCRDirectory rootDirectory = MCRDirectory.getRootDirectory(owner);
    MCRPath rootPath = getPath(owner, "", this);
    if (rootDirectory != null) {
        throw new FileAlreadyExistsException(rootPath.toString());
    }
    try {
        rootDirectory = new MCRDirectory(owner);
    } catch (RuntimeException e) {
        LogManager.getLogger(getClass()).warn("Catched run time exception while creating new root directory.", e);
        throw new FileSystemException(rootPath.toString(), null, e.getMessage());
    }
    LogManager.getLogger(getClass()).info("Created root directory: {}", rootPath);
}
Also used : FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) FileSystemException(java.nio.file.FileSystemException) MCRDirectory(org.mycore.datamodel.ifs.MCRDirectory) MCRPath(org.mycore.datamodel.niofs.MCRPath)

Aggregations

MCRPath (org.mycore.datamodel.niofs.MCRPath)96 IOException (java.io.IOException)49 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)26 Path (java.nio.file.Path)25 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)22 MCRDerivate (org.mycore.datamodel.metadata.MCRDerivate)22 Document (org.jdom2.Document)15 JDOMException (org.jdom2.JDOMException)15 MCRPersistenceException (org.mycore.common.MCRPersistenceException)14 MCRException (org.mycore.common.MCRException)13 MCRDirectory (org.mycore.datamodel.ifs.MCRDirectory)13 MCRAccessException (org.mycore.access.MCRAccessException)12 Files (java.nio.file.Files)11 Collectors (java.util.stream.Collectors)11 LogManager (org.apache.logging.log4j.LogManager)11 Logger (org.apache.logging.log4j.Logger)11 FileVisitResult (java.nio.file.FileVisitResult)10 NoSuchFileException (java.nio.file.NoSuchFileException)10 Date (java.util.Date)10 List (java.util.List)10