|
1 |
| -package com.codingchili; |
2 |
| - |
3 |
| -import java.util.Optional; |
4 |
| -import java.util.logging.Level; |
5 |
| -import java.util.logging.Logger; |
6 |
| - |
7 |
| -import com.codingchili.Controller.Website; |
8 |
| -import com.codingchili.Model.Configuration; |
9 |
| -import com.codingchili.Model.ElasticWriter; |
10 |
| -import com.codingchili.Model.FileParser; |
11 |
| -import com.codingchili.Model.ParserException; |
12 |
| - |
13 |
| -import io.vertx.core.*; |
14 |
| - |
15 |
| -/** |
16 |
| - * @author Robin Duda |
17 |
| - * |
18 |
| - * Launcher class to bootstrap the application. |
19 |
| - */ |
20 |
| -public class Launcher { |
21 |
| - private static final Logger logger = Logger.getLogger(Launcher.class.getName()); |
22 |
| - private static final String COMMAND_IMPORT = "import"; |
23 |
| - private Vertx vertx = Vertx.vertx(); |
24 |
| - |
25 |
| - public static void main(String[] args) { |
26 |
| - new Launcher(args); |
27 |
| - } |
28 |
| - |
29 |
| - public Launcher(String[] args) { |
30 |
| - logger.info("Starting application.."); |
31 |
| - Future<Void> future = Future.future(); |
32 |
| - |
33 |
| - future.setHandler(done -> { |
34 |
| - if (done.succeeded()) { |
35 |
| - logger.info("Successfully started application"); |
36 |
| - getImportFileName(args).ifPresent(this::importFile); |
37 |
| - } else { |
38 |
| - logger.log(Level.SEVERE, "Failed to start application", done.cause()); |
39 |
| - } |
40 |
| - }); |
41 |
| - |
42 |
| - start(future); |
43 |
| - } |
44 |
| - |
45 |
| - private void importFile(String fileName) { |
46 |
| - logger.info(String.format("Loading file %s from filesystem..", fileName)); |
47 |
| - vertx.fileSystem().readFile(fileName, file -> { |
48 |
| - if (file.succeeded()) { |
49 |
| - logger.info("Parsing XLSX file to JSON.."); |
50 |
| - try { |
51 |
| - FileParser parser = new FileParser(file.result().getBytes(), 1); |
52 |
| - logger.info("File parsed, starting import to elasticsearch.."); |
53 |
| - vertx.eventBus().send(Configuration.INDEXING_ELASTICSEARCH, parser.toImportableObject()); |
54 |
| - } catch (ParserException e) { |
55 |
| - logger.log(Level.SEVERE, String.format("Failed to import file %s", fileName)); |
56 |
| - } |
57 |
| - } else { |
58 |
| - logger.log(Level.SEVERE, String.format("Failed to load file %s", fileName), file.cause()); |
59 |
| - } |
60 |
| - }); |
61 |
| - } |
62 |
| - |
63 |
| - private static Optional<String> getImportFileName(String[] args) { |
64 |
| - for (int i = 0; i < args.length; i++) { |
65 |
| - switch (args[i]) { |
66 |
| - case COMMAND_IMPORT: |
67 |
| - return Optional.of(args[++i]); |
68 |
| - } |
69 |
| - } |
70 |
| - return Optional.empty(); |
71 |
| - } |
72 |
| - |
73 |
| - public void start(Future<Void> start) { |
74 |
| - Future<String> writer = Future.future(); |
75 |
| - Future<String> website = Future.future(); |
76 |
| - vertx.deployVerticle(new ElasticWriter(), writer.completer()); |
77 |
| - vertx.deployVerticle(new Website(), website.completer()); |
78 |
| - |
79 |
| - CompositeFuture.all(writer, website).setHandler(done -> { |
80 |
| - if (done.succeeded()) { |
81 |
| - start.complete(); |
82 |
| - } else { |
83 |
| - start.fail(done.cause()); |
84 |
| - } |
85 |
| - }); |
86 |
| - } |
87 |
| -} |
| 1 | +package com.codingchili; |
| 2 | + |
| 3 | +import java.util.logging.Level; |
| 4 | +import java.util.logging.Logger; |
| 5 | + |
| 6 | +import com.codingchili.Controller.Website; |
| 7 | +import com.codingchili.Model.Configuration; |
| 8 | +import com.codingchili.Model.ElasticWriter; |
| 9 | +import com.codingchili.Model.FileParser; |
| 10 | +import com.codingchili.Model.ParserException; |
| 11 | + |
| 12 | +import io.vertx.core.*; |
| 13 | + |
| 14 | +/** |
| 15 | + * @author Robin Duda |
| 16 | + * |
| 17 | + * ApplicationLauncher class to bootstrap the application. |
| 18 | + */ |
| 19 | +public class ApplicationLauncher { |
| 20 | + private static final Logger logger = Logger.getLogger(ApplicationLauncher.class.getName()); |
| 21 | + public static String version = "1.1.0"; |
| 22 | + private Vertx vertx = Vertx.vertx(); |
| 23 | + private String[] args; |
| 24 | + |
| 25 | + public static void main(String[] args) { |
| 26 | + new ApplicationLauncher(args); |
| 27 | + } |
| 28 | + |
| 29 | + public ApplicationLauncher(String[] args) { |
| 30 | + this.args = args; |
| 31 | + |
| 32 | + logger.info(String.format("Starting excelastic %s..", version)); |
| 33 | + Future<Void> future = Future.future(); |
| 34 | + |
| 35 | + future.setHandler(done -> { |
| 36 | + if (done.succeeded()) { |
| 37 | + logger.info("Successfully started application"); |
| 38 | + |
| 39 | + if (args.length == 3) { |
| 40 | + importFile(getFileName(), getIndexName()); |
| 41 | + } |
| 42 | + } else { |
| 43 | + logger.log(Level.SEVERE, "Failed to start application", done.cause()); |
| 44 | + vertx.close(); |
| 45 | + } |
| 46 | + }); |
| 47 | + start(future); |
| 48 | + } |
| 49 | + |
| 50 | + private String getFileName() { |
| 51 | + return args[1]; |
| 52 | + } |
| 53 | + |
| 54 | + private String getIndexName() { |
| 55 | + return args[2]; |
| 56 | + } |
| 57 | + |
| 58 | + private void importFile(String fileName, String indexName) { |
| 59 | + logger.info(String.format("Loading file %s from filesystem..", fileName)); |
| 60 | + vertx.fileSystem().readFile(fileName, file -> { |
| 61 | + if (file.succeeded()) { |
| 62 | + logger.info("Parsing XLSX file to JSON.."); |
| 63 | + try { |
| 64 | + FileParser parser = new FileParser(file.result().getBytes(), 1, fileName); |
| 65 | + logger.info("File parsed, starting import to elasticsearch.."); |
| 66 | + vertx.eventBus().send(Configuration.INDEXING_ELASTICSEARCH, parser.toImportable(indexName)); |
| 67 | + } catch (ParserException e) { |
| 68 | + logger.log(Level.SEVERE, String.format("Failed to import file %s", fileName)); |
| 69 | + } |
| 70 | + } else { |
| 71 | + logger.log(Level.SEVERE, String.format("Failed to load file %s", fileName), file.cause()); |
| 72 | + } |
| 73 | + }); |
| 74 | + } |
| 75 | + |
| 76 | + public void start(Future<Void> start) { |
| 77 | + Future<String> writer = Future.future(); |
| 78 | + Future<String> website = Future.future(); |
| 79 | + vertx.deployVerticle(new ElasticWriter(), writer.completer()); |
| 80 | + vertx.deployVerticle(new Website(), website.completer()); |
| 81 | + |
| 82 | + CompositeFuture.all(writer, website).setHandler(done -> { |
| 83 | + if (done.succeeded()) { |
| 84 | + start.complete(); |
| 85 | + } else { |
| 86 | + start.fail(done.cause()); |
| 87 | + } |
| 88 | + }); |
| 89 | + } |
| 90 | +} |
0 commit comments