Example usage for org.springframework.core.io.support EncodedResource EncodedResource

List of usage examples for org.springframework.core.io.support EncodedResource EncodedResource

Introduction

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

Prototype

public EncodedResource(Resource resource, @Nullable Charset charset) 

Source Link

Document

Create a new EncodedResource for the given Resource , using the specified Charset .

Usage

From source file:com.xyxy.platform.modules.core.test.data.DataFixtures.java

public static void executeScript(DataSource dataSource, String... sqlResourcePaths) throws DataAccessException {
    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    for (String sqlResourcePath : sqlResourcePaths) {
        Resource resource = resourceLoader.getResource(sqlResourcePath);
        JdbcTestUtils.executeSqlScript(jdbcTemplate, new EncodedResource(resource, DEFAULT_ENCODING), true);
    }//  w  w w . j a  va  2  s  .  c  om
}

From source file:net.noday.core.dao.AppDao.java

/**
 * sql/*from w ww. j a v a2  s.c om*/
 * // TODO ?????
 * @param sqlFilePath
 */
private void executeSql(String sqlFilePath) {
    if (sqlFilePath == null) {
        throw new AppStartupException("sql[" + sqlFilePath + "]?");
    }
    try {
        log.info("[" + sqlFilePath + "]");
        Resource sqlRes = new ClassPathResource(sqlFilePath);
        EncodedResource encRes = new EncodedResource(sqlRes, "UTF-8");
        String sqls = null;
        sqls = FileCopyUtils.copyToString(encRes.getReader());
        String[] sqlArr = sqls.split(";");
        log.info("");
        for (String sql : sqlArr) {
            log.info(sql);
            jdbc.execute(sql);
        }
        log.info("?");
    } catch (IOException e) {
        log.error("?[" + sqlFilePath + "]", e);
        throw new AppStartupException("?[" + sqlFilePath + "]", e);
    } catch (Exception e) {
        log.error("[" + sqlFilePath + "]", e);
        throw new AppStartupException("[" + sqlFilePath + "]", e);
    }
}

From source file:io.lavagna.model.MailConfig.java

private JavaMailSender toMailSender() {
    JavaMailSenderImpl r = new JavaMailSenderImpl();
    r.setDefaultEncoding("UTF-8");
    r.setHost(host);/* w ww .j av  a  2 s  .  c om*/
    r.setPort(port);
    r.setProtocol(protocol);
    r.setUsername(username);
    r.setPassword(password);
    if (properties != null) {
        try {
            Properties prop = PropertiesLoaderUtils.loadProperties(
                    new EncodedResource(new ByteArrayResource(properties.getBytes("UTF-8")), "UTF-8"));
            r.setJavaMailProperties(prop);
        } catch (IOException e) {
            LOG.warn("error while setting the mail sender properties", e);
        }
    }
    return r;
}

From source file:com.excilys.ebi.bank.jdbc.SimpleBatchResourceDatabasePopulator.java

private EncodedResource applyEncodingIfNecessary(Resource script) {
    if (script instanceof EncodedResource) {
        return (EncodedResource) script;
    } else {//from  w  w  w .j  a  v a  2s  .c o  m
        return new EncodedResource(script, this.sqlScriptEncoding);
    }
}

From source file:com.scf.core.context.app.cfg.module.ModuleConfigHandler.java

/**
 *
 * @param classPackage// w w w  .j a v a 2  s  .  co  m
 * @param marges
 */
private static void loadModuleConfigs(String classPackage, Properties marges) {
    Resource[] resources = ClassScanner.scan(classPackage, "config.properties");
    for (Resource resource : resources) {
        try {
            if (!resource.exists()) {
                continue;
            }
            Properties props = new Properties();
            // ?file??
            String moduleName = resource.createRelative("/").getFilename();
            PropertiesLoaderUtils.fillProperties(props, new EncodedResource(resource, "utf-8"));
            ConfigParams cp = loadModuleConfig(marges, moduleName, props);
            _logger.info("Loaded module config for " + moduleName + " has " + cp.getParams().keySet().size()
                    + " properties.");
            map.put(moduleName, cp);
        } catch (IOException ex) {
            _logger.warn("Can not load module config for " + resource, ex);
        }
    }
}

From source file:alfio.manager.system.SmtpMailer.java

private JavaMailSender toMailSender(Event event) {
    JavaMailSenderImpl r = new CustomJavaMailSenderImpl();
    r.setDefaultEncoding("UTF-8");

    r.setHost(configurationManager/*ww  w. jav  a2 s  .c om*/
            .getRequiredValue(Configuration.from(event.getOrganizationId(), event.getId(), SMTP_HOST)));
    r.setPort(Integer.valueOf(configurationManager
            .getRequiredValue(Configuration.from(event.getOrganizationId(), event.getId(), SMTP_PORT))));
    r.setProtocol(configurationManager
            .getRequiredValue(Configuration.from(event.getOrganizationId(), event.getId(), SMTP_PROTOCOL)));
    r.setUsername(configurationManager.getStringConfigValue(
            Configuration.from(event.getOrganizationId(), event.getId(), SMTP_USERNAME), null));
    r.setPassword(configurationManager.getStringConfigValue(
            Configuration.from(event.getOrganizationId(), event.getId(), SMTP_PASSWORD), null));

    String properties = configurationManager.getStringConfigValue(
            Configuration.from(event.getOrganizationId(), event.getId(), SMTP_PROPERTIES), null);

    if (properties != null) {
        try {
            Properties prop = PropertiesLoaderUtils.loadProperties(new EncodedResource(
                    new ByteArrayResource(properties.getBytes(StandardCharsets.UTF_8)), "UTF-8"));
            r.setJavaMailProperties(prop);
        } catch (IOException e) {
            log.warn("error while setting the mail sender properties", e);
        }
    }
    return r;
}

From source file:com.sfxie.extension.spring4.properties.SwitchPropertiesLoaderSupport.java

/**
 * Load properties into the given instance.
 * @param props the Properties instance to load into
 * @throws IOException in case of I/O errors
 * @see #setLocations/* www.  j a  va  2s.  com*/
 */
protected void loadProperties(Properties props) throws IOException {
    if (this.locations != null) {
        IPropertiesFileFilter propertiesFileFilter = getPropertiesFileFilter();
        InputStream inStream = this.getClass().getClassLoader()
                .getResourceAsStream(propertiesFileFilter.loadPrePropertiesPath());
        if (null != inStream) {
            Properties properties = new Properties();
            properties.load(inStream);
            Context.enviroment = properties.getProperty("enviroment");
            propertiesFileFilter.validate(properties);
            LoggerUtil.system(SystemLogger.class, "?:" + Context.enviroment);
            for (Resource location : this.locations) {
                /*System.out.println(location.getFilename());
                String path = location.getURI().getPath();
                if(null==path || path.equals("")){
                   path = location.getFile().getPath();
                }*/
                //?????????
                if (propertiesFileFilter.filter(properties, location.getFilename())) {
                    if (null != logger && logger.isInfoEnabled()) {
                        logger.info("Loading properties file from " + location);
                    }
                    try {
                        PropertiesLoaderUtils.fillProperties(props,
                                new EncodedResource(location, this.fileEncoding), this.propertiesPersister);
                    } catch (IOException ex) {
                        if (this.ignoreResourceNotFound) {
                            if (null != logger && logger.isWarnEnabled()) {
                                logger.warn(
                                        "Could not load properties from " + location + ": " + ex.getMessage());
                            }
                        } else {
                            throw ex;
                        }
                    }
                }

            }
        } else {
            throw new IOException("classpath:configenviroment.properties.");
        }
    }
}

From source file:org.cfr.capsicum.test.AbstractCayenneJUnit4DbUnitSpringContextTests.java

/**
 * Execute the given SQL script. Use with caution outside of a transaction!
 * <p>//w ww  . j ava  2 s.c o m
 * The script will normally be loaded by classpath. There should be one
 * statement per line. Any semicolons will be removed. <b>Do not use this
 * method to execute DDL if you expect rollback.</b>
 *
 * @param sqlResourcePath
 *            the Spring resource path for the SQL script
 * @param continueOnError
 *            whether or not to continue without throwing an exception in
 *            the event of an error
 * @throws DataAccessException
 *             if there is an error executing a statement and
 *             continueOnError was <code>false</code>
 */
protected void executeSqlScript(String sqlResourcePath, boolean continueOnError) throws DataAccessException {

    Resource resource = this.applicationContext.getResource(sqlResourcePath);
    SimpleJdbcTestUtils.executeSqlScript(this.simpleJdbcTemplate,
            new EncodedResource(resource, this.sqlScriptEncoding), continueOnError);
}

From source file:org.axonframework.eventhandling.scheduling.quartz.QuartzTableMaker.java

private void executeSqlScript(String sqlResourcePath) throws DataAccessException {
    EncodedResource resource = new EncodedResource(applicationContext.getResource(sqlResourcePath), "UTF-8");
    List<String> statements = new LinkedList<String>();
    try {/*from w w  w . j  a  v a2 s  . c om*/
        LineNumberReader lnr = new LineNumberReader(resource.getReader());
        String script = JdbcTestUtils.readScript(lnr);
        char delimiter = ';';
        if (!JdbcTestUtils.containsSqlScriptDelimiters(script, delimiter)) {
            delimiter = '\n';
        }
        JdbcTestUtils.splitSqlScript(script, delimiter, statements);
        for (String statement : statements) {
            this.entityManager.createNativeQuery(statement).executeUpdate();
        }
    } catch (IOException ex) {
        throw new DataAccessResourceFailureException("Failed to open SQL script '" + sqlResourcePath + "'", ex);
    }
}