use of org.opentripplanner.scripting.impl.BSFOTPScript in project OpenTripPlanner by opentripplanner.
the class OTPMain method run.
/**
* Making OTPMain a concrete class and placing this logic an instance method instead of embedding it in the static
* main method makes it possible to build graphs from web services or scripts, not just from the command line.
*/
public void run() {
// TODO do params.infer() here to ensure coherency?
/* Create the top-level objects that represent the OTP server. */
makeGraphService();
otpServer = new OTPServer(params, graphService);
/* Start graph builder if requested */
if (params.build != null) {
// TODO multiple directories
GraphBuilder graphBuilder = GraphBuilder.forDirectory(params, params.build);
if (graphBuilder != null) {
graphBuilder.run();
/* If requested, hand off the graph to the server as the default graph using an in-memory GraphSource. */
if (params.inMemory || params.preFlight) {
Graph graph = graphBuilder.getGraph();
graph.index(new DefaultStreetVertexIndexFactory());
// FIXME set true router IDs
graphService.registerGraph("", new MemoryGraphSource("", graph));
}
} else {
LOG.error("An error occurred while building the graph. Exiting.");
System.exit(-1);
}
}
// FIXME eventually router IDs will be present even when just building a graph.
if ((params.routerIds != null && params.routerIds.size() > 0) || params.autoScan) {
/* Auto-register pre-existing graph on disk, with optional auto-scan. */
GraphScanner graphScanner = new GraphScanner(graphService, params.graphDirectory, params.autoScan);
graphScanner.basePath = params.graphDirectory;
if (params.routerIds != null && params.routerIds.size() > 0) {
graphScanner.defaultRouterId = params.routerIds.get(0);
}
graphScanner.autoRegister = params.routerIds;
graphScanner.startup();
}
/* Start visualizer if requested */
if (params.visualize) {
Router defaultRouter = graphService.getRouter();
defaultRouter.graphVisualizer = new GraphVisualizer(defaultRouter);
defaultRouter.graphVisualizer.run();
// avoid timeouts due to search animation
defaultRouter.timeouts = new double[] { 60 };
}
/* Start script if requested */
if (params.scriptFile != null) {
try {
OTPScript otpScript = new BSFOTPScript(otpServer, params.scriptFile);
if (otpScript != null) {
Object retval = otpScript.run();
if (retval != null) {
LOG.warn("Your script returned something, no idea what to do with it: {}", retval);
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/* Start web server if requested */
if (params.server) {
GrizzlyServer grizzlyServer = new GrizzlyServer(params, otpServer);
while (true) {
// Loop to restart server on uncaught fatal exceptions.
try {
grizzlyServer.run();
return;
} catch (Throwable throwable) {
LOG.error("An uncaught {} occurred inside OTP. Restarting server.", throwable.getClass().getSimpleName(), throwable);
}
}
}
}
Aggregations