|
| 1 | +/* |
| 2 | + * Copyright 2015-2021 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 | + * https://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.cloud.zookeeper.config; |
| 18 | + |
| 19 | +import org.springframework.boot.SpringApplication; |
| 20 | +import org.springframework.boot.context.config.ConfigDataEnvironmentPostProcessor; |
| 21 | +import org.springframework.boot.diagnostics.AbstractFailureAnalyzer; |
| 22 | +import org.springframework.boot.diagnostics.FailureAnalysis; |
| 23 | +import org.springframework.boot.env.EnvironmentPostProcessor; |
| 24 | +import org.springframework.cloud.zookeeper.ZookeeperProperties; |
| 25 | +import org.springframework.core.Ordered; |
| 26 | +import org.springframework.core.env.ConfigurableEnvironment; |
| 27 | +import org.springframework.util.StringUtils; |
| 28 | + |
| 29 | +import static org.springframework.cloud.util.PropertyUtils.bootstrapEnabled; |
| 30 | +import static org.springframework.cloud.util.PropertyUtils.useLegacyProcessing; |
| 31 | +import static org.springframework.cloud.zookeeper.config.ZookeeperConfigDataLocationResolver.PREFIX; |
| 32 | + |
| 33 | +public class ZookeeperConfigDataMissingEnvironmentPostProcessor implements EnvironmentPostProcessor, Ordered { |
| 34 | + |
| 35 | + /** |
| 36 | + * Order of post processor, set to run after |
| 37 | + * {@link ConfigDataEnvironmentPostProcessor}. |
| 38 | + */ |
| 39 | + public static final int ORDER = ConfigDataEnvironmentPostProcessor.ORDER + 1000; |
| 40 | + |
| 41 | + @Override |
| 42 | + public int getOrder() { |
| 43 | + return ORDER; |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { |
| 48 | + // don't run if using bootstrap or legacy processing |
| 49 | + if (bootstrapEnabled(environment) || useLegacyProcessing(environment)) { |
| 50 | + return; |
| 51 | + } |
| 52 | + boolean coreEnabled = environment.getProperty(ZookeeperProperties.PREFIX + ".enabled", Boolean.class, |
| 53 | + true); |
| 54 | + boolean configEnabled = environment.getProperty(ZookeeperConfigProperties.PREFIX + ".enabled", Boolean.class, |
| 55 | + true); |
| 56 | + boolean importCheckEnabled = environment.getProperty(ZookeeperConfigProperties.PREFIX + ".import-check.enabled", |
| 57 | + Boolean.class, true); |
| 58 | + if (!coreEnabled || !configEnabled || !importCheckEnabled) { |
| 59 | + return; |
| 60 | + } |
| 61 | + String property = environment.getProperty("spring.config.import"); |
| 62 | + if (!StringUtils.hasText(property)) { |
| 63 | + throw new ImportException("No spring.config.import set", false); |
| 64 | + } |
| 65 | + if (!property.contains(PREFIX)) { |
| 66 | + throw new ImportException("spring.config.import missing " + PREFIX, true); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + static class ImportException extends RuntimeException { |
| 71 | + |
| 72 | + final boolean missingPrefix; |
| 73 | + |
| 74 | + ImportException(String message, boolean missingPrefix) { |
| 75 | + super(message); |
| 76 | + this.missingPrefix = missingPrefix; |
| 77 | + } |
| 78 | + |
| 79 | + } |
| 80 | + |
| 81 | + static class ImportExceptionFailureAnalyzer extends AbstractFailureAnalyzer<ImportException> { |
| 82 | + |
| 83 | + @Override |
| 84 | + protected FailureAnalysis analyze(Throwable rootFailure, ImportException cause) { |
| 85 | + String description; |
| 86 | + if (cause.missingPrefix) { |
| 87 | + description = "The spring.config.import property is missing a " + PREFIX + " entry"; |
| 88 | + } |
| 89 | + else { |
| 90 | + description = "No spring.config.import property has been defined"; |
| 91 | + } |
| 92 | + String action = "Add a spring.config.import=zookeeper: property to your configuration.\n" |
| 93 | + + "\tIf configuration in not required add spring.config.import=optional:zookeeper: instead.\n" |
| 94 | + + "\tTo disable this check, set spring.cloud.zookeeper.config.enabled=false or \n" |
| 95 | + + "\tspring.cloud.zookeeper.config.import-check.enabled=false."; |
| 96 | + return new FailureAnalysis(description, action, cause); |
| 97 | + } |
| 98 | + |
| 99 | + } |
| 100 | + |
| 101 | +} |
0 commit comments