Skip to content

Commit 9b49d65

Browse files
committed
add dns-cache-manipulator-tool implementation alibaba#24
1 parent 1db2c6a commit 9b49d65

File tree

5 files changed

+334
-0
lines changed

5 files changed

+334
-0
lines changed

pom.xml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>com.alibaba</groupId>
6+
<artifactId>dns-cache-manipulator-parent</artifactId>
7+
<version>1.4.0-SNAPSHOT</version>
8+
<packaging>pom</packaging>
9+
<name>${project.artifactId}</name>
10+
<inceptionYear>2015</inceptionYear>
11+
12+
<licenses>
13+
<license>
14+
<name>Apache 2</name>
15+
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
16+
<distribution>repo</distribution>
17+
<comments>A business-friendly OSS license</comments>
18+
</license>
19+
</licenses>
20+
21+
<modules>
22+
<module>library</module>
23+
<module>tool</module>
24+
</modules>
25+
26+
<developers>
27+
<developer>
28+
<name>Jerry Lee</name>
29+
<id>oldratlee</id>
30+
<email>oldratlee(AT)gmail(DOT)com</email>
31+
<roles>
32+
<role>Developer</role>
33+
</roles>
34+
<timezone>+8</timezone>
35+
</developer>
36+
</developers>
37+
</project>

tool/README.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
Java Dns Cache Manipulator Tool
2+
=================================
3+
4+
:point_right: 修改/查看 运行中`JVM`进程的`DNS Cache`
5+
6+
:wrench: 功能
7+
=================================
8+
9+
- 设置/重置`DNS`
10+
- 查看`DNS Cache`内容
11+
- 删除一条`DNS Cache`(即重新`Lookup DNS`
12+
- 清空`DNS Cache`(即所有的域名重新`Lookup DNS`
13+
- 修改/查看`JVM`缺省的`DNS`的缓存时间
14+
15+
:busts_in_silhouette: User Guide
16+
=================================
17+
18+
下载
19+
----------
20+
21+
运行脚本`dcm.sh`/`dcm.bat`,执行操作。
22+
23+
```bash
24+
$ ./dcm.sh -h
25+
usage: Options
26+
-a,--action <arg> action
27+
-h,--help show help
28+
-p,--pid <arg> java process id to attach
29+
```
30+
31+
设置/重置`DNS`
32+
---------------
33+
34+
```bash
35+
$ ./dcm.sh -p 12345 set baidu.com 1.1.1.1
36+
```
37+
38+
查看`DNS Cache`内容
39+
---------------
40+
41+
查看单条
42+
43+
```bash
44+
$ ./dcm.sh -p 12345 get baidu.com
45+
```
46+
47+
查看全部
48+
49+
```bash
50+
$ ./dcm.sh -p 12345 list
51+
```
52+
53+
清空`DNS Cache`
54+
---------------
55+
56+
```bash
57+
$ ./dcm.sh -p 12345 clear
58+
```
59+
60+
修改/查看`JVM`缺省的`DNS`的缓存时间
61+
---------------
62+
63+
```bash
64+
# 查看缓存时间,单位秒。-1表示永远缓存,0表示不缓存
65+
$ ./dcm.sh -p 12345 getPolicy
66+
67+
# 设置缓存时间
68+
$ ./dcm.sh --pid 12345 setPolicy 5
69+
70+
# 查看未命中条目的缓存时间,单位秒。-1表示永远缓存,0表示不缓存
71+
$ ./dcm.sh -p 12345 getNegativePolicy
72+
73+
# 修改未命中条目的缓存时间
74+
$ ./dcm.sh -p 12345 setNegativePolicy 0
75+
```
76+
77+
:mortar_board: Developer Guide
78+
=================================
79+
80+
TODO
81+
82+
:books: 相关资料
83+
=================================
84+
85+
* [Java Agent规范](http://docs.oracle.com/javase/7/docs/api/java/lang/instrument/package-summary.html)
86+
* [Java SE 6 新特性: Instrumentation 新功能](http://www.ibm.com/developerworks/cn/java/j-lo-jse61/)
87+
* [Creation, dynamic loading and instrumentation with javaagents](http://dhruba.name/2010/02/07/creation-dynamic-loading-and-instrumentation-with-javaagents/)
88+
* [JavaAgent加载机制分析](http://nijiaben.iteye.com/blog/1847212)

tool/pom.xml

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>com.alibaba</groupId>
6+
<artifactId>dns-cache-manipulator-tool</artifactId>
7+
<version>1.4.0-beta-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
<name>${project.artifactId}</name>
10+
<inceptionYear>2015</inceptionYear>
11+
12+
<licenses>
13+
<license>
14+
<name>Apache 2</name>
15+
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
16+
<distribution>repo</distribution>
17+
<comments>A business-friendly OSS license</comments>
18+
</license>
19+
</licenses>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
</properties>
24+
25+
<dependencies>
26+
<dependency>
27+
<groupId>commons-cli</groupId>
28+
<artifactId>commons-cli</artifactId>
29+
<version>1.3</version>
30+
</dependency>
31+
<dependency>
32+
<groupId>com.sun</groupId>
33+
<artifactId>tools</artifactId>
34+
<version>1.6.0</version>
35+
<scope>system</scope>
36+
<systemPath>${tools-jar-path}</systemPath>
37+
</dependency>
38+
<dependency>
39+
<groupId>junit</groupId>
40+
<artifactId>junit</artifactId>
41+
<version>4.12</version>
42+
<scope>test</scope>
43+
</dependency>
44+
</dependencies>
45+
46+
<developers>
47+
<developer>
48+
<name>Jerry Lee</name>
49+
<id>oldratlee</id>
50+
<email>oldratlee(AT)gmail(DOT)com</email>
51+
<roles>
52+
<role>Developer</role>
53+
</roles>
54+
<timezone>+8</timezone>
55+
</developer>
56+
</developers>
57+
58+
<build>
59+
<plugins>
60+
<plugin>
61+
<groupId>org.apache.maven.plugins</groupId>
62+
<artifactId>maven-compiler-plugin</artifactId>
63+
<version>3.3</version>
64+
<configuration>
65+
<source>1.6</source>
66+
<target>1.6</target>
67+
<encoding>${project.build.sourceEncoding}</encoding>
68+
</configuration>
69+
</plugin>
70+
</plugins>
71+
</build>
72+
73+
<profiles>
74+
<profile>
75+
<id>use-standard-tools-jar</id>
76+
<activation>
77+
<file>
78+
<exists>${java.home}/../lib/tools.jar</exists>
79+
</file>
80+
</activation>
81+
<properties>
82+
<tools-jar-path>${java.home}/../lib/tools.jar</tools-jar-path>
83+
</properties>
84+
</profile>
85+
<profile>
86+
<!-- Only for mac brewed jdk, tools.jar is put at ${java.home}/../Classes/classes.jar. -->
87+
<id>use-classes-jar-instead-of-tools-jar</id>
88+
<activation>
89+
<file>
90+
<exists>${java.home}/../Classes/classes.jar</exists>
91+
</file>
92+
</activation>
93+
<properties>
94+
<tools-jar-path>${java.home}/../Classes/classes.jar</tools-jar-path>
95+
</properties>
96+
</profile>
97+
</profiles>
98+
</project>
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.alibaba.dcm.tool;
2+
3+
import com.sun.tools.attach.VirtualMachine;
4+
import org.apache.commons.cli.CommandLine;
5+
import org.apache.commons.cli.CommandLineParser;
6+
import org.apache.commons.cli.DefaultParser;
7+
import org.apache.commons.cli.HelpFormatter;
8+
import org.apache.commons.cli.Options;
9+
10+
import java.util.ArrayList;
11+
import java.util.List;
12+
13+
/**
14+
* @author Jerry Lee (oldratlee at gmail dot com)
15+
*/
16+
public class DcmTool {
17+
static List<String> actionList = new ArrayList<String>();
18+
19+
static {
20+
actionList.add("set");
21+
actionList.add("get");
22+
23+
actionList.add("list");
24+
actionList.add("clear");
25+
26+
actionList.add("setPolicy");
27+
actionList.add("getPolicy");
28+
actionList.add("setNegativePolicy");
29+
actionList.add("getNegativePolicy");
30+
}
31+
32+
33+
public static void main(String[] args) throws Exception {
34+
final String tmpFile = System.getenv("DCM_TOOLS_TMP_FILE");
35+
final String agentJar = System.getenv("DCM_TOOLS_AGENT_JAR");
36+
37+
Options options = new Options();
38+
options.addOption("p", "pid", true, "java process id to attach");
39+
options.addOption("h", "help", false, "show help");
40+
41+
CommandLineParser parser = new DefaultParser();
42+
CommandLine cmd = parser.parse(options, args);
43+
44+
if (cmd.hasOption('h')) {
45+
HelpFormatter hf = new HelpFormatter();
46+
hf.printHelp("Options", options);
47+
return;
48+
}
49+
50+
String pid = null;
51+
if (cmd.hasOption('p')) {
52+
pid = cmd.getOptionValue('p');
53+
} else {
54+
throw new IllegalStateException("required pid argument!");
55+
}
56+
57+
final String[] arguments = cmd.getArgs();
58+
if (arguments.length < 1) {
59+
System.out.println("No Action! Available action: " + actionList);
60+
}
61+
62+
String action = arguments[0].trim().toLowerCase();
63+
if (!actionList.contains(action)) {
64+
throw new IllegalStateException("Unknown action " + action + ". Available action: " + actionList);
65+
}
66+
67+
StringBuilder agentArgument = new StringBuilder();
68+
agentArgument.append(action).append(' ');
69+
for (int i = 1; i < arguments.length; i++) {
70+
String s = arguments[i];
71+
agentArgument.append(' ').append(s);
72+
}
73+
74+
agentArgument.append(" file ").append(tmpFile);
75+
76+
VirtualMachine vm = VirtualMachine.attach(pid); // target java process pid
77+
System.out.println(vm);
78+
vm.loadAgent(agentJar, agentArgument.toString());
79+
80+
Thread.sleep(1000);
81+
vm.detach();
82+
}
83+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.alibaba.dcm.tool;
2+
3+
import java.net.InetAddress;
4+
import java.util.Date;
5+
6+
/**
7+
* @author Jerry Lee (oldratlee at gmail dot com)
8+
*/
9+
public class TestMainForAgentInject {
10+
public static void main(String[] args) throws Exception {
11+
while (true) {
12+
System.out.println(new Date());
13+
14+
System.out.println("\tClass path:");
15+
final String classPath = System.getProperty("java.class.path");
16+
final String[] split = classPath.split(":");
17+
for (int i = 0; i < split.length; i++) {
18+
if (split[i].trim().isEmpty()) continue;
19+
System.out.printf("\t%6s %s\n", i + 1, split[i]);
20+
}
21+
22+
System.out.printf("\tbaidu.com: %s\n", InetAddress.getByName("baidu.com").getHostAddress());
23+
System.out.println();
24+
25+
Thread.sleep(1000);
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)