Skip to content

Commit 4fe1324

Browse files
committed
occupy any free port instead of hardcoded 12100
1 parent ac4e989 commit 4fe1324

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

src/main/java/io/unlogged/Runtime.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
*/
2727
public class Runtime {
2828

29-
public static final int AGENT_SERVER_PORT = 12100;
3029
private static Runtime instance;
3130
private static List<Pair<String, String>> pendingClassRegistrations = new ArrayList<>();
3231
private final ScheduledExecutorService probeReaderExecutor = Executors.newSingleThreadScheduledExecutor();
@@ -57,9 +56,10 @@ private Runtime(String args) {
5756

5857
ServerMetadata serverMetadata =
5958
new ServerMetadata(weaveParameters.getIncludedNames().toString(), Constants.AGENT_VERSION,
60-
AGENT_SERVER_PORT);
59+
0);
6160

62-
httpServer = new AgentCommandServer(AGENT_SERVER_PORT, serverMetadata);
61+
httpServer = new AgentCommandServer(0, serverMetadata);
62+
httpServer.start(NanoHTTPD.SOCKET_READ_TIMEOUT, false);
6363

6464

6565
outputDir = new File(weaveParameters.getOutputDirname());
@@ -84,6 +84,9 @@ private Runtime(String args) {
8484
errorLogger.log("Java version: " + System.getProperty("java.version"));
8585
errorLogger.log("Agent version: " + Constants.AGENT_VERSION);
8686
errorLogger.log("Params: " + args);
87+
88+
serverMetadata.setAgentServerUrl("http://localhost:" + httpServer.getListeningPort());
89+
serverMetadata.setAgentServerPort(httpServer.getListeningPort());
8790
errorLogger.log(serverMetadata.toString());
8891

8992
System.out.println("[unlogged]" +
@@ -143,7 +146,8 @@ private Runtime(String args) {
143146

144147

145148
httpServer.setAgentCommandExecutor(new AgentCommandExecutorImpl(logger.getObjectMapper(), logger));
146-
httpServer.start(NanoHTTPD.SOCKET_READ_TIMEOUT, false);
149+
150+
System.out.println("[unlogged] agent server started at port " + httpServer.getListeningPort());
147151

148152
java.lang.Runtime.getRuntime()
149153
.addShutdownHook(new Thread(this::close));

src/main/java/io/unlogged/command/ServerMetadata.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package io.unlogged.command;
22

33
public class ServerMetadata {
4-
private final int agentServerPort;
4+
public void setAgentServerPort(int agentServerPort) {
5+
this.agentServerPort = agentServerPort;
6+
}
7+
8+
private int agentServerPort;
59
String includePackageName;
610
String agentVersion;
711
String agentServerUrl;

src/main/java/io/unlogged/weaver/RuntimeWeaver.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ public class RuntimeWeaver implements ClassFileTransformer {
5353
* @param instrumentation object provided to the java agent for hooking up the class loader
5454
*/
5555
public RuntimeWeaver(String args, Instrumentation instrumentation) {
56-
System.out.println("[unlogged] agent server started at port " + AGENT_SERVER_PORT);
5756
this.instrumentation = instrumentation;
5857
runtime = Runtime.getInstance(args);
5958
logger = runtime.getLogger();

0 commit comments

Comments
 (0)