001/* 002 * Copyright 2012-2018 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.boot.env; 018 019import java.io.IOException; 020import java.util.Collections; 021import java.util.List; 022import java.util.Map; 023 024import org.springframework.core.env.PropertySource; 025import org.springframework.core.io.Resource; 026import org.springframework.core.io.support.PropertiesLoaderUtils; 027 028/** 029 * Strategy to load '.properties' files into a {@link PropertySource}. 030 * 031 * @author Dave Syer 032 * @author Phillip Webb 033 * @author Madhura Bhave 034 */ 035public class PropertiesPropertySourceLoader implements PropertySourceLoader { 036 037 private static final String XML_FILE_EXTENSION = ".xml"; 038 039 @Override 040 public String[] getFileExtensions() { 041 return new String[] { "properties", "xml" }; 042 } 043 044 @Override 045 public List<PropertySource<?>> load(String name, Resource resource) 046 throws IOException { 047 Map<String, ?> properties = loadProperties(resource); 048 if (properties.isEmpty()) { 049 return Collections.emptyList(); 050 } 051 return Collections 052 .singletonList(new OriginTrackedMapPropertySource(name, properties)); 053 } 054 055 @SuppressWarnings({ "unchecked", "rawtypes" }) 056 private Map<String, ?> loadProperties(Resource resource) throws IOException { 057 String filename = resource.getFilename(); 058 if (filename != null && filename.endsWith(XML_FILE_EXTENSION)) { 059 return (Map) PropertiesLoaderUtils.loadProperties(resource); 060 } 061 return new OriginTrackedPropertiesLoader(resource).load(); 062 } 063 064}