|
| 1 | +/* |
| 2 | + * Copyright 2021 - 2022 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 | +package org.springframework.sbm.boot.upgrade_27_30.report.helper; |
| 17 | + |
| 18 | +import org.apache.commons.lang3.tuple.ImmutablePair; |
| 19 | +import org.apache.commons.lang3.tuple.Pair; |
| 20 | +import org.springframework.sbm.boot.common.conditions.IsSpringBootProject; |
| 21 | +import org.springframework.sbm.boot.upgrade_27_30.report.SpringBootUpgradeReportSection; |
| 22 | +import org.springframework.sbm.common.filter.PathPatternMatchingProjectResourceFinder; |
| 23 | +import org.springframework.sbm.engine.context.ProjectContext; |
| 24 | +import org.springframework.sbm.project.resource.ProjectResource; |
| 25 | + |
| 26 | +import java.io.ByteArrayInputStream; |
| 27 | +import java.io.IOException; |
| 28 | +import java.util.List; |
| 29 | +import java.util.Map; |
| 30 | +import java.util.Optional; |
| 31 | +import java.util.Properties; |
| 32 | + |
| 33 | +/** |
| 34 | + * @author Fabian Krüger |
| 35 | + */ |
| 36 | +} |
| 37 | +public class AutoConfigurationRegistrationHelper extends SpringBootUpgradeReportSection.AbstractHelper<List<Pair<Properties, ProjectResource>>> { |
| 38 | + |
| 39 | + public static final String VERSION_PATTERN = "(2\\.7\\..*)|(3\\.0\\..*)"; |
| 40 | + |
| 41 | + private static final String SPRING_FACTORIES_PATH = "/**/src/**/resources/META-INF/spring.factories"; |
| 42 | + |
| 43 | + private Map<String, List<Pair<Properties, ProjectResource>>> matchingSpringFactoriesFiles; |
| 44 | + |
| 45 | + @Override |
| 46 | + public boolean evaluate(ProjectContext context) { |
| 47 | + IsSpringBootProject isSpringBootProjectCondition = new IsSpringBootProject(); |
| 48 | + isSpringBootProjectCondition.setVersionPattern(VERSION_PATTERN); |
| 49 | + boolean isSpringBoot3Application = isSpringBootProjectCondition.evaluate(context); |
| 50 | + if(! isSpringBoot3Application) { |
| 51 | + return false; |
| 52 | + } |
| 53 | + |
| 54 | + Optional<Pair<Properties, ProjectResource>> props = getSpringFactoriesProperties(context); |
| 55 | + |
| 56 | + if (props.isEmpty()) { |
| 57 | + return false; |
| 58 | + } |
| 59 | + |
| 60 | + this.matchingSpringFactoriesFiles = props.get(); |
| 61 | + return true; |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public Map<String, List<Pair<Properties, ProjectResource>>> getData() { |
| 66 | + return matchingSpringFactoriesFiles; |
| 67 | + } |
| 68 | + |
| 69 | + |
| 70 | + private Optional<Pair<Properties, ProjectResource>> getSpringFactoriesProperties(ProjectContext context) { |
| 71 | + List<ProjectResource> search = context.search( |
| 72 | + new PathPatternMatchingProjectResourceFinder( |
| 73 | + SPRING_FACTORIES_PATH |
| 74 | + )); |
| 75 | + |
| 76 | + if (search.size() > 0) { |
| 77 | + String oldConfigFile = search.get(0).print(); |
| 78 | + Properties prop = new Properties(); |
| 79 | + |
| 80 | + try { |
| 81 | + prop.load(new ByteArrayInputStream(oldConfigFile.getBytes())); |
| 82 | + return Optional.of(new ImmutablePair<>(prop, search.get(0))); |
| 83 | + } catch (IOException e) { |
| 84 | + throw new RuntimeException("Error whilst reading property file " + SPRING_FACTORIES_PATH, e); |
| 85 | + } |
| 86 | + } |
| 87 | + return Optional.empty(); |
| 88 | + } |
| 89 | +} |
0 commit comments