Example usage for org.springframework.core.io Resource getFilename

List of usage examples for org.springframework.core.io Resource getFilename

Introduction

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

Prototype

@Nullable
String getFilename();

Source Link

Document

Determine a filename for this resource, i.e.

Usage

From source file:org.mitre.jose.TestJWKSetKeyStore.java

@Test
public void ksEmptyConstructorkLoc() {

    JWKSetKeyStore ks = new JWKSetKeyStore();

    File file = new File(ks_file);

    Resource loc = new FileSystemResource(file);
    assertTrue(loc.exists());//from  w  w  w .  j  a va2  s. co  m
    assertTrue(loc.isReadable());

    ks.setLocation(loc);

    assertEquals(loc.getFilename(), ks.getLocation().getFilename());
}

From source file:de.langmi.spring.batch.examples.readers.file.gzip.GZipBufferedReaderFactory.java

/**
 * Creates Bufferedreader for gzip Resource, handles normal resources
 * too./*from   ww  w.jav  a 2  s.c  om*/
 * 
 * @param resource
 * @param encoding
 * @return
 * @throws UnsupportedEncodingException
 * @throws IOException 
 */
@Override
public BufferedReader create(Resource resource, String encoding)
        throws UnsupportedEncodingException, IOException {
    for (String suffix : gzipSuffixes) {
        // test for filename and description, description is used when 
        // handling itemStreamResources
        if (resource.getFilename().endsWith(suffix) || resource.getDescription().endsWith(suffix)) {
            return new BufferedReader(
                    new InputStreamReader(new GZIPInputStream(resource.getInputStream()), encoding));
        }
    }
    return new BufferedReader(new InputStreamReader(resource.getInputStream(), encoding));
}

From source file:org.toobsframework.pres.base.ManagerBase.java

public void loadConfig(final Class<?> clazz) {
    final ResourceUnmarshaller unmarshaller = new ResourceUnmarshaller();
    if (configFiles == null) {
        return;/*from  w w  w .  j  a va 2  s  . c o  m*/
    }
    if (resourceCache == null) {
        initCache();
    }

    for (int fileCounter = 0; fileCounter < resourceCache.length; fileCounter++) {
        String fileSpec = configFiles.get(fileCounter);
        try {
            if (log.isDebugEnabled()) {
                log.debug("Checking Configuration file spec: " + fileSpec);
            }
            Resource[] resources = null;
            if (doReload) {
                resources = resourceCache[fileCounter].checkIfModified();
                // This call is only done if doReload is desired.  This call can be
                // low performance so it is only good for development
                if (resources == null) {
                    continue;
                }
            }
            resourceCache[fileCounter].load(resources, doReload, new IResourceCacheLoader() {
                public void load(Resource resource) throws IOException {
                    Object object = unmarshaller.unmarshall(resource, clazz);
                    registerConfiguration(object, resource.getFilename());
                }
            });
        } catch (Exception ex) {
            log.error("ComponentLayout initialization failed " + ex.getMessage(), ex);
        }
    }
    initDone = true;
}

From source file:org.carewebframework.api.alias.AliasTypeRegistry.java

/**
 * Load aliases from a property file.// w w  w. ja v  a2  s .  co m
 * 
 * @param applicationContext The application context.
 * @param propertyFile A property file.
 */
private void loadAliases(ApplicationContext applicationContext, String propertyFile) {
    if (propertyFile.isEmpty()) {
        return;
    }

    Resource[] resources;

    try {
        resources = applicationContext.getResources(propertyFile);
    } catch (IOException e) {
        log.error("Failed to locate alias property file: " + propertyFile, e);
        return;
    }

    for (Resource resource : resources) {
        if (!resource.exists()) {
            log.info("Did not find alias property file: " + resource.getFilename());
            continue;
        }

        InputStream is = null;

        try {
            is = resource.getInputStream();
            Properties props = new Properties();
            props.load(is);

            for (Entry<Object, Object> entry : props.entrySet()) {
                try {
                    register((String) entry.getKey(), (String) entry.getValue());
                    entryCount++;
                } catch (Exception e) {
                    log.error("Error registering alias for '" + entry.getKey() + "'.", e);
                }
            }

            fileCount++;
        } catch (IOException e) {
            log.error("Failed to load alias property file: " + resource.getFilename(), e);
        } finally {
            IOUtils.closeQuietly(is);
        }
    }
}

From source file:annis.administration.AbstractAdminstrationDao.java

/**
 * executes an SQL script from $ANNIS_HOME/scripts, substituting the
 * parameters found in args//  ww w  .  j ava2s .  c o  m
 *
 * @param script
 * @param args
 * @return
 */
protected PreparedStatement executeSqlFromScript(String script, MapSqlParameterSource args) {
    File fScript = new File(scriptPath, script);
    if (fScript.canRead() && fScript.isFile()) {
        Resource resource = new FileSystemResource(fScript);
        log.debug("executing SQL script: " + resource.getFilename());
        String sql = readSqlFromResource(resource, args);
        CancelableStatements cancelableStats = new CancelableStatements(sql, statementController);

        // register the statement, so we could try to interrupt it in the gui.
        if (statementController != null) {
            statementController.registerStatement(cancelableStats.statement);
        } else {
            log.debug("statement controller is not initialized");
        }

        jdbcTemplate.execute(cancelableStats, cancelableStats);
        return cancelableStats.statement;
    } else {
        log.debug("SQL script " + fScript.getName() + " does not exist");
        return null;
    }
}

From source file:org.jnap.core.assets.HandlebarsAssetsHandler.java

@Override
public void handle() {
    final Handlebars handlebars = new Handlebars();
    try {/*w  w  w.java  2s . c  om*/
        Resource[] resources = this.resourceResolver.getResources(source);
        Resource destRes = new ServletContextResource(servletContext, destination);
        resetResource(destRes);
        BufferedWriter writer = new BufferedWriter(
                new FileWriterWithEncoding(destRes.getFile(), this.encoding, true));

        writer.write("(function() {");
        writer.write(IOUtils.LINE_SEPARATOR);
        writer.write("var template = Handlebars.template, ");
        writer.write("templates = Handlebars.templates = Handlebars.templates || {};");
        writer.write(IOUtils.LINE_SEPARATOR);
        final Set<String> templateNames = new TreeSet<String>();
        for (Resource resource : resources) {
            Template template = handlebars
                    .compile(StringUtils.trimToEmpty(IOUtils.toString(resource.getInputStream())));
            final String templateName = FilenameUtils.getBaseName(resource.getFilename());
            templateNames.add(templateName);
            writer.write("templates[\"" + templateName + "\"] = ");
            writer.write("template(");
            writer.write(template.toJavaScript());
            writer.write(");");
            writer.write(IOUtils.LINE_SEPARATOR);
        }

        writer.write(IOUtils.LINE_SEPARATOR);
        if (this.bindToBackboneView) {
            writer.write("$(function() {");
            writer.write(IOUtils.LINE_SEPARATOR);
            for (String templateName : templateNames) {
                writer.write(format("if (window[\"{0}\"]) {0}.prototype.template " + "= templates[\"{0}\"];",
                        templateName));
                writer.write(IOUtils.LINE_SEPARATOR);
            }
            writer.write("});");
            writer.write(IOUtils.LINE_SEPARATOR);
        }

        writer.write("})();");
        IOUtils.closeQuietly(writer);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:de.ingrid.interfaces.csw.harvest.impl.TestSuiteHarvester.java

@Override
protected List<Serializable> fetchRecords(Date lastExecutionDate) throws Exception {
    // get list of test datasets
    Resource[] resources = FileUtils.getPackageContent("classpath*:gdide_test_data/*xml");
    List<Serializable> cacheIds = new ArrayList<Serializable>();
    statusProvider.addState(this.getId() + "harvesting", "Fetch records... [" + resources.length + "]");
    for (Resource resource : resources) {
        String iso = FileUtils.convertStreamToString(resource.getInputStream());
        Record record = IdfTool.createIdfRecord(iso, true);
        Serializable cacheId = this.cache.put(record);
        if (log.isDebugEnabled()) {
            log.debug("Fetched record " + resource.getFilename() + ". Cache id: " + cacheId);
        }//from  ww w .j  av a2 s  .c  o  m
        cacheIds.add(cacheId);
    }
    return cacheIds;
}

From source file:com.thinkbiganalytics.spring.FileResourceService.java

public String resourceAsString(Resource resource) {
    try {//from  w  w w.j a  va  2s.com
        if (resource != null) {
            InputStream is = resource.getInputStream();
            return IOUtils.toString(is, Charset.defaultCharset());
        }
    } catch (IOException e) {
        log.error("Unable to load file {} ", resource.getFilename(), e);
    }
    return null;
}

From source file:com.yahoo.bard.webservice.config.ConfigResourceLoader.java

/**
 * Build a configuration object from a resource, processing it as a properties file.
 *
 * @param resource  The resource referring to a properties file
 *
 * @return a Configuration object containing a properties configuration
 *///from w w  w.  j  a va2s  .  co  m
public Configuration loadConfigFromResource(Resource resource) {
    PropertiesConfiguration result = new PropertiesConfiguration();
    try {
        result.load(resource.getInputStream());
        return result;
    } catch (ConfigurationException | IOException e) {
        LOG.error(CONFIGURATION_LOAD_ERROR.format(resource.getFilename()), e);
        throw new SystemConfigException(CONFIGURATION_LOAD_ERROR.format(resource.getFilename()), e);
    }
}