Skip to content

Commit a233619

Browse files
jkorrioldratlee
authored andcommitted
feat: support for use through jvm -javaagent command line option (#289)
1 parent 9f70eb6 commit a233619

File tree

5 files changed

+76
-9
lines changed

5 files changed

+76
-9
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,23 @@ $ dcm -p 12345 getNegativePolicy
421421
$ dcm -p 12345 setNegativePolicy 0
422422
```
423423

424+
### Load Cache Entries from File
425+
426+
```bash
427+
$ dcm -p 12345 load /foo/bar/my-cache.properties
428+
```
429+
430+
### Manipulate cache with a command line option at JVM startup
431+
432+
To manipulate dns entries at jvm startup, add the following jvm-options:
433+
434+
```bash
435+
# Set individual entry
436+
java -javaagent:<path to jar>="set foo.com 1.1.1.1" --add-opens java.base/java.net=ALL-UNNAMED --add-opens java.base/sun.net=ALL-UNNAMED ...
437+
# Load entries from file
438+
java -javaagent:<path to jar>="load my-cache.properties" --add-opens java.base/java.net=ALL-UNNAMED --add-opens java.base/sun.net=ALL-UNNAMED ...
439+
```
440+
424441
## 📚 Related information
425442

426443
* [Java Agent Specification](http://docs.oracle.com/javase/7/docs/api/java/lang/instrument/package-summary.html)

library/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
<configuration>
7878
<archive>
7979
<manifestEntries>
80+
<Premain-Class>com.alibaba.dcm.agent.DcmAgent</Premain-Class>
8081
<Agent-Class>com.alibaba.dcm.agent.DcmAgent</Agent-Class>
8182
<Can-Redefine-Classes>false</Can-Redefine-Classes>
8283
<Can-Retransform-Classes>false</Can-Retransform-Classes>

library/src/main/java/com/alibaba/dcm/DnsCacheManipulator.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import javax.annotation.Nullable;
1010
import javax.annotation.ParametersAreNonnullByDefault;
11+
import java.io.FileInputStream;
1112
import java.io.InputStream;
1213
import java.util.Arrays;
1314
import java.util.List;
@@ -144,6 +145,27 @@ public static void loadDnsCacheConfig(String propertiesFileName) {
144145
}
145146
}
146147

148+
/**
149+
* Load dns config from the specified properties file in filesystem, then set dns cache.
150+
*
151+
* @param propertiesFileName specified properties file name in filesystem.
152+
* @throws DnsCacheManipulatorException Operation fail
153+
* @see DnsCacheManipulator#setDnsCache(java.util.Properties)
154+
*/
155+
public static void loadDnsCacheConfigFromFileSystem(String propertiesFileName) {
156+
try {
157+
InputStream inputStream = new FileInputStream(propertiesFileName);
158+
Properties properties = new Properties();
159+
properties.load(inputStream);
160+
inputStream.close();
161+
setDnsCache(properties);
162+
} catch (Exception e) {
163+
final String message = String.format("Fail to loadDnsCacheConfig from %s, cause: %s",
164+
propertiesFileName, e);
165+
throw new DnsCacheManipulatorException(message, e);
166+
}
167+
}
168+
147169
/**
148170
* Get a dns cache entry by {@code host}.
149171
*

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,14 @@ public class DcmAgent {
3838
static final String DCM_AGENT_SUCCESS_MARK_LINE = "!!DCM SUCCESS!!";
3939

4040
/**
41-
* Entrance method of DCM Java Agent.
41+
* Entrance method of DCM Java Agent when used through a jvm command line option.
42+
*/
43+
public static void premain(@Nonnull String agentArgument) throws Exception {
44+
agentmain(agentArgument);
45+
}
46+
47+
/**
48+
* Entrance method of DCM Java Agent when connecting to a running jvm.
4249
*/
4350
@SuppressFBWarnings("THROWS_METHOD_THROWS_CLAUSE_BASIC_EXCEPTION")
4451
public static void agentmain(@Nonnull String agentArgument) throws Exception {
@@ -264,6 +271,8 @@ private static synchronized void initAction2Method() throws NoSuchMethodExceptio
264271
map.put("ls", DnsCacheManipulator.class.getMethod("getWholeDnsCache"));
265272
map.put("clear", DnsCacheManipulator.class.getMethod("clearDnsCache"));
266273

274+
map.put("load", DnsCacheManipulator.class.getMethod("loadDnsCacheConfigFromFileSystem", String.class));
275+
267276
map.put("setPolicy", DnsCacheManipulator.class.getMethod("setDnsCachePolicy", int.class));
268277
map.put("getPolicy", DnsCacheManipulator.class.getMethod("getDnsCachePolicy"));
269278
map.put("setNegativePolicy", DnsCacheManipulator.class.getMethod("setDnsNegativeCachePolicy", int.class));

library/src/test/java/com/alibaba/dcm/DnsCacheManipulatorTests.kt

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,12 @@ package com.alibaba.dcm
33
import com.alibaba.dcm.internal.InetAddressCacheUtilCommons.isNewInetAddressImpl
44
import io.kotest.assertions.throwables.shouldThrow
55
import io.kotest.core.spec.style.AnnotationSpec
6-
import io.kotest.matchers.collections.shouldBeEmpty
7-
import io.kotest.matchers.collections.shouldContainExactly
8-
import io.kotest.matchers.collections.shouldContainExactlyInAnyOrder
9-
import io.kotest.matchers.collections.shouldHaveSize
6+
import io.kotest.matchers.collections.*
107
import io.kotest.matchers.longs.shouldBeBetween
118
import io.kotest.matchers.nulls.shouldBeNull
129
import io.kotest.matchers.shouldBe
1310
import io.kotest.matchers.types.shouldNotBeSameInstanceAs
14-
11+
import kotlin.io.path.writeText
1512

1613
private const val IP1 = "42.42.42.1"
1714
private const val IP2 = "42.42.42.2"
@@ -276,13 +273,13 @@ class DnsCacheManipulatorTests : AnnotationSpec() {
276273
}
277274

278275
@Test
279-
fun test_loadDnsCacheConfig_fromMyConfig() {
276+
fun test_loadDnsCacheConfig_fromMyConfigClassPath() {
280277
DnsCacheManipulator.loadDnsCacheConfig("my-dns-cache.properties")
281278
"www.hello2.com".lookupIpByName() shouldBe IP2
282279
}
283280

284281
@Test
285-
fun test_multi_ips_in_config_file() {
282+
fun test_multi_ips_in_config_file_classpath() {
286283
DnsCacheManipulator.loadDnsCacheConfig("dns-cache-multi-ips.properties")
287284

288285
val host = "www.hello-multi-ips.com"
@@ -299,11 +296,32 @@ class DnsCacheManipulatorTests : AnnotationSpec() {
299296
}
300297

301298
@Test
302-
fun test_configNotFound() {
299+
fun test_configNotFound_classpath() {
303300
val ex = shouldThrow<DnsCacheManipulatorException> {
304301
DnsCacheManipulator.loadDnsCacheConfig("not-existed.properties")
305302
}
306303

307304
ex.message shouldBe "Fail to find not-existed.properties on classpath!"
308305
}
306+
307+
@Test
308+
fun test_loadDnsCacheConfig_fromMyConfigFileSystem() {
309+
val tempFile = kotlin.io.path.createTempFile("dns-cache-test", ".properties")
310+
tempFile.writeText("www.hello2.com $IP2")
311+
tempFile.toFile().deleteOnExit()
312+
313+
DnsCacheManipulator.loadDnsCacheConfigFromFileSystem(tempFile.toAbsolutePath().toString())
314+
"www.hello2.com".lookupIpByName() shouldBe IP2
315+
}
316+
317+
@Test
318+
fun test_configNotFound_fileSystem() {
319+
val ex = shouldThrow<DnsCacheManipulatorException> {
320+
DnsCacheManipulator.loadDnsCacheConfigFromFileSystem("not-existed.properties")
321+
}
322+
323+
ex.message shouldBeIn arrayOf(
324+
"Fail to loadDnsCacheConfig from not-existed.properties, cause: java.io.FileNotFoundException: not-existed.properties (No such file or directory)",
325+
"Fail to loadDnsCacheConfig from not-existed.properties, cause: java.io.FileNotFoundException: not-existed.properties (The system cannot find the file specified)")
326+
}
309327
}

0 commit comments

Comments
 (0)