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;
}
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);
}
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);
}
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);
}
}
}
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;
}
Aggregations