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

org.structr.files.ssh.StructrPosixFileAttributes Maven / Gradle / Ivy

Go to download

Structr is an open source framework based on the popular Neo4j graph database.

The newest version!
/**
 * Copyright (C) 2010-2016 Structr GmbH
 *
 * This file is part of Structr .
 *
 * Structr is free software: you can redistribute it and/or modify
 * it under the terms of the 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.
 *
 * Structr 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with Structr.  If not, see .
 */
package org.structr.files.ssh;

import java.nio.file.attribute.FileTime;
import java.nio.file.attribute.GroupPrincipal;
import java.nio.file.attribute.PosixFileAttributes;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.UserPrincipal;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.structr.common.error.FrameworkException;
import org.structr.core.app.StructrApp;
import org.structr.core.entity.Group;
import org.structr.core.graph.Tx;
import org.structr.web.entity.AbstractFile;
import org.structr.web.entity.FileBase;
import org.structr.web.entity.Folder;
import org.structr.web.entity.User;

/**
 *
 */
public class StructrPosixFileAttributes implements PosixFileAttributes {
	
	private static final Logger logger = Logger.getLogger(StructrPosixFileAttributes.class.getName());
	
	final AbstractFile file;
	
	StructrPosixFileAttributes(final StructrSSHFile path) {
		file = ((StructrSSHFile) path).getActualFile();
	}
	
	@Override
	public UserPrincipal owner() {

		UserPrincipal owner = null;
		
		try (Tx tx = StructrApp.getInstance().tx()) {
			owner = file.getOwnerNode()::getName;
			tx.success();
		} catch (FrameworkException fex) {
			logger.log(Level.SEVERE, "", fex);
		}
		
		return owner;
	}

	@Override
	public GroupPrincipal group() {
		final List groups = file.getOwnerNode().getProperty(User.groups);
		return groups != null && groups.size() > 0 ? groups.get(0)::getName : null;
	}

	@Override
	public Set permissions() {

		final Set permissions = new HashSet<>();
		permissions.add(PosixFilePermission.OWNER_READ);

		return permissions;
	}

	@Override
	public FileTime lastModifiedTime() {
		
		FileTime time = null;
		
		try (Tx tx = StructrApp.getInstance().tx()) {
			time = FileTime.fromMillis(file.getLastModifiedDate().getTime());
			tx.success();
		} catch (FrameworkException fex) {
			logger.log(Level.SEVERE, "", fex);
		}
		
		return time;
		
	}

	@Override
	public FileTime lastAccessTime() {

		FileTime time = null;
		
		try (Tx tx = StructrApp.getInstance().tx()) {
			// Same as lastModifiedTime() as we don't store last access time in Structr yet
			time = FileTime.fromMillis(file.getLastModifiedDate().getTime());
			tx.success();
		} catch (FrameworkException fex) {
			logger.log(Level.SEVERE, "", fex);
		}
		
		return time;
	}

	@Override
	public FileTime creationTime() {
		
		FileTime time = null;
		
		try (Tx tx = StructrApp.getInstance().tx()) {
			time = FileTime.fromMillis(file.getCreatedDate().getTime());
			tx.success();
		} catch (FrameworkException fex) {
			logger.log(Level.SEVERE, "", fex);
		}
		
		return time;
	}

	@Override
	public boolean isRegularFile() {
		
		boolean isRegularFile = false;

		try (Tx tx = StructrApp.getInstance().tx()) {
			isRegularFile = file.getProperty(FileBase.isFile);
			tx.success();
		} catch (FrameworkException fex) {
			logger.log(Level.SEVERE, "", fex);
		}
		
		return isRegularFile;
	}

	@Override
	public boolean isDirectory() {
		
		boolean isDirectory = false;

		try (Tx tx = StructrApp.getInstance().tx()) {
			isDirectory = file.getProperty(Folder.isFolder);
			tx.success();
		} catch (FrameworkException fex) {
			logger.log(Level.SEVERE, "", fex);
		}
		
		return isDirectory;
	}

	@Override
	public boolean isSymbolicLink() {
		// Structr doesn't support symbolic links yet
		return false;
	}

	@Override
	public boolean isOther() {
		return false;
	}

	@Override
	public long size() {
		
		long size = 0;

		try (Tx tx = StructrApp.getInstance().tx()) {
			size = file.getProperty(FileBase.size);
			tx.success();
		} catch (FrameworkException fex) {
			logger.log(Level.SEVERE, "", fex);
		}
		
		return size;
	}

	@Override
	public Object fileKey() {
		
		String uuid = null;
		try (Tx tx = StructrApp.getInstance().tx()) {
			uuid = file.getUuid();
			tx.success();
		} catch (FrameworkException fex) {
			logger.log(Level.SEVERE, "", fex);
		}
		
		return uuid;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy