|
| 1 | +/* |
| 2 | + * Copyright 2012-2017 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot; |
| 18 | + |
| 19 | +import java.util.Collections; |
| 20 | +import java.util.HashSet; |
| 21 | +import java.util.Set; |
| 22 | + |
| 23 | +import org.springframework.core.env.ConfigurableEnvironment; |
| 24 | +import org.springframework.core.env.Environment; |
| 25 | +import org.springframework.core.env.MutablePropertySources; |
| 26 | +import org.springframework.core.env.PropertySource; |
| 27 | +import org.springframework.core.env.StandardEnvironment; |
| 28 | +import org.springframework.util.ClassUtils; |
| 29 | +import org.springframework.web.context.ConfigurableWebEnvironment; |
| 30 | +import org.springframework.web.context.support.StandardServletEnvironment; |
| 31 | + |
| 32 | +/** |
| 33 | + * Utility class for converting one type of {@link Environment} to another. |
| 34 | + * |
| 35 | + * @author Ethan Rubinson |
| 36 | + * @author Andy Wilkinson |
| 37 | + */ |
| 38 | +final class EnvironmentConverter { |
| 39 | + |
| 40 | + private static final String CONFIGURABLE_WEB_ENVIRONMENT_CLASS = "org.springframework.web.context.ConfigurableWebEnvironment"; |
| 41 | + |
| 42 | + private static final Set<String> SERVLET_ENVIRONMENT_SOURCE_NAMES; |
| 43 | + |
| 44 | + static { |
| 45 | + final Set<String> names = new HashSet<String>(); |
| 46 | + names.add(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME); |
| 47 | + names.add(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME); |
| 48 | + names.add(StandardServletEnvironment.JNDI_PROPERTY_SOURCE_NAME); |
| 49 | + SERVLET_ENVIRONMENT_SOURCE_NAMES = Collections.unmodifiableSet(names); |
| 50 | + } |
| 51 | + |
| 52 | + private final ClassLoader classLoader; |
| 53 | + |
| 54 | + /** |
| 55 | + * Creates a new {@link EnvironmentConverter} that will use the given |
| 56 | + * {@code classLoader} during conversion. |
| 57 | + * @param classLoader the class loader to use |
| 58 | + */ |
| 59 | + EnvironmentConverter(ClassLoader classLoader) { |
| 60 | + this.classLoader = classLoader; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Converts the given {@code environment} to a {@link StandardEnvironment}. If the |
| 65 | + * environment is already a {@code StandardEnvironment} and is not a |
| 66 | + * {@link ConfigurableWebEnvironment} no conversion is performed and it is returned |
| 67 | + * unchanged. |
| 68 | + * |
| 69 | + * @param environment The Environment to convert |
| 70 | + * @return The converted Environment |
| 71 | + */ |
| 72 | + StandardEnvironment convertToStandardEnvironmentIfNecessary( |
| 73 | + ConfigurableEnvironment environment) { |
| 74 | + if (environment instanceof StandardEnvironment |
| 75 | + && !isWebEnvironment(environment, this.classLoader)) { |
| 76 | + return (StandardEnvironment) environment; |
| 77 | + } |
| 78 | + return convertToStandardEnvironment(environment); |
| 79 | + } |
| 80 | + |
| 81 | + private boolean isWebEnvironment(ConfigurableEnvironment environment, |
| 82 | + ClassLoader classLoader) { |
| 83 | + try { |
| 84 | + Class<?> webEnvironmentClass = ClassUtils |
| 85 | + .forName(CONFIGURABLE_WEB_ENVIRONMENT_CLASS, classLoader); |
| 86 | + return (webEnvironmentClass.isInstance(environment)); |
| 87 | + } |
| 88 | + catch (Throwable ex) { |
| 89 | + return false; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + private StandardEnvironment convertToStandardEnvironment( |
| 94 | + ConfigurableEnvironment environment) { |
| 95 | + StandardEnvironment result = new StandardEnvironment(); |
| 96 | + result.setActiveProfiles(environment.getActiveProfiles()); |
| 97 | + result.setConversionService(environment.getConversionService()); |
| 98 | + copyNonServletPropertySources(environment, result); |
| 99 | + return result; |
| 100 | + } |
| 101 | + |
| 102 | + private void copyNonServletPropertySources(ConfigurableEnvironment source, |
| 103 | + StandardEnvironment target) { |
| 104 | + removeAllPropertySources(target.getPropertySources()); |
| 105 | + for (PropertySource<?> propertySource : source.getPropertySources()) { |
| 106 | + if (!SERVLET_ENVIRONMENT_SOURCE_NAMES.contains(propertySource.getName())) { |
| 107 | + target.getPropertySources().addLast(propertySource); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + private void removeAllPropertySources(MutablePropertySources propertySources) { |
| 113 | + Set<String> names = new HashSet<String>(); |
| 114 | + for (PropertySource<?> propertySource : propertySources) { |
| 115 | + names.add(propertySource.getName()); |
| 116 | + } |
| 117 | + for (String name : names) { |
| 118 | + propertySources.remove(name); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | +} |
0 commit comments