1
1
package io .vertx .example .util ;
2
2
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
+
3
11
/*
4
12
* @author <a href="http://tfox.org">Tim Fox</a>
5
13
*/
@@ -12,21 +20,21 @@ public class Runner {
12
20
private static final String CORE_EXAMPLES_RUBY_DIR = CORE_EXAMPLES_DIR + "/src/main/rb/" ;
13
21
14
22
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 );
16
24
}
17
25
18
26
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 );
20
28
}
21
29
22
30
// JavaScript examples
23
31
24
32
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 ) );
26
34
}
27
35
28
36
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 ) );
30
38
}
31
39
32
40
static class JSEchoServerRunner {
@@ -176,11 +184,11 @@ public static void main(String[] args) {
176
184
// Groovy examples
177
185
178
186
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 ) );
180
188
}
181
189
182
190
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 ) );
184
192
}
185
193
186
194
static class GroovyEchoServerRunner {
@@ -330,11 +338,11 @@ public static void main(String[] args) {
330
338
// Ruby examples
331
339
332
340
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 ) );
334
342
}
335
343
336
344
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 ) );
338
346
}
339
347
340
348
static class RubyEchoServerRunner {
@@ -480,4 +488,63 @@ public static void main(String[] args) {
480
488
Runner .runGroovyExample ("io/vertx/example/core/verticle/deploy/deploy_example.rb" );
481
489
}
482
490
}
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
+ }
483
550
}
0 commit comments