Skip to content

Commit a3810e1

Browse files
author
yangzhao
committed
新增PlatformUtil和CommandUtil
1 parent e4ab453 commit a3810e1

File tree

2 files changed

+287
-0
lines changed

2 files changed

+287
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.yz.common.core.utils;
2+
3+
import java.io.BufferedReader;
4+
import java.io.InputStream;
5+
import java.io.InputStreamReader;
6+
7+
import org.slf4j.Logger;
8+
import org.slf4j.LoggerFactory;
9+
10+
/**
11+
* @author: yangzhao
12+
* @Date: 2020-01-09 12:55
13+
* @Description:
14+
*/
15+
public class CommandUtil {
16+
17+
private static final Logger logger = LoggerFactory.getLogger(CommandUtil.class);
18+
19+
public static String exec(String[] cmd) throws Exception {
20+
Process exec = Runtime.getRuntime().exec(cmd);
21+
exec.waitFor();
22+
InputStream inputStream = exec.getInputStream();
23+
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
24+
String readLine;
25+
StringBuilder resultBuilder = new StringBuilder();
26+
while ((readLine = br.readLine()) != null) {
27+
resultBuilder.append(readLine);
28+
}
29+
logger.info("command result is " + resultBuilder.toString());
30+
return resultBuilder.toString();
31+
}
32+
}
Lines changed: 255 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,255 @@
1+
package com.yz.common.core.utils;
2+
3+
import java.io.File;
4+
import java.io.FileInputStream;
5+
import java.io.IOException;
6+
import java.io.InputStream;
7+
import java.net.MalformedURLException;
8+
import java.net.URL;
9+
import java.security.AccessController;
10+
import java.security.PrivilegedAction;
11+
import java.util.Properties;
12+
13+
/**
14+
* @author: yangzhao
15+
* @Date: 2020-01-07 18:57
16+
* @Description:
17+
*/
18+
public class PlatformUtil {
19+
20+
// NOTE: since this class can be initialized by application code in some
21+
// cases, we must encapsulate all calls to System.getProperty("...") in
22+
// a doPrivileged block except for standard JVM properties such as
23+
// os.name, os.version, os.arch, java.vm.name, etc.
24+
25+
private static final String os = System.getProperty("os.name");
26+
private static final String version = System.getProperty("os.version");
27+
private static final boolean embedded;
28+
private static final String embeddedType;
29+
private static final boolean useEGL;
30+
private static final boolean doEGLCompositing;
31+
// a property used to denote a non-default impl for this host
32+
private static String javafxPlatform;
33+
34+
static {
35+
javafxPlatform =
36+
AccessController.doPrivileged((PrivilegedAction<String>)() -> System.getProperty("javafx.platform"));
37+
loadProperties();
38+
embedded = AccessController
39+
.doPrivileged((PrivilegedAction<Boolean>)() -> Boolean.getBoolean("com.sun.javafx.isEmbedded"));
40+
embeddedType = AccessController.doPrivileged((PrivilegedAction<String>)() -> System.getProperty("embedded"));
41+
useEGL = AccessController.doPrivileged((PrivilegedAction<Boolean>)() -> Boolean.getBoolean("use.egl"));
42+
if (useEGL) {
43+
doEGLCompositing =
44+
AccessController.doPrivileged((PrivilegedAction<Boolean>)() -> Boolean.getBoolean("doNativeComposite"));
45+
} else
46+
doEGLCompositing = false;
47+
}
48+
49+
private static final boolean ANDROID =
50+
"android".equals(javafxPlatform) || "Dalvik".equals(System.getProperty("java.vm.name"));
51+
private static final boolean WINDOWS = os.startsWith("Windows");
52+
private static final boolean WINDOWS_VISTA_OR_LATER = WINDOWS && versionNumberGreaterThanOrEqualTo(6.0f);
53+
private static final boolean WINDOWS_7_OR_LATER = WINDOWS && versionNumberGreaterThanOrEqualTo(6.1f);
54+
private static final boolean MAC = os.startsWith("Mac");
55+
private static final boolean LINUX = os.startsWith("Linux") && !ANDROID;
56+
private static final boolean SOLARIS = os.startsWith("SunOS");
57+
private static final boolean IOS = os.startsWith("iOS");
58+
59+
/**
60+
* Utility method used to determine whether the version number as reported by system properties is greater than or
61+
* equal to a given value.
62+
*
63+
* @param value
64+
* The value to test against.
65+
* @return false if the version number cannot be parsed as a float, otherwise the comparison against value.
66+
*/
67+
private static boolean versionNumberGreaterThanOrEqualTo(float value) {
68+
try {
69+
return Float.parseFloat(version) >= value;
70+
} catch (Exception e) {
71+
return false;
72+
}
73+
}
74+
75+
/**
76+
* Returns true if the operating system is a form of Windows.
77+
*/
78+
public static boolean isWindows() {
79+
return WINDOWS;
80+
}
81+
82+
/**
83+
* Returns true if the operating system is at least Windows Vista(v6.0).
84+
*/
85+
public static boolean isWinVistaOrLater() {
86+
return WINDOWS_VISTA_OR_LATER;
87+
}
88+
89+
/**
90+
* Returns true if the operating system is at least Windows 7(v6.1).
91+
*/
92+
public static boolean isWin7OrLater() {
93+
return WINDOWS_7_OR_LATER;
94+
}
95+
96+
/**
97+
* Returns true if the operating system is a form of Mac OS.
98+
*/
99+
public static boolean isMac() {
100+
return MAC;
101+
}
102+
103+
/**
104+
* Returns true if the operating system is a form of Linux.
105+
*/
106+
public static boolean isLinux() {
107+
return LINUX;
108+
}
109+
110+
public static boolean useEGL() {
111+
return useEGL;
112+
}
113+
114+
public static boolean useEGLWindowComposition() {
115+
return doEGLCompositing;
116+
}
117+
118+
public static boolean useGLES2() {
119+
String useGles2 = "false";
120+
useGles2 = AccessController.doPrivileged((PrivilegedAction<String>)() -> System.getProperty("use.gles2"));
121+
if ("true".equals(useGles2))
122+
return true;
123+
else
124+
return false;
125+
}
126+
127+
/**
128+
* Returns true if the operating system is a form of Unix, including Linux.
129+
*/
130+
public static boolean isSolaris() {
131+
return SOLARIS;
132+
}
133+
134+
/**
135+
* Returns true if the operating system is a form of Linux or Solaris
136+
*/
137+
public static boolean isUnix() {
138+
return LINUX || SOLARIS;
139+
}
140+
141+
/**
142+
* Returns true if the platform is embedded.
143+
*/
144+
public static boolean isEmbedded() {
145+
return embedded;
146+
}
147+
148+
/**
149+
* Returns a string with the embedded type - ie eglx11, eglfb, dfb or null.
150+
*/
151+
public static String getEmbeddedType() {
152+
return embeddedType;
153+
}
154+
155+
/**
156+
* Returns true if the operating system is iOS
157+
*/
158+
public static boolean isIOS() {
159+
return IOS;
160+
}
161+
162+
private static void loadPropertiesFromFile(final File file) {
163+
Properties p = new Properties();
164+
try {
165+
InputStream in = new FileInputStream(file);
166+
p.load(in);
167+
in.close();
168+
} catch (IOException e) {
169+
e.printStackTrace();
170+
}
171+
if (javafxPlatform == null) {
172+
javafxPlatform = p.getProperty("javafx.platform");
173+
}
174+
String prefix = javafxPlatform + ".";
175+
int prefixLength = prefix.length();
176+
boolean foundPlatform = false;
177+
for (Object o : p.keySet()) {
178+
String key = (String)o;
179+
if (key.startsWith(prefix)) {
180+
foundPlatform = true;
181+
String systemKey = key.substring(prefixLength);
182+
if (System.getProperty(systemKey) == null) {
183+
String value = p.getProperty(key);
184+
System.setProperty(systemKey, value);
185+
}
186+
}
187+
}
188+
if (!foundPlatform) {
189+
System.err.println("Warning: No settings found for javafx.platform='" + javafxPlatform + "'");
190+
}
191+
}
192+
193+
/**
194+
* Returns the directory containing the JavaFX runtime, or null if the directory cannot be located
195+
*/
196+
private static File getRTDir() {
197+
try {
198+
String theClassFile = "PlatformUtil.class";
199+
Class theClass = com.sun.javafx.PlatformUtil.class;
200+
URL url = theClass.getResource(theClassFile);
201+
if (url == null)
202+
return null;
203+
String classUrlString = url.toString();
204+
if (!classUrlString.startsWith("jar:file:") || classUrlString.indexOf('!') == -1) {
205+
return null;
206+
}
207+
// Strip out the "jar:" and everything after and including the "!"
208+
String s = classUrlString.substring(4, classUrlString.lastIndexOf('!'));
209+
// Strip everything after the last "/" or "\" to get rid of the jar filename
210+
int lastIndexOfSlash = Math.max(s.lastIndexOf('/'), s.lastIndexOf('\\'));
211+
return new File(new URL(s.substring(0, lastIndexOfSlash + 1)).getPath()).getParentFile();
212+
} catch (MalformedURLException e) {
213+
return null;
214+
}
215+
}
216+
217+
private static void loadProperties() {
218+
final String vmname = System.getProperty("java.vm.name");
219+
final String arch = System.getProperty("os.arch");
220+
221+
if (!(javafxPlatform != null || (arch != null && arch.equals("arm"))
222+
|| (vmname != null && vmname.indexOf("Embedded") > 0))) {
223+
return;
224+
}
225+
AccessController.doPrivileged((PrivilegedAction<Void>)() -> {
226+
final File rtDir = getRTDir();
227+
final String propertyFilename = "javafx.platform.properties";
228+
File rtProperties = new File(rtDir, propertyFilename);
229+
// First look for javafx.platform.properties in the JavaFX runtime
230+
// Then in the installation directory of the JRE
231+
if (rtProperties.exists()) {
232+
loadPropertiesFromFile(rtProperties);
233+
return null;
234+
}
235+
String javaHome = System.getProperty("java.home");
236+
File javaHomeProperties = new File(javaHome, "lib" + File.separator + propertyFilename);
237+
if (javaHomeProperties.exists()) {
238+
loadPropertiesFromFile(javaHomeProperties);
239+
return null;
240+
}
241+
242+
String javafxRuntimePath = System.getProperty("javafx.runtime.path");
243+
File javafxRuntimePathProperties = new File(javafxRuntimePath, File.separator + propertyFilename);
244+
if (javafxRuntimePathProperties.exists()) {
245+
loadPropertiesFromFile(javafxRuntimePathProperties);
246+
return null;
247+
}
248+
return null;
249+
});
250+
}
251+
252+
public static boolean isAndroid() {
253+
return ANDROID;
254+
}
255+
}

0 commit comments

Comments
 (0)