Skip to content

Commit e33b87c

Browse files
committed
Refactor Parser properties
* Change properties file name * properties initialized with PostProcessor * Cache added to ExecutionContext on bean declaration * Remove unused property
1 parent f362c1f commit e33b87c

File tree

8 files changed

+181
-30
lines changed

8 files changed

+181
-30
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2021 - 2023 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;
17+
18+
import org.springframework.boot.SpringApplication;
19+
import org.springframework.boot.env.EnvironmentPostProcessor;
20+
import org.springframework.boot.env.PropertiesPropertySourceLoader;
21+
import org.springframework.core.env.ConfigurableEnvironment;
22+
import org.springframework.core.env.PropertySource;
23+
import org.springframework.core.io.ClassPathResource;
24+
import org.springframework.core.io.Resource;
25+
import org.springframework.stereotype.Component;
26+
import org.springframework.util.Assert;
27+
28+
import java.io.IOException;
29+
30+
/**
31+
* Add default values from 'sbm-support-rewrite.properties' to environment.
32+
*/
33+
@Component
34+
public class ParserPropertiesPostProcessor implements EnvironmentPostProcessor {
35+
36+
private final PropertiesPropertySourceLoader loader = new PropertiesPropertySourceLoader();
37+
38+
@Override
39+
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
40+
Resource path = new ClassPathResource("/META-INF/sbm-support-rewrite.properties");
41+
PropertySource<?> propertySource = loadProperties(path);
42+
environment.getPropertySources().addLast(propertySource);
43+
}
44+
45+
private PropertySource<?> loadProperties(Resource path) {
46+
Assert.isTrue(path.exists(), () -> "Resource " + path + " does not exist");
47+
try {
48+
return this.loader.load("custom-resource", path).get(0);
49+
}
50+
catch (IOException ex) {
51+
throw new IllegalStateException("Failed to load properties configuration from " + path, ex);
52+
}
53+
}
54+
55+
}

sbm-support-rewrite/src/main/java/org/springframework/sbm/RewriteParserConfig.java

-12
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,18 @@
1616
package org.springframework.sbm;
1717

1818
import lombok.extern.slf4j.Slf4j;
19-
import org.openrewrite.ExecutionContext;
20-
import org.openrewrite.Recipe;
21-
import org.openrewrite.RecipeRun;
22-
import org.openrewrite.SourceFile;
23-
import org.openrewrite.internal.InMemoryLargeSourceSet;
24-
import org.openrewrite.maven.MavenExecutionContextView;
2519
import org.openrewrite.maven.cache.CompositeMavenPomCache;
2620
import org.openrewrite.maven.cache.InMemoryMavenPomCache;
2721
import org.openrewrite.maven.cache.MavenPomCache;
2822
import org.openrewrite.maven.cache.RocksdbMavenPomCache;
29-
import org.openrewrite.xml.tree.Xml;
30-
import org.springframework.boot.SpringApplication;
31-
import org.springframework.boot.SpringBootConfiguration;
3223
import org.springframework.boot.autoconfigure.SpringBootApplication;
3324
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
34-
import org.springframework.context.ConfigurableApplicationContext;
3525
import org.springframework.context.annotation.Bean;
3626
import org.springframework.sbm.parsers.ParserProperties;
3727

3828
import java.io.PrintWriter;
3929
import java.io.StringWriter;
4030
import java.nio.file.Path;
41-
import java.util.List;
4231

4332

4433
/**
@@ -74,7 +63,6 @@ MavenPomCache mavenPomCache(ExecutionContext executionContext, ParserSettings pa
7463
}
7564
}
7665
}
77-
MavenExecutionContextView.view(executionContext).setPomCache(mavenPomCache);
7866
return mavenPomCache;
7967
}
8068
}

sbm-support-rewrite/src/main/java/org/springframework/sbm/parsers/ParserProperties.java

+1-7
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
/**
2727
* ConfigurationProperties with prefix {@code parser}.
28+
* Defaults coming from {@code META-INF/sbm-support-rewrite.properties}
2829
*
2930
* @author Fabian Krüger
3031
*/
@@ -47,13 +48,6 @@ public class ParserSettings {
4748
private List<String> activeProfiles = List.of("default");
4849
private Set<String> ignoredPathPatterns = new HashSet<>();
4950

50-
/**
51-
* @return fully qualified classname of the logger to use.
52-
*/
53-
public String getLoggerClass() {
54-
return loggerClass;
55-
}
56-
5751
public void setLoggerClass(String loggerClass) {
5852
this.loggerClass = loggerClass;
5953
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#
2+
# Copyright 2021 - 2023 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+
# Enable/Disable the MavenPomCache
18+
# With 'false' OpenRewrite's InMemoryMavenPomCache will be used.
19+
# With 'true' a composite cache of RocksdbMavenPomCache and InMemoryMavenPomCache will be used.
20+
parser.pomCacheEnabled=false
21+
22+
# Defines the cache dir for RocksdbMavenPomCache when `parser.pomCacheEnabled` is `true`.
23+
parser.pomCacheDirectory=~/.rewrite-cache
24+
parser.skipMavenParsing=false
25+
parser.plainTextMasks=*.txt
26+
parser.sizeThresholdMb=10
27+
parser.runPerSubmodule=true
28+
parser.failOnInvalidActiveRecipes=true
29+
parser.activeProfiles=default
30+
parser.ignoredPathPatterns=**.idea,**.git
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
org.springframework.boot.env.EnvironmentPostProcessor=org.springframework.sbm.ParserPropertiesPostProcessor

sbm-support-rewrite/src/main/resources/parser.properties

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Copyright 2021 - 2023 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.parsers;
17+
18+
import org.junit.jupiter.api.DisplayName;
19+
import org.junit.jupiter.api.Nested;
20+
import org.junit.jupiter.api.Test;
21+
import org.springframework.beans.factory.annotation.Autowired;
22+
import org.springframework.boot.test.context.SpringBootTest;
23+
24+
import static org.assertj.core.api.Assertions.assertThat;
25+
26+
27+
28+
class ParserPropertiesTest {
29+
30+
@Nested
31+
@SpringBootTest
32+
public class GivenDefaultProperties {
33+
34+
@Autowired
35+
private ParserProperties parserProperties;
36+
37+
@Test
38+
@DisplayName("parser.pomCacheEnabled")
39+
void validPomCacheEnabled() {
40+
assertThat(parserProperties.isPomCacheEnabled()).isFalse();
41+
}
42+
43+
44+
@Test
45+
@DisplayName("parser.pomCacheDirectory")
46+
void validPomCacheDirectory() {
47+
assertThat(parserProperties.getPomCacheDirectory()).isEqualTo("~/.rewrite-cache");
48+
}
49+
50+
@Test
51+
@DisplayName("parser.skipMavenParsing")
52+
void validSkipMavenParsing() {
53+
assertThat(parserProperties.isSkipMavenParsing()).isFalse();
54+
}
55+
56+
@Test
57+
@DisplayName("parser.plainTextMasks")
58+
void validPlainTextMasks() {
59+
assertThat(parserProperties.getPlainTextMasks()).containsExactlyInAnyOrder("*.txt");
60+
}
61+
62+
@Test
63+
@DisplayName("parser.sizeThresholdMb")
64+
void validSizeThresholdMb() {
65+
assertThat(parserProperties.getSizeThresholdMb()).isEqualTo(10);
66+
}
67+
68+
@Test
69+
@DisplayName("parser.runPerSubmodule")
70+
void validRunPerSubmodule() {
71+
assertThat(parserProperties.isRunPerSubmodule()).isTrue();
72+
}
73+
74+
@Test
75+
@DisplayName("parser.failOnInvalidActiveRecipes")
76+
void validFailOnInvalidActiveRecipes() {
77+
assertThat(parserProperties.isFailOnInvalidActiveRecipes()).isTrue();
78+
}
79+
80+
@Test
81+
@DisplayName("parser.activeProfiles")
82+
void validActiveProfiles() {
83+
assertThat(parserProperties.getActiveProfiles()).containsExactlyInAnyOrder("default");
84+
}
85+
86+
@Test
87+
@DisplayName("parser.ignoredPathPatterns")
88+
void validIgnoredPathPatterns() {
89+
assertThat(parserProperties.getIgnoredPathPatterns()).containsExactlyInAnyOrder("**.idea","**.git");
90+
}
91+
92+
}
93+
94+
}

sbm-support-rewrite/src/test/resources/parser-settings-test.properties

-11
This file was deleted.

0 commit comments

Comments
 (0)