forked from seeebiii/osm-routing-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DropwizardApplication.java
129 lines (101 loc) · 5.17 KB
/
DropwizardApplication.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package de.sebastianhesse.pbf.dropwizard;
import de.sebastianhesse.pbf.dropwizard.healtchecks.GraphHealthCheck;
import de.sebastianhesse.pbf.dropwizard.healtchecks.StrategyHealthCheck;
import de.sebastianhesse.pbf.dropwizard.resources.HelloWorldResource;
import de.sebastianhesse.pbf.dropwizard.resources.MetaResource;
import de.sebastianhesse.pbf.dropwizard.resources.PoiResource;
import de.sebastianhesse.pbf.dropwizard.resources.RoutingResource;
import de.sebastianhesse.pbf.dropwizard.resources.TrafficResource;
import de.sebastianhesse.pbf.reader.NodeEdgeReader;
import de.sebastianhesse.pbf.reader.OptimizedNodeEdgeReader;
import de.sebastianhesse.pbf.reader.SimpleNodeEdgeReader;
import de.sebastianhesse.pbf.storage.Graph;
import de.sebastianhesse.pbf.storage.traffic.TrafficHandler;
import io.dropwizard.Application;
import io.dropwizard.assets.AssetsBundle;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Arrays;
/**
* Application to start up a dropwizard server. Configures resources, health checks and loads the OSM file.
*/
public class DropwizardApplication extends Application<DropwizardConfiguration> {
private static final Logger logger = LoggerFactory.getLogger(DropwizardApplication.class);
private String osmFile = "";
private String locationListPath = "";
private String eventListPath = "";
private String tmcDataDirectory = "";
public DropwizardApplication(String osmFile, String locationListPath, String eventListPath, String tmcDataDirectory) {
this.osmFile = osmFile;
this.locationListPath = locationListPath;
this.eventListPath = eventListPath;
this.tmcDataDirectory = tmcDataDirectory;
}
public static void main(String[] args) throws Exception {
// strip out the osm file path, otherwise Dropwizard complains about an unknown parameter;
// the unknown parameter could be addressed by adding a custom command, but it's okay for this use case
String[] argsWithoutOsmFile = Arrays.copyOfRange(args, 0, 2);
String locationListPath = args.length > 3 ? args[3] : "";
String eventListPath = args.length > 4 ? args[4] : "";
String tmcDataDirectory = args.length > 5 ? args[5] : "";
new DropwizardApplication(args[2], locationListPath, eventListPath, tmcDataDirectory).run(argsWithoutOsmFile);
}
@Override
public String getName() {
return "dropwizard-osm-server";
}
@Override
public void initialize(Bootstrap<DropwizardConfiguration> bootstrap) {
// in order to serve HTML files and alike, the root path of the server serves our assets
bootstrap.addBundle(new AssetsBundle("/assets/", "/", "index.html"));
bootstrap.addBundle(new AssetsBundle("/assets/css", "/css", null, "css"));
bootstrap.addBundle(new AssetsBundle("/assets/js", "/js", null, "js"));
bootstrap.addBundle(new AssetsBundle("/assets/fonts", "/fonts", null, "fonts"));
}
@Override
public void run(DropwizardConfiguration configuration,
Environment environment) {
// set url pattern for resources, i.e. they can be accessed by /api/{resourcePath}
environment.jersey().setUrlPattern("/api/*");
environment.jersey().register(new HelloWorldResource());
// import OSM data and register routing resource
NodeEdgeReader reader = getNodeEdgeReader(configuration);
Graph graph = null;
TrafficHandler trafficHandler = null;
try {
reader.importData();
graph = reader.getGraph();
trafficHandler = reader.getTrafficHandler();
trafficHandler.setGraph(graph);
} catch (Exception e) {
logger.info("Something went wrong while reading OSM data. See error log.");
logger.error("", e);
}
final RoutingResource routingResource = new RoutingResource(graph);
environment.jersey().register(routingResource);
final PoiResource poiResource = new PoiResource(graph);
environment.jersey().register(poiResource);
final MetaResource metaResource = new MetaResource(configuration, osmFile, graph);
environment.jersey().register(metaResource);
final TrafficResource trafficResource = new TrafficResource(trafficHandler);
environment.jersey().register(trafficResource);
// health checks
environment.healthChecks().register("GraphHealthCheck", new GraphHealthCheck(graph));
environment.healthChecks().register("ReaderStrategyHealthCheck", new StrategyHealthCheck(configuration));
}
private NodeEdgeReader getNodeEdgeReader(DropwizardConfiguration configuration) {
NodeEdgeReader reader;
switch (configuration.getReaderStrategy()) {
case OPTIMIZED:
reader = new OptimizedNodeEdgeReader(osmFile, locationListPath, eventListPath, tmcDataDirectory);
break;
case SIMPLE:
default:
reader = new SimpleNodeEdgeReader(osmFile, locationListPath, eventListPath, tmcDataDirectory);
break;
}
return reader;
}
}