Skip to content

Commit 32ee7a9

Browse files
committed
Add DebugSettings
Closes gh-73
1 parent 5af15ac commit 32ee7a9

File tree

3 files changed

+107
-22
lines changed

3 files changed

+107
-22
lines changed

README.adoc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,30 @@ static CommonsExecWebServerFactoryBean configServer() {
164164
}
165165
----
166166

167+
=== Debugging
167168

169+
If you need to start the application in debug mode, you can do so using the `DebugSettings`.
170+
171+
[source,java]
172+
----
173+
@Bean
174+
@OAuth2ClientProviderIssuerUri
175+
static CommonsExecWebServerFactoryBean authorizationServer() {
176+
// @formatter:off
177+
return CommonsExecWebServerFactoryBean.builder()
178+
// ...
179+
.debug((settings) -> settings
180+
.enabled(true)
181+
// Optional properties with their explicit defaults shown below
182+
.suspend(true)
183+
.port(5005)
184+
);
185+
// @formatter:on
186+
}
187+
----
188+
189+
When starting the remote debugger, it is important to remember that the classpath of the `CommonsExecWebServerFactoryBean` is independent of the project it runs in.
190+
This means, the classpath of the debugger will need to match the classpat of the `CommonsExecWebServerFactoryBean` rather than the project it exists in.
168191

169192
[[dynamicproperty]]
170193
== @DynamicProperty

spring-boot-testjars/src/main/java/org/springframework/experimental/boot/server/exec/CommonsExecWebServerFactoryBean.java

Lines changed: 54 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,7 @@ public class CommonsExecWebServerFactoryBean
6262

6363
private CommonsExecWebServer webServer;
6464

65-
private Integer debugPort;
66-
67-
private boolean suspend = true;
65+
private final DebugSettings debugSettings = new DebugSettings();
6866

6967
CommonsExecWebServerFactoryBean() {
7068
Class<?> jarDetector = ClassUtils.resolveClassName(this.mainClass, null);
@@ -121,23 +119,12 @@ public CommonsExecWebServerFactoryBean systemProperties(Consumer<Map<String, Str
121119
}
122120

123121
/**
124-
* If set, will start up in debug mode listening on the specified port.
125-
* @param debugPort the port to listen on or null (default) to disable debug mode.
122+
* If set, will start up in debug mode using the provided settings.
123+
* @param debugSettings the settings to use.
126124
* @return the {@link CommonsExecWebServerFactoryBean} for customization.
127125
*/
128-
public CommonsExecWebServerFactoryBean debugPort(Integer debugPort) {
129-
this.debugPort = debugPort;
130-
return this;
131-
}
132-
133-
/**
134-
* If {@link #debugPort(Integer)} is set, then this determines if the server should be
135-
* suspended or not.
136-
* @param suspend true if should suspend, else false.
137-
* @return the {@link CommonsExecWebServerFactoryBean} for customization.
138-
*/
139-
public CommonsExecWebServerFactoryBean suspend(boolean suspend) {
140-
this.suspend = suspend;
126+
public CommonsExecWebServerFactoryBean debug(Consumer<DebugSettings> debugSettings) {
127+
debugSettings.accept(this.debugSettings);
141128
return this;
142129
}
143130

@@ -148,10 +135,10 @@ public boolean isEagerInit() {
148135

149136
private CommonsExecWebServer build() {
150137
CommandLine commandLine = new CommandLine(this.executable);
151-
if (this.debugPort != null) {
152-
String s = (this.suspend) ? "y" : "n";
153-
commandLine.addArgument(
154-
"-agentlib:jdwp=transport=dt_socket,server=y,suspend=" + s + ",address=*:" + this.debugPort);
138+
if (this.debugSettings.enabled) {
139+
String s = (this.debugSettings.suspend) ? "y" : "n";
140+
commandLine.addArgument("-agentlib:jdwp=transport=dt_socket,server=y,suspend=" + s + ",address=*:"
141+
+ this.debugSettings.port);
155142
}
156143
commandLine.addArguments(createSystemPropertyArgs(), false);
157144
commandLine.addArgument("-classpath", false);
@@ -221,4 +208,49 @@ public void destroy() throws Exception {
221208
}
222209
}
223210

211+
/**
212+
* The settings for debugging.
213+
*
214+
* @author Rob Winch
215+
*/
216+
public static class DebugSettings {
217+
218+
private boolean enabled;
219+
220+
private int port = 5005;
221+
222+
private boolean suspend = true;
223+
224+
/**
225+
* Sets if debug is enabled.
226+
* @param enabled if debug is enabled or not (default is false).
227+
* @return the {@link DebugSettings} for additional customization.
228+
*/
229+
public DebugSettings enabled(boolean enabled) {
230+
this.enabled = enabled;
231+
return this;
232+
}
233+
234+
/**
235+
* Sets the port to be used for debugging.
236+
* @param port the port to be used (default 5005)
237+
* @return the {@link DebugSettings} for additional customization.
238+
*/
239+
public DebugSettings port(int port) {
240+
this.port = port;
241+
return this;
242+
}
243+
244+
/**
245+
* Sets if the debugger should suspend on startup.
246+
* @param suspend sets if should suspend on startup (default true)
247+
* @return the {@link DebugSettings} for additional customization.
248+
*/
249+
public DebugSettings suspend(boolean suspend) {
250+
this.suspend = suspend;
251+
return this;
252+
}
253+
254+
}
255+
224256
}

spring-boot-testjars/src/test/java/org/springframework/experimental/boot/server/exec/CommonsExecWebServerFactoryBeanTests.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,36 @@ void setAdditionalBeanClassNamesWhenMultipleNames() {
177177
(props) -> assertThat(props).containsEntry(envName, classNames[0] + "," + classNames[1]));
178178
}
179179

180+
@Test
181+
void debugWithDefaults() throws Exception {
182+
CommonsExecWebServerFactoryBean factory = CommonsExecWebServerFactoryBean.builder()
183+
.debug((debug) -> debug.enabled(true));
184+
CommonsExecWebServer server = factory.getObject();
185+
// IDEA's default debug port is 5005
186+
assertThat(server.getCommandLine().getArguments())
187+
.contains("-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=*:5005");
188+
}
189+
190+
@Test
191+
void debugWithCustomPort() throws Exception {
192+
CommonsExecWebServerFactoryBean factory = CommonsExecWebServerFactoryBean.builder()
193+
.debug((debug) -> debug.enabled(true).port(1234));
194+
CommonsExecWebServer server = factory.getObject();
195+
// IDEA's default debug port is 5005
196+
assertThat(server.getCommandLine().getArguments())
197+
.contains("-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=*:1234");
198+
}
199+
200+
@Test
201+
void debugWithSuspendFalse() throws Exception {
202+
CommonsExecWebServerFactoryBean factory = CommonsExecWebServerFactoryBean.builder()
203+
.debug((debug) -> debug.enabled(true).suspend(false));
204+
CommonsExecWebServer server = factory.getObject();
205+
// IDEA's default debug port is 5005
206+
assertThat(server.getCommandLine().getArguments())
207+
.contains("-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005");
208+
}
209+
180210
private void assertClasspathContainsResourceWithContent(List<ClasspathEntry> classpath, String resourceName,
181211
String expectedContent) {
182212
ClasspathEntry lastEntry = classpath.get(classpath.size() - 1);

0 commit comments

Comments
 (0)