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

oshi.util.UserGroupInfo Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2022 The OSHI Project Contributors
 * SPDX-License-Identifier: MIT
 */
package oshi.util;

import static oshi.util.Memoizer.memoize;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;

import com.sun.jna.Platform;

import oshi.annotation.concurrent.ThreadSafe;

/**
 * Utility class to temporarily cache the userID and group maps in *nix, for parsing process ownership. Cache expires
 * after one minute.
 */
@ThreadSafe
public final class UserGroupInfo {

    // Temporarily cache users and groups, update each minute
    private static final Supplier> USERS_ID_MAP = memoize(UserGroupInfo::getUserMap,
            TimeUnit.MINUTES.toNanos(1));
    private static final Supplier> GROUPS_ID_MAP = memoize(UserGroupInfo::getGroupMap,
            TimeUnit.MINUTES.toNanos(1));

    private UserGroupInfo() {
    }

    /**
     * Gets a user from their ID
     *
     * @param userId a user ID
     * @return a pair containing that user id as the first element and the user name as the second
     */
    public static String getUser(String userId) {
        return USERS_ID_MAP.get().getOrDefault(userId, Constants.UNKNOWN);
    }

    /**
     * Gets the group name for a given ID
     *
     * @param groupId a {@link java.lang.String} object.
     * @return a {@link java.lang.String} object.
     */
    public static String getGroupName(String groupId) {
        return GROUPS_ID_MAP.get().getOrDefault(groupId, Constants.UNKNOWN);
    }

    private static Map getUserMap() {
        HashMap userMap = new HashMap<>();
        List passwd;
        if (Platform.isAIX()) {
            passwd = FileUtil.readFile("/etc/passwd");
        } else {
            passwd = ExecutingCommand.runNative("getent passwd");
        }
        // see man 5 passwd for the fields
        for (String entry : passwd) {
            String[] split = entry.split(":");
            if (split.length > 2) {
                String userName = split[0];
                String uid = split[2];
                // it is allowed to have multiple entries for the same userId,
                // we use the first one
                userMap.putIfAbsent(uid, userName);
            }
        }
        return userMap;
    }

    private static Map getGroupMap() {
        Map groupMap = new HashMap<>();
        List group;
        if (Platform.isAIX()) {
            group = FileUtil.readFile("/etc/group");
        } else {
            group = ExecutingCommand.runNative("getent group");
        }
        // see man 5 group for the fields
        for (String entry : group) {
            String[] split = entry.split(":");
            if (split.length > 2) {
                String groupName = split[0];
                String gid = split[2];
                groupMap.putIfAbsent(gid, groupName);
            }
        }
        return groupMap;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy