类java.nio.file.attribute.UserPrincipalLookupService源码实例Demo

下面列出了怎么用java.nio.file.attribute.UserPrincipalLookupService的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: uyuni   文件: SaltActionChainGeneratorService.java
/**
 * Make sure the {@literal actionchains} subdir exists.
 * @return the {@link Path} of the {@literal} actionchains directory
 */
private Path createActionChainsDir() {
    Path targetDir = getTargetDir();
    if (!Files.exists(targetDir)) {
        try {
            Files.createDirectories(targetDir);
            if (!skipSetOwner) {
                FileSystem fileSystem = FileSystems.getDefault();
                UserPrincipalLookupService service = fileSystem.getUserPrincipalLookupService();
                UserPrincipal tomcatUser = service.lookupPrincipalByName("tomcat");
                Files.setOwner(targetDir, tomcatUser);
            }
        }
        catch (IOException e) {
            LOG.error("Could not create action chain directory " + targetDir, e);
            throw new RuntimeException(e);
        }
    }
    return targetDir;
}
 
源代码2 项目: jumbune   文件: CommandDelegatorMethods.java
/** This method changes the ownership of folder/file to the user provided
 * @param destinationLocation : the location of the path to be owned
 * @param user : the user which needs to own the folder/file
 */
private void changeOwnership(String destinationLocation, String user){
	
	Path path = Paths.get(destinationLocation);
    FileOwnerAttributeView view = Files.getFileAttributeView(path,
        FileOwnerAttributeView.class);
    UserPrincipalLookupService lookupService = FileSystems.getDefault()
        .getUserPrincipalLookupService();
    UserPrincipal userPrincipal;
	try {
		userPrincipal = lookupService.lookupPrincipalByName(user);
		Files.setOwner(path, userPrincipal);
	} catch (IOException e) {
		LOGGER.error("Error while changing ownership of destination location[" + destinationLocation +"]" + "to user[" + user + "]" , e);
	}
}
 
源代码3 项目: jimfs   文件: UserLookupServiceTest.java
@Test
public void testUserLookupService() throws IOException {
  UserPrincipalLookupService service = new UserLookupService(true);
  UserPrincipal bob1 = service.lookupPrincipalByName("bob");
  UserPrincipal bob2 = service.lookupPrincipalByName("bob");
  UserPrincipal alice = service.lookupPrincipalByName("alice");

  assertThat(bob1).isEqualTo(bob2);
  assertThat(bob1).isNotEqualTo(alice);

  GroupPrincipal group1 = service.lookupPrincipalByGroupName("group");
  GroupPrincipal group2 = service.lookupPrincipalByGroupName("group");
  GroupPrincipal foo = service.lookupPrincipalByGroupName("foo");

  assertThat(group1).isEqualTo(group2);
  assertThat(group1).isNotEqualTo(foo);
}
 
源代码4 项目: uyuni   文件: SCCRefreshLock.java
/**
 * tries to obtain the lock or throws an exception
 */
public static void tryGetLock() {
    try {
        f = new File(REFRESH_FILE_LOCKED_PATH);
        synchronized (f) {
            // create the lock file
            channel = new RandomAccessFile(f, "rw").getChannel();
            // create it with correct user
            FileSystem fileSystem = FileSystems.getDefault();
            UserPrincipalLookupService service = fileSystem.getUserPrincipalLookupService();
            UserPrincipal rootUser = service.lookupPrincipalByName("root");
            UserPrincipal tomcatUser = service.lookupPrincipalByName("tomcat");
            if (Files.getOwner(f.toPath(), LinkOption.NOFOLLOW_LINKS).equals(rootUser)) {
                Files.setOwner(f.toPath(), tomcatUser);
            }
            lock = channel.tryLock();
            if (lock == null) {
                // File is lock by other application
                channel.close();
                log.warn("SCC refresh is already running.");
                throw new OverlappingFileLockException();
            }
            log.info("Got the Lock for scc refresh");
            // Add on exit handler to release lock when application shutdown
            OnExitHandler onExitHandler = new OnExitHandler();
            Runtime.getRuntime().addShutdownHook(onExitHandler);
        }
    }
    catch (IOException e) {
        throw new RuntimeException("Could not start process.", e);
    }
}
 
源代码5 项目: uyuni   文件: SaltServerActionService.java
private void setFileOwner(Path path) throws IOException {
    FileSystem fileSystem = FileSystems.getDefault();
    UserPrincipalLookupService service = fileSystem.getUserPrincipalLookupService();
    UserPrincipal tomcatUser = service.lookupPrincipalByName("tomcat");

    Files.setOwner(path, tomcatUser);
}
 
源代码6 项目: vespa   文件: UnixPath.java
public UnixPath setOwner(String owner) {
    UserPrincipalLookupService service = path.getFileSystem().getUserPrincipalLookupService();
    UserPrincipal principal = uncheck(
            () -> service.lookupPrincipalByName(owner),
            "While looking up user %s", owner);
    uncheck(() -> Files.setOwner(path, principal));
    return this;
}
 
源代码7 项目: vespa   文件: UnixPath.java
public UnixPath setGroup(String group) {
    UserPrincipalLookupService service = path.getFileSystem().getUserPrincipalLookupService();
    GroupPrincipal principal = uncheck(
            () -> service.lookupPrincipalByGroupName(group),
            "while looking up group %s", group);
    uncheck(() -> Files.getFileAttributeView(path, PosixFileAttributeView.class).setGroup(principal));
    return this;
}
 
源代码8 项目: skUtilities   文件: SExprFileOwner.java
public void change(Event e, Object[] delta, Changer.ChangeMode mode) {
  if (mode == Changer.ChangeMode.SET) {
    Path pth = Paths.get(skUtilities.getDefaultPath(path.getSingle(e)));
    try {
      UserPrincipalLookupService lookupService = FileSystems.getDefault().getUserPrincipalLookupService();
      Files.setOwner(pth, lookupService.lookupPrincipalByName((String) delta[0]));
    } catch (Exception x) {
      skUtilities.prSysE("File: '" + pth + "' doesn't exist, or is not readable!", getClass().getSimpleName(), x);
    }
  }
}
 
源代码9 项目: yajsync   文件: UnixFileAttributeManager.java
private UserPrincipal getUserPrincipalFrom(String userName) throws IOException
{
    try {
        if (_isCacheEnabled) {
            return _nameToUserPrincipal.get(userName);
        }
        UserPrincipalLookupService service =
                FileSystems.getDefault().getUserPrincipalLookupService();
        return service.lookupPrincipalByName(userName);
    } catch (IOException | UnsupportedOperationException e) {
        return null;
    }
}
 
源代码10 项目: yajsync   文件: UnixFileAttributeManager.java
private GroupPrincipal getGroupPrincipalFrom(String groupName) throws IOException
{
    try {
        if (_isCacheEnabled) {
            return _nameToGroupPrincipal.get(groupName);
        }
        UserPrincipalLookupService service =
                FileSystems.getDefault().getUserPrincipalLookupService();
        return service.lookupPrincipalByGroupName(groupName);
    } catch (IOException | UnsupportedOperationException e) {
        return null;
    }
}
 
源代码11 项目: yajsync   文件: PosixFileAttributeManager.java
private UserPrincipal getUserPrincipalFrom(String userName) throws IOException
{
    try {
        UserPrincipal principal = _nameToUserPrincipal.get(userName);
        if (principal == null) {
            UserPrincipalLookupService service =
                    FileSystems.getDefault().getUserPrincipalLookupService();
            principal = service.lookupPrincipalByName(userName);
            _nameToUserPrincipal.put(userName, principal);
        }
        return principal;
    } catch (UnsupportedOperationException e) {
        throw new IOException(e);
    }
}
 
源代码12 项目: yajsync   文件: PosixFileAttributeManager.java
private GroupPrincipal getGroupPrincipalFrom(String groupName) throws IOException
{
    try {
        GroupPrincipal principal = _nameToGroupPrincipal.get(groupName);
        if (principal == null) {
            UserPrincipalLookupService service =
                    FileSystems.getDefault().getUserPrincipalLookupService();
            principal = service.lookupPrincipalByGroupName(groupName);
            _nameToGroupPrincipal.put(groupName, principal);
        }
        return principal;
    } catch (UnsupportedOperationException e) {
        throw new IOException(e);
    }
}
 
@Override
public UserPrincipal getOwner() throws IOException {
  UserPrincipalLookupService ls = this.path.getFileSystem()
      .getUserPrincipalLookupService();
  FileStatus fileStatus = path.getFileSystem().getHDFS()
      .getFileStatus(path.getRawResolvedPath());
  return ls.lookupPrincipalByName(fileStatus.getOwner());
}
 
/**
 * Test UserPrincipalLookupService support.
 * 
 * @throws IOException
 */
@Test
public void testGetPosixViewSetOwner() throws IOException {
  Path rootPath = Paths.get(clusterUri);

  UserPrincipalLookupService lus = rootPath.getFileSystem()
      .getUserPrincipalLookupService();
  assertNotNull(lus);
}
 
源代码15 项目: jimfs   文件: UserLookupServiceTest.java
@Test
public void testServiceNotSupportingGroups() throws IOException {
  UserPrincipalLookupService service = new UserLookupService(false);

  try {
    service.lookupPrincipalByGroupName("group");
    fail();
  } catch (UserPrincipalNotFoundException expected) {
    assertThat(expected.getName()).isEqualTo("group");
  }
}
 
源代码16 项目: logging-log4j2   文件: FileUtils.java
/**
 * Define file posix attribute view on a path/file.
 *
 * @param path Target path
 * @param filePermissions Permissions to apply
 * @param fileOwner File owner
 * @param fileGroup File group
 * @throws IOException If IO error during definition of file attribute view
 */
public static void defineFilePosixAttributeView(final Path path,
        final Set<PosixFilePermission> filePermissions,
        final String fileOwner,
        final String fileGroup) throws IOException {
    final PosixFileAttributeView view = Files.getFileAttributeView(path, PosixFileAttributeView.class);
    if (view != null) {
        final UserPrincipalLookupService lookupService = FileSystems.getDefault()
                .getUserPrincipalLookupService();
        if (fileOwner != null) {
            final UserPrincipal userPrincipal = lookupService.lookupPrincipalByName(fileOwner);
            if (userPrincipal != null) {
                // If not sudoers member, it will throw Operation not permitted
                // Only processes with an effective user ID equal to the user ID
                // of the file or with appropriate privileges may change the ownership of a file.
                // If _POSIX_CHOWN_RESTRICTED is in effect for path
                view.setOwner(userPrincipal);
            }
        }
        if (fileGroup != null) {
            final GroupPrincipal groupPrincipal = lookupService.lookupPrincipalByGroupName(fileGroup);
            if (groupPrincipal != null) {
                // The current user id should be members of this group,
                // if not will raise Operation not permitted
                view.setGroup(groupPrincipal);
            }
        }
        if (filePermissions != null) {
            view.setPermissions(filePermissions);
        }
    }
}
 
源代码17 项目: dragonwell8_jdk   文件: FaultyFileSystem.java
@Override
public UserPrincipalLookupService getUserPrincipalLookupService() {
    // assume that unwrapped objects aren't exposed
    return delegate.getUserPrincipalLookupService();
}
 
源代码18 项目: TencentKona-8   文件: FaultyFileSystem.java
@Override
public UserPrincipalLookupService getUserPrincipalLookupService() {
    // assume that unwrapped objects aren't exposed
    return delegate.getUserPrincipalLookupService();
}
 
源代码19 项目: uyuni   文件: SaltService.java
/**
 * {@inheritDoc}
 */
public Map<Boolean, String> storeMinionScapFiles(
        MinionServer minion, String uploadDir, Long actionId) {
    String actionPath = ScapFileManager
            .getActionPath(minion.getOrg().getId(),
                    minion.getId(), actionId);
    Path mountPoint = Paths.get(com.redhat.rhn.common.conf.Config.get()
            .getString(ConfigDefaults.MOUNT_POINT));
    try {
        // create dirs
        Path actionDir = Files.createDirectories(mountPoint.resolve(actionPath));

        UserPrincipalLookupService lookupService = FileSystems.getDefault()
                .getUserPrincipalLookupService();
        GroupPrincipal susemanagerGroup = lookupService
                .lookupPrincipalByGroupName("susemanager");
        GroupPrincipal wwwGroup = lookupService
                .lookupPrincipalByGroupName("www");
        // systems/<orgId>/<serverId>/actions/<actionId>
        changeGroupAndPerms(actionDir, susemanagerGroup);
        // systems/<orgId>/<serverId>/actions
        actionDir = actionDir.getParent();
        while (!actionDir.equals(mountPoint)) {
            changeGroupAndPerms(actionDir, wwwGroup);
            actionDir = actionDir.getParent();
        }

    }
    catch (IOException e) {
        LOG.error("Error creating dir " + mountPoint.resolve(actionPath), e);
    }

    RunnerCall<Map<Boolean, String>> call = MgrUtilRunner.moveMinionUploadedFiles(
            minion.getMinionId(),
            uploadDir,
            com.redhat.rhn.common.conf.Config.get()
                    .getString(ConfigDefaults.MOUNT_POINT),
            actionPath);
    Optional<Map<Boolean, String>> result = callSync(call,
            err -> err.fold(
                    e -> {
                        LOG.error("Function [" + e.getFunctionName() +
                                " not available for runner call " +
                                "[mgrutil.move_minion_uploaded_files].");
                        return Optional.of(Collections.singletonMap(false,
                                "Function [" + e.getFunctionName()));
                    },
                    e -> {
                        LOG.error("Module [" + e.getModuleName() +
                                "] not supported for runner call " +
                                "[mgrutil.move_minion_uploaded_files].");
                        return Optional.of(Collections.singletonMap(false,
                                "Module [" + e.getModuleName() + "] not supported"));
                    },
                    e -> {
                        LOG.error("Error parsing json response from " +
                                "runner call [mgrutil.move_minion_uploaded_files]: " +
                                e.getJson());
                        return Optional.of(Collections.singletonMap(false,
                                "Error parsing json response: " + e.getJson()));
                    },
                    e -> {
                        LOG.error("Generic Salt error for runner call " +
                                "[mgrutil.move_minion_uploaded_files]: " +
                                e.getMessage());
                        return Optional.of(Collections.singletonMap(false,
                                "Generic Salt error: " + e.getMessage()));
                    }
            )
    );
    return result.orElseGet(() ->
            Collections.singletonMap(false, "Error moving scap result files." +
                    " Please check the logs.")
    );
}
 
源代码20 项目: jdk8u60   文件: FaultyFileSystem.java
@Override
public UserPrincipalLookupService getUserPrincipalLookupService() {
    // assume that unwrapped objects aren't exposed
    return delegate.getUserPrincipalLookupService();
}
 
源代码21 项目: openjdk-jdk8u   文件: FaultyFileSystem.java
@Override
public UserPrincipalLookupService getUserPrincipalLookupService() {
    // assume that unwrapped objects aren't exposed
    return delegate.getUserPrincipalLookupService();
}
 
源代码22 项目: openjdk-systemtest   文件: MemoryFileSystem.java
@Override
public UserPrincipalLookupService getUserPrincipalLookupService() {
	return null;
}
 
源代码23 项目: openjdk-jdk8u-backup   文件: FaultyFileSystem.java
@Override
public UserPrincipalLookupService getUserPrincipalLookupService() {
    // assume that unwrapped objects aren't exposed
    return delegate.getUserPrincipalLookupService();
}
 
源代码24 项目: Bytecoder   文件: JrtFileSystem.java
@Override
public final UserPrincipalLookupService getUserPrincipalLookupService() {
    throw new UnsupportedOperationException();
}
 
源代码25 项目: openjdk-jdk9   文件: JrtFileSystem.java
@Override
public final UserPrincipalLookupService getUserPrincipalLookupService() {
    throw new UnsupportedOperationException();
}
 
源代码26 项目: openjdk-jdk9   文件: TestProvider.java
@Override
public UserPrincipalLookupService getUserPrincipalLookupService() {
    return delegate.getUserPrincipalLookupService();
}
 
源代码27 项目: openjdk-jdk9   文件: FaultyFileSystem.java
@Override
public UserPrincipalLookupService getUserPrincipalLookupService() {
    // assume that unwrapped objects aren't exposed
    return delegate.getUserPrincipalLookupService();
}
 
源代码28 项目: jdk8u-jdk   文件: FaultyFileSystem.java
@Override
public UserPrincipalLookupService getUserPrincipalLookupService() {
    // assume that unwrapped objects aren't exposed
    return delegate.getUserPrincipalLookupService();
}
 
源代码29 项目: hottub   文件: FaultyFileSystem.java
@Override
public UserPrincipalLookupService getUserPrincipalLookupService() {
    // assume that unwrapped objects aren't exposed
    return delegate.getUserPrincipalLookupService();
}
 
源代码30 项目: openjdk-8-source   文件: FaultyFileSystem.java
@Override
public UserPrincipalLookupService getUserPrincipalLookupService() {
    // assume that unwrapped objects aren't exposed
    return delegate.getUserPrincipalLookupService();
}
 
 类所在包
 同包方法