Search in sources :

Example 11 with MCRDirectory

use of org.mycore.datamodel.ifs.MCRDirectory 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 12 with MCRDirectory

use of org.mycore.datamodel.ifs.MCRDirectory 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)

Example 13 with MCRDirectory

use of org.mycore.datamodel.ifs.MCRDirectory in project mycore by MyCoRe-Org.

the class MCRIFSFileSystem method removeRoot.

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

Example 14 with MCRDirectory

use of org.mycore.datamodel.ifs.MCRDirectory in project mycore by MyCoRe-Org.

the class MCRTransferPackageFileContainer method processNode.

/**
 * @param node the node to process
 */
private void processNode(MCRFilesystemNode node) {
    MCRDirectory dir = (MCRDirectory) node;
    MCRFilesystemNode[] children = dir.getChildren();
    for (MCRFilesystemNode child : children) {
        if (child instanceof MCRDirectory) {
            processNode(child);
        }
        if (child instanceof MCRFile) {
            this.fileList.add((MCRFile) child);
        }
    }
}
Also used : MCRFile(org.mycore.datamodel.ifs.MCRFile) MCRDirectory(org.mycore.datamodel.ifs.MCRDirectory) MCRFilesystemNode(org.mycore.datamodel.ifs.MCRFilesystemNode)

Example 15 with MCRDirectory

use of org.mycore.datamodel.ifs.MCRDirectory in project mycore by MyCoRe-Org.

the class MCRFileSystemProvider method getRootDirectory.

static MCRDirectory getRootDirectory(MCRPath ifsPath) throws NoSuchFileException {
    LOGGER.debug("Get root directory of {}", ifsPath.getOwner());
    MCRDirectory root = MCRDirectory.getRootDirectory(ifsPath.getOwner());
    if (root == null) {
        throw new NoSuchFileException(ifsPath.toString(), null, "Could not get root directory.");
    }
    // warm-up cache
    root.getChildren();
    return root;
}
Also used : MCRDirectory(org.mycore.datamodel.ifs.MCRDirectory) NoSuchFileException(java.nio.file.NoSuchFileException)

Aggregations

MCRDirectory (org.mycore.datamodel.ifs.MCRDirectory)21 MCRFilesystemNode (org.mycore.datamodel.ifs.MCRFilesystemNode)13 MCRPath (org.mycore.datamodel.niofs.MCRPath)13 MCRFile (org.mycore.datamodel.ifs.MCRFile)10 NoSuchFileException (java.nio.file.NoSuchFileException)9 IOException (java.io.IOException)7 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)5 NotDirectoryException (java.nio.file.NotDirectoryException)4 DirectoryNotEmptyException (java.nio.file.DirectoryNotEmptyException)3 Path (java.nio.file.Path)3 CopyOption (java.nio.file.CopyOption)2 FileSystemException (java.nio.file.FileSystemException)2 InvalidPathException (java.nio.file.InvalidPathException)2 ProviderMismatchException (java.nio.file.ProviderMismatchException)2 StandardCopyOption (java.nio.file.StandardCopyOption)2 BasicFileAttributeView (java.nio.file.attribute.BasicFileAttributeView)2 GregorianCalendar (java.util.GregorianCalendar)2 MCRException (org.mycore.common.MCRException)2 MCRPersistenceException (org.mycore.common.MCRPersistenceException)2 MCRDerivate (org.mycore.datamodel.metadata.MCRDerivate)2