Skip to content

Commit b61af03

Browse files
committed
Simplify maven plugin and hotswap
* remove dcevm (it is nice but hotswap need to be more clever)
1 parent 9ebb2cb commit b61af03

5 files changed

Lines changed: 92 additions & 45 deletions

File tree

jooby-hotreload/TODO

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
compile on the fly?
2+
http://search.maven.org/#artifactdetails%7Corg.eclipse.jdt.core.compiler%7Cecj%7C4.4.2%7Cjar
3+
4+
how do I detect a hotswap? and only then bounce the app?
5+
6+
ideal implementation?
7+
+ bounce when hotswap fail
8+
+ find a way that a module could say bounce the app when this annotation, param, method, etc... changes on this type of class.

jooby-hotreload/pom.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,22 @@
2929
</configuration>
3030
</plugin>
3131

32+
<plugin>
33+
<groupId>org.apache.maven.plugins</groupId>
34+
<artifactId>maven-jar-plugin</artifactId>
35+
<version>2.6</version>
36+
<configuration>
37+
<archive>
38+
<manifestEntries>
39+
<Premain-Class>org.jooby.Hotswap</Premain-Class>
40+
<Can-Redefine-Classes>true</Can-Redefine-Classes>
41+
<Can-Retransform-Classes>true</Can-Retransform-Classes>
42+
<Can-Set-Native-Method-Prefix>true</Can-Set-Native-Method-Prefix>
43+
</manifestEntries>
44+
</archive>
45+
</configuration>
46+
</plugin>
47+
3248
</plugins>
3349
</build>
3450

jooby-hotreload/src/main/java/org/jooby/Hotswap.java

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import java.io.File;
2222
import java.io.IOException;
23+
import java.lang.instrument.Instrumentation;
2324
import java.lang.reflect.InvocationTargetException;
2425
import java.net.MalformedURLException;
2526
import java.net.URL;
@@ -34,20 +35,7 @@
3435
import java.util.concurrent.ExecutorService;
3536
import java.util.concurrent.Executors;
3637

37-
/**
38-
* Start a target app with a custom classloader. The classloader is responsible for loading
39-
* resources from:
40-
*
41-
* 1. public and config directories (probably src/main/resources too)
42-
* 2. target/classes, current app (*.class)
43-
*
44-
* The parent classloader must load 1) and all the *.jars files.
45-
*
46-
* On changes ONLY the custom classloader (App classlaoder) is bounced it.
47-
*
48-
* @author edgar
49-
*
50-
*/
38+
5139
public class Hotswap {
5240

5341
private URLClassLoader loader;
@@ -68,7 +56,7 @@ public class Hotswap {
6856

6957
private PathMatcher excludes;
7058

71-
private boolean dcevm;
59+
// OFF private boolean dcevm;
7260

7361
private List<File> dirs;
7462

@@ -84,7 +72,11 @@ public Hotswap(final String mainClass, final File[] cp) throws IOException {
8472
this.paths = toPath(dirs.toArray(new File[dirs.size()]));
8573
this.executor = Executors.newSingleThreadExecutor();
8674
this.scanner = new Watcher(this::onChange, paths);
87-
dcevm = System.getProperty("java.vm.version").toLowerCase().contains("dcevm");
75+
// OFF dcevm = System.getProperty("java.vm.version").toLowerCase().contains("dcevm");
76+
}
77+
78+
public static void premain(final String args, final Instrumentation inst) throws Exception {
79+
// TODO:
8880
}
8981

9082
public static void main(final String[] args) throws Exception {
@@ -141,9 +133,9 @@ private Hotswap excludes(final String excludes) {
141133

142134
public void run() {
143135
System.out.printf("Hotswap available on: %s\n", dirs);
144-
System.out.printf(" unlimited runtime class redefinition: %s\n", dcevm
145-
? "yes"
146-
: "no (see https://github.com/dcevm/dcevm)");
136+
// System.out.printf(" unlimited runtime class redefinition: %s\n", dcevm
137+
// ? "yes"
138+
// : "no (see https://github.com/dcevm/dcevm)");
147139
System.out.printf(" includes: %s\n", includes);
148140
System.out.printf(" excludes: %s\n", excludes);
149141

@@ -273,4 +265,5 @@ public String toString() {
273265
}
274266
};
275267
}
268+
276269
}

jooby-maven-plugin/src/main/java/org/jooby/Command.java

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class Command {
2929

3030
private String name;
3131

32-
private List<String> arguments;
32+
private List<String> args;
3333

3434
private File workdir;
3535

@@ -42,7 +42,7 @@ public class Command {
4242
public Command(final String alias, final String name, final List<String> args) {
4343
this.alias = alias;
4444
this.name = name;
45-
this.arguments = new ArrayList<String>(args);
45+
this.args = new ArrayList<String>(args);
4646
}
4747

4848
public Command() {
@@ -74,12 +74,12 @@ public void setName(final String name) {
7474
this.name = name;
7575
}
7676

77-
public List<String> getArguments() {
78-
return arguments;
77+
public List<String> getArgs() {
78+
return args;
7979
}
8080

81-
public void setArguments(final List<String> arguments) {
82-
this.arguments = arguments;
81+
public void setArgs(final List<String> arguments) {
82+
this.args = arguments;
8383
}
8484

8585
public void stop() throws InterruptedException {
@@ -95,7 +95,7 @@ public void stop() throws InterruptedException {
9595
public void execute() throws IOException, InterruptedException {
9696
List<String> cmd = new ArrayList<String>();
9797
cmd.add(name);
98-
cmd.addAll(arguments);
98+
cmd.addAll(args);
9999
process = new ProcessBuilder(cmd)
100100
.directory(workdir)
101101
.redirectErrorStream(true)
@@ -113,7 +113,7 @@ public String toString() {
113113
StringBuilder str = new StringBuilder();
114114
if (alias == null) {
115115
str.append(name);
116-
for (String arg : arguments) {
116+
for (String arg : args) {
117117
str.append(" ").append(arg);
118118
}
119119
} else {
@@ -122,10 +122,20 @@ public String toString() {
122122
return str.toString();
123123
}
124124

125+
public void set(final String cmd) {
126+
String[] cmdline = cmd.split("\\s+");
127+
setName(cmdline[0].trim());
128+
List<String> args = new ArrayList<String>();
129+
for (int i = 1; i < cmdline.length; i++) {
130+
args.add(cmdline[i].trim());
131+
}
132+
setArgs(args);
133+
}
134+
125135
public String debug() {
126136
StringBuilder str = new StringBuilder();
127137
str.append(name);
128-
for (String arg : arguments) {
138+
for (String arg : args) {
129139
str.append(" ").append(arg);
130140
}
131141
return str.toString();

jooby-maven-plugin/src/main/java/org/jooby/JoobyMojo.java

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ public class JoobyMojo extends AbstractMojo {
6565
@Parameter(property = "jooby.excludes")
6666
private List<String> excludes;
6767

68+
@Parameter(property = "application.debug", defaultValue = "true")
69+
private String debug;
70+
6871
@Parameter(defaultValue = "${plugin.artifacts}")
6972
private List<org.apache.maven.artifact.Artifact> pluginArtifacts;
7073

@@ -84,8 +87,8 @@ public void execute() throws MojoExecutionException, MojoFailureException {
8487

8588
Set<String> classpath = new LinkedHashSet<String>();
8689

87-
Optional<Artifact> hotreload = hotreload(pluginArtifacts);
88-
classpath.add(hotreload.get().getFile().getAbsolutePath());
90+
String hotreload = hotreload(pluginArtifacts).get().getFile().getAbsolutePath();
91+
classpath.add(hotreload);
8992

9093
for (Artifact artifact : artifacts) {
9194
String scope = artifact.getScope();
@@ -101,7 +104,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
101104
cmds.addAll(0, this.commands);
102105
}
103106
List<String> args = new ArrayList<String>();
104-
args.addAll(vmArgs(vmArgs));
107+
args.addAll(vmArgs(hotreload, vmArgs));
105108
args.add("-cp");
106109
args.add(cp);
107110
args.add("org.jooby.Hotswap");
@@ -147,11 +150,27 @@ private String join(final List<String> includes) {
147150
return buff.toString();
148151
}
149152

150-
private List<String> vmArgs(final List<String> vmArgs) {
153+
private List<String> vmArgs(final String agentpath, final List<String> vmArgs) {
151154
List<String> results = new ArrayList<String>();
152155
if (vmArgs != null) {
153156
results.addAll(vmArgs);
154157
}
158+
results.add("-javaagent:" + agentpath);
159+
if (!"false".equals(debug)) {
160+
// true, number, debug line
161+
if ("true".equals(debug)) {
162+
// default debug
163+
results.add("-agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=n");
164+
} else {
165+
try {
166+
int port = Integer.parseInt(debug);
167+
results.add("-agentlib:jdwp=transport=dt_socket,address=" + port + ",server=y,suspend=n");
168+
} catch (NumberFormatException ex) {
169+
// assume it is a debug line
170+
results.add(debug);
171+
}
172+
}
173+
}
155174
// logback
156175
File[] logbackFiles = {localFile("config", "logback-test.xml"),
157176
localFile("config", "logback.xml") };
@@ -161,19 +180,20 @@ private List<String> vmArgs(final List<String> vmArgs) {
161180
break;
162181
}
163182
}
164-
// dcevm?
165-
String altjvm = null;
166-
for (String boot : System.getProperty("sun.boot.library.path", "").split(File.pathSeparator)) {
167-
File dcevm = new File(boot, "dcevm");
168-
if (dcevm.exists()) {
169-
altjvm = dcevm.getName();
170-
}
171-
}
172-
if (altjvm == null) {
173-
getLog().warn("dcevm not found, we recommend to install it: https://github.com/dcevm/dcevm");
174-
} else {
175-
results.add("-XXaltjvm=" + altjvm);
176-
}
183+
// dcevm? OFF
184+
// String altjvm = null;
185+
// for (String boot : System.getProperty("sun.boot.library.path", "").split(File.pathSeparator))
186+
// {
187+
// File dcevm = new File(boot, "dcevm");
188+
// if (dcevm.exists()) {
189+
// altjvm = dcevm.getName();
190+
// }
191+
// }
192+
// if (altjvm == null) {
193+
// getLog().warn("dcevm not found, we recommend to install it: https://github.com/dcevm/dcevm");
194+
// } else {
195+
// results.add("-XXaltjvm=" + altjvm);
196+
// }
177197
return results;
178198
}
179199

0 commit comments

Comments
 (0)