Skip to content

Commit 922abf3

Browse files
committed
enh: add sendReloadCommand to Deployer for browser reload functionality
1 parent 2286147 commit 922abf3

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

src/main/java/com/flowlogix/maven/plugins/Deployer.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,26 @@ private CommandResult sendCommand(String command, Map<String, String> parameters
120120
return response.statusCode() == 200 ? CommandResult.SUCCESS : CommandResult.ERROR;
121121
}
122122

123+
@SuppressWarnings("checkstyle:MagicNumber")
124+
@SneakyThrows({IOException.class, InterruptedException.class})
125+
public CommandResult sendReloadCommand(String applicationUrl,
126+
@NonNull BiConsumer<String, CommandResponse> responseCallback) {
127+
HttpResponse<Void> response;
128+
try {
129+
HttpClient client = HttpClient.newHttpClient();
130+
HttpRequest request = HttpRequest.newBuilder()
131+
.uri(URI.create("%s/flowlogix/reload".formatted(applicationUrl)))
132+
.POST(HttpRequest.BodyPublishers.noBody())
133+
.build();
134+
response = client.send(request, HttpResponse.BodyHandlers.discarding());
135+
} catch (ConnectException e) {
136+
responseCallback.accept("reload", null);
137+
return CommandResult.NO_CONNECTION;
138+
}
139+
responseCallback.accept("reload", new CommandResponse(response.statusCode(), null));
140+
return response.statusCode() == 200 ? CommandResult.SUCCESS : CommandResult.ERROR;
141+
}
142+
123143
@SuppressWarnings("checkstyle:MagicNumber")
124144
void printResponse(String command, CommandResponse response) {
125145
if (response == null) {

src/main/java/com/flowlogix/maven/plugins/DevModeMojo.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ public class DevModeMojo extends CommonDevMojo {
4848
private final Path explodedWarDir = Paths.get(project.getBuild().getDirectory(), project.getBuild().getFinalName());
4949
@Getter(lazy = true)
5050
private final Path srcMainDir = Paths.get(project.getBasedir().getAbsolutePath(), "src", "main");
51+
@Getter(lazy = true)
52+
private final String appURL = computeApplicationURL();
5153

5254
@Override
5355
@SneakyThrows(IOException.class)
@@ -79,11 +81,10 @@ private void enableOrDeploy() throws IOException {
7981
}
8082
deployer.sendDeployCommand(deployer::printResponse, 0);
8183
}
82-
String httpUrl = payaraAminURL.replaceFirst(":\\d+$", ":" + payaraHttpPort);
83-
var browseURL = URI.create("%s/%s".formatted(httpUrl, project.getBuild().getFinalName()));
84-
getLog().info("Application URL at " + browseURL);
84+
85+
getLog().info("Application URL at " + getAppURL());
8586
try {
86-
Desktop.getDesktop().browse(browseURL);
87+
Desktop.getDesktop().browse(URI.create(getAppURL()));
8788
} catch (UnsupportedOperationException e) {
8889
getLog().debug("Cannot open browser" , e);
8990
}
@@ -101,6 +102,9 @@ private void onChange(Set<Path> modifiedFiles) {
101102
deployer.sendEnableCommand(deployer::printResponse);
102103
}
103104
}
105+
if (deployer.sendReloadCommand(getAppURL(), deployer::printResponse) == CommandResult.ERROR) {
106+
getLog().warn("Reload failed");
107+
}
104108
}
105109

106110
private boolean compileSources() {
@@ -120,4 +124,9 @@ private boolean isSourceCode(Path path) {
120124
.resolve("src/main").relativize(path);
121125
return CODE_CONTAINING_SRC_DIRS.stream().anyMatch(relativePath::startsWith);
122126
}
127+
128+
private String computeApplicationURL() {
129+
String httpUrl = payaraAminURL.replaceFirst(":\\d+$", ":" + payaraHttpPort);
130+
return "%s/%s".formatted(httpUrl, project.getBuild().getFinalName());
131+
}
123132
}

0 commit comments

Comments
 (0)