翻译:高级工具:使用Java本机访问
2017-07-13 00:00
330 查看
原文:Advanced tools: playing with Java Native Access - Layer4
Advanced tools: playing with Java Native Access
高级工具:使用Java本机访问
By Vincent DEVILLERS Java October 17, 2016
This post results from a recent deep diving in the source code of Elasticsearch, which uses JNA mainly for memory management when configuring the mlockall. I will present you how to use JNA in a very simple example: how to check the user who has launched the JVM.
这篇文章是最近对Elasticsearch的源代码深入研究的总结,它主要使用JNA进行内存管理。
这篇文章的结果是最近对Elasticsearch的源代码中进行了深入的研究。在配置mlockall时,Elasticsearch主要使用JNA进行内存管理。这里将向您展示如何在一个简单的示例中使用JNA:如何检查启动JVM的用户。
Note
Remember that when you are thinking about solution using OS native calls, you must deal and depend with platform librairies. So use it carefully and only for specific needs.
请记住,当你使用本机操作系统原生调用来考虑解决方案时,必须依赖于平台应用库进行处理,请针对特定的环境谨慎地使用。
First of all, we need JNA:
首先,需要JNA
Now let’s encapsulate the JNA calls in 2 dedicated classes, a specific JNA class using the C library and a facade for all native calls using JNA:
现在,将JNA封装在两个专用类中,一个使用C代码库的JNA类,以及一个使用JNA类来触发系统原生调用的门面类(facade)。
Now with a simple test:
简单的测试用例
When running this test with my profile:
使用devil和root两个用户来执行测试用例
Credits:
– by Markus Spiske, licensed under CC0 1.0
Advanced tools: playing with Java Native Access
高级工具:使用Java本机访问
By Vincent DEVILLERS Java October 17, 2016
This post results from a recent deep diving in the source code of Elasticsearch, which uses JNA mainly for memory management when configuring the mlockall. I will present you how to use JNA in a very simple example: how to check the user who has launched the JVM.
这篇文章是最近对Elasticsearch的源代码深入研究的总结,它主要使用JNA进行内存管理。
这篇文章的结果是最近对Elasticsearch的源代码中进行了深入的研究。在配置mlockall时,Elasticsearch主要使用JNA进行内存管理。这里将向您展示如何在一个简单的示例中使用JNA:如何检查启动JVM的用户。
Note
Remember that when you are thinking about solution using OS native calls, you must deal and depend with platform librairies. So use it carefully and only for specific needs.
请记住,当你使用本机操作系统原生调用来考虑解决方案时,必须依赖于平台应用库进行处理,请针对特定的环境谨慎地使用。
First of all, we need JNA:
首先,需要JNA
<dependency> <groupId>net.java.dev.jna</groupId> <artifactId>jna</artifactId> <optional>true</optional> <version>3.2.3</version> </dependency>
Now let’s encapsulate the JNA calls in 2 dedicated classes, a specific JNA class using the C library and a facade for all native calls using JNA:
现在,将JNA封装在两个专用类中,一个使用C代码库的JNA类,以及一个使用JNA类来触发系统原生调用的门面类(facade)。
public final class JNANatives { /** no instantiation */ private JNANatives() {} /** * Returns true if user is root, false if not, or if we don't know */ public static boolean isRunningAsRoot() { return getUid() == 0; } public static int getUid() { if (System.getProperty("os.name").startsWith("Windows")) { return -1; // don't know } try { return JNACLibrary.geteuid(); } catch (UnsatisfiedLinkError e) { return -1; } } }
import com.sun.jna.Native; import lombok.extern.slf4j.Slf4j; @Slf4j public static final class JNACLibrary { static { try { Native.register("c"); } catch (UnsatisfiedLinkError e) { log.warn("Unable to link C library, native methods (geteuid) are disabled.", e); } } static native int geteuid(); private JNACLibrary() { } }
Now with a simple test:
简单的测试用例
public class RootTest { @Test public void test() { System.err.println("Running with uid: " + JNANatives.getUid() + " (is root? -> " + JNANatives.isRunningAsRoot() + ")"); Assert.assertFalse(JNANatives.isRunningAsRoot()); } }
When running this test with my profile:
使用devil和root两个用户来执行测试用例
$ whoami devil $ cat /etc/passwd | grep devil devil:x:1000:1000:devil,,,:/home/devil:/usr/bin/zsh the result is: Running with uid: 1000 (is root? -> false) $ whoami root $ cat /etc/passwd | grep root root:x:0:0:root:/root:/bin/zsh the result is: Running with uid: 0 (is root? -> true)
Credits:
– by Markus Spiske, licensed under CC0 1.0
相关文章推荐
- 翻译:高级工具:使用Java本机访问
- Java中REST API使用示例——基于云平台+云服务打造自己的在线翻译工具
- Spring - Java/J2EE Application Framework 应用框架 第 11 章 使用ORM工具进行数据访问
- 【java工具】使用jdbc访问数据库获取某个存储过程信息及下面参数信息
- 小工具-使用java SSL通过url获得访问转为String-http/https
- Java爬虫入门简介(四)——抓包工具的使用以及使用HttpClient模拟用户登录的访问
- 使用 Java 5 RowSet 新特性访问 IBM DB2 数据库
- ※jakarta-ant的使用(java编译工具)※
- jakarta-ant的使用(java编译工具)
- [原创]JAVA 开发工具Jcreator使用技巧总结
- 使用Java混淆工具yguard
- jakarta-ant的使用(java编译工具)
- 【转载】JAVA 开发工具Jcreator使用技巧总结
- [原创]Java中,使用JDBC访问Oracle与SQL Server时的区别
- C Java PHP Perl Python 的程序代码美化工具(Pretty Print Program/Source Code Beautifier)使用
- 使用 ADO.NET 和 Oracle 进行高级数据访问
- Java高级编程:使用打印服务API
- jakarta-ant的使用(java编译工具)
- [翻译] 第四章,高级串口编程 - 使用 ioctl 和 select
- 使用hibernate扩展工具Hbm2JavaTask根据配置文件生成持久化对象类(2.1.2)