use of org.onebusaway.transit_data_federation.bundle.model.GtfsBundle in project onebusaway-application-modules by camsys.
the class GtfsReadingSupport method readGtfsIntoStore.
/**
* Read gtfs, as defined by {@link GtfsBundles} entries in the application
* context, into the specified data store. Gtfs will be read in quasi-paralle
* mode using {@link GtfsMultiReaderImpl}. Any
* {@link EntityReplacementStrategy} strategies defined in the application
* context will be applied as well.
*
* @param context
* @param store
* @param factory
* @param disableStopConsolidation
* @throws IOException
*/
public static void readGtfsIntoStore(ApplicationContext context, GenericMutableDao store, DefaultEntitySchemaFactory factory, boolean disableStopConsolidation) throws IOException {
GtfsMultiReaderImpl multiReader = new GtfsMultiReaderImpl();
multiReader.setStore(store);
if (!disableStopConsolidation && context.containsBean("entityReplacementStrategy")) {
EntityReplacementStrategy strategy = (EntityReplacementStrategy) context.getBean("entityReplacementStrategy");
multiReader.setEntityReplacementStrategy(strategy);
if (context.containsBean("multiCSVLogger")) {
MultiCSVLogger csvLogger = (MultiCSVLogger) context.getBean("multiCSVLogger");
if (context.containsBean("entityReplacementLogger")) {
EntityReplacementLogger entityLogger = (EntityReplacementLogger) context.getBean("entityReplacementLogger");
entityLogger.setMultiCSVLogger(csvLogger);
csvLogger.addListener(entityLogger.getListener());
multiReader.setEntityReplacementLogger(entityLogger);
}
}
}
GtfsBundles gtfsBundles = getGtfsBundles(context);
for (GtfsBundle gtfsBundle : gtfsBundles.getBundles()) {
System.out.println("gtfs=" + gtfsBundle.getPath());
GtfsReader reader = new GtfsReader();
reader.setEntitySchemaFactory(factory);
reader.setInputLocation(gtfsBundle.getPath());
if (gtfsBundle.getDefaultAgencyId() != null)
reader.setDefaultAgencyId(gtfsBundle.getDefaultAgencyId());
for (Map.Entry<String, String> entry : gtfsBundle.getAgencyIdMappings().entrySet()) reader.addAgencyIdMapping(entry.getKey(), entry.getValue());
multiReader.addGtfsReader(reader);
}
multiReader.run();
}
use of org.onebusaway.transit_data_federation.bundle.model.GtfsBundle in project onebusaway-application-modules by camsys.
the class GtfsReadingSupport method getGtfsBundles.
/**
* Looks for instances of {@link GtfsBundles} or {@link GtfsBundle} in the
* application context.
*
* @param context
* @return
*/
public static GtfsBundles getGtfsBundles(ApplicationContext context) {
GtfsBundles bundles = (GtfsBundles) context.getBean("gtfs-bundles");
if (bundles != null)
return bundles;
GtfsBundle bundle = (GtfsBundle) context.getBean("gtfs-bundle");
if (bundle != null) {
bundles = new GtfsBundles();
bundles.getBundles().add(bundle);
return bundles;
}
throw new IllegalStateException("must define either \"gtfs-bundles\" or \"gtfs-bundle\" in config");
}
use of org.onebusaway.transit_data_federation.bundle.model.GtfsBundle in project onebusaway-application-modules by camsys.
the class FederatedTransitDataBundleCreatorMain method run.
public void run(String[] args) throws Exception {
try {
Parser parser = new GnuParser();
Options options = new Options();
buildOptions(options);
CommandLine commandLine = parser.parse(options, args);
String[] remainingArgs = commandLine.getArgs();
if (remainingArgs.length < 2) {
printUsage();
System.exit(-1);
}
FederatedTransitDataBundleCreator creator = new FederatedTransitDataBundleCreator();
Map<String, BeanDefinition> beans = new HashMap<String, BeanDefinition>();
creator.setContextBeans(beans);
List<GtfsBundle> gtfsBundles = new ArrayList<GtfsBundle>();
List<String> contextPaths = new ArrayList<String>();
for (int i = 0; i < remainingArgs.length - 1; i++) {
File path = new File(remainingArgs[i]);
if (path.isDirectory() || path.getName().endsWith(".zip")) {
GtfsBundle gtfsBundle = new GtfsBundle();
gtfsBundle.setPath(path);
gtfsBundles.add(gtfsBundle);
} else {
contextPaths.add("file:" + path);
}
}
if (!gtfsBundles.isEmpty()) {
BeanDefinitionBuilder bean = BeanDefinitionBuilder.genericBeanDefinition(GtfsBundles.class);
bean.addPropertyValue("bundles", gtfsBundles);
beans.put("gtfs-bundles", bean.getBeanDefinition());
}
if (commandLine.hasOption(ARG_USE_DATABASE_FOR_GTFS)) {
contextPaths.add("classpath:org/onebusaway/gtfs/application-context.xml");
} else {
BeanDefinitionBuilder bean = BeanDefinitionBuilder.genericBeanDefinition(GtfsRelationalDaoImpl.class);
beans.put("gtfsRelationalDaoImpl", bean.getBeanDefinition());
}
if (commandLine.hasOption(ARG_DATASOURCE_URL)) {
String dataSourceUrl = commandLine.getOptionValue(ARG_DATASOURCE_URL);
BeanDefinitionBuilder bean = BeanDefinitionBuilder.genericBeanDefinition(DriverManagerDataSource.class);
bean.addPropertyValue("url", dataSourceUrl);
if (commandLine.hasOption(ARG_DATASOURCE_DRIVER_CLASS_NAME))
bean.addPropertyValue("driverClassName", commandLine.getOptionValue(ARG_DATASOURCE_DRIVER_CLASS_NAME));
if (commandLine.hasOption(ARG_DATASOURCE_USERNAME))
bean.addPropertyValue("username", commandLine.getOptionValue(ARG_DATASOURCE_USERNAME));
if (commandLine.hasOption(ARG_DATASOURCE_PASSWORD))
bean.addPropertyValue("password", commandLine.getOptionValue(ARG_DATASOURCE_PASSWORD));
beans.put("dataSource", bean.getBeanDefinition());
}
File outputPath = new File(remainingArgs[remainingArgs.length - 1]);
if (commandLine.hasOption(ARG_ONLY_IF_DNE) && outputPath.exists()) {
System.err.println("Bundle path already exists. Exiting...");
System.exit(0);
}
if (commandLine.hasOption(ARG_RANDOMIZE_CACHE_DIR))
creator.setRandomizeCacheDir(true);
if (commandLine.hasOption(ARG_BUNDLE_KEY)) {
String key = commandLine.getOptionValue(ARG_BUNDLE_KEY);
creator.setBundleKey(key);
}
/**
* Optionally override any system properties (ok this duplicates existing
* functionality, yes, but it allows for -D arguments after the main
* class)
*/
if (commandLine.hasOption("D")) {
Properties props = commandLine.getOptionProperties("D");
for (Object key : props.keySet()) {
String propName = (String) key;
String propValue = props.getProperty(propName);
System.setProperty(propName, propValue);
}
}
/**
* Optionally override any system properties (ok this duplicates existing
* functionality, yes, but it allows for -D arguments after the main
* class)
*/
if (commandLine.hasOption("P")) {
Properties props = commandLine.getOptionProperties("P");
creator.setAdditionalBeanPropertyOverrides(props);
}
setStagesToSkip(commandLine, creator);
creator.setOutputPath(outputPath);
creator.setContextPaths(contextPaths);
try {
if (commandLine.hasOption(ARG_ADDITIONAL_RESOURCES_DIRECTORY)) {
File additionalResourceDirectory = new File(commandLine.getOptionValue(ARG_ADDITIONAL_RESOURCES_DIRECTORY));
copyFiles(additionalResourceDirectory, outputPath);
}
creator.run();
} catch (Exception ex) {
_log.error("error building transit data bundle", ex);
System.exit(-1);
}
} catch (ParseException ex) {
System.err.println(ex.getLocalizedMessage());
printUsage();
System.exit(-1);
}
System.exit(0);
}
use of org.onebusaway.transit_data_federation.bundle.model.GtfsBundle 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();
}
}
}
use of org.onebusaway.transit_data_federation.bundle.model.GtfsBundle in project onebusaway-application-modules by camsys.
the class GtfsArchiveTask method run.
@Override
public void run() {
if (!requestResponse.getRequest().getArchiveFlag()) {
_log.info("archive flag not set, exiting");
return;
}
long start = SystemTime.currentTimeMillis();
_log.info("archiving gtfs");
Configuration config = getConfiguration();
if (config == null) {
_log.error("missing configuration, GTFS will not be archived");
return;
}
SessionFactory sessionFactory = config.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
HibernateGtfsFactory factory = new HibernateGtfsFactory(sessionFactory);
GtfsBundles gtfsBundles = getGtfsBundles(_applicationContext);
Integer gtfsBundleInfoId = createMetaData(session, requestResponse);
for (GtfsBundle gtfsBundle : gtfsBundles.getBundles()) {
GtfsReader reader = new GtfsReader();
reader.getEntityClasses().add(PatternPair.class);
try {
cleanTempTables(session);
reader.setInputLocation(gtfsBundle.getPath());
GtfsMutableRelationalDao dao = factory.getDao();
reader.setEntityStore(dao);
_log.info("running for gtfs=" + gtfsBundle.getPath());
reader.run();
reader.close();
archiveData(session, gtfsBundleInfoId);
} catch (IOException e) {
_log.error("gtfs archive failure:", e);
}
}
cleanTempTables(session);
transaction.commit();
session.flush();
session.close();
long stop = SystemTime.currentTimeMillis();
_log.info("archiving gtfs complete in " + (stop - start) / 1000 + "s");
}
Aggregations