All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.github.dennisit.vplus.data.utils.SystemUtils Maven / Gradle / Ivy
/*--------------------------------------------------------------------------
* Copyright (c) 2010-2020, Elon.su All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of the elon developer nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* Author: Elon.su, you can also mail [email protected]
*--------------------------------------------------------------------------
*/
package com.github.dennisit.vplus.data.utils;
import com.spring.boxes.dollar.$;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.swing.text.html.Option;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.util.Enumeration;
import java.util.List;
import java.util.Optional;
/**
* Created by Elon.su on 17/5/13.
*/
@Slf4j
public class SystemUtils {
private static final Logger LOG = LoggerFactory.getLogger(SystemUtils.class);
public static String getOsName() {
return System.getProperty("os.name");
}
public static boolean isWindowsOS() {
return Optional.ofNullable(getOsName()).orElse(StringUtils.EMPTY).toLowerCase().indexOf("windows") > -1;
}
public static String getLocalIP() {
try {
return isWindowsOS() ? InetAddress.getLocalHost().getHostAddress() : getLinuxLocalIp();
} catch (UnknownHostException e) {
log.error(e.getLocalizedMessage(), e);
}
return StringUtils.EMPTY;
}
public static String getLocalHostName() {
try {
return InetAddress.getLocalHost().getHostName();
} catch (UnknownHostException e) {
log.error(e.getLocalizedMessage(), e);
}
return StringUtils.EMPTY;
}
private static String getLinuxLocalIp() {
String ip = "127.0.0.1";
try {
for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
NetworkInterface intf = en.nextElement();
String name = intf.getName();
if (!name.contains("docker") && !name.contains("lo")) {
for (Enumeration enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
String ipAddress = inetAddress.getHostAddress();
if (!ipAddress.contains("::") && !ipAddress.contains("0:0:") && !ipAddress.contains("fe80")) {
ip = ipAddress;
log.debug(ipAddress);
}
}
}
}
}
} catch (SocketException ex) {
log.debug("获取ip地址异常");
log.error(ex.getLocalizedMessage(), ex);
}
log.debug("IP:" + ip);
return ip;
}
@Deprecated
public static void shell(String script) {
Process process = null;
try {
String[] cmd = {"sh", script};
process = Runtime.getRuntime().exec(cmd);
process.waitFor();
} catch (Exception e) {
LOG.error(e.getMessage(), e);
} finally {
if (null != process) {
process.destroy();
}
}
}
/**
* 获取系统支持的字形名集合
*
* @return 字形名数组
*/
public static String[] getSupportFontNames() {
return GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
}
public static class MySQL {
public static void dump(String host, String user, String pass, java.util.List databases) {
dump(host, 3306, user, pass, databases, "/opt/backup/");
}
public static void dump(String host, int port, String user, String pass, java.util.List databases) {
dump(host, port, user, pass, databases, "/opt/backup/");
}
public static void dump(String host, String user, String pass, java.util.List databases, String backPath) {
dump(host, 3306, user, pass, databases, backPath);
}
public static void dump(String host, int port, String user, String pass, List databases, String backPath) {
if (StringUtils.isBlank(user) || StringUtils.isBlank(pass) || CollectionUtils.isEmpty(databases)) {
throw new IllegalArgumentException("参数缺失");
}
if (port <= 0) {
port = 3306;
}
if (StringUtils.isBlank(host)) {
host = "127.0.0.1:3306";
}
if (StringUtils.isBlank(backPath)) {
backPath = "/opt/backup/";
}
Process process = null;
try {
String fileName = backPath + "SQL-" + $.nowFormat("yyyyMMddHHmmss") + ".sql";
FileUtils.write(new File(fileName), "", StandardCharsets.UTF_8);
String dumpShell = "mysqldump -h{host} -P{port} -u{user} -p'{pass}' --default-character-set=utf8 --lock-tables=false --databases {databases} > {backPath}"
.replaceAll("\\{host\\}", host)
.replaceAll("\\{port\\}", String.valueOf(port))
.replaceAll("\\{user\\}", user)
.replaceAll("\\{pass\\}", pass)
.replaceAll("\\{databases\\}", StringUtils.join(databases, " "))
.replaceAll("\\{backPath\\}", fileName);
process = Runtime.getRuntime().exec(dumpShell);
process.waitFor();
} catch (Exception e) {
LOG.error(e.getMessage(), e);
} finally {
if (null != process) {
process.destroy();
}
}
}
}
}