use of org.onebusaway.transit_data_federation.bundle.FederatedTransitDataBundleCreator in project onebusaway-application-modules by camsys.
the class BundleBuildingServiceImpl method build.
/**
* call FederatedTransitDataBundleCreator
*/
@Override
public int build(BundleBuildRequest request, BundleBuildResponse response) {
/*
* this follows the example from FederatedTransitDataBundleCreatorMain
*/
PrintStream stdOut = System.out;
PrintStream logFile = null;
// pass a mini spring context to the bundle builder so we can cleanup
ConfigurableApplicationContext context = null;
try {
File outputPath = new File(response.getBundleDataDirectory());
File loggingPath = new File(response.getBundleOutputDirectory());
// beans assume bundlePath is set -- this will be where files are written!
System.setProperty("bundlePath", outputPath.getAbsolutePath());
String logFilename = outputPath + File.separator + "bundleBuilder.out.txt";
logFile = new PrintStream(new FileOutputStream(new File(logFilename)));
// swap standard out for logging
System.setOut(logFile);
configureLogging(System.out);
FederatedTransitDataBundleCreator creator = new FederatedTransitDataBundleCreator();
Map<String, BeanDefinition> beans = new HashMap<String, BeanDefinition>();
creator.setContextBeans(beans);
List<GtfsBundle> gtfsBundles = createGtfsBundles(response);
List<String> contextPaths = new ArrayList<String>();
contextPaths.add(BUNDLE_RESOURCE);
BeanDefinitionBuilder bean = BeanDefinitionBuilder.genericBeanDefinition(GtfsBundles.class);
bean.addPropertyValue("bundles", gtfsBundles);
beans.put("gtfs-bundles", bean.getBeanDefinition());
bean = BeanDefinitionBuilder.genericBeanDefinition(GtfsRelationalDaoImpl.class);
beans.put("gtfsRelationalDaoImpl", bean.getBeanDefinition());
BeanDefinitionBuilder multiCSVLogger = BeanDefinitionBuilder.genericBeanDefinition(MultiCSVLogger.class);
multiCSVLogger.addPropertyValue("basePath", loggingPath);
beans.put("multiCSVLogger", multiCSVLogger.getBeanDefinition());
BeanDefinitionBuilder entityReplacementLogger = BeanDefinitionBuilder.genericBeanDefinition(EntityReplacementLoggerImpl.class);
beans.put("entityReplacementLogger", entityReplacementLogger.getBeanDefinition());
BeanDefinitionBuilder requestDef = BeanDefinitionBuilder.genericBeanDefinition(BundleRequestResponse.class);
requestDef.addPropertyValue("request", request);
requestDef.addPropertyValue("response", response);
beans.put("bundleRequestResponse", requestDef.getBeanDefinition());
// configure for NYC specifics
BeanDefinitionBuilder bundle = BeanDefinitionBuilder.genericBeanDefinition(FederatedTransitDataBundle.class);
bundle.addPropertyValue("path", outputPath);
beans.put("bundle", bundle.getBeanDefinition());
BeanDefinitionBuilder outputDirectoryReference = BeanDefinitionBuilder.genericBeanDefinition(String.class);
outputDirectoryReference.addPropertyValue("", response.getBundleOutputDirectory());
// TODO move this to application-context-bunlde-admin.xml and have it look for config to turn on/off
BeanDefinitionBuilder task = null;
if (isStifTaskApplicable()) {
addStifTask(beans, request, response);
}
if (isStopConsolidationApplicable()) {
addStopConsolidationMappings(beans, request, response);
}
_log.debug("setting outputPath=" + outputPath);
creator.setOutputPath(outputPath);
creator.setContextPaths(contextPaths);
// manage our own overrides, as we use our own context
Properties cmdOverrides = new Properties();
cmdOverrides.setProperty(ARG_THROW_EXCEPTION_INVALID_STOPS, "false");
cmdOverrides.setProperty(ARG_LENIENT_ARRIVAL_DEPARTURE, "true");
if (this.getStopVerificationURL() != null) {
cmdOverrides.setProperty("stopVerificationTask.path", this.getStopVerificationURL());
cmdOverrides.setProperty("stopVerificationDistanceTask.path", this.getStopVerificationURL());
}
String stopMappingUrl = getStopMappingUrl();
if (stopMappingUrl != null) {
cmdOverrides.setProperty("stopConsolidationFileTask.stopConsolidationUrl", stopMappingUrl);
}
creator.setAdditionalBeanPropertyOverrides(cmdOverrides);
BeanDefinitionBuilder propertyOverrides = BeanDefinitionBuilder.genericBeanDefinition(PropertyOverrideConfigurer.class);
propertyOverrides.addPropertyValue("properties", cmdOverrides);
beans.put("myCustomPropertyOverrides", propertyOverrides.getBeanDefinition());
// manage our own context to recover from exceptions
Map<String, BeanDefinition> contextBeans = new HashMap<String, BeanDefinition>();
contextBeans.putAll(beans);
context = ContainerLibrary.createContext(contextPaths, contextBeans);
creator.setContext(context);
response.addStatusMessage("building bundle");
monitorStatus(response, creator.getStatusMessages());
creator.run();
demonitorStatus();
// If this is a rebuild of a bundle, re-use the previous bundle id.
updateBundleId(request, response);
response.addStatusMessage("bundle build complete");
return 0;
} catch (Exception e) {
_log.error(e.toString(), e);
response.setException(e);
return 1;
} catch (Throwable t) {
_log.error(t.toString(), t);
response.setException(new RuntimeException(t.toString()));
return -1;
} finally {
if (context != null) {
try {
/*
* here we cleanup the spring context so we can process follow on requests.
*/
context.stop();
context.close();
} catch (Throwable t) {
_log.error("buried context close:", t);
}
}
// restore standard out
deconfigureLogging(System.out);
System.setOut(stdOut);
if (logFile != null) {
logFile.close();
}
}
}
Aggregations