Skip to content

Commit 1b5326c

Browse files
committed
Avoid depending on examples-utils to run the examples
This ease the execution of the examples from the command line. This is related to vert-x3#97
1 parent a01d18c commit 1b5326c

File tree

16 files changed

+813
-130
lines changed

16 files changed

+813
-130
lines changed

core-examples/src/main/java/io/vertx/example/core/ha/Server.java

-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22

33
import io.vertx.core.AbstractVerticle;
44
import io.vertx.core.Launcher;
5-
import io.vertx.core.Vertx;
6-
import io.vertx.core.VertxOptions;
7-
import io.vertx.example.util.ExampleRunner;
8-
import io.vertx.example.util.Runner;
95

106
import java.lang.management.ManagementFactory;
117

core-examples/src/main/java/io/vertx/example/util/Runner.java

+75-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
package io.vertx.example.util;
22

3+
import io.vertx.core.DeploymentOptions;
4+
import io.vertx.core.Vertx;
5+
import io.vertx.core.VertxOptions;
6+
7+
import java.io.File;
8+
import java.io.IOException;
9+
import java.util.function.Consumer;
10+
311
/*
412
* @author <a href="http://tfox.org">Tim Fox</a>
513
*/
@@ -12,21 +20,21 @@ public class Runner {
1220
private static final String CORE_EXAMPLES_RUBY_DIR = CORE_EXAMPLES_DIR + "/src/main/rb/";
1321

1422
public static void runClusteredExample(Class clazz) {
15-
ExampleRunner.runJavaExample(CORE_EXAMPLES_JAVA_DIR, clazz, true);
23+
runExample(CORE_EXAMPLES_JAVA_DIR, clazz, new VertxOptions().setClustered(true), null);
1624
}
1725

1826
public static void runExample(Class clazz) {
19-
ExampleRunner.runJavaExample(CORE_EXAMPLES_JAVA_DIR, clazz, false);
27+
runExample(CORE_EXAMPLES_JAVA_DIR, clazz, new VertxOptions().setClustered(false), null);
2028
}
2129

2230
// JavaScript examples
2331

2432
public static void runJSExample(String scriptName) {
25-
ExampleRunner.runScriptExample(CORE_EXAMPLES_JS_DIR, scriptName, false);
33+
runScriptExample(CORE_EXAMPLES_JS_DIR, scriptName, new VertxOptions().setClustered(false));
2634
}
2735

2836
public static void runJSExampleClustered(String scriptName) {
29-
ExampleRunner.runScriptExample(CORE_EXAMPLES_JS_DIR, scriptName, true);
37+
runScriptExample(CORE_EXAMPLES_JS_DIR, scriptName, new VertxOptions().setClustered(true));
3038
}
3139

3240
static class JSEchoServerRunner {
@@ -176,11 +184,11 @@ public static void main(String[] args) {
176184
// Groovy examples
177185

178186
public static void runGroovyExample(String scriptName) {
179-
ExampleRunner.runScriptExample(CORE_EXAMPLES_GROOVY_DIR, scriptName, false);
187+
runScriptExample(CORE_EXAMPLES_GROOVY_DIR, scriptName, new VertxOptions().setClustered(false));
180188
}
181189

182190
public static void runGroovyExampleClustered(String scriptName) {
183-
ExampleRunner.runScriptExample(CORE_EXAMPLES_GROOVY_DIR, scriptName, true);
191+
runScriptExample(CORE_EXAMPLES_GROOVY_DIR, scriptName, new VertxOptions().setClustered(true));
184192
}
185193

186194
static class GroovyEchoServerRunner {
@@ -330,11 +338,11 @@ public static void main(String[] args) {
330338
// Ruby examples
331339

332340
public static void runRubyExample(String scriptName) {
333-
ExampleRunner.runScriptExample(CORE_EXAMPLES_RUBY_DIR, scriptName, false);
341+
runScriptExample(CORE_EXAMPLES_RUBY_DIR, scriptName, new VertxOptions().setClustered(false));
334342
}
335343

336344
public static void runRubyExampleClustered(String scriptName) {
337-
ExampleRunner.runScriptExample(CORE_EXAMPLES_GROOVY_DIR, scriptName, true);
345+
runScriptExample(CORE_EXAMPLES_GROOVY_DIR, scriptName, new VertxOptions().setClustered(true));
338346
}
339347

340348
static class RubyEchoServerRunner {
@@ -480,4 +488,63 @@ public static void main(String[] args) {
480488
Runner.runGroovyExample("io/vertx/example/core/verticle/deploy/deploy_example.rb");
481489
}
482490
}
491+
492+
public static void runExample(String exampleDir, Class clazz, VertxOptions options, DeploymentOptions
493+
deploymentOptions) {
494+
runExample(exampleDir + clazz.getPackage().getName().replace(".", "/"), clazz.getName(), options, deploymentOptions);
495+
}
496+
497+
498+
public static void runScriptExample(String prefix, String scriptName, VertxOptions options) {
499+
File file = new File(scriptName);
500+
String dirPart = file.getParent();
501+
String scriptDir = prefix + dirPart;
502+
runExample(scriptDir, scriptDir + "/" + file.getName(), options, null);
503+
}
504+
505+
public static void runExample(String exampleDir, String verticleID, VertxOptions options, DeploymentOptions deploymentOptions) {
506+
if (options == null) {
507+
// Default parameter
508+
options = new VertxOptions();
509+
}
510+
// Smart cwd detection
511+
512+
// Based on the current directory (.) and the desired directory (exampleDir), we try to compute the vertx.cwd
513+
// directory:
514+
try {
515+
// We need to use the canonical file. Without the file name is .
516+
File current = new File(".").getCanonicalFile();
517+
if (exampleDir.startsWith(current.getName()) && ! exampleDir.equals(current.getName())) {
518+
exampleDir = exampleDir.substring(current.getName().length() + 1);
519+
}
520+
} catch (IOException e) {
521+
// Ignore it.
522+
}
523+
524+
System.setProperty("vertx.cwd", exampleDir);
525+
Consumer<Vertx> runner = vertx -> {
526+
try {
527+
if (deploymentOptions != null) {
528+
vertx.deployVerticle(verticleID, deploymentOptions);
529+
} else {
530+
vertx.deployVerticle(verticleID);
531+
}
532+
} catch (Throwable t) {
533+
t.printStackTrace();
534+
}
535+
};
536+
if (options.isClustered()) {
537+
Vertx.clusteredVertx(options, res -> {
538+
if (res.succeeded()) {
539+
Vertx vertx = res.result();
540+
runner.accept(vertx);
541+
} else {
542+
res.cause().printStackTrace();
543+
}
544+
});
545+
} else {
546+
Vertx vertx = Vertx.vertx(options);
547+
runner.accept(vertx);
548+
}
549+
}
483550
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
package io.vertx.example.util;
22

33
import io.vertx.core.DeploymentOptions;
4+
import io.vertx.core.Vertx;
5+
import io.vertx.core.VertxOptions;
6+
7+
import java.io.File;
8+
import java.io.IOException;
9+
import java.util.function.Consumer;
410

511
/*
612
* @author <a href="http://tfox.org">Tim Fox</a>
@@ -15,44 +21,105 @@ public class Runner {
1521
private static final String EXAMPLES_RUBY_DIR = EXAMPLES_DIR + "/src/main/rb/";
1622

1723
public static void runClusteredExample(Class clazz) {
18-
ExampleRunner.runJavaExample(EXAMPLES_JAVA_DIR, clazz, true);
24+
runExample(EXAMPLES_JAVA_DIR, clazz,
25+
new VertxOptions().setClustered(true), null);
1926
}
2027

2128
public static void runExample(Class clazz) {
22-
ExampleRunner.runJavaExample(EXAMPLES_JAVA_DIR, clazz, false);
29+
runExample(EXAMPLES_JAVA_DIR, clazz,
30+
new VertxOptions().setClustered(false), null);
2331
}
2432

2533
public static void runExample(Class clazz, DeploymentOptions options) {
26-
ExampleRunner.runJavaExample(EXAMPLES_JAVA_DIR, clazz, options);
34+
runExample(EXAMPLES_JAVA_DIR, clazz,
35+
new VertxOptions().setClustered(false), options);
2736
}
2837

2938
// JavaScript examples
3039

3140
public static void runJSExample(String scriptName) {
32-
ExampleRunner.runScriptExample(EXAMPLES_JS_DIR, scriptName, false);
41+
runScriptExample(EXAMPLES_JS_DIR, scriptName, new VertxOptions().setClustered(false));
3342
}
3443

3544
public static void runJSExampleClustered(String scriptName) {
36-
ExampleRunner.runScriptExample(EXAMPLES_JS_DIR, scriptName, true);
45+
runScriptExample(EXAMPLES_JS_DIR, scriptName, new VertxOptions().setClustered(true));
3746
}
3847

3948
// Groovy examples
4049

4150
public static void runGroovyExample(String scriptName) {
42-
ExampleRunner.runScriptExample(EXAMPLES_GROOVY_DIR, scriptName, false);
51+
runScriptExample(EXAMPLES_GROOVY_DIR, scriptName, new VertxOptions().setClustered(false));
4352
}
4453

4554
public static void runGroovyExampleClustered(String scriptName) {
46-
ExampleRunner.runScriptExample(EXAMPLES_GROOVY_DIR, scriptName, true);
55+
runScriptExample(EXAMPLES_GROOVY_DIR, scriptName, new VertxOptions().setClustered(true));
4756
}
4857

4958
// Ruby examples
5059

5160
public static void runRubyExample(String scriptName) {
52-
ExampleRunner.runScriptExample(EXAMPLES_RUBY_DIR, scriptName, false);
61+
runScriptExample(EXAMPLES_RUBY_DIR, scriptName, new VertxOptions().setClustered(false));
5362
}
5463

5564
public static void runRubyExampleClustered(String scriptName) {
56-
ExampleRunner.runScriptExample(EXAMPLES_RUBY_DIR, scriptName, true);
65+
runScriptExample(EXAMPLES_RUBY_DIR, scriptName, new VertxOptions().setClustered(true));
66+
}
67+
68+
public static void runExample(String exampleDir, Class clazz, VertxOptions options, DeploymentOptions
69+
deploymentOptions) {
70+
runExample(exampleDir + clazz.getPackage().getName().replace(".", "/"), clazz.getName(), options, deploymentOptions);
71+
}
72+
73+
public static void runScriptExample(String prefix, String scriptName, VertxOptions options) {
74+
File file = new File(scriptName);
75+
String dirPart = file.getParent();
76+
String scriptDir = prefix + dirPart;
77+
runExample(scriptDir, scriptDir + "/" + file.getName(), options, null);
78+
}
79+
80+
public static void runExample(String exampleDir, String verticleID, VertxOptions options, DeploymentOptions deploymentOptions) {
81+
if (options == null) {
82+
// Default parameter
83+
options = new VertxOptions();
84+
}
85+
// Smart cwd detection
86+
87+
// Based on the current directory (.) and the desired directory (exampleDir), we try to compute the vertx.cwd
88+
// directory:
89+
try {
90+
// We need to use the canonical file. Without the file name is .
91+
File current = new File(".").getCanonicalFile();
92+
if (exampleDir.startsWith(current.getName()) && !exampleDir.equals(current.getName())) {
93+
exampleDir = exampleDir.substring(current.getName().length() + 1);
94+
}
95+
} catch (IOException e) {
96+
// Ignore it.
97+
}
98+
99+
System.setProperty("vertx.cwd", exampleDir);
100+
Consumer<Vertx> runner = vertx -> {
101+
try {
102+
if (deploymentOptions != null) {
103+
vertx.deployVerticle(verticleID, deploymentOptions);
104+
} else {
105+
vertx.deployVerticle(verticleID);
106+
}
107+
} catch (Throwable t) {
108+
t.printStackTrace();
109+
}
110+
};
111+
if (options.isClustered()) {
112+
Vertx.clusteredVertx(options, res -> {
113+
if (res.succeeded()) {
114+
Vertx vertx = res.result();
115+
runner.accept(vertx);
116+
} else {
117+
res.cause().printStackTrace();
118+
}
119+
});
120+
} else {
121+
Vertx vertx = Vertx.vertx(options);
122+
runner.accept(vertx);
123+
}
57124
}
58125
}

0 commit comments

Comments
 (0)