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.devtools.env;
018
019import java.io.File;
020import java.io.IOException;
021import java.util.Properties;
022
023import org.springframework.boot.SpringApplication;
024import org.springframework.boot.env.EnvironmentPostProcessor;
025import org.springframework.core.env.ConfigurableEnvironment;
026import org.springframework.core.env.PropertiesPropertySource;
027import org.springframework.core.io.FileSystemResource;
028import org.springframework.core.io.support.PropertiesLoaderUtils;
029import org.springframework.util.StringUtils;
030
031/**
032 * {@link EnvironmentPostProcessor} to add devtools properties from the user's home
033 * folder.
034 *
035 * @author Phillip Webb
036 * @author Andy Wilkinson
037 * @since 1.3.0
038 */
039public class DevToolsHomePropertiesPostProcessor implements EnvironmentPostProcessor {
040
041        private static final String FILE_NAME = ".spring-boot-devtools.properties";
042
043        @Override
044        public void postProcessEnvironment(ConfigurableEnvironment environment,
045                        SpringApplication application) {
046                File home = getHomeFolder();
047                File propertyFile = (home != null) ? new File(home, FILE_NAME) : null;
048                if (propertyFile != null && propertyFile.exists() && propertyFile.isFile()) {
049                        FileSystemResource resource = new FileSystemResource(propertyFile);
050                        Properties properties;
051                        try {
052                                properties = PropertiesLoaderUtils.loadProperties(resource);
053                                environment.getPropertySources().addFirst(
054                                                new PropertiesPropertySource("devtools-local", properties));
055                        }
056                        catch (IOException ex) {
057                                throw new IllegalStateException("Unable to load " + FILE_NAME, ex);
058                        }
059                }
060        }
061
062        protected File getHomeFolder() {
063                String home = System.getProperty("user.home");
064                if (StringUtils.hasLength(home)) {
065                        return new File(home);
066                }
067                return null;
068        }
069
070}