com.ovea.tadjin.util.SystemInfo Maven / Gradle / Ivy
The newest version!
/**
* Copyright (C) 2011 Ovea
*
* 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 com.ovea.tadjin.util;
import java.io.File;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.net.*;
import java.util.Enumeration;
import java.util.Properties;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public final class SystemInfo {
private SystemInfo() {
}
public static final String JAR_DIRECTORY = "systeminfo.jardir";
public static final String WORKING_DIRECTORY = "systeminfo.workdir";
public static final String OS_FAMILY = "systeminfo.osfamily";
public static final String HOSTNAME = "systeminfo.hostname";
public static final String IP = "systeminfo.ip";
public static final String PID = "systeminfo.pid";
private static final Pattern DIGITS = Pattern.compile("(\\d+)");
public static long getPid() {
try {
String name = ManagementFactory.getRuntimeMXBean().getName();
Matcher m = DIGITS.matcher(name);
m.find();
return Integer.parseInt(m.group());
} catch (Exception e) {
return 0;
}
}
public static File getJarDirectory() {
File file = getContainerFile(SystemInfo.class);
try {
return file.isDirectory() ? file.getCanonicalFile() : file.getParentFile().getCanonicalFile();
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}
public static File getWorkingDirectory() {
try {
return new File(".").getCanonicalFile();
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
public static OsFamily getOsFamily() {
return OsFamily.get();
}
public static String getHostnames() {
try {
return InetAddress.getLocalHost().getHostName();
} catch (UnknownHostException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
public static String[] getIpAddresses() {
try {
TreeSet ips = new TreeSet();
TreeSet locals = new TreeSet();
Enumeration ifs = NetworkInterface.getNetworkInterfaces();
while (ifs.hasMoreElements()) {
NetworkInterface ni = ifs.nextElement();
Enumeration adds = ni.getInetAddresses();
while (adds.hasMoreElements()) {
InetAddress add = adds.nextElement();
if (add.isLoopbackAddress())
locals.add(add.getHostAddress());
else
ips.add(add.getHostAddress());
}
}
return ips.isEmpty() ? locals.toArray(new String[locals.size()]) : ips.toArray(new String[ips.size()]);
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}
public static Properties asProperties() {
Properties p = new Properties();
p.setProperty(JAR_DIRECTORY, getJarDirectory().getAbsolutePath());
p.setProperty(WORKING_DIRECTORY, getWorkingDirectory().getAbsolutePath());
p.setProperty(OS_FAMILY, getOsFamily().getName());
p.setProperty(HOSTNAME, getHostnames());
p.setProperty(IP, asString(getIpAddresses()));
p.setProperty(PID, "" + getPid());
return p;
}
private static String asString(String[] array) {
if (array.length == 0) return "";
if (array.length == 1) return array[0];
String str = array[0];
for (int i = 1; i < array.length; i++)
str = str + "," + array[i];
return str;
}
private static File getContainerFile(String resource, ClassLoader cl) {
URL u = getContainer(resource, cl);
try {
File container = new File(u.toURI());
if (container.exists())
return container;
} catch (URISyntaxException e) {
throw new IllegalArgumentException("not a file: " + e.getMessage(), e);
}
throw new IllegalArgumentException("not a file");
}
private static URL getContainer(String resource, ClassLoader cl) {
final URL resUrl = cl.getResource(resource);
if (resUrl == null) {
throw new IllegalArgumentException("Resource not found: " + resource);
}
final StringBuilder url = new StringBuilder(resUrl.toExternalForm());
try {
if ("jar".equalsIgnoreCase(resUrl.getProtocol())) {
url.delete(0, 4).delete(url.indexOf(resource) - 2, url.length());
} else if ("file".equalsIgnoreCase(resUrl.getProtocol())) {
url.delete(url.indexOf(resource), url.length());
} else {
throw new UnsupportedOperationException("Cannot get container for resource [" + resource + "]. Unsupported URL protocol [" + resUrl.getProtocol() + "].");
}
return new URL(url.toString());
}
catch (MalformedURLException e) {
throw new UnsupportedOperationException("Cannot get container for resource [" + resource + "]. Malformed URL [" + url + "].");
}
}
private static File getContainerFile(Class c) {
return getContainerFile(c.getName().replaceAll("\\.", "/") + ".class", c.getClassLoader());
}
}