Example usage for org.springframework.core.io FileSystemResource FileSystemResource

List of usage examples for org.springframework.core.io FileSystemResource FileSystemResource

Introduction

In this page you can find the example usage for org.springframework.core.io FileSystemResource FileSystemResource.

Prototype

public FileSystemResource(Path filePath) 

Source Link

Document

Create a new FileSystemResource from a Path handle, performing all file system interactions via NIO.2 instead of File .

Usage

From source file:org.mzd.shap.spring.cli.ConfigSetup.java

public static void main(String[] args) {
    // check args
    if (args.length != 1) {
        exitOnError(1, null);/*  w w  w.  j  ava 2  s  .  c  o  m*/
    }

    // check file existance
    File analyzerXML = new File(args[0]);
    if (!analyzerXML.exists()) {
        exitOnError(1, "'" + analyzerXML.getPath() + "' did not exist\n");
    }

    // prompt user whether existing data should be purged
    boolean isPurged = false;
    String ormContext = null;
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    try {
        System.out.println("\nDo you wish to purge the database before running setup?");
        System.out.println("WARNING: all existing data in SHAP will be lost!");
        System.out.println("Really purge? yes/[NO]");
        String ans = br.readLine();
        if (ans.toLowerCase().equals("yes")) {
            System.out.println("Purging enabled");
            ormContext = "orm-purge-context.xml";
            isPurged = true;
        } else {
            System.out.println("Purging disabled");
            ormContext = "orm-context.xml";
        }
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }

    // run tool
    try {
        // Using a generic application context since we're referencing
        // both classpath and filesystem resources.
        GenericApplicationContext ctx = new GenericApplicationContext();
        XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(ctx);
        xmlReader.loadBeanDefinitions(new ClassPathResource("datasource-context.xml"),
                new ClassPathResource(ormContext), new FileSystemResource(analyzerXML));
        ctx.refresh();

        /*
         * Create an base admin user.
         */
        if (isPurged) {
            //only attempted if we've wiped the old database.
            RoleDao roleDao = (RoleDao) ctx.getBean("roleDao");
            Role adminRole = roleDao.saveOrUpdate(new Role("admin", "ROLE_ADMIN"));
            Role userRole = roleDao.saveOrUpdate(new Role("user", "ROLE_USER"));
            UserDao userDao = (UserDao) ctx.getBean("userDao");
            userDao.saveOrUpdate(new User("admin", "admin", "shap01", adminRole, userRole));
        }

        /*
         * Create some predefined analyzers. Users should have modified
         * the configuration file to suit their environment.
         */
        AnnotatorDao annotatorDao = (AnnotatorDao) ctx.getBean("annotatorDao");
        DetectorDao detectorDao = (DetectorDao) ctx.getBean("detectorDao");

        ConfigSetup config = (ConfigSetup) ctx.getBean("configuration");

        for (Annotator an : config.getAnnotators()) {
            System.out.println("Adding annotator: " + an.getName());
            annotatorDao.saveOrUpdate(an);
        }

        for (Detector dt : config.getDetectors()) {
            System.out.println("Adding detector: " + dt.getName());
            detectorDao.saveOrUpdate(dt);
        }

        System.exit(0);
    } catch (Throwable t) {
        System.err.println(t.getMessage());
        System.exit(1);
    }
}

From source file:ca.uhn.hunit.run.TestRunner.java

/**
 * @param args//from w w w. j a v a 2 s  . co  m
 * @throws URISyntaxException
 * @throws JAXBException
 * @throws ConfigurationException
 * @throws InterfaceWontStartException
 * @throws FileNotFoundException
 * @throws ParseException
 */
public static void main(String[] theArgs) throws URISyntaxException, JAXBException, InterfaceWontStartException,
        ConfigurationException, FileNotFoundException, ParseException {
    Options options = new Options();

    OptionGroup fileOptionGroup = new OptionGroup();
    fileOptionGroup.setRequired(false);

    Option option = new Option("f", "file", true, "The path to the file to load the test battery from");
    option.setValueSeparator('=');
    fileOptionGroup.addOption(option);
    option = new Option("c", "classpath", true, "The classpath path to the file to load the test battery from");
    option.setValueSeparator('=');
    fileOptionGroup.addOption(option);
    options.addOptionGroup(fileOptionGroup);

    OptionGroup uiOptionGroup = new OptionGroup();
    option = new Option("g", "gui", false, "Start hUnit in GUI mode (default)");
    uiOptionGroup.addOption(option);
    option = new Option("x", "text", false, "Start hUnit in Text mode");
    uiOptionGroup.addOption(option);
    options.addOptionGroup(uiOptionGroup);

    option = new Option("t", "tests", true, "A comma separated list of tests to execute (default is all)");
    option.setValueSeparator('=');
    option.setRequired(false);
    options.addOption(option);

    Resource defFile = null;
    CommandLine parser;
    boolean textMode = false;

    try {
        parser = new PosixParser().parse(options, theArgs);

        if (parser.hasOption("f")) {
            defFile = new FileSystemResource(parser.getOptionValue("f"));
        } else if (parser.hasOption("c")) {
            defFile = new ClassPathResource(parser.getOptionValue("c"));
        }

        if (parser.hasOption("x")) {
            textMode = true;
        }
    } catch (Exception e) {
        HelpFormatter hf = new HelpFormatter();
        hf.printHelp("java -jar hunit-[version]-jar-with-dependencies.jar [-c FILE|-f FILE] [options]",
                options);

        return;
    }

    String[] testsToExecute = null;

    if (parser.hasOption("t")) {
        testsToExecute = parser.getOptionValue("t").split(",");
    }

    if (textMode) {
        executeInTextMode(defFile, testsToExecute);
    } else {
        executeInGuiMode(defFile, testsToExecute);
    }
}

From source file:config.ResourceUtils.java

/**
 * Gets a resource by its relative path. If the resource is not found on the
 * file system, the classpath is searched. If nothing is found, null is
 * returned./*from   w w  w. jav  a  2 s  .  c o  m*/
 *
 * @param fileName the name of the resource
 * @return the found resource
 */
public static Resource getResourceByRelativePath(String fileName) {
    Resource resource = new FileSystemResource(RESOURCES_FOLDER + File.separator + fileName);
    if (!resource.exists()) {
        //try to find it on the classpath
        resource = new ClassPathResource(fileName);
        if (!resource.exists()) {
            // making sure to run on Netbeans..
            resource = new FileSystemResource("src" + File.separator + "main" + File.separator
                    + RESOURCES_FOLDER + File.separator + fileName);
            if (!resource.exists()) {
                resource = null;
            }
        }
    }
    return resource;
}

From source file:com.wavemaker.commons.util.SpringUtils.java

public static Object getBean(File cfg, String beanName) {
    return getBean(new FileSystemResource(cfg), beanName);
}

From source file:io.gravitee.gateway.env.PropertiesConfiguration.java

@Bean(name = "graviteeProperties")
public static Properties graviteeProperties() throws IOException {
    LOGGER.info("Loading Gravitee configuration.");

    YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();

    String yamlConfiguration = System.getProperty(GRAVITEE_CONFIGURATION);
    Resource yamlResource = new FileSystemResource(yamlConfiguration);

    LOGGER.info("\tGravitee configuration loaded from {}", yamlResource.getURL().getPath());

    yaml.setResources(yamlResource);/*  w  w w. ja v  a  2  s  .  c  o  m*/
    Properties properties = yaml.getObject();
    LOGGER.info("Loading Gravitee configuration. DONE");

    return properties;
}

From source file:io.gravitee.management.rest.spring.PropertiesConfiguration.java

@Bean(name = "graviteeProperties")
public static Properties graviteeProperties() throws IOException {
    LOGGER.info("Loading Gravitee Management configuration.");

    YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();

    String yamlConfiguration = System.getProperty(GRAVITEE_CONFIGURATION);
    Resource yamlResource = new FileSystemResource(yamlConfiguration);

    LOGGER.info("\tGravitee Management configuration loaded from {}", yamlResource.getURL().getPath());

    yaml.setResources(yamlResource);/*from   ww w  .  jav a 2s  .c  o  m*/
    Properties properties = yaml.getObject();
    LOGGER.info("Loading Gravitee Management configuration. DONE");

    return properties;
}

From source file:org.brekka.stillingar.spring.resource.dir.BaseDirUtils.java

/**
 * @param envVar/*from  w  w  w.  ja v a2  s. c  o  m*/
 * @param object
 * @return
 */
public static Resource resourceFromVariable(String baseFromVariable, String subPath) {
    if (baseFromVariable == null) {
        return new UnresolvableResource("Not set");
    }
    Resource resource;
    File baseDir = new File(baseFromVariable);
    resource = verifyDir(baseDir);
    File dir = baseDir;
    if (resource == null) {
        if (subPath != null) {
            dir = new File(baseDir, subPath);
            resource = verifyDir(dir);
        }
    }
    if (resource == null) {
        resource = new FileSystemResource(dir.getAbsolutePath() + "/");
    }
    return resource;
}

From source file:my.sandbox.spring.batch.demo.readers.ProductReader.java

public void setInputFile(String inputFile) {
    setResource(new FileSystemResource(inputFile));
}

From source file:org.secsm.multipartResolver.java

@Bean
public FileSystemResource fileSystemResource() {
    String path = "C:/fileupload/";
    FileSystemResource resource = new FileSystemResource(path);
    return resource;
}

From source file:be.ordina.springbatch.batch.writer.FineInformationWriter.java

public FineInformationWriter() {
    this.setResource(new FileSystemResource("src/main/resources/output.csv"));
    BeanWrapperFieldExtractor<Fine> fieldExtractor = new BeanWrapperFieldExtractor<>();
    fieldExtractor.setNames(new String[] { "licensePlate", "speed", "amountToPay", "graveError" });
    DelimitedLineAggregator<Fine> delLineAgg = new DelimitedLineAggregator<>();
    delLineAgg.setDelimiter(",");
    delLineAgg.setFieldExtractor(fieldExtractor);
    this.setLineAggregator(delLineAgg);
}