Java Code Examples for com.sun.jna.Platform#isLinux()

The following examples show how to use com.sun.jna.Platform#isLinux() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CloudPlatform.java    From hivemq-community-edition with Apache License 2.0 6 votes vote down vote up
public String getCloudPlatform() {
    try {
        if (!Platform.isLinux()) {
            return null;
        }

        //can generate false positives, but seems to be the simplest and least intrusive solution
        //see https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/identify_ec2_instances.html
        final File uuidFile = getUuidFile();
        if (uuidFile.exists() && uuidFile.canRead()) {
            final String content = FileUtils.readFileToString(uuidFile, StandardCharsets.UTF_8);
            if (content.startsWith("ec2") || content.startsWith("EC2")) {
                return "AWS EC2";
            }
        }
    } catch (final Exception ex) {
        log.trace("not able to determine if running on cloud platform", ex);
    }

    return null;
}
 
Example 2
Source File: Processes.java    From Java-Memory-Manipulation with Apache License 2.0 6 votes vote down vote up
public static Process byId(int id) {
	if ((Platform.isMac() || Platform.isLinux()) && !isSudo()) {
		throw new RuntimeException("You need to run as root/sudo for unix/osx based environments.");
	}
	
	if (Platform.isWindows()) {
		return new Win32Process(id, Kernel32.OpenProcess(0x438, true, id));
	} else if (Platform.isLinux()) {
		return new UnixProcess(id);
	} else if (Platform.isMac()) {
		IntByReference out = new IntByReference();
		if (mac.task_for_pid(mac.mach_task_self(), id, out) != 0) {
			throw new IllegalStateException("Failed to find mach task port for process, ensure you are running as sudo.");
		}
		return new MacProcess(id, out.getValue());
	}
	throw new IllegalStateException("Process " + id + " was not found. Are you sure its running?");
}
 
Example 3
Source File: Processes.java    From Java-Memory-Manipulation with Apache License 2.0 6 votes vote down vote up
public static Process byName(String name) {
	if (Platform.isWindows()) {
		Tlhelp32.PROCESSENTRY32.ByReference entry = new Tlhelp32.PROCESSENTRY32.ByReference();
		Pointer snapshot = Kernel32.CreateToolhelp32Snapshot(Tlhelp32.TH32CS_SNAPALL.intValue(), 0);
		try {
			while (Kernel32.Process32NextW(snapshot, entry)) {
				String processName = Native.toString(entry.szExeFile);
				if (name.equals(processName)) {
					return byId(entry.th32ProcessID.intValue());
				}
			}
		} finally {
			Kernel32.CloseHandle(snapshot);
		}
	} else if (Platform.isMac() || Platform.isLinux()) {
		return byId(Utils.exec("bash", "-c", "ps -A | grep -m1 \"" + name + "\" | awk '{print $1}'"));
	} else {
		throw new UnsupportedOperationException("Unknown operating system! (" + System.getProperty("os.name") + ")");
	}
	throw new IllegalStateException("Process '" + name + "' was not found. Are you sure its running?");
}
 
Example 4
Source File: NativeCallsJNAImpl.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
private static final NativeCalls getImplInstance() {
  if (Platform.isLinux()) {
    return new LinuxNativeCalls();
  }
  if (Platform.isWindows()) {
    return new WinNativeCalls();
  }
  if (Platform.isSolaris()) {
    return new SolarisNativeCalls();
  }
  if (Platform.isMac()) {
    return new MacOSXNativeCalls();
  }
  if (Platform.isFreeBSD()) {
    return new FreeBSDNativeCalls();
  }
  return new POSIXNativeCalls();
}
 
Example 5
Source File: BaseTest.java    From mariadb-connector-j with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Check if server and client are on same host (not using containers).
 *
 * @return true if server and client are really on same host
 */
public boolean hasSameHost() {
  try {
    Statement st = sharedConnection.createStatement();
    ResultSet rs = st.executeQuery("select @@version_compile_os");
    if (rs.next()) {
      if ((rs.getString(1).contains("linux") && Platform.isWindows())
          || (rs.getString(1).contains("win") && Platform.isLinux())) {
        return false;
      }
    }
  } catch (SQLException sqle) {
    // eat
  }
  return true;
}
 
Example 6
Source File: NativeIOTestCase.java    From vespa with Apache License 2.0 6 votes vote down vote up
@Test
public void requireThatDropFileFromCacheDoesNotThrow() throws IOException {
    File testFile = new File("testfile");
    FileOutputStream output = new FileOutputStream(testFile);
    output.write('t');
    output.flush();
    output.close();
    NativeIO nativeIO = new NativeIO();
    if (Platform.isLinux()) {
        assertTrue(nativeIO.valid());
    } else {
        assertFalse(nativeIO.valid());
        assertEquals("Platform is unsúpported. Only supported on linux.", nativeIO.getError().getMessage());
    }
    nativeIO.dropFileFromCache(output.getFD());
    nativeIO.dropFileFromCache(testFile);
    testFile.delete();
    nativeIO.dropFileFromCache(new File("file.that.does.not.exist"));
}
 
Example 7
Source File: Processes.java    From Java-Memory-Manipulation with Apache License 2.0 6 votes vote down vote up
public static Process byId(int id) {
	if ((Platform.isMac() || Platform.isLinux()) && !isSudo()) {
		throw new RuntimeException("You need to run as root/sudo for unix/osx based environments.");
	}
	
	if (Platform.isWindows()) {
		return new Win32Process(id, Kernel32.OpenProcess(0x438, true, id));
	} else if (Platform.isLinux()) {
		return new UnixProcess(id);
	} else if (Platform.isMac()) {
		IntByReference out = new IntByReference();
		if (mac.task_for_pid(mac.mach_task_self(), id, out) != 0) {
			throw new IllegalStateException("Failed to find mach task port for process, ensure you are running as sudo.");
		}
		return new MacProcess(id, out.getValue());
	}
	throw new IllegalStateException("Process " + id + " was not found. Are you sure its running?");
}
 
Example 8
Source File: Processes.java    From Java-Memory-Manipulation with Apache License 2.0 6 votes vote down vote up
public static Process byName(String name) {
	if (Platform.isWindows()) {
		Tlhelp32.PROCESSENTRY32.ByReference entry = new Tlhelp32.PROCESSENTRY32.ByReference();
		Pointer snapshot = Kernel32.CreateToolhelp32Snapshot(Tlhelp32.TH32CS_SNAPALL.intValue(), 0);
		try {
			while (Kernel32.Process32NextW(snapshot, entry)) {
				String processName = Native.toString(entry.szExeFile);
				if (name.equals(processName)) {
					return byId(entry.th32ProcessID.intValue());
				}
			}
		} finally {
			Kernel32.CloseHandle(snapshot);
		}
	} else if (Platform.isMac() || Platform.isLinux()) {
		return byId(Utils.exec("bash", "-c", "ps -A | grep -m1 \"" + name + "\" | awk '{print $1}'"));
	} else {
		throw new UnsupportedOperationException("Unknown operating system! (" + System.getProperty("os.name") + ")");
	}
	throw new IllegalStateException("Process '" + name + "' was not found. Are you sure its running?");
}
 
Example 9
Source File: NativeCallsJNAImpl.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
private static final NativeCalls getImplInstance() {
  if (Platform.isLinux()) {
    return new LinuxNativeCalls();
  }
  if (Platform.isWindows()) {
    return new WinNativeCalls();
  }
  if (Platform.isSolaris()) {
    return new SolarisNativeCalls();
  }
  if (Platform.isMac()) {
    return new MacOSXNativeCalls();
  }
  if (Platform.isFreeBSD()) {
    return new FreeBSDNativeCalls();
  }
  return new POSIXNativeCalls();
}
 
Example 10
Source File: PtyUtil.java    From WebIDE-Backend with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private static String getPlatformFolder() {
    String result;

    if (Platform.isMac()) {
        result = "macosx";
    } else if (Platform.isWindows()) {
        result = "win";
    } else if (Platform.isLinux()) {
        result = "linux";
    } else if (Platform.isFreeBSD()) {
        result = "freebsd";
    } else if (Platform.isOpenBSD()) {
        result = "openbsd";
    } else {
        throw new IllegalStateException("Platform " + Platform.getOSType() + " is not supported");
    }

    return result;
}
 
Example 11
Source File: ContainerEnvironment.java    From hivemq-community-edition with Apache License 2.0 6 votes vote down vote up
public String getContainerEnvironment() {

        try {
            if (!Platform.isLinux()) {
                return null;
            }

            //a docker container always has the content "docker" in the cgroup file
            //see https://stackoverflow.com/questions/23513045/
            final File cgroupFile = getCgroupFile();
            if (cgroupFile.exists() && cgroupFile.canRead()) {
                final String content = FileUtils.readFileToString(cgroupFile, StandardCharsets.UTF_8);
                if (content.contains("docker")) {
                    return "Docker";
                }
            }
        } catch (final Exception ex) {
            log.trace("not able to determine if running in container", ex);
        }

        return null;
    }
 
Example 12
Source File: ProcessUtil.java    From datax-web with MIT License 6 votes vote down vote up
public static String getProcessId(Process process) {
    long pid = -1;
    Field field;
    if (Platform.isWindows()) {
        try {
            field = process.getClass().getDeclaredField("handle");
            field.setAccessible(true);
            pid = Kernel32.INSTANCE.GetProcessId((Long) field.get(process));
        } catch (Exception ex) {
            logger.error("get process id for windows error {0}", ex);
        }
    } else if (Platform.isLinux() || Platform.isAIX()) {
        try {
            Class<?> clazz = Class.forName("java.lang.UNIXProcess");
            field = clazz.getDeclaredField("pid");
            field.setAccessible(true);
            pid = (Integer) field.get(process);
        } catch (Throwable e) {
            logger.error("get process id for unix error {0}", e);
        }
    }
    return String.valueOf(pid);
}
 
Example 13
Source File: PtyUtil.java    From WebIDE-Backend with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static String getNativeLibraryName() {
    String result;

    if (Platform.isMac()) {
        result = "libpty.dylib";
    } else if (Platform.isWindows()) {
        result = "winpty.dll";
    } else if (Platform.isLinux() || Platform.isFreeBSD() || Platform.isOpenBSD()) {
        result = "libpty.so";
    } else {
        throw new IllegalStateException("Platform " + Platform.getOSType() + " is not supported");
    }

    return result;
}
 
Example 14
Source File: ScreenAccessor.java    From typometer with Apache License 2.0 5 votes vote down vote up
static ScreenAccessor create(boolean isNative) {
    if (isNative) {
        if (Platform.isWindows()) {
            return new WindowsScreenAccessor();
        }
        if (Platform.isLinux()) {
            return new LinuxScreenAccessor();
        }
    }
    return new AwtRobotScreenAccessor();
}
 
Example 15
Source File: LocalContainer.java    From sylph with Apache License 2.0 5 votes vote down vote up
public static String getProcessId(Process process)
{
    long pid = -1;
    Field field = null;
    if (Platform.isWindows()) {
        try {
            field = process.getClass().getDeclaredField("handle");
            field.setAccessible(true);
            pid = Kernel32.INSTANCE.getProcessId((Long) field.get(process));
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    else if (Platform.isLinux() || Platform.isAIX()) {
        try {
            Class<?> clazz = Class.forName("java.lang.UNIXProcess");
            field = clazz.getDeclaredField("pid");
            field.setAccessible(true);
            pid = (Integer) field.get(process);
        }
        catch (Throwable e) {
            e.printStackTrace();
        }
    }
    return String.valueOf(pid);
}
 
Example 16
Source File: IOSDeviceListenerService.java    From APICloud-Studio with GNU General Public License v3.0 5 votes vote down vote up
public void run() {
	ANBImplement.getInstance().listen();
	while ((!(Thread.interrupted())) && (Platform.isLinux())) {
		try {
			Thread.sleep(5000L);
		} catch (Exception ignored) {
		}
	}
}
 
Example 17
Source File: CLibrary.java    From keycloak with Apache License 2.0 5 votes vote down vote up
private static CLibrary init() {
    if (Platform.isMac() || Platform.isOpenBSD()) {
        return (CLibrary) Native.loadLibrary("c", BSDCLibrary.class);
    } else if (Platform.isFreeBSD()) {
        return (CLibrary) Native.loadLibrary("c", FreeBSDCLibrary.class);
    } else if (Platform.isSolaris()) {
        return (CLibrary) Native.loadLibrary("c", SolarisCLibrary.class);
    } else if (Platform.isLinux()) {
        return (CLibrary) Native.loadLibrary("c", LinuxCLibrary.class);
    } else {
        return (CLibrary) Native.loadLibrary("c", CLibrary.class);
    }
}
 
Example 18
Source File: ScreenAccessor.java    From typometer with Apache License 2.0 4 votes vote down vote up
static boolean isNativeApiSupported() {
    return Platform.isWindows() || Platform.isLinux();
}