Skip to content

Commit dbb171b

Browse files
committed
Revamp/855 make pomCache configurable through properties (#907)
* cleanup * Log statement when Maven parsing is skipped * Refactor Parser properties * Change properties file name * properties initialized with PostProcessor * Cache added to ExecutionContext on bean declaration * Remove unused property * Remove pomCache from parser * Rename ParserSettings to ParserProperties * build action
1 parent 28eaafb commit dbb171b

25 files changed

+453
-257
lines changed

.github/workflows/build-sbm-support-rewrite.yml

+1-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ name: Build SBM Revamp
22
on:
33
push:
44
branches:
5-
- "revamp/**"
6-
- "version/revamp"
7-
branches-ignore:
8-
- "main"
5+
- "**"
96
paths:
107
- "sbm-support-rewrite/**"
118
jobs:

sbm-support-rewrite/README.adoc

+38
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,44 @@ The `OpenRewriteProjectParser` aims to provide the exact same parsing capabiliti
8080

8181
This allows parsing a given project to it's OpenRewrite AST representation and then provide the AST to OR recipes and visitors to run OpenRewrite recipes without requiring a build plugin.
8282

83+
## Configuration
84+
85+
### Maven Pom Cache
86+
OpenRewrite downloads Maven Pom files to resolve dependencies.
87+
The pom files get cached and the cache depends on the system.
88+
89+
- 32-Bit systems use the `InMemoryPomCache`.
90+
- 64-Bit systems use the `RocksdbMavenPomCache`.
91+
92+
####
93+
94+
#### Pom Cache Properties
95+
96+
|===
97+
|Name |Description |Default Value
98+
99+
|`parser.isPomCacheEnabled`
100+
|If the flag is set to false, only the default, in-memory cache is used.
101+
|`false`
102+
103+
|`parser.pomCacheDirectory`
104+
|Defines the cache dir for RocksdbMavenPomCache when `parser.isPomCacheEnabled` is `true`
105+
|`~/.rewrite-cache`
106+
|===
107+
108+
#### Custom Pom Cache
109+
Users could provide a custom `MavenPomCache` implementation by overwriting the bean definition in `RewriteMavenPomCacheConfig.mavenPomCache()`.
110+
111+
[source,java]
112+
....
113+
@Bean
114+
@Primary
115+
public MavenPomCache mavenPomCache() {
116+
return new CustomMavenPomCache();
117+
}
118+
....
119+
120+
83121
## Running OpenRewrite recipes
84122

85123
### Scan the codebase
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

+38-13
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,54 @@
1515
*/
1616
package org.springframework.sbm;
1717

18-
import org.openrewrite.Recipe;
19-
import org.openrewrite.RecipeRun;
20-
import org.openrewrite.SourceFile;
21-
import org.openrewrite.internal.InMemoryLargeSourceSet;
22-
import org.openrewrite.xml.tree.Xml;
23-
import org.springframework.boot.SpringApplication;
24-
import org.springframework.boot.SpringBootConfiguration;
18+
import lombok.extern.slf4j.Slf4j;
19+
import org.openrewrite.maven.cache.CompositeMavenPomCache;
20+
import org.openrewrite.maven.cache.InMemoryMavenPomCache;
21+
import org.openrewrite.maven.cache.MavenPomCache;
22+
import org.openrewrite.maven.cache.RocksdbMavenPomCache;
2523
import org.springframework.boot.autoconfigure.SpringBootApplication;
26-
import org.springframework.context.ConfigurableApplicationContext;
27-
import org.springframework.context.annotation.ComponentScan;
28-
import org.springframework.context.annotation.Configuration;
29-
import org.springframework.sbm.parsers.*;
30-
import org.springframework.sbm.recipes.RewriteRecipeDiscovery;
24+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
25+
import org.springframework.context.annotation.Bean;
26+
import org.springframework.sbm.parsers.ParserProperties;
3127

28+
import java.io.PrintWriter;
29+
import java.io.StringWriter;
3230
import java.nio.file.Path;
33-
import java.util.List;
31+
3432

3533
/**
3634
* Module configuration.
3735
*
3836
* @author Fabian Krüger
3937
*/
38+
@Slf4j
4039
@SpringBootApplication
4140
public class RewriteParserConfig {
4241

42+
@Bean
43+
@ConditionalOnMissingBean(MavenPomCache.class)
44+
MavenPomCache mavenPomCache(ParserProperties parserProperties) {
45+
MavenPomCache mavenPomCache = new InMemoryMavenPomCache();
46+
if (parserProperties.isPomCacheEnabled()) {
47+
if (!"64".equals(System.getProperty("sun.arch.data.model", "64"))) {
48+
log.warn("parser.isPomCacheEnabled was set to true but RocksdbMavenPomCache is not supported on 32-bit JVM. falling back to InMemoryMavenPomCache");
49+
} else {
50+
try {
51+
mavenPomCache = new CompositeMavenPomCache(
52+
new InMemoryMavenPomCache(),
53+
new RocksdbMavenPomCache(Path.of(parserProperties.getPomCacheDirectory()))
54+
);
55+
} catch (Exception e) {
56+
log.warn("Unable to initialize RocksdbMavenPomCache, falling back to InMemoryMavenPomCache");
57+
if (log.isDebugEnabled()) {
58+
StringWriter sw = new StringWriter();
59+
e.printStackTrace(new PrintWriter(sw));
60+
String exceptionAsString = sw.toString();
61+
log.debug(exceptionAsString);
62+
}
63+
}
64+
}
65+
}
66+
return mavenPomCache;
67+
}
4368
}

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

+1-43
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@
5151
@RequiredArgsConstructor
5252
class BuildFileParser {
5353

54-
private final ParserSettings parserSettings;
55-
5654
/**
5755
* Parse a list of Maven Pom files to a Map of {@code Path} and their parsed {@link Xml.Document}s.
5856
* The {@link Xml.Document}s are marked with {@link org.openrewrite.maven.tree.MavenResolutionResult} and the provided provenance markers.
@@ -81,6 +79,7 @@ public Map<Path, Xml.Document> parseBuildFiles(
8179
Assert.isTrue(resourcesWithoutProvenanceMarker.isEmpty(), "No provenance marker provided for these pom files %s".formatted(resourcesWithoutProvenanceMarker.stream().map(r -> ResourceUtil.getPath(r).toAbsolutePath()).toList()));
8280

8381
if(skipMavenParsing) {
82+
log.info("Maven parsing skipped [parser.skipMavenParsing=true].");
8483
return Map.of();
8584
}
8685

@@ -95,14 +94,6 @@ public Map<Path, Xml.Document> parseBuildFiles(
9594
// 385 : 387
9695
initializeMavenSettings(executionContext);
9796

98-
// 389 : 393
99-
if (parserSettings.isPomCacheEnabled()) {
100-
//The default pom cache is enabled as a two-layer cache L1 == in-memory and L2 == RocksDb
101-
//If the flag is set to false, only the default, in-memory cache is used.
102-
MavenPomCache pomCache = getPomCache();
103-
MavenExecutionContextView.view(executionContext).setPomCache(pomCache);
104-
}
105-
10697
// 395 : 398
10798

10899
mavenParserBuilder.activeProfiles(activeProfiles.toArray(new String[]{}));
@@ -168,39 +159,6 @@ private List<SourceFile> parsePoms(Path baseDir, List<Resource> pomFiles, MavenP
168159
return mavenParserBuilder.build().parseInputs(pomFileInputs, baseDir, executionContext).toList();
169160
}
170161

171-
/**
172-
* {@link MavenMojoProjectParser##getPomCache()}
173-
*/
174-
private static MavenPomCache getPomCache() {
175-
// if (pomCache == null) {
176-
// if (isJvm64Bit()) {
177-
// try {
178-
// if (pomCacheDirectory == null) {
179-
// //Default directory in the RocksdbMavenPomCache is ".rewrite-cache"
180-
// pomCache = new CompositeMavenPomCache(
181-
// new InMemoryMavenPomCache(),
182-
// new RocksdbMavenPomCache(Paths.get(System.getProperty("user.home")))
183-
// );
184-
// } else {
185-
// pomCache = new CompositeMavenPomCache(
186-
// new InMemoryMavenPomCache(),
187-
// new RocksdbMavenPomCache(Paths.get(pomCacheDirectory))
188-
// );
189-
// }
190-
// } catch (Exception e) {
191-
// logger.warn("Unable to initialize RocksdbMavenPomCache, falling back to InMemoryMavenPomCache");
192-
// logger.debug(e);
193-
// }
194-
// } else {
195-
// logger.warn("RocksdbMavenPomCache is not supported on 32-bit JVM. falling back to InMemoryMavenPomCache");
196-
// }
197-
// }
198-
// if (pomCache == null) {
199-
MavenPomCache pomCache = new InMemoryMavenPomCache();
200-
// }
201-
return pomCache;
202-
}
203-
204162
private void initializeMavenSettings(ExecutionContext executionContext) {
205163

206164
}

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

+15-15
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,19 @@
3939
@RequiredArgsConstructor
4040
class MavenMojoProjectParserFactory {
4141

42-
private final ParserSettings parserSettings;
42+
private final ParserProperties parserProperties;
4343

4444
public MavenMojoProjectParser create(Path baseDir, List<MavenProject> mavenProjects, PlexusContainer plexusContainer, MavenSession session) {
4545
return buildMavenMojoProjectParser(
4646
baseDir,
4747
mavenProjects,
48-
parserSettings.isPomCacheEnabled(),
49-
parserSettings.getPomCacheDirectory(),
50-
parserSettings.isSkipMavenParsing(),
51-
parserSettings.getIgnoredPathPatterns(),
52-
parserSettings.getPlainTextMasks(),
53-
parserSettings.getSizeThresholdMb(),
54-
parserSettings.isRunPerSubmodule(),
48+
parserProperties.isPomCacheEnabled(),
49+
parserProperties.getPomCacheDirectory(),
50+
parserProperties.isSkipMavenParsing(),
51+
parserProperties.getIgnoredPathPatterns(),
52+
parserProperties.getPlainTextMasks(),
53+
parserProperties.getSizeThresholdMb(),
54+
parserProperties.isRunPerSubmodule(),
5555
plexusContainer,
5656
session);
5757
}
@@ -98,16 +98,16 @@ public MavenMojoProjectParser create(Path baseDir, RuntimeInformation runtimeInf
9898
return new MavenMojoProjectParser(
9999
new Slf4jToMavenLoggerAdapter(log),
100100
baseDir,
101-
parserSettings.isPomCacheEnabled(),
102-
parserSettings.getPomCacheDirectory(),
101+
parserProperties.isPomCacheEnabled(),
102+
parserProperties.getPomCacheDirectory(),
103103
runtimeInformation,
104-
parserSettings.isSkipMavenParsing(),
105-
parserSettings.getIgnoredPathPatterns(),
106-
parserSettings.getPlainTextMasks(),
107-
parserSettings.getSizeThresholdMb(),
104+
parserProperties.isSkipMavenParsing(),
105+
parserProperties.getIgnoredPathPatterns(),
106+
parserProperties.getPlainTextMasks(),
107+
parserProperties.getSizeThresholdMb(),
108108
null,
109109
settingsDecrypter,
110-
parserSettings.isRunPerSubmodule()
110+
parserProperties.isRunPerSubmodule()
111111
);
112112
}
113113
}

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

+4-15
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,16 @@
1616
package org.springframework.sbm.parsers;
1717

1818
import lombok.*;
19-
import org.apache.commons.logging.Log;
2019
import org.springframework.boot.context.properties.ConfigurationProperties;
21-
import org.springframework.boot.context.properties.ConfigurationPropertiesBinding;
2220
import org.springframework.stereotype.Component;
2321

24-
import java.nio.file.Path;
2522
import java.util.HashSet;
2623
import java.util.List;
2724
import java.util.Set;
2825

2926
/**
3027
* ConfigurationProperties with prefix {@code parser}.
28+
* Defaults coming from {@code META-INF/sbm-support-rewrite.properties}
3129
*
3230
* @author Fabian Krüger
3331
*/
@@ -37,13 +35,11 @@
3735
@AllArgsConstructor
3836
@NoArgsConstructor
3937
@ConfigurationProperties(prefix = "parser")
40-
public class ParserSettings {
38+
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
39+
public class ParserProperties {
4140

4241
private String loggerClass;
43-
/**
44-
* Defines if pom files get cached or if they are always downloaded.
45-
*/
46-
private boolean pomCacheEnabled = true;
42+
private boolean pomCacheEnabled = false;
4743
private String pomCacheDirectory;
4844
private boolean skipMavenParsing = false;
4945
private Set<String> plainTextMasks = new HashSet<>();
@@ -53,13 +49,6 @@ public class ParserSettings {
5349
private List<String> activeProfiles = List.of("default");
5450
private Set<String> ignoredPathPatterns = new HashSet<>();
5551

56-
/**
57-
* @return fully qualified classname of the logger to use.
58-
*/
59-
public String getLoggerClass() {
60-
return loggerClass;
61-
}
62-
6352
public void setLoggerClass(String loggerClass) {
6453
this.loggerClass = loggerClass;
6554
}

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import java.nio.file.PathMatcher;
3030
import java.util.List;
3131
import java.util.Optional;
32-
import java.util.Set;
3332
import java.util.stream.Stream;
3433

3534
/**
@@ -40,7 +39,7 @@
4039
@RequiredArgsConstructor
4140
class ProjectScanner {
4241
private final ResourceLoader resourceLoader;
43-
private final ParserSettings parserSettings;
42+
private final ParserProperties parserProperties;
4443

4544
public List<Resource> scan(Path baseDir) {
4645
if(!baseDir.isAbsolute()) {
@@ -55,7 +54,7 @@ public List<Resource> scan(Path baseDir) {
5554
try {
5655
Resource[] resources = ResourcePatternUtils.getResourcePatternResolver(resourceLoader).getResources(pattern);
5756

58-
List<PathMatcher> pathMatchers = parserSettings.getIgnoredPathPatterns().stream()
57+
List<PathMatcher> pathMatchers = parserProperties.getIgnoredPathPatterns().stream()
5958
.map(p -> p.startsWith("glob:") ? p : "glob:" + p)
6059
.map(baseDir.getFileSystem()::getPathMatcher)
6160
.toList();

0 commit comments

Comments
 (0)