Skip to content

Commit e99d4ce

Browse files
ishu-thakurfabapp2
authored andcommitted
adding the jmx endpoint exposure related files . (#473)
1 parent c1777bb commit e99d4ce

File tree

9 files changed

+322
-3
lines changed

9 files changed

+322
-3
lines changed

applications/spring-shell/src/test/java/org/springframework/sbm/BootUpgrade_27_30_MultiModule_IntegrationTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,8 @@ private void verifyPropertyConfigurationUpdate() {
174174
"spring.cassandra.request.timeout=10s\n" +
175175
"spring.cassandra.connection.connect-timeout=10s\n" +
176176
"spring.cassandra.connection.init-query-timeout=10s\n" +
177-
"logging.pattern.dateformat=yyyy-MM-dd HH:mm:ss.SSS\n");
177+
"logging.pattern.dateformat=yyyy-MM-dd HH:mm:ss.SSS\n" +
178+
"management.endpoints.jmx.exposure.include=*\n");
178179
}
179180

180181
private void verifyEhCacheVersionIsUpgraded() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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.actions;
17+
18+
import org.springframework.sbm.boot.properties.finder.SpringBootDefaultPropertiesFinder;
19+
import org.springframework.sbm.engine.context.ProjectContext;
20+
import org.springframework.sbm.engine.recipe.AbstractAction;
21+
22+
import java.util.Optional;
23+
24+
public class Boot_27_30_JmxEndpointExposureAction extends AbstractAction {
25+
26+
public static final String JMX_ENDPOINT = "management.endpoints.jmx.exposure.include";
27+
28+
@Override
29+
public void apply(ProjectContext context) {
30+
SpringBootDefaultPropertiesFinder springBootDefaultPropertiesFinder = new SpringBootDefaultPropertiesFinder();
31+
32+
context.getApplicationModules()
33+
.getTopmostApplicationModules()
34+
.stream()
35+
.map(m -> m.searchMainResources(springBootDefaultPropertiesFinder))
36+
.filter(Optional::isPresent)
37+
.map(Optional::get)
38+
.forEach(p -> {
39+
if (!p.getProperty(JMX_ENDPOINT).isPresent()) {
40+
p.setProperty(JMX_ENDPOINT, "*");
41+
}
42+
});
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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.conditions;
17+
18+
import org.springframework.sbm.boot.upgrade_27_30.filter.JmxEndpointExposureFinder;
19+
import org.springframework.sbm.engine.context.ProjectContext;
20+
import org.springframework.sbm.engine.recipe.Condition;
21+
22+
public class JmxEndpointExposureCondition implements Condition {
23+
@Override
24+
public String getDescription() {
25+
return "Check if 'management.endpoints.jmx.exposure.include' is declared.";
26+
}
27+
28+
@Override
29+
public boolean evaluate(ProjectContext context) {
30+
return context.search(new JmxEndpointExposureFinder()).isEmpty();
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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.filter;
17+
18+
import org.springframework.sbm.boot.properties.api.SpringBootApplicationProperties;
19+
import org.springframework.sbm.boot.properties.search.SpringBootApplicationPropertiesResourceListFilter;
20+
import org.springframework.sbm.project.resource.ProjectResourceSet;
21+
import org.springframework.sbm.project.resource.filter.ProjectResourceFinder;
22+
import org.springframework.sbm.properties.api.PropertiesSource;
23+
24+
import java.util.List;
25+
26+
public class JmxEndpointExposureFinder implements ProjectResourceFinder<List<? extends PropertiesSource>> {
27+
public static final String JMX_ENDPOINT_KEY = "management.endpoints.jmx.exposure.include";
28+
29+
@Override
30+
public List<? extends PropertiesSource> apply(ProjectResourceSet projectResourceSet) {
31+
List<SpringBootApplicationProperties> springBootApplicationProperties = new SpringBootApplicationPropertiesResourceListFilter().apply(projectResourceSet);
32+
return springBootApplicationProperties.stream()
33+
.filter(find -> find.getProperty(JMX_ENDPOINT_KEY).isPresent())
34+
.toList();
35+
}
36+
}

components/sbm-recipes-boot-upgrade/src/main/resources/recipes/boot-2.7-3.0-dependency-version-update.yaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -693,10 +693,15 @@
693693
condition:
694694
type: org.springframework.sbm.boot.upgrade_27_30.conditions.LoggingDateFormatCondition
695695
addDefaultPropertiesFileToTopModules: true
696-
- type: org.springframework.sbm.boot.upgrade_27_30.actions.Boot_27_30_AddLoggingDateFormat
697696

697+
- type: org.springframework.sbm.boot.upgrade_27_30.actions.Boot_27_30_AddLoggingDateFormat
698698
description: "Sets logging date format to yyyy-MM-dd HH:mm:ss.SSS"
699699
condition:
700700
type: org.springframework.sbm.boot.upgrade_27_30.conditions.LoggingDateFormatCondition
701701

702+
- type: org.springframework.sbm.boot.upgrade_27_30.actions.Boot_27_30_JmxEndpointExposureAction
703+
description: "Sets JMX endpoint exposure include to *"
704+
condition:
705+
type: org.springframework.sbm.boot.upgrade_27_30.conditions.JmxEndpointExposureCondition
706+
702707

components/sbm-recipes-boot-upgrade/src/main/resources/recipes/boot-2.7-3.0-upgrade-report.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@
1616
- type: org.springframework.sbm.boot.upgrade_27_30.actions.Boot_27_30_AddLoggingDateFormat
1717
description: "Sets logging date format to yyyy-MM-dd HH:mm:ss.SSS"
1818
condition:
19-
type: org.springframework.sbm.boot.upgrade_27_30.conditions.LoggingDateFormatCondition
19+
type: org.springframework.sbm.boot.upgrade_27_30.conditions.LoggingDateFormatCondition
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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.actions;
17+
18+
import org.junit.jupiter.api.Test;
19+
import org.springframework.sbm.boot.properties.SpringApplicationPropertiesPathMatcher;
20+
import org.springframework.sbm.boot.properties.SpringBootApplicationPropertiesRegistrar;
21+
import org.springframework.sbm.boot.properties.api.SpringBootApplicationProperties;
22+
import org.springframework.sbm.boot.properties.search.SpringBootApplicationPropertiesResourceListFilter;
23+
import org.springframework.sbm.engine.context.ProjectContext;
24+
import org.springframework.sbm.project.resource.TestProjectContext;
25+
26+
import java.util.List;
27+
28+
import static org.assertj.core.api.Assertions.assertThat;
29+
30+
public class Boot_27_30_JmxEndpointExposureActionTest {
31+
private final String DUMMY_PROPERTY_FILE = "foo=bar\n" +
32+
"defaultBasePackage=org.springframework.sbm";
33+
34+
@Test
35+
public void givenAProjectWithoutJmxEndpointExposureOverride_andSpringBootProperties_applyAction_expectPropertyAdded() {
36+
ProjectContext projectContext = TestProjectContext.buildProjectContext()
37+
.addRegistrar(new SpringBootApplicationPropertiesRegistrar(new SpringApplicationPropertiesPathMatcher()))
38+
.addProjectResource("src/main/resources/application.properties", DUMMY_PROPERTY_FILE)
39+
.build();
40+
41+
Boot_27_30_JmxEndpointExposureAction boot_27_30_jmxEndpointExposureAction = new Boot_27_30_JmxEndpointExposureAction();
42+
boot_27_30_jmxEndpointExposureAction.apply(projectContext);
43+
44+
List<SpringBootApplicationProperties> bootApplicationProperties = new SpringBootApplicationPropertiesResourceListFilter().apply(projectContext.getProjectResources());
45+
assertThat(bootApplicationProperties.size()).isEqualTo(1);
46+
assertThat(bootApplicationProperties.get(0).getProperty("management.endpoints.jmx.exposure.include").isPresent()).isTrue();
47+
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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.conditions;
17+
18+
import org.junit.jupiter.api.Test;
19+
import org.springframework.sbm.boot.properties.SpringApplicationPropertiesPathMatcher;
20+
import org.springframework.sbm.boot.properties.SpringBootApplicationPropertiesRegistrar;
21+
import org.springframework.sbm.engine.context.ProjectContext;
22+
import org.springframework.sbm.project.resource.TestProjectContext;
23+
24+
import java.nio.file.Path;
25+
26+
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
27+
28+
public class JmxEndpointExposureConditionTest {
29+
30+
private static final String APPLICATION_PROPERTIES_WITH_JMX_ENDPOINT_EXPOSED = "foo=bar\n" +
31+
"migrate=true\n" +
32+
"management.endpoints.jmx.exposure.include=*\n";
33+
34+
private static final String APPLICATION_PROPERTIES_WITHOUT_JMX_ENDPOINT_EXPOSED = "foo=bar\n" +
35+
"migrate=true\n";
36+
37+
@Test
38+
public void givenProjectWithJmxEndpointExposureCustomization_evaluateCondition_expectFalse() {
39+
ProjectContext projectContext = TestProjectContext.buildProjectContext()
40+
.addRegistrar(new SpringBootApplicationPropertiesRegistrar(new SpringApplicationPropertiesPathMatcher()))
41+
.addProjectResource(Path.of("src", "main", "resources", "application.properties"), APPLICATION_PROPERTIES_WITH_JMX_ENDPOINT_EXPOSED)
42+
.build();
43+
44+
JmxEndpointExposureCondition jmxEndpointExposureCondition = new JmxEndpointExposureCondition();
45+
assertThat(jmxEndpointExposureCondition.evaluate(projectContext)).isFalse();
46+
}
47+
48+
@Test
49+
public void givenProjectWithJmxEndpointExposureCustomization_evaluateCondition_expectTrue() {
50+
ProjectContext projectContext = TestProjectContext.buildProjectContext()
51+
.addRegistrar(new SpringBootApplicationPropertiesRegistrar(new SpringApplicationPropertiesPathMatcher()))
52+
.addProjectResource(Path.of("src", "main", "resources", "application.properties"), APPLICATION_PROPERTIES_WITHOUT_JMX_ENDPOINT_EXPOSED)
53+
.build();
54+
55+
JmxEndpointExposureCondition jmxEndpointExposureCondition = new JmxEndpointExposureCondition();
56+
assertThat(jmxEndpointExposureCondition.evaluate(projectContext)).isTrue();
57+
}
58+
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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.filter;
17+
18+
import org.junit.jupiter.api.Test;
19+
import org.springframework.sbm.boot.properties.SpringApplicationPropertiesPathMatcher;
20+
import org.springframework.sbm.boot.properties.SpringBootApplicationPropertiesRegistrar;
21+
import org.springframework.sbm.engine.context.ProjectContext;
22+
import org.springframework.sbm.project.resource.TestProjectContext;
23+
import org.springframework.sbm.properties.api.PropertiesSource;
24+
25+
import java.nio.file.Path;
26+
import java.util.List;
27+
28+
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
29+
30+
public class JmxEndpointExposureFinderTest {
31+
private static final String APPLICATION_PROPERTIES_WITH_JMX_ENDPOINT_EXPOSED = "foo=bar\n" +
32+
"migrate=true\n" +
33+
"management.endpoints.jmx.exposure.include=*\n";
34+
35+
private static final String MULTI_MODULE_POM_XML = "<project xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " +
36+
"xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\">\n" +
37+
" <modelVersion>4.0.0</modelVersion>\n" +
38+
"\n" +
39+
" <groupId>org.springframework.sbm</groupId>\n" +
40+
" <artifactId>spring-boot-migrator</artifactId>\n" +
41+
" <version>0.11.2-SNAPSHOT</version>\n" +
42+
" <packaging>pom</packaging>\n" +
43+
" <modules>\n" +
44+
" <module>module1</module>\n" +
45+
" <module>module2</module>\n" +
46+
" </modules>\n" +
47+
"</project>";
48+
49+
private static final String SUB_MODULE_POM_XML = "<project xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " +
50+
"xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\">\n" +
51+
" <parent>\n" +
52+
" <artifactId>spring-boot-migrator</artifactId>\n" +
53+
" <groupId>org.springframework.sbm</groupId>\n" +
54+
" <version>0.11.2-SNAPSHOT</version>\n" +
55+
" <relativePath>../../pom.xml</relativePath>\n" +
56+
" </parent>\n" +
57+
" <modelVersion>4.0.0</modelVersion>\n" +
58+
"\n" +
59+
" <artifactId>{{module}}</artifactId>\n" +
60+
"</project>";
61+
@Test
62+
public void givenProjectWithJmxEndpointExposureCustomization_findResources_returnResource(){
63+
ProjectContext projectContext = TestProjectContext.buildProjectContext()
64+
.addRegistrar(new SpringBootApplicationPropertiesRegistrar(new SpringApplicationPropertiesPathMatcher()))
65+
.addProjectResource(Path.of("src", "main", "resources", "application.properties"), APPLICATION_PROPERTIES_WITH_JMX_ENDPOINT_EXPOSED)
66+
.build();
67+
68+
JmxEndpointExposureFinder jmxEndpointExposureFinder = new JmxEndpointExposureFinder();
69+
List<? extends PropertiesSource> propertiesSources = jmxEndpointExposureFinder.apply(projectContext.getProjectResources());
70+
71+
assertThat(propertiesSources.size()).isEqualTo(1);
72+
assertThat(propertiesSources.get(0).getProperty("management.endpoints.jmx.exposure.include").isPresent()).isTrue();
73+
}
74+
75+
@Test
76+
public void givenMultiModuleProjectWithJmxEndpointExposureCustomization_findResources_returnResource(){
77+
ProjectContext projectContext = TestProjectContext.buildProjectContext()
78+
.withMavenRootBuildFileSource(MULTI_MODULE_POM_XML)
79+
.addProjectResource(Path.of("module1","pom.xml"),SUB_MODULE_POM_XML.replace("{{module}}", "module1"))
80+
.addProjectResource(Path.of("module2","pom.xml"),SUB_MODULE_POM_XML.replace("{{module}}", "module2"))
81+
.addRegistrar(new SpringBootApplicationPropertiesRegistrar(new SpringApplicationPropertiesPathMatcher()))
82+
.addProjectResource(Path.of("module1","src", "main", "resources", "application.properties"), APPLICATION_PROPERTIES_WITH_JMX_ENDPOINT_EXPOSED)
83+
.addProjectResource(Path.of("module2","src", "main", "resources", "application.properties"), APPLICATION_PROPERTIES_WITH_JMX_ENDPOINT_EXPOSED)
84+
.build();
85+
86+
JmxEndpointExposureFinder jmxEndpointExposureFinder = new JmxEndpointExposureFinder();
87+
List<? extends PropertiesSource> propertiesSources = jmxEndpointExposureFinder.apply(projectContext.getProjectResources());
88+
89+
assertThat(propertiesSources.size()).isEqualTo(2);
90+
assertThat(propertiesSources.get(0).getProperty("management.endpoints.jmx.exposure.include").isPresent()).isTrue();
91+
assertThat(propertiesSources.get(1).getProperty("management.endpoints.jmx.exposure.include").isPresent()).isTrue();
92+
}
93+
}

0 commit comments

Comments
 (0)