Skip to content

Commit 6be3b83

Browse files
committed
add rm action to DcmAgent; update documentation for dcm tool #24
1 parent 4c0cb44 commit 6be3b83

File tree

8 files changed

+59
-23
lines changed

8 files changed

+59
-23
lines changed

library/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ openjdk7 64-Bit | Linux | travis-ci
164164
oraclejdk8 64-Bit | Linux | travis-ci
165165
Mac自带的jdk6 64-Bit | Mac | 个人Mac
166166
oraclejdk7 64-Bit | Mac | 个人Mac
167+
oraclejdk8 64-Bit | Mac | 个人Mac
167168
oraclejdk7 64-Bit | windows server 2012 r2 | appveyor
168169
oraclejdk7 32-Bit | windows server 2012 r2 | appveyor
169170
oraclejdk8 64-Bit | windows server 2012 r2 | appveyor

library/src/main/java/com/alibaba/dcm/agent/DcmAgent.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
/**
1717
* @author Jerry Lee (oldratlee at gmail dot com)
18+
* @since 1.4.0
1819
*/
1920
public class DcmAgent {
2021
public static final String FILE = "file";
@@ -140,9 +141,13 @@ static Object doAction(String action, String[] arguments) throws Exception {
140141
static void printResult(String action, Object result, PrintWriter writer) {
141142
final Method method = action2Method.get(action);
142143
if (method.getReturnType() != void.class) {
143-
writer.println(result.toString());
144+
if (writer != null) {
145+
writer.println(result.toString());
146+
}
147+
}
148+
if (writer != null) {
149+
writer.printf("%s DONE.\n", action);
144150
}
145-
writer.printf("%s DONE.\n", action);
146151
}
147152

148153
static volatile Map<String, Method> action2Method;
@@ -153,6 +158,7 @@ static synchronized void initAction2Method() throws Exception {
153158
Map<String, Method> map = new HashMap<String, Method>();
154159
map.put("set", DnsCacheManipulator.class.getMethod("setDnsCache", String.class, String[].class));
155160
map.put("get", DnsCacheManipulator.class.getMethod("getDnsCache", String.class));
161+
map.put("rm", DnsCacheManipulator.class.getMethod("removeDnsCache", String.class));
156162

157163
map.put("list", DnsCacheManipulator.class.getMethod("getWholeDnsCache"));
158164
map.put("clear", DnsCacheManipulator.class.getMethod("clearDnsCache"));

library/src/test/java/com/alibaba/dcm/agent/DcmAgentTest.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
import java.util.List;
1111

1212
import static org.hamcrest.CoreMatchers.containsString;
13-
import static org.hamcrest.CoreMatchers.startsWith;
1413
import static org.junit.Assert.assertArrayEquals;
1514
import static org.junit.Assert.assertEquals;
15+
import static org.junit.Assert.assertNotNull;
16+
import static org.junit.Assert.assertNull;
1617
import static org.junit.Assert.assertThat;
1718
import static org.junit.Assert.assertTrue;
18-
import static org.junit.Assert.fail;
1919

2020
/**
2121
* @author Jerry Lee (oldratlee at gmail dot com)
@@ -87,6 +87,23 @@ public void test_agentmain_get() throws Exception {
8787
DcmAgent.agentmain("get baidu.com");
8888
}
8989

90+
@Test
91+
public void test_agentmain_rm() throws Exception {
92+
DnsCacheManipulator.setDnsCache("baidu.com", "3.3.3.3");
93+
DcmAgent.agentmain("rm baidu.com");
94+
95+
assertNull(DnsCacheManipulator.getDnsCache("baidu.com"));
96+
}
97+
98+
@Test
99+
public void test_agentmain_rm_withFile() throws Exception {
100+
DnsCacheManipulator.setDnsCache("baidu.com", "3.3.3.3");
101+
assertNotNull(DnsCacheManipulator.getDnsCache("baidu.com"));
102+
DcmAgent.agentmain("rm baidu.com file " + outputFilePath);
103+
104+
assertNull(DnsCacheManipulator.getDnsCache("baidu.com"));
105+
}
106+
90107
@Test
91108
public void test_agentmain_list() throws Exception {
92109
DcmAgent.agentmain(" list ");

tool/README.md

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@ Java Dns Cache Manipulator Tool
2626
下载
2727
----------
2828

29-
运行脚本`dcm.sh`/`dcm.bat`,执行操作。
29+
[项目Release页面](https://github.com/alibaba/java-dns-cache-manipulator/releases)下载文件`dcm-tool-x.x.x.tar.gz`
30+
31+
解压后,运行`bin`目录下的`dcm`脚本(`Shell脚本`)。
3032

3133
```bash
32-
$ ./dcm.sh -h
34+
$ dcm -h
3335
usage: Options
34-
-a,--action <arg> action
3536
-h,--help show help
3637
-p,--pid <arg> java process id to attach
3738
```
@@ -40,7 +41,9 @@ usage: Options
4041
---------------
4142

4243
```bash
43-
$ ./dcm.sh -p 12345 set baidu.com 1.1.1.1
44+
# 对进程ID是12345的Java进程,设置域名 baidu.com IP为 1.1.1.1
45+
$ dcm -p 12345 set baidu.com 1.1.1.1
46+
set DONE.
4447
```
4548

4649
查看`DNS Cache`内容
@@ -49,44 +52,51 @@ $ ./dcm.sh -p 12345 set baidu.com 1.1.1.1
4952
查看单条
5053

5154
```bash
52-
$ ./dcm.sh -p 12345 get baidu.com
55+
# 对进程ID是12345的Java进程,获取域名 baidu.com 的DNS条目信息
56+
$ dcm -p 12345 get baidu.com
57+
DnsCacheEntry{host='baidu.com', ips=[1.1.1.1], expiration=292278994-08-17 15:12:55.807+0800}
58+
get DONE.
5359
```
5460

5561
查看全部
5662

5763
```bash
58-
$ ./dcm.sh -p 12345 list
64+
$ dcm -p 12345 list
65+
DnsCache{cache=[DnsCacheEntry{host='bar.com', ips=[1.1.1.1], expiration=292278994-08-17 15:12:55.807+0800}, DnsCacheEntry{host='foo.com', ips=[1.1.1.1], expiration=292278994-08-17 15:12:55.807+0800}, DnsCacheEntry{host='baidu.com', ips=[180.149.132.47, 123.125.114.144, 220.181.57.217], expiration=2015-06-03 17:49:42.077+0800}], negativeCache=[]}
66+
list DONE.
5967
```
6068

6169
清空`DNS Cache`
6270
---------------
6371

6472
```bash
65-
$ ./dcm.sh -p 12345 clear
73+
$ dcm -p 12345 clear
74+
clear DONE.
6675
```
6776

6877
修改/查看`JVM`缺省的`DNS`的缓存时间
6978
---------------
7079

7180
```bash
7281
# 查看缓存时间,单位秒。-1表示永远缓存,0表示不缓存
73-
$ ./dcm.sh -p 12345 getPolicy
82+
$ dcm -p 12345 getPolicy
83+
30
84+
getPolicy DONE.
7485

7586
# 设置缓存时间
76-
$ ./dcm.sh --pid 12345 setPolicy 5
87+
$ dcm --pid 12345 setPolicy 5
88+
setPolicy DONE.
7789

7890
# 查看未命中条目的缓存时间,单位秒。-1表示永远缓存,0表示不缓存
79-
$ ./dcm.sh -p 12345 getNegativePolicy
91+
$ dcm -p 12345 getNegativePolicy
92+
10
93+
getNegativePolicy DONE.
8094

8195
# 修改未命中条目的缓存时间
82-
$ ./dcm.sh -p 12345 setNegativePolicy 0
96+
$ dcm -p 12345 setNegativePolicy 0
97+
setNegativePolicy DONE.
8398
```
8499

85-
:mortar_board: Developer Guide
86-
=================================
87-
88-
TODO
89-
90100
:books: 相关资料
91101
=================================
92102

tool/assembly.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<formats>
66
<format>tar.gz</format>
77
</formats>
8-
<includeBaseDirectory>false</includeBaseDirectory>
8+
<baseDirectory>dcm-${project.version}</baseDirectory>
99
<dependencySets>
1010
<dependencySet>
1111
<outputDirectory>dependencies</outputDirectory>
File renamed without changes.

tool/src/main/java/com/alibaba/dcm/tool/DcmTool.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
/**
1818
* @author Jerry Lee (oldratlee at gmail dot com)
19+
* @since 1.4.0
1920
*/
2021
public class DcmTool {
2122
static final String DCM_AGENT_SUCCESS_MARK_LINE = "!!DCM SUCCESS!!";
@@ -27,6 +28,7 @@ public class DcmTool {
2728
static {
2829
actionList.add("set");
2930
actionList.add("get");
31+
actionList.add("rm");
3032

3133
actionList.add("list");
3234
actionList.add("clear");
@@ -76,7 +78,7 @@ public static void main(String[] args) throws Exception {
7678
}
7779

7880
StringBuilder agentArgument = new StringBuilder();
79-
agentArgument.append(action).append(' ');
81+
agentArgument.append(action);
8082
for (int i = 1; i < arguments.length; i++) {
8183
String s = arguments[i];
8284
agentArgument.append(' ').append(s);

tool/src/test/java/com/alibaba/dcm/tool/TestMainForAgentInject.java renamed to tool/src/test/java/com/alibaba/dcm/tool/MainForAgentInject.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/**
77
* @author Jerry Lee (oldratlee at gmail dot com)
88
*/
9-
public class TestMainForAgentInject {
9+
public class MainForAgentInject {
1010
public static void main(String[] args) throws Exception {
1111
while (true) {
1212
System.out.printf("%s: baidu.com: %s\n", new Date(),

0 commit comments

Comments
 (0)