org.dromara.hutool.extra.management.UserInfo Maven / Gradle / Ivy
/*
* Copyright (c) 2013-2024 Hutool Team and hutool.cn
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.dromara.hutool.extra.management;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.core.util.SystemUtil;
import java.io.File;
import java.io.Serializable;
/**
* 代表当前用户的信息。
*/
public class UserInfo implements Serializable{
private static final long serialVersionUID = 1L;
private final String USER_NAME;
private final String USER_HOME;
private final String USER_DIR;
private final String JAVA_IO_TMPDIR;
private final String USER_LANGUAGE;
private final String USER_COUNTRY;
/**
* 构造
*/
public UserInfo(){
USER_NAME = SystemUtil.get("user.name", false);
USER_HOME = fixPath(SystemUtil.get("user.home", false));
USER_DIR = fixPath(SystemUtil.get("user.dir", false));
JAVA_IO_TMPDIR = fixPath(SystemUtil.get("java.io.tmpdir", false));
USER_LANGUAGE = SystemUtil.get("user.language", false);
// JDK1.4 {@code user.country},JDK1.2 {@code user.region}
String userCountry = SystemUtil.get("user.country", false);
if(null == userCountry){
userCountry = SystemUtil.get("user.region", false);
}
USER_COUNTRY = userCountry;
}
/**
* 取得当前登录用户的名字(取自系统属性:{@code user.name})。
*
*
* 例如:{@code "admin"}
*
*
* @return 属性值,如果不能取得(因为Java安全限制)或值不存在,则返回{@code null}。
*
* @since Java 1.1
*/
public final String getName() {
return USER_NAME;
}
/**
* 取得当前登录用户的home目录(取自系统属性:{@code user.home})。
*
*
* 例如:{@code "/home/admin/"}
*
*
* @return 属性值,如果不能取得(因为Java安全限制)或值不存在,则返回{@code null}。
*
* @since Java 1.1
*/
public final String getHomeDir() {
return USER_HOME;
}
/**
* 取得当前目录(取自系统属性:{@code user.dir})。
*
*
* 例如:{@code "/home/admin/working/"}
*
*
* @return 属性值,如果不能取得(因为Java安全限制)或值不存在,则返回{@code null}。
*
* @since Java 1.1
*/
public final String getCurrentDir() {
return USER_DIR;
}
/**
* 取得临时目录(取自系统属性:{@code java.io.tmpdir})。
*
*
* 例如:{@code "/tmp/"}
*
*
* @return 属性值,如果不能取得(因为Java安全限制)或值不存在,则返回{@code null}。
*
*
*/
public final String getTempDir() {
return JAVA_IO_TMPDIR;
}
/**
* 取得当前登录用户的语言设置(取自系统属性:{@code user.language})。
*
*
* 例如:{@code "zh"}、{@code "en"}等
*
*
* @return 属性值,如果不能取得(因为Java安全限制)或值不存在,则返回{@code null}。
*
*/
public final String getLanguage() {
return USER_LANGUAGE;
}
/**
* 取得当前登录用户的国家或区域设置(取自系统属性:JDK1.4 {@code user.country}或JDK1.2 {@code user.region})。
*
*
* 例如:{@code "CN"}、{@code "US"}等
*
*
* @return 属性值,如果不能取得(因为Java安全限制)或值不存在,则返回{@code null}。
*
*/
public final String getCountry() {
return USER_COUNTRY;
}
/**
* 将当前用户的信息转换成字符串。
*
* @return 用户信息的字符串表示
*/
@Override
public final String toString() {
final StringBuilder builder = new StringBuilder();
ManagementUtil.append(builder, "User Name: ", getName());
ManagementUtil.append(builder, "User Home Dir: ", getHomeDir());
ManagementUtil.append(builder, "User Current Dir: ", getCurrentDir());
ManagementUtil.append(builder, "User Temp Dir: ", getTempDir());
ManagementUtil.append(builder, "User Language: ", getLanguage());
ManagementUtil.append(builder, "User Country: ", getCountry());
return builder.toString();
}
/**
* 修正路径,包括:
*
*
* - 1. 末尾补充 /
*
* @param path 路径
* @return 修正后的路径
* @since 5.6.4
*/
private static String fixPath(final String path){
return StrUtil.addSuffixIfNot(path, File.separator);
}
}