/** * Return resource by path * * @param path * Resource path * @return Resource * * @see org.springframework.core.io.Resource */ public Resource getResource(String path) { return applicationContext.getResource(contextPath + path); }
@Nullable protected Resource getResource(String location) { if (this.resourceLoaderPaths != null) { for (String path : this.resourceLoaderPaths) { Resource resource = obtainApplicationContext().getResource(path + location); if (resource.exists()) { return resource; } } } return null; }
@Nullable protected Resource getResource(String location) { if (this.resourceLoaderPaths != null) { for (String path : this.resourceLoaderPaths) { Resource resource = obtainApplicationContext().getResource(path + location); if (resource.exists()) { return resource; } } } return null; }
private InputStream getConfigStream() throws IOException { Resource resource = ctx.getResource(redissonProperties.getConfig()); InputStream is = resource.getInputStream(); return is; }
@Nullable protected Resource getResource(String location) { if (this.resourceLoaderPaths != null) { for (String path : this.resourceLoaderPaths) { Resource resource = obtainApplicationContext().getResource(path + location); if (resource.exists()) { return resource; } } } return null; }
/** * Read the raw PDF resource into an iText PdfReader. * <p>The default implementation resolve the specified "url" property * as ApplicationContext resource. * @return the PdfReader instance * @throws IOException if resource access failed * @see #setUrl */ protected PdfReader readPdfResource() throws IOException { String url = getUrl(); Assert.state(url != null, "'url' not set"); return new PdfReader(obtainApplicationContext().getResource(url).getInputStream()); }
location = location.substring(endIndex + 1); Resource resource = applicationContext.getResource(location); this.locations.add(resource); if (charset != null) {
/** * Get the XSLT {@link Source} for the XSLT template under the {@link #setUrl configured URL}. * @return the Source object */ protected Source getStylesheetSource() { String url = getUrl(); Assert.state(url != null, "'url' not set"); if (logger.isDebugEnabled()) { logger.debug("Applying stylesheet [" + url + "]"); } try { Resource resource = obtainApplicationContext().getResource(url); return new StreamSource(resource.getInputStream(), resource.getURI().toASCIIString()); } catch (IOException ex) { throw new ApplicationContextException("Can't load XSLT stylesheet from '" + url + "'", ex); } }
/** * Execute the given SQL script. * <p>Use with caution outside of a transaction! * <p>The script will normally be loaded by classpath. * <p><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 * @see ResourceDatabasePopulator * @see #setSqlScriptEncoding */ protected void executeSqlScript(String sqlResourcePath, boolean continueOnError) throws DataAccessException { DataSource ds = this.jdbcTemplate.getDataSource(); Assert.state(ds != null, "No DataSource set"); Assert.state(this.applicationContext != null, "No ApplicationContext set"); Resource resource = this.applicationContext.getResource(sqlResourcePath); new ResourceDatabasePopulator(continueOnError, false, this.sqlScriptEncoding, resource).execute(ds); }
/** * Execute the given SQL script. * <p>Use with caution outside of a transaction! * <p>The script will normally be loaded by classpath. * <p><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 * @see ResourceDatabasePopulator * @see #setSqlScriptEncoding */ protected void executeSqlScript(String sqlResourcePath, boolean continueOnError) throws DataAccessException { DataSource ds = this.jdbcTemplate.getDataSource(); Assert.state(ds != null, "No DataSource set"); Assert.state(this.applicationContext != null, "No ApplicationContext available"); Resource resource = this.applicationContext.getResource(sqlResourcePath); new ResourceDatabasePopulator(continueOnError, false, this.sqlScriptEncoding, resource).execute(ds); }
private void importLdif(InMemoryDirectoryServer directoryServer) { if (StringUtils.hasText(this.ldif)) { Resource resource = this.context.getResource(this.ldif); try { if (resource.exists()) { try (InputStream inputStream = resource.getInputStream()) { directoryServer.importFromLDIF(false, new LDIFReader(inputStream)); } } } catch (Exception ex) { throw new IllegalStateException("Unable to load LDIF " + this.ldif, ex); } } }
/** * Read the raw PDF resource into an iText PdfReader. * <p>The default implementation resolve the specified "url" property * as ApplicationContext resource. * @return the PdfReader instance * @throws IOException if resource access failed * @see #setUrl */ protected PdfReader readPdfResource() throws IOException { String url = getUrl(); Assert.state(url != null, "'url' not set"); return new PdfReader(obtainApplicationContext().getResource(url).getInputStream()); }
protected void executeSqlScript(String sqlResourcePath, boolean continueOnError) throws DataAccessException { Resource resource = this.applicationContext.getResource(sqlResourcePath); new ResourceDatabasePopulator(continueOnError, false, this.sqlScriptEncoding, resource).execute(jdbcTemplate.getDataSource()); }
/** * Initialize the view bean factory from the XML file. * Synchronized because of access by parallel threads. * @throws BeansException in case of initialization errors */ protected synchronized BeanFactory initFactory() throws BeansException { if (this.cachedFactory != null) { return this.cachedFactory; } ApplicationContext applicationContext = obtainApplicationContext(); Resource actualLocation = this.location; if (actualLocation == null) { actualLocation = applicationContext.getResource(DEFAULT_LOCATION); } // Create child ApplicationContext for views. GenericWebApplicationContext factory = new GenericWebApplicationContext(); factory.setParent(applicationContext); factory.setServletContext(getServletContext()); // Load XML resource with context-aware entity resolver. XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory); reader.setEnvironment(applicationContext.getEnvironment()); reader.setEntityResolver(new ResourceEntityResolver(applicationContext)); reader.loadBeanDefinitions(actualLocation); factory.refresh(); if (isCache()) { this.cachedFactory = factory; } return factory; }
@Test public void missingDataSourceAndTxMgr() throws Exception { ApplicationContext ctx = mock(ApplicationContext.class); given(ctx.getResource(anyString())).willReturn(mock(Resource.class)); given(ctx.getAutowireCapableBeanFactory()).willReturn(mock(AutowireCapableBeanFactory.class)); Class<?> clazz = MissingDataSourceAndTxMgr.class; BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz); given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("foo")); given(testContext.getApplicationContext()).willReturn(ctx); assertExceptionContains("supply at least a DataSource or PlatformTransactionManager"); }
@Test public void isolatedTxModeDeclaredWithoutTxMgr() throws Exception { ApplicationContext ctx = mock(ApplicationContext.class); given(ctx.getResource(anyString())).willReturn(mock(Resource.class)); given(ctx.getAutowireCapableBeanFactory()).willReturn(mock(AutowireCapableBeanFactory.class)); Class<?> clazz = IsolatedWithoutTxMgr.class; BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz); given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("foo")); given(testContext.getApplicationContext()).willReturn(ctx); assertExceptionContains("cannot execute SQL scripts using Transaction Mode [ISOLATED] without a PlatformTransactionManager"); }
@Test public void testSaveStateDisabled() throws Exception { this.ldifReader = new LdifReaderBuilder().saveState(false).resource(context.getResource("classpath:/test.ldif")) .build(); ExecutionContext executionContext = new ExecutionContext(); firstRead(executionContext); this.ldifReader.update(executionContext); assertEquals("ExecutionContext should have been empty", 0, executionContext.size()); }
@Test public void testSkipRecord() throws Exception { this.ldifReader = new LdifReaderBuilder().recordsToSkip(1).resource(context.getResource("classpath:/test.ldif")) .name("foo").build(); LdapAttributes ldapAttributes = firstRead(); assertEquals("The attribute name for the second record did not match expected result", "cn=Bjorn Jensen, ou=Accounting, dc=airius, dc=com", ldapAttributes.getName().toString()); }
@Test public void testBasicRead() throws Exception { this.mappingLdifReader = new MappingLdifReaderBuilder<LdapAttributes>() .recordMapper(new TestMapper()) .resource(context.getResource("classpath:/test.ldif")) .name("foo") .build(); LdapAttributes ldapAttributes = firstRead(); assertEquals("The attribute name for the first record did not match expected result", "cn=Barbara Jensen, ou=Product Development, dc=airius, dc=com", ldapAttributes.getName().toString()); }
@Test public void testMaxItemCount() throws Exception { this.ldifReader = new LdifReaderBuilder().maxItemCount(1).resource(context.getResource("classpath:/test.ldif")) .name("foo").build(); LdapAttributes ldapAttributes = firstRead(); assertEquals("The attribute name for the first record did not match expected result", "cn=Barbara Jensen, ou=Product Development, dc=airius, dc=com", ldapAttributes.getName().toString()); ldapAttributes = this.ldifReader.read(); assertNull("The second read should have returned null", ldapAttributes); }