/** * Read a script from the provided resource, using the supplied comment prefix * and statement separator, and build a {@code String} containing the lines. * <p>Lines <em>beginning</em> with the comment prefix are excluded from the * results; however, line comments anywhere else — for example, within * a statement — will be included in the results. * @param resource the {@code EncodedResource} containing the script * to be processed * @param commentPrefix the prefix that identifies comments in the SQL script — * typically "--" * @param separator the statement separator in the SQL script — typically ";" * @return a {@code String} containing the script lines * @throws IOException in case of I/O errors */ private static String readScript(EncodedResource resource, @Nullable String commentPrefix, @Nullable String separator) throws IOException { LineNumberReader lnr = new LineNumberReader(resource.getReader()); try { return readScript(lnr, commentPrefix, separator); } finally { lnr.close(); } }
@Override public String getScriptAsString() throws IOException { synchronized (this.lastModifiedMonitor) { this.lastModified = retrieveLastModifiedTime(); } Reader reader = this.resource.getReader(); return FileCopyUtils.copyToString(reader); }
@Override public String getScriptAsString() throws IOException { synchronized (this.lastModifiedMonitor) { this.lastModified = retrieveLastModifiedTime(); } Reader reader = this.resource.getReader(); return FileCopyUtils.copyToString(reader); }
@Override public void setAsText(String text) throws IllegalArgumentException { this.resourceEditor.setAsText(text); Resource resource = (Resource) this.resourceEditor.getValue(); try { setValue(resource != null ? new EncodedResource(resource).getReader() : null); } catch (IOException ex) { throw new IllegalArgumentException("Failed to retrieve Reader for " + resource, ex); } }
@Override public void setAsText(String text) throws IllegalArgumentException { this.resourceEditor.setAsText(text); Resource resource = (Resource) this.resourceEditor.getValue(); try { setValue(resource != null ? new EncodedResource(resource).getReader() : null); } catch (IOException ex) { throw new IllegalArgumentException("Failed to retrieve Reader for " + resource, ex); } }
reader = resource.getReader(); persister.load(props, reader);
reader = resource.getReader(); persister.load(props, reader);
try { GroovyShell shell = new GroovyShell(getBeanClassLoader(), binding); shell.evaluate(encodedResource.getReader(), "beans");
@Test public void resourceInjection() throws IOException { System.setProperty("logfile", "do_not_delete_me.txt"); try (AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(ResourceInjectionBean.class)) { ResourceInjectionBean resourceInjectionBean = ac.getBean(ResourceInjectionBean.class); Resource resource = new ClassPathResource("do_not_delete_me.txt"); assertEquals(resource, resourceInjectionBean.resource); assertEquals(resource.getURL(), resourceInjectionBean.url); assertEquals(resource.getURI(), resourceInjectionBean.uri); assertEquals(resource.getFile(), resourceInjectionBean.file); assertArrayEquals(FileCopyUtils.copyToByteArray(resource.getInputStream()), FileCopyUtils.copyToByteArray(resourceInjectionBean.inputStream)); assertEquals(FileCopyUtils.copyToString(new EncodedResource(resource).getReader()), FileCopyUtils.copyToString(resourceInjectionBean.reader)); } finally { System.getProperties().remove("logfile"); } }
try { GroovyShell shell = new GroovyShell(getBeanClassLoader(), binding); shell.evaluate(encodedResource.getReader(), "beans");
public static void main(String[] args) throws Exception { System.out.println(System.getProperty("user.dir")); ResourceLoader resourceLoader = new DefaultResourceLoader(); Resource resource = resourceLoader.getResource("classpath:/mybatis/mybatis-config.xml"); EncodedResource encodedResource = new EncodedResource(resource, "UTF-8"); Reader reader = encodedResource.getReader(); SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder(); SqlSessionFactory sqlSessionFactory = builder.build(reader, "dev", new Properties()); SqlSession sqlSession = sqlSessionFactory.openSession(); User user = sqlSession.selectOne("com.segmentfault.springbootlesson7.mapper.UserMapper.selectOneUser", 145); System.out.println(user); sqlSession.close(); }
public static void main(String[] args) throws Throwable { Resource res = new ClassPathResource("example/file1.txt"); EncodedResource encodedRes = new EncodedResource(res, "UTF-8"); String content = FileCopyUtils.copyToString(encodedRes.getReader()); System.out.println(content); } }
/** * Read a script from the provided resource, using the supplied comment prefix * and statement separator, and build a {@code String} containing the lines. * <p>Lines <em>beginning</em> with the comment prefix are excluded from the * results; however, line comments anywhere else — for example, within * a statement — will be included in the results. * @param resource the {@code EncodedResource} containing the script * to be processed * @param commentPrefix the prefix that identifies comments in the SQL script — * typically "--" * @param separator the statement separator in the SQL script — typically ";" * @return a {@code String} containing the script lines * @throws IOException in case of I/O errors */ private static String readScript(EncodedResource resource, @Nullable String commentPrefix, @Nullable String separator) throws IOException { LineNumberReader lnr = new LineNumberReader(resource.getReader()); try { return readScript(lnr, commentPrefix, separator); } finally { lnr.close(); } }
/** * Read a script from the given resource and build a String containing the lines. * @param resource the resource to be read * @return {@code String} containing the script lines * @throws IOException in case of I/O errors */ private String readScript(EncodedResource resource) throws IOException { LineNumberReader lnr = new LineNumberReader(resource.getReader()); try { String currentStatement = lnr.readLine(); StringBuilder scriptBuilder = new StringBuilder(); while (currentStatement != null) { if (StringUtils.hasText(currentStatement) && (this.commentPrefix != null && !currentStatement.startsWith(this.commentPrefix))) { if (scriptBuilder.length() > 0) { scriptBuilder.append('\n'); } scriptBuilder.append(currentStatement); } currentStatement = lnr.readLine(); } maybeAddSeparatorToScript(scriptBuilder); return scriptBuilder.toString(); } finally { lnr.close(); } }
@Override public String getScriptAsString() throws IOException { synchronized (this.lastModifiedMonitor) { this.lastModified = retrieveLastModifiedTime(); } Reader reader = this.resource.getReader(); return FileCopyUtils.copyToString(reader); }
private Resource substitutePlaceholdersForResource(Resource resource) { StringBuilder script = new StringBuilder(); try { EncodedResource er = new EncodedResource(resource); LineNumberReader lnr = new LineNumberReader(er.getReader()); String line = lnr.readLine(); while (line != null) { if (tableName != null && line.contains(TABLE_PLACEHOLDER)) { logger.debug("Substituting '" + TABLE_PLACEHOLDER + "' with '" + tableName + "' in '" + line + "'"); line = line.replace(TABLE_PLACEHOLDER, tableName); } if (line.contains(COLUMNS_PLACEHOLDER)) { logger.debug("Substituting '" + COLUMNS_PLACEHOLDER + "' with '" + columns + "' in '" + line + "'"); line = line.replace(COLUMNS_PLACEHOLDER, columns); } script.append(line + "\n"); line = lnr.readLine(); } lnr.close(); return new ByteArrayResource(script.toString().getBytes()); } catch (IOException e) { throw new InvalidDataAccessResourceUsageException("Unable to read script " + resource, e); } } }
@Override public void setAsText(String text) throws IllegalArgumentException { this.resourceEditor.setAsText(text); Resource resource = (Resource) this.resourceEditor.getValue(); try { setValue(resource != null ? new EncodedResource(resource).getReader() : null); } catch (IOException ex) { throw new IllegalArgumentException("Failed to retrieve Reader for " + resource, ex); } }
reader = resource.getReader(); persister.load(props, reader);
reader = resource.getReader(); persister.load(props, reader);
try { GroovyShell shell = new GroovyShell(getResourceLoader().getClassLoader(), binding); shell.evaluate(encodedResource.getReader(), "beans");