Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package org.sparta.springwebutils.queryloader.impl;
 
 
Loads Queries from files. It's necessary to inform scriptsFolder at the creation time HOW TO USE: Declare the bean into applicationContext.xml (or beans.xml) <bean id="loader" class="net.sf.sparta.springwebutils.queryloader.impl.FileQueryLoader"> <constructor-arg name="scriptsFolder" value="<<SCRIPTS_FOLDER_NAME>>" /> </bean>

Author(s):
Daniel Conde Diehl
public class FileQueryLoader implements QueryLoaderInitializingBean {
	private static final ConcurrentMap<StringStringQUERY_CACHE = new ConcurrentHashMap<>(50);
	private String scriptsFolder;

Constructor, receives the path for the folder containing all the queries

Parameters:
scriptsFolder
	public FileQueryLoader(String scriptsFolder) {
		this. = scriptsFolder;
	}
	public String load(String queryName) {
	    if (!.containsKey(queryName)) {
	        .put(queryNameloadfromFromFile(queryName));
	    }
	    
		return .get(queryName); 
	}
    /*
     * (non-Javadoc)
     * @see net.sf.sparta.springwebutils.queryloader.QueryLoader#loadMultiple(java.lang.String, char)
     */
    @Override
    public String[] loadMultiple(String queryNamechar separator) {
        final String fullSql = load(queryName);
        
        return StringUtils.split(fullSqlseparator);
    }

Loads the query from the informed path and adds to cache

Parameters:
queryName File name for the query to be loaded
Returns:
requested Query,
Throws:
java.lang.IllegalStateException In case query was not found
	private String loadfromFromFile(String queryNamethrows IllegalStateException {
		try (final InputStream is = getClass().getResourceAsStream( + queryName + ".sql");) {
			return StringUtils.join(IOUtils.readLines(is), ' ');
catch (Exception e) {
			throw new IllegalStateException("Could not load query " +  + queryNamee);
		}
	}
	public void afterPropertiesSet() throws Exception {
		Assert.notNull("scriptsFolder cannot be null");
	}
New to GrepCode? Check out our FAQ X