Example usage for java.nio.file.attribute GroupPrincipal getName

List of usage examples for java.nio.file.attribute GroupPrincipal getName

Introduction

In this page you can find the example usage for java.nio.file.attribute GroupPrincipal getName.

Prototype

public String getName();

Source Link

Document

Returns the name of this principal.

Usage

From source file:at.beris.virtualfile.shell.Shell.java

private void list(boolean local) throws IOException {
    File file = local ? localFile : workingFile;

    if (file == null && !local) {
        System.out.println(NOT_CONNECTED_MSG);
        return;/*from  w  w w.ja v  a 2 s  . c o  m*/
    }

    int maxLengthOwner = 0, maxLengthGroup = 0, maxLengthSize = 0;
    int maxLengthDateStr = 0, maxLengthTimeStr = 0;

    StringBuilder sb = new StringBuilder();
    List<ExtFileModel> fileModelList = new ArrayList<>();
    for (File childFile : file.list()) {
        ExtFileModel model = new ExtFileModel();
        model.setUrl(childFile.getUrl());
        model.setAttributes(childFile.getAttributes());
        model.setDirectory(childFile.isDirectory());
        model.setOwner(childFile.getOwner());
        model.setGroup(childFile.getGroup());
        model.setSize(childFile.getSize());
        model.setLastModifiedTime(childFile.getLastModifiedTime());

        UserPrincipal owner = model.getOwner();
        if (owner != null && maxLengthOwner < owner.getName().length())
            maxLengthOwner = model.getOwner().getName().length();
        GroupPrincipal group = model.getGroup();
        if (group != null && maxLengthGroup < group.getName().length())
            maxLengthGroup = model.getGroup().getName().length();
        String sizeString = String.valueOf(model.getSize());
        if (maxLengthSize < sizeString.length())
            maxLengthSize = sizeString.length();

        if (model.getLastModifiedTime() != null) {
            Date lastModifiedDate = new Date(model.getLastModifiedTime().toMillis());
            model.dateStr = DATE_FORMATTER.format(lastModifiedDate);
            if (maxLengthDateStr < model.dateStr.length())
                maxLengthDateStr = model.dateStr.length();
            model.timeStr = TIME_FORMATTER.format(lastModifiedDate);
            if (maxLengthTimeStr < model.timeStr.length())
                maxLengthTimeStr = model.timeStr.length();
        } else {
            model.dateStr = "";
            model.timeStr = "";
        }

        fileModelList.add(model);
    }

    for (ExtFileModel model : fileModelList) {
        sb.setLength(0);
        sb.append(model.isDirectory() ? 'd' : '-');
        sb.append(model.getAttributes().contains(PosixFilePermission.OWNER_READ) ? 'r' : '-');
        sb.append(model.getAttributes().contains(PosixFilePermission.OWNER_WRITE) ? 'w' : '-');
        sb.append(model.getAttributes().contains(PosixFilePermission.OWNER_EXECUTE) ? 'x' : '-');
        sb.append(model.getAttributes().contains(PosixFilePermission.GROUP_READ) ? 'r' : '-');
        sb.append(model.getAttributes().contains(PosixFilePermission.GROUP_WRITE) ? 'w' : '-');
        sb.append(model.getAttributes().contains(PosixFilePermission.GROUP_EXECUTE) ? 'x' : '-');
        sb.append(model.getAttributes().contains(PosixFilePermission.OTHERS_READ) ? 'r' : '-');
        sb.append(model.getAttributes().contains(PosixFilePermission.OTHERS_WRITE) ? 'w' : '-');
        sb.append(model.getAttributes().contains(PosixFilePermission.OTHERS_EXECUTE) ? 'x' : '-').append(' ');
        sb.append(StringUtils.rightPad(model.getOwner().getName(), maxLengthOwner, ' ')).append(' ');
        sb.append(StringUtils.rightPad(model.getGroup().getName(), maxLengthGroup, ' ')).append(' ');
        sb.append(StringUtils.leftPad(String.valueOf(model.getSize()), maxLengthSize, ' ')).append(' ');
        sb.append(StringUtils.leftPad(model.dateStr, maxLengthDateStr, ' ')).append(' ');
        sb.append(StringUtils.leftPad(model.timeStr, maxLengthTimeStr, ' ')).append(' ');
        sb.append(FileUtils.getName(model.getUrl().toString()));
        System.out.println(sb.toString());
    }

    for (FileModel fileModel : fileModelList)
        fileModel.clear();
    fileModelList.clear();
}

From source file:configuration.Util.java

/**
 * Get the owner's group of the current jar file
 * @return the owner's group of the  current jar file
 *//*from  w  w w  .  j  a v  a 2  s .co m*/
public static String getGroupJar() {
    Path jpath = Paths.get(currentPath());
    try {
        GroupPrincipal group = Files.readAttributes(jpath, PosixFileAttributes.class, LinkOption.NOFOLLOW_LINKS)
                .group();
        return group.getName();
    } catch (IOException ex) {
        Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex);
        System.out.println("Can't get Owner or the jar file");
        System.out.println(ex);
        return "";
    }
}