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

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

Introduction

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

Prototype

public String getName();

Source Link

Document

Returns the name of this principal.

Usage

From source file:Test.java

public static void main(String[] args) throws Exception {
    Path path = Paths.get("C:/home/docs/users.txt");
    FileOwnerAttributeView view = Files.getFileAttributeView(path, FileOwnerAttributeView.class);
    UserPrincipal userPrincipal = view.getOwner();
    System.out.println(userPrincipal.getName());
}

From source file:Main.java

public static void main(String[] args) throws Exception {

    Path path = Paths.get("C:\\Java_Dev\\test1.txt");

    FileOwnerAttributeView foav = Files.getFileAttributeView(path, FileOwnerAttributeView.class);

    UserPrincipal owner = foav.getOwner();
    System.out.format("Original owner  of  %s  is %s%n", path, owner.getName());

    FileSystem fs = FileSystems.getDefault();
    UserPrincipalLookupService upls = fs.getUserPrincipalLookupService();

    UserPrincipal newOwner = upls.lookupPrincipalByName("brice");
    foav.setOwner(newOwner);/*from   ww w  .j a v a2 s . c  om*/

    UserPrincipal changedOwner = foav.getOwner();
    System.out.format("New owner  of  %s  is %s%n", path, changedOwner.getName());

}

From source file:Test.java

public static void main(String[] args) throws Exception {
    Path path = Paths.get("C:/home/docs/users.txt");
    FileOwnerAttributeView view = Files.getFileAttributeView(path, FileOwnerAttributeView.class);
    UserPrincipal userPrincipal = view.getOwner();

    UserPrincipalLookupService lookupService = FileSystems.getDefault().getUserPrincipalLookupService();
    userPrincipal = lookupService.lookupPrincipalByName("users");
    view.setOwner(userPrincipal);/*from  ww w  . j a v  a2 s  . c  o  m*/
    System.out.println("UserPrincipal set: " + userPrincipal.getName());

}

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  ww  w.  j  av a2  s. c om
    }

    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:oracle.kv.sample.fileloader.FileLoader.java

/**
 * After successful validation this method is run to load the content of a
 * TXT file into the given table/* w w  w  . j a v  a  2  s  .  c  o  m*/
 * 
 * @throws IOException
 */
private void loadData() throws IOException {
    if (inputPathStr != null) {
        dir = new File(inputPathStr);
        File file = null;
        File[] files = null;
        int len = 0;

        // If input path is a directory then load data from all the files
        // that are under the directory. Make sure all the files are of same
        // type
        // i.e. TXT (mix and match is not allowed)
        if (dir.exists()) {
            System.out.println("There are " + dir.listFiles().length + " to be loaded.");
            if (dir.isDirectory()) {
                files = dir.listFiles();
                len = files.length;

                // loop through all the files and load the content one by
                // one
                for (int i = 0; i < len; i++) {
                    file = files[i];
                    try {
                        Path filePath = Paths.get(file.getPath());
                        BasicFileAttributes attr = Files.readAttributes(filePath, BasicFileAttributes.class,
                                LinkOption.NOFOLLOW_LINKS);
                        DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
                        FileOwnerAttributeView fileOwnerAttributeView = Files.getFileAttributeView(filePath,
                                FileOwnerAttributeView.class);
                        UserPrincipal userPrincipal = fileOwnerAttributeView.getOwner();

                        id = Integer.toString(i);
                        fileDate = formatter.format(attr.lastAccessTime().toMillis());
                        fileOwner = userPrincipal.getName();
                        binaryFile = FileUtils.readFileToByteArray(file);

                        row = table.createRow();
                        row.put("id", id);
                        row.put("date", fileDate.toString());
                        row.put("owner", fileOwner);
                        row.put("file", binaryFile);

                        tableh.put(row, null, null);
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            } else {
                System.out.println("There are no Files to Load");
            }
        }
    }

    if (fileId != null) {
        getData();
    }
}

From source file:org.mitre.provenance.capture.linux.PROCtor.java

/**
 * Get or create a new PLUSInvocation on the basis of a proc PID file, e.g. /proc/56 (pid 56)
 * Returns null for insufficient permissions, or when  you shouldn't log a particular pid.  (For 
 * example, this program will not log its own run)
 *//*from ww w.j a va2  s  .co m*/
public PLUSInvocation createOrRetrieveInvocation(File procPID) throws NoSuchAlgorithmException, IOException {
    String procFileID = getIDForFile(procPID);
    if (procFileID == null)
        return null;

    String pid = procPID.getName();
    if (pid.equals(myPID))
        return null; // Don't log myself.

    String[] children = procPID.list();
    if (children == null)
        return null; // No permissions.

    if (cache.containsKey(procFileID))
        return (PLUSInvocation) cache.get(procFileID);

    try {
        ProvenanceCollection results = Neo4JPLUSObjectFactory.loadBySingleMetadataField(User.DEFAULT_USER_GOD,
                UUID_KEY, procFileID);
        if (results != null && results.countNodes() > 0) {
            PLUSInvocation i = (PLUSInvocation) results.getNodes().toArray()[0];
            cache.put(procFileID, i);
            return i;
        }
    } catch (PLUSException exc) {
        exc.printStackTrace();
    }

    long lmod = procPID.lastModified();

    String cmdline = slurp(new File(procPID, "cmdline"));
    File exe = new File(procPID, "exe").getCanonicalFile();
    File cwd = new File(procPID, "cwd").getCanonicalFile();

    PLUSInvocation inv = new PLUSInvocation(exe.getCanonicalPath());
    inv.getMetadata().put("pid", pid);
    inv.getMetadata().put("cwd", cwd.getCanonicalPath());
    inv.getMetadata().put("cmdline", cmdline);
    inv.getMetadata().put("started", "" + lmod);
    inv.getMetadata().put(UUID_KEY, procFileID);
    inv.getMetadata().put(Metadata.CONTENT_HASH_SHA_256, procFileID);

    Path path = Paths.get(procPID.getAbsolutePath());
    UserPrincipal owner = Files.getOwner(path);
    String username = owner.getName();
    try {
        inv.setOwner(Neo4JPLUSObjectFactory.getActor(username, true));
    } catch (PLUSException exc) {
        log.warning("Failed to set owner for " + inv + ": " + exc.getMessage());
    }

    cache.put(procFileID, inv); // Cache this so we don't go back over it.

    return inv;
}