|
| 1 | +package dev.dsf.bpe.v2; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.InputStream; |
| 5 | +import java.io.InputStreamReader; |
| 6 | +import java.io.Reader; |
| 7 | +import java.nio.charset.StandardCharsets; |
| 8 | +import java.time.LocalDate; |
| 9 | +import java.time.ZonedDateTime; |
| 10 | +import java.time.format.DateTimeParseException; |
| 11 | +import java.util.Properties; |
| 12 | + |
| 13 | +import org.slf4j.Logger; |
| 14 | +import org.slf4j.LoggerFactory; |
| 15 | + |
| 16 | +/** |
| 17 | + * Implements {@link #getName()}, {@link #getVersion()} and {@link #getReleaseDate()} based on properties defined in a |
| 18 | + * {@value #PROPERTIES_FILE} file. The UTF-8 encoded file needs to contain property entries for {@value #NAME_PROPERTY}, |
| 19 | + * {@value #VERSION_PROPERTY} (suffixes like <code>-SNAPSHOT</code> will be removed from the value, regex: |
| 20 | + * <code>-.*$</code>) and {@value #RELEASE_DATE_PROPERTY} (with the value formated as a ISO-8601 timestamp, see |
| 21 | + * {@link ZonedDateTime#parse(CharSequence)}). |
| 22 | + * <p> |
| 23 | + * Using maven the file should be located at <code>src/main/resources/plugin.properties</code> with the following |
| 24 | + * content: |
| 25 | + * {@snippet id = "plugin.properties" : |
| 26 | + * release-date=${project.build.outputTimestamp} |
| 27 | + * version=${project.version} |
| 28 | + * name=${project.artifactId} |
| 29 | + * } |
| 30 | + * <p> |
| 31 | + * The maven pom.xml file needs to define the <code>project.build.outputTimestamp</code> property (also needed for |
| 32 | + * reproducible builds) and enable resource filtering for the <code>plugin.properties</code> file: |
| 33 | + * {@snippet id = "pom.xml" : |
| 34 | + * <project> |
| 35 | + * <properties> |
| 36 | + * <project.build.outputTimestamp>2025-07-22T16:45:00Z</project.build.outputTimestamp> |
| 37 | + * <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> |
| 38 | + * </properties> |
| 39 | + * <build> |
| 40 | + * <resources> |
| 41 | + * <resource> |
| 42 | + * <directory>src/main/resources</directory> |
| 43 | + * <filtering>false</filtering> |
| 44 | + * <excludes> |
| 45 | + * <exclude>plugin.properties</exclude> |
| 46 | + * </excludes> |
| 47 | + * </resource> |
| 48 | + * <resource> |
| 49 | + * <directory>src/main/resources</directory> |
| 50 | + * <filtering>true</filtering> |
| 51 | + * <includes> |
| 52 | + * <include>plugin.properties</include> |
| 53 | + * </includes> |
| 54 | + * </resource> |
| 55 | + * </resources> |
| 56 | + * </build> |
| 57 | + * </project> |
| 58 | + * } |
| 59 | + */ |
| 60 | +public abstract class AbstractProcessPluginDefinition implements ProcessPluginDefinition |
| 61 | +{ |
| 62 | + private static final Logger logger = LoggerFactory.getLogger(AbstractProcessPluginDefinition.class); |
| 63 | + |
| 64 | + private static final String PROPERTIES_FILE = "plugin.properties"; |
| 65 | + |
| 66 | + private static final String NAME_PROPERTY = "name"; |
| 67 | + private static final String VERSION_PROPERTY = "version"; |
| 68 | + private static final String RELEASE_DATE_PROPERTY = "release-date"; |
| 69 | + |
| 70 | + private final String name; |
| 71 | + private final String version; |
| 72 | + private final LocalDate releaseDate; |
| 73 | + |
| 74 | + public AbstractProcessPluginDefinition() |
| 75 | + { |
| 76 | + InputStream in = AbstractProcessPluginDefinition.class.getClassLoader().getResourceAsStream(PROPERTIES_FILE); |
| 77 | + if (in == null) |
| 78 | + { |
| 79 | + logger.warn("{} file not found in root folder", PROPERTIES_FILE); |
| 80 | + throw new RuntimeException(PROPERTIES_FILE + " file not found"); |
| 81 | + } |
| 82 | + |
| 83 | + try (in; Reader reader = new InputStreamReader(in, StandardCharsets.UTF_8)) |
| 84 | + { |
| 85 | + Properties properties = new Properties(); |
| 86 | + properties.load(reader); |
| 87 | + |
| 88 | + name = getPropertyAndCheckNotNullNotEmpty(properties, NAME_PROPERTY); |
| 89 | + version = getPropertyAndCheckNotNullNotEmpty(properties, VERSION_PROPERTY).replaceFirst("-.*$", ""); |
| 90 | + |
| 91 | + try |
| 92 | + { |
| 93 | + releaseDate = ZonedDateTime.parse(getPropertyAndCheckNotNullNotEmpty(properties, RELEASE_DATE_PROPERTY)) |
| 94 | + .toLocalDate(); |
| 95 | + } |
| 96 | + catch (DateTimeParseException e) |
| 97 | + { |
| 98 | + logger.warn("Property {} defined in {} file not parsable as ISO-8601 timestamp: {} - {}", |
| 99 | + RELEASE_DATE_PROPERTY, PROPERTIES_FILE, e.getClass().getName(), e.getMessage()); |
| 100 | + throw e; |
| 101 | + } |
| 102 | + } |
| 103 | + catch (IOException e) |
| 104 | + { |
| 105 | + logger.warn("Unable to read {} file: {} - {}", PROPERTIES_FILE, e.getClass().getName(), e.getMessage()); |
| 106 | + throw new RuntimeException(e); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + private String getPropertyAndCheckNotNullNotEmpty(Properties properties, String key) |
| 111 | + { |
| 112 | + String value = properties.getProperty(key); |
| 113 | + |
| 114 | + if (value == null) |
| 115 | + { |
| 116 | + logger.warn("Property {} not defined in {} file", key, PROPERTIES_FILE); |
| 117 | + throw new RuntimeException("Property " + key + " not defined"); |
| 118 | + } |
| 119 | + else if (value.isBlank()) |
| 120 | + { |
| 121 | + logger.warn("Property {} defined in {} file is blank", key, PROPERTIES_FILE); |
| 122 | + throw new RuntimeException("Property " + key + " is blank"); |
| 123 | + } |
| 124 | + |
| 125 | + return value; |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public String getName() |
| 130 | + { |
| 131 | + return name; |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + public String getVersion() |
| 136 | + { |
| 137 | + return version; |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + public LocalDate getReleaseDate() |
| 142 | + { |
| 143 | + return releaseDate; |
| 144 | + } |
| 145 | +} |
0 commit comments