Search in sources :

Example 1 with FederatedTransitDataBundleCreator

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();
        }
    }
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) PrintStream(java.io.PrintStream) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) Properties(java.util.Properties) RemoteConnectFailureException(org.springframework.remoting.RemoteConnectFailureException) BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) FederatedTransitDataBundleCreator(org.onebusaway.transit_data_federation.bundle.FederatedTransitDataBundleCreator) GtfsBundle(org.onebusaway.transit_data_federation.bundle.model.GtfsBundle) FileOutputStream(java.io.FileOutputStream) GtfsRelationalDaoImpl(org.onebusaway.gtfs.impl.GtfsRelationalDaoImpl) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Aggregations

File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 PrintStream (java.io.PrintStream)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Properties (java.util.Properties)1 ZipFile (java.util.zip.ZipFile)1 GtfsRelationalDaoImpl (org.onebusaway.gtfs.impl.GtfsRelationalDaoImpl)1 FederatedTransitDataBundleCreator (org.onebusaway.transit_data_federation.bundle.FederatedTransitDataBundleCreator)1 GtfsBundle (org.onebusaway.transit_data_federation.bundle.model.GtfsBundle)1 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)1 BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)1 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)1 RemoteConnectFailureException (org.springframework.remoting.RemoteConnectFailureException)1