Skip to content

Commit d2173e4

Browse files
committed
Introduce conditional configuration file format support
1 parent 2b435a9 commit d2173e4

File tree

3 files changed

+111
-1
lines changed

3 files changed

+111
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2012 - present 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 io.spring.initializr.generator.condition;
18+
19+
import java.lang.annotation.Documented;
20+
import java.lang.annotation.ElementType;
21+
import java.lang.annotation.Retention;
22+
import java.lang.annotation.RetentionPolicy;
23+
import java.lang.annotation.Target;
24+
25+
import io.spring.initializr.generator.configuration.format.ConfigurationFileFormat;
26+
27+
import org.springframework.context.annotation.Conditional;
28+
29+
/**
30+
* Condition that matches when a generated project uses a particular
31+
* {@link ConfigurationFileFormat}.
32+
*
33+
* @author Sijun Yang
34+
*/
35+
@Retention(RetentionPolicy.RUNTIME)
36+
@Target({ ElementType.TYPE, ElementType.METHOD })
37+
@Documented
38+
@Conditional(OnConfigurationFileFormatCondition.class)
39+
public @interface ConditionalOnConfigurationFileFormat {
40+
41+
String value();
42+
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2012 - present 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 io.spring.initializr.generator.condition;
18+
19+
import io.spring.initializr.generator.configuration.format.ConfigurationFileFormat;
20+
import io.spring.initializr.generator.project.ProjectDescription;
21+
22+
import org.springframework.context.annotation.ConditionContext;
23+
import org.springframework.core.type.AnnotatedTypeMetadata;
24+
25+
/**
26+
* {@link ProjectGenerationCondition Condition} implementation for
27+
* {@link ConditionalOnConfigurationFileFormat}.
28+
*
29+
* @author Sijun Yang
30+
*/
31+
class OnConfigurationFileFormatCondition extends ProjectGenerationCondition {
32+
33+
@Override
34+
protected boolean matches(ProjectDescription description, ConditionContext context,
35+
AnnotatedTypeMetadata metadata) {
36+
if (description.getConfigurationFileFormat() == null) {
37+
return false;
38+
}
39+
String configurationFileFormatId = (String) metadata
40+
.getAllAnnotationAttributes(ConditionalOnConfigurationFileFormat.class.getName())
41+
.getFirst("value");
42+
ConfigurationFileFormat configurationFileFormat = ConfigurationFileFormat.forId(configurationFileFormatId);
43+
return description.getConfigurationFileFormat().id().equals(configurationFileFormat.id());
44+
}
45+
46+
}

initializr-generator/src/main/java/io/spring/initializr/generator/configuration/format/ConfigurationFileFormat.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,33 @@
1616

1717
package io.spring.initializr.generator.configuration.format;
1818

19+
import java.util.Objects;
20+
21+
import org.springframework.core.io.support.SpringFactoriesLoader;
22+
1923
public interface ConfigurationFileFormat {
2024

2125
/**
22-
* Return the id of the packaging.
26+
* Return the id of the configuration file format.
2327
* @return the id
2428
*/
2529
String id();
2630

31+
/**
32+
* Creates the configuration file format for the given id.
33+
* @param id the id
34+
* @return the configuration file format
35+
* @throws IllegalStateException if the configuration file format with the given id
36+
* can't be found
37+
*/
38+
static ConfigurationFileFormat forId(String id) {
39+
return SpringFactoriesLoader
40+
.loadFactories(ConfigurationFileFormatFactory.class, ConfigurationFileFormat.class.getClassLoader())
41+
.stream()
42+
.map((factory) -> factory.createConfigurationFileFormat(id))
43+
.filter(Objects::nonNull)
44+
.findFirst()
45+
.orElseThrow(() -> new IllegalStateException("Unrecognized configuration file format id '" + id + "'"));
46+
}
47+
2748
}

0 commit comments

Comments
 (0)