org.springframework.core.io.FileSystemResourceLoader Java Examples

The following examples show how to use org.springframework.core.io.FileSystemResourceLoader. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RiceTestCase.java    From rice with Educational Community License v2.0 6 votes vote down vote up
/**
    * configures logging using custom properties file if specified, or the default one.
    * Log4j also uses any file called log4.properties in the classpath
    *
    * <p>To configure a custom logging file, set a JVM system property on using -D. For example
    * -Dalt.log4j.config.location=file:/home/me/kuali/test/dev/log4j.properties
    * </p>
    *
    * <p>The above option can also be set in the run configuration for the unit test in the IDE.
    * To avoid log4j using files called log4j.properties that are defined in the classpath, add the following system property:
    * -Dlog4j.defaultInitOverride=true
    * </p>
    * @throws IOException
    */
protected void configureLogging() throws IOException {
       ResourceLoader resourceLoader = new FileSystemResourceLoader();
       String altLog4jConfigLocation = System.getProperty(ALT_LOG4J_CONFIG_LOCATION_PROP);
       Resource log4jConfigResource = null;
       if (!StringUtils.isEmpty(altLog4jConfigLocation)) { 
           log4jConfigResource = resourceLoader.getResource(altLog4jConfigLocation);
       }
       if (log4jConfigResource == null || !log4jConfigResource.exists()) {
           System.out.println("Alternate Log4j config resource does not exist! " + altLog4jConfigLocation);
           System.out.println("Using default log4j configuration: " + DEFAULT_LOG4J_CONFIG);
           log4jConfigResource = resourceLoader.getResource(DEFAULT_LOG4J_CONFIG);
       } else {
           System.out.println("Using alternate log4j configuration at: " + altLog4jConfigLocation);
       }
       Properties p = new Properties();
       p.load(log4jConfigResource.getInputStream());
       PropertyConfigurator.configure(p);
   }
 
Example #2
Source File: CustomizedResourcePatternResolverDemo.java    From geekbang-lessons with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws IOException {
    // 读取当前 package 对应的所有的 .java 文件
    // *.java
    String currentPackagePath = "/" + System.getProperty("user.dir") + "/thinking-in-spring/resource/src/main/java/org/geekbang/thinking/in/spring/resource/";
    String locationPattern = currentPackagePath + "*.java";
    PathMatchingResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver(new FileSystemResourceLoader());

    resourcePatternResolver.setPathMatcher(new JavaFilePathMatcher());

    Resource[] resources = resourcePatternResolver.getResources(locationPattern);

    Stream.of(resources).map(ResourceUtils::getContent).forEach(System.out::println);
}
 
Example #3
Source File: NotificationPropertyPlaceholderConfigurer.java    From rice with Educational Community License v2.0 5 votes vote down vote up
/**
 * This method consolidates config properties.
 * @param properties
 * @return Properties
 * @throws IOException
 */
protected Properties transformProperties(Properties properties) throws IOException {
    String cfgLocation = System.getProperty(CFG_LOCATION_PROPERTY);

    if (cfgLocation != null) {
        Resource resource = new FileSystemResourceLoader().getResource(cfgLocation);
        if (resource != null && resource.exists()) {
            new DefaultPropertiesPersister().load(properties, resource.getInputStream());
        }
    }
    
    return properties;
}
 
Example #4
Source File: ApsAdminBaseTestCase.java    From entando-core with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
protected void setUp() throws Exception {
    boolean refresh = false;
    if (null == applicationContext) {
        // Link the servlet context and the Spring context
        servletContext = new MockServletContext("", new FileSystemResourceLoader());
        applicationContext = this.getConfigUtils().createApplicationContext(servletContext);
        servletContext.setAttribute(
                WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, applicationContext);
    } else {
        refresh = true;
    }
    RequestContext reqCtx = BaseTestCase.createRequestContext(applicationContext, servletContext);
    this.request = new MockHttpServletRequest();
    this.request.setAttribute(RequestContext.REQCTX, reqCtx);
    this.response = new MockHttpServletResponse();
    this.request.setSession(new MockHttpSession(servletContext));
    if (refresh) {
        try {
            ApsWebApplicationUtils.executeSystemRefresh(this.request);
            this.waitNotifyingThread();
        } catch (Throwable e) {
        }
    }
    // Use spring as the object factory for Struts
    StrutsSpringObjectFactory ssf = new StrutsSpringObjectFactory(null, null, null, null, servletContext, null, this.createContainer());
    ssf.setApplicationContext(applicationContext);
    // Dispatcher is the guy that actually handles all requests.  Pass in
    // an empty Map as the parameters but if you want to change stuff like
    // what config files to read, you need to specify them here
    // (see Dispatcher's source code)
    java.net.URL url = ClassLoader.getSystemResource("struts.properties");
    Properties props = new Properties();
    props.load(url.openStream());
    this.setInitParameters(props);
    Map params = new HashMap(props);
    this.dispatcher = new Dispatcher(servletContext, params);
    this.dispatcher.init();
    Dispatcher.setInstance(this.dispatcher);
}
 
Example #5
Source File: BaseTestCase.java    From entando-core with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
protected void setUp() throws Exception {
    try {
        super.setUp();
        ServletContext srvCtx = new MockServletContext("", new FileSystemResourceLoader());
        ApplicationContext applicationContext = this.getConfigUtils().createApplicationContext(srvCtx);
        this.setApplicationContext(applicationContext);
        RequestContext reqCtx = createRequestContext(applicationContext, srvCtx);
        this.setRequestContext(reqCtx);
        this.setUserOnSession("guest");
    } catch (Exception e) {
        throw e;
    }
}
 
Example #6
Source File: UserProfileManagerTest.java    From entando-core with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    MockServletContext servletContext = new MockServletContext("", new FileSystemResourceLoader());
    new ConfigTestUtils().createApplicationContext(servletContext);
    MockitoAnnotations.initMocks(this);
    this.userProfileManager.setEntityClassName(className);
    this.userProfileManager.setConfigItemName(configItemName);
    this.userProfileManager.setBeanName(this.beanName);
}
 
Example #7
Source File: TestDependencies.java    From spring-cloud-dataflow with Apache License 2.0 5 votes vote down vote up
@Bean
public DelegatingResourceLoader resourceLoader(MavenProperties mavenProperties) {
	Map<String, ResourceLoader> resourceLoaders = new HashMap<>();
	resourceLoaders.put("maven", new MavenResourceLoader(mavenProperties));
	resourceLoaders.put("file", new FileSystemResourceLoader());

	DelegatingResourceLoader delegatingResourceLoader = new DelegatingResourceLoader(resourceLoaders);
	return delegatingResourceLoader;
}
 
Example #8
Source File: Log4jWebConfigurerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testLog4jConfigListener() {
	Log4jConfigListener listener = new Log4jConfigListener();

	MockServletContext sc = new MockServletContext("", new FileSystemResourceLoader());
	sc.addInitParameter(Log4jWebConfigurer.CONFIG_LOCATION_PARAM, RELATIVE_PATH);
	listener.contextInitialized(new ServletContextEvent(sc));

	try {
		assertLogOutput();
	} finally {
		listener.contextDestroyed(new ServletContextEvent(sc));
	}
	assertTrue(MockLog4jAppender.closeCalled);
}
 
Example #9
Source File: Log4jWebConfigurerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private void initLogging(String location, boolean refreshInterval) {
	MockServletContext sc = new MockServletContext("", new FileSystemResourceLoader());
	sc.addInitParameter(Log4jWebConfigurer.CONFIG_LOCATION_PARAM, location);
	if (refreshInterval) {
		sc.addInitParameter(Log4jWebConfigurer.REFRESH_INTERVAL_PARAM, "10");
	}
	Log4jWebConfigurer.initLogging(sc);

	try {
		assertLogOutput();
	} finally {
		Log4jWebConfigurer.shutdownLogging(sc);
	}
	assertTrue(MockLog4jAppender.closeCalled);
}
 
Example #10
Source File: WebMvcConfigurationSupportExtensionTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
	this.context = new StaticWebApplicationContext();
	this.context.setServletContext(new MockServletContext(new FileSystemResourceLoader()));
	this.context.registerSingleton("controller", TestController.class);

	this.config = new TestWebMvcConfigurationSupport();
	this.config.setApplicationContext(this.context);
	this.config.setServletContext(this.context.getServletContext());
}
 
Example #11
Source File: TestDependencies.java    From spring-cloud-dashboard with Apache License 2.0 5 votes vote down vote up
@Bean
public ResourceLoader resourceLoader() {
	MavenProperties mavenProperties = new MavenProperties();
	mavenProperties.setRemoteRepositories(new HashMap<>(Collections.singletonMap("springRepo",
			new MavenProperties.RemoteRepository("https://repo.spring.io/libs-snapshot"))));

	Map<String, ResourceLoader> resourceLoaders = new HashMap<>();
	resourceLoaders.put("maven", new MavenResourceLoader(mavenProperties));
	resourceLoaders.put("file", new FileSystemResourceLoader());

	DelegatingResourceLoader delegatingResourceLoader = new DelegatingResourceLoader(resourceLoaders);
	return delegatingResourceLoader;
}
 
Example #12
Source File: WebMvcConfigurationSupportExtensionTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Before
public void setUp() {
	this.context = new StaticWebApplicationContext();
	this.context.setServletContext(new MockServletContext(new FileSystemResourceLoader()));
	this.context.registerSingleton("controller", TestController.class);
	this.context.registerSingleton("userController", UserController.class);

	this.config = new TestWebMvcConfigurationSupport();
	this.config.setApplicationContext(this.context);
	this.config.setServletContext(this.context.getServletContext());
}
 
Example #13
Source File: EncodedFileSystemResourceLoaderDemo.java    From geekbang-lessons with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws IOException {
    String currentJavaFilePath = "/" + System.getProperty("user.dir") + "/thinking-in-spring/resource/src/main/java/org/geekbang/thinking/in/spring/resource/EncodedFileSystemResourceLoaderDemo.java";
    // 新建一个 FileSystemResourceLoader 对象
    FileSystemResourceLoader resourceLoader = new FileSystemResourceLoader();
    // FileSystemResource => WritableResource => Resource
    Resource resource = resourceLoader.getResource(currentJavaFilePath);
    EncodedResource encodedResource = new EncodedResource(resource, "UTF-8");
    // 字符输入流
    try (Reader reader = encodedResource.getReader()) {
        System.out.println(IOUtils.toString(reader));
    }
}
 
Example #14
Source File: WebMvcConfigurationSupportExtensionTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Before
public void setUp() {
	this.context = new StaticWebApplicationContext();
	this.context.setServletContext(new MockServletContext(new FileSystemResourceLoader()));
	this.context.registerSingleton("controller", TestController.class);
	this.context.registerSingleton("userController", UserController.class);

	this.config = new TestWebMvcConfigurationSupport();
	this.config.setApplicationContext(this.context);
	this.config.setServletContext(this.context.getServletContext());
}
 
Example #15
Source File: WorkspaceInitializerTest.java    From bonita-ui-designer with GNU General Public License v2.0 4 votes vote down vote up
@Before
public void initializeWorkspaceInitializer() {
    workspaceInitializer.setServletContext(new MockServletContext(WAR_BASE_PATH, new FileSystemResourceLoader()));
    workspaceInitializer.setMigrations(Arrays.<LiveRepositoryUpdate>asList(pageRepositoryLiveUpdate, widgetRepositoryLiveUpdate));
}
 
Example #16
Source File: FileMonitorConfigurationTest.java    From spring-cloud-config with Apache License 2.0 4 votes vote down vote up
@Before
public void setup() {
	fileMonitorConfiguration.setResourceLoader(new FileSystemResourceLoader());
}
 
Example #17
Source File: GenericWebContextLoader.java    From maven-framework-project with MIT License 4 votes vote down vote up
public GenericWebContextLoader(String warRootDir, boolean isClasspathRelative) {
	ResourceLoader resourceLoader = isClasspathRelative ? new DefaultResourceLoader() : new FileSystemResourceLoader();
	this.servletContext = initServletContext(warRootDir, resourceLoader);
}
 
Example #18
Source File: GenericWebContextLoader.java    From maven-framework-project with MIT License 4 votes vote down vote up
public GenericWebContextLoader(String warRootDir, boolean isClasspathRelative) {
	ResourceLoader resourceLoader = isClasspathRelative ? new DefaultResourceLoader() : new FileSystemResourceLoader();
	this.servletContext = initServletContext(warRootDir, resourceLoader);
}
 
Example #19
Source File: GenericWebContextLoader.java    From maven-framework-project with MIT License 4 votes vote down vote up
public GenericWebContextLoader(String warRootDir, boolean isClasspathRelative) {
	ResourceLoader resourceLoader = isClasspathRelative ? new DefaultResourceLoader() : new FileSystemResourceLoader();
	this.servletContext = initServletContext(warRootDir, resourceLoader);
}
 
Example #20
Source File: FolderScanningSiteListResolverTest.java    From engine with GNU General Public License v3.0 4 votes vote down vote up
private ResourceLoader createResourceLoader() {
    return new FileSystemResourceLoader();
}