@Override public org.springframework.core.env.PropertySource createPropertySource(String name, EncodedResource resource) throws IOException { Properties props = PropertiesLoaderUtils.loadProperties(resource); return new org.springframework.core.env.PropertySource<Properties>("my" + name, props) { @Override public Object getProperty(String name) { String value = props.getProperty(name); return (value != null ? value.toUpperCase() : null); } }; } }
private static List<HandlerMapping> initFallback(ApplicationContext applicationContext) { Properties props; String path = "DispatcherServlet.properties"; try { Resource resource = new ClassPathResource(path, DispatcherServlet.class); props = PropertiesLoaderUtils.loadProperties(resource); } catch (IOException ex) { throw new IllegalStateException("Could not load '" + path + "': " + ex.getMessage()); } String value = props.getProperty(HandlerMapping.class.getName()); String[] names = StringUtils.commaDelimitedListToStringArray(value); List<HandlerMapping> result = new ArrayList<>(names.length); for (String name : names) { try { Class<?> clazz = ClassUtils.forName(name, DispatcherServlet.class.getClassLoader()); Object mapping = applicationContext.getAutowireCapableBeanFactory().createBean(clazz); result.add((HandlerMapping) mapping); } catch (ClassNotFoundException ex) { throw new IllegalStateException("Could not find default HandlerMapping [" + name + "]"); } } return result; }
private static Map<String, List<String>> loadSpringFactories(@Nullable ClassLoader classLoader) { MultiValueMap<String, String> result = cache.get(classLoader); if (result != null) { return result; } try { Enumeration<URL> urls = (classLoader != null ? classLoader.getResources(FACTORIES_RESOURCE_LOCATION) : ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION)); result = new LinkedMultiValueMap<>(); while (urls.hasMoreElements()) { URL url = urls.nextElement(); UrlResource resource = new UrlResource(url); Properties properties = PropertiesLoaderUtils.loadProperties(resource); for (Map.Entry<?, ?> entry : properties.entrySet()) { String factoryClassName = ((String) entry.getKey()).trim(); for (String factoryName : StringUtils.commaDelimitedListToStringArray((String) entry.getValue())) { result.add(factoryClassName, factoryName.trim()); } } } cache.put(classLoader, result); return result; } catch (IOException ex) { throw new IllegalArgumentException("Unable to load factories from location [" + FACTORIES_RESOURCE_LOCATION + "]", ex); } }
@Nullable private static CandidateComponentsIndex doLoadIndex(ClassLoader classLoader) { if (shouldIgnoreIndex) { return null; } try { Enumeration<URL> urls = classLoader.getResources(COMPONENTS_RESOURCE_LOCATION); if (!urls.hasMoreElements()) { return null; } List<Properties> result = new ArrayList<>(); while (urls.hasMoreElements()) { URL url = urls.nextElement(); Properties properties = PropertiesLoaderUtils.loadProperties(new UrlResource(url)); result.add(properties); } if (logger.isDebugEnabled()) { logger.debug("Loaded " + result.size() + "] index(es)"); } int totalCount = result.stream().mapToInt(Properties::size).sum(); return (totalCount > 0 ? new CandidateComponentsIndex(result) : null); } catch (IOException ex) { throw new IllegalStateException("Unable to load indexes from location [" + COMPONENTS_RESOURCE_LOCATION + "]", ex); } }
/** * Create a PropertySource based on Properties loaded from the given resource. * The name of the PropertySource will be generated based on the * {@link Resource#getDescription() description} of the given resource. */ public ResourcePropertySource(Resource resource) throws IOException { super(getNameForResource(resource), PropertiesLoaderUtils.loadProperties(new EncodedResource(resource))); this.resourceName = null; }
/** * Create a PropertySource having the given name based on Properties * loaded from the given encoded resource. */ public ResourcePropertySource(String name, EncodedResource resource) throws IOException { super(name, PropertiesLoaderUtils.loadProperties(resource)); this.resourceName = getNameForResource(resource.getResource()); }
/** * Create a PropertySource based on Properties loaded from the given resource. * The name of the PropertySource will be generated based on the * {@link Resource#getDescription() description} of the given resource. */ public ResourcePropertySource(EncodedResource resource) throws IOException { super(getNameForResource(resource.getResource()), PropertiesLoaderUtils.loadProperties(resource)); this.resourceName = null; }
/** * Create a PropertySource having the given name based on Properties * loaded from the given encoded resource. */ public ResourcePropertySource(String name, Resource resource) throws IOException { super(name, PropertiesLoaderUtils.loadProperties(new EncodedResource(resource))); this.resourceName = getNameForResource(resource); }
@Nullable private static CandidateComponentsIndex doLoadIndex(ClassLoader classLoader) { if (shouldIgnoreIndex) { return null; } try { Enumeration<URL> urls = classLoader.getResources(COMPONENTS_RESOURCE_LOCATION); if (!urls.hasMoreElements()) { return null; } List<Properties> result = new ArrayList<>(); while (urls.hasMoreElements()) { URL url = urls.nextElement(); Properties properties = PropertiesLoaderUtils.loadProperties(new UrlResource(url)); result.add(properties); } if (logger.isDebugEnabled()) { logger.debug("Loaded " + result.size() + "] index(es)"); } int totalCount = result.stream().mapToInt(Properties::size).sum(); return (totalCount > 0 ? new CandidateComponentsIndex(result) : null); } catch (IOException ex) { throw new IllegalStateException("Unable to load indexes from location [" + COMPONENTS_RESOURCE_LOCATION + "]", ex); } }
public static Properties loadI18nProp(){ if (prop != null) { return prop; } try { // build i18n prop String i18n = XxlJobAdminConfig.getAdminConfig().getI18n(); i18n = StringUtils.isNotBlank(i18n)?("_"+i18n):i18n; String i18nFile = MessageFormat.format("i18n/message{0}.properties", i18n); // load prop Resource resource = new ClassPathResource(i18nFile); EncodedResource encodedResource = new EncodedResource(resource,"UTF-8"); prop = PropertiesLoaderUtils.loadProperties(encodedResource); } catch (IOException e) { logger.error(e.getMessage(), e); } return prop; }
private static Map<String, List<String>> loadSpringFactories(@Nullable ClassLoader classLoader) { MultiValueMap<String, String> result = cache.get(classLoader); if (result != null) { return result; } try { Enumeration<URL> urls = (classLoader != null ? classLoader.getResources(FACTORIES_RESOURCE_LOCATION) : ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION)); result = new LinkedMultiValueMap<>(); while (urls.hasMoreElements()) { URL url = urls.nextElement(); UrlResource resource = new UrlResource(url); Properties properties = PropertiesLoaderUtils.loadProperties(resource); for (Map.Entry<?, ?> entry : properties.entrySet()) { String factoryClassName = ((String) entry.getKey()).trim(); for (String factoryName : StringUtils.commaDelimitedListToStringArray((String) entry.getValue())) { result.add(factoryClassName, factoryName.trim()); } } } cache.put(classLoader, result); return result; } catch (IOException ex) { throw new IllegalArgumentException("Unable to load factories from location [" + FACTORIES_RESOURCE_LOCATION + "]", ex); } }
private static List<HandlerMapping> initFallback(ApplicationContext applicationContext) { Properties props; String path = "DispatcherServlet.properties"; try { Resource resource = new ClassPathResource(path, DispatcherServlet.class); props = PropertiesLoaderUtils.loadProperties(resource); } catch (IOException ex) { throw new IllegalStateException("Could not load '" + path + "': " + ex.getMessage()); } String value = props.getProperty(HandlerMapping.class.getName()); String[] names = StringUtils.commaDelimitedListToStringArray(value); List<HandlerMapping> result = new ArrayList<>(names.length); for (String name : names) { try { Class<?> clazz = ClassUtils.forName(name, DispatcherServlet.class.getClassLoader()); Object mapping = applicationContext.getAutowireCapableBeanFactory().createBean(clazz); result.add((HandlerMapping) mapping); } catch (ClassNotFoundException ex) { throw new IllegalStateException("Could not find default HandlerMapping [" + name + "]"); } } return result; }
/** * Create a PropertySource based on Properties loaded from the given resource. * The name of the PropertySource will be generated based on the * {@link Resource#getDescription() description} of the given resource. */ public ResourcePropertySource(Resource resource) throws IOException { super(getNameForResource(resource), PropertiesLoaderUtils.loadProperties(new EncodedResource(resource))); this.resourceName = null; }
/** * Create a PropertySource having the given name based on Properties * loaded from the given encoded resource. */ public ResourcePropertySource(String name, EncodedResource resource) throws IOException { super(name, PropertiesLoaderUtils.loadProperties(resource)); this.resourceName = getNameForResource(resource.getResource()); }
/** * Create a PropertySource based on Properties loaded from the given resource. * The name of the PropertySource will be generated based on the * {@link Resource#getDescription() description} of the given resource. */ public ResourcePropertySource(EncodedResource resource) throws IOException { super(getNameForResource(resource.getResource()), PropertiesLoaderUtils.loadProperties(resource)); this.resourceName = null; }
/** * Create a PropertySource having the given name based on Properties * loaded from the given encoded resource. */ public ResourcePropertySource(String name, Resource resource) throws IOException { super(name, PropertiesLoaderUtils.loadProperties(new EncodedResource(resource))); this.resourceName = getNameForResource(resource); }
@SuppressWarnings({ "unchecked", "rawtypes" }) private Map<String, ?> loadProperties(Resource resource) throws IOException { String filename = resource.getFilename(); if (filename != null && filename.endsWith(XML_FILE_EXTENSION)) { return (Map) PropertiesLoaderUtils.loadProperties(resource); } return new OriginTrackedPropertiesLoader(resource).load(); }
private Properties properties(String localPath) { File f = file(localPath); try { return PropertiesLoaderUtils.loadProperties(new FileSystemResource(f)); } catch (Exception ex) { throw new IllegalStateException("Cannot load Properties", ex); } }
@Test public void validateWithCustomBaseDir() throws Exception { File file = new File(this.folder.getRoot(), "metadata-store.properties"); PropertiesPersistingMetadataStore metadataStore = new PropertiesPersistingMetadataStore(); metadataStore.setBaseDirectory(folder.getRoot().getAbsolutePath()); metadataStore.afterPropertiesSet(); metadataStore.put("foo", "bar"); metadataStore.close(); assertTrue(file.exists()); Properties persistentProperties = PropertiesLoaderUtils.loadProperties(new FileSystemResource(file)); assertNotNull(persistentProperties); assertEquals(1, persistentProperties.size()); assertEquals("bar", persistentProperties.get("foo")); }
@Test public void validateWithCustomFileName() throws Exception { File file = new File(this.folder.getRoot(), "foo.properties"); PropertiesPersistingMetadataStore metadataStore = new PropertiesPersistingMetadataStore(); metadataStore.setBaseDirectory(folder.getRoot().getAbsolutePath()); metadataStore.setFileName("foo.properties"); metadataStore.afterPropertiesSet(); metadataStore.put("foo", "bar"); metadataStore.close(); assertTrue(file.exists()); Properties persistentProperties = PropertiesLoaderUtils.loadProperties(new FileSystemResource(file)); assertNotNull(persistentProperties); assertEquals(1, persistentProperties.size()); assertEquals("bar", persistentProperties.get("foo")); }