Skip to content

Commit 501060d

Browse files
committed
chore: implement ClasspathPropertiesVersionProvider for version retrieval and refactor ProvenanceManager to use it
1 parent db56685 commit 501060d

File tree

3 files changed

+79
-24
lines changed

3 files changed

+79
-24
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package edu.kit.datamanager.ro_crate.util;
2+
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
import java.net.URL;
6+
import java.util.Properties;
7+
8+
public class ClasspathPropertiesVersionProvider implements VersionProvider {
9+
private static final String PROPERTIES_FILE = "ro-crate-java.properties";
10+
private static final String VERSION_KEY = "version";
11+
12+
/**
13+
* Cached version to avoid repeated file/resource reads.
14+
*/
15+
private String cachedVersion = null;
16+
17+
/**
18+
* Constructs a ClasspathPropertiesVersionProvider that reads the version from a properties file in the classpath.
19+
*/
20+
public ClasspathPropertiesVersionProvider() {
21+
this.cachedVersion = getVersion();
22+
}
23+
24+
@Override
25+
public String getVersion() {
26+
if (cachedVersion != null) {
27+
return cachedVersion;
28+
}
29+
30+
URL resource = this.getClass().getResource("/version.properties");
31+
if (resource == null) {
32+
throw new IllegalStateException(
33+
"version.properties not found in classpath. This indicates a build configuration issue.");
34+
}
35+
36+
try (InputStream input = resource.openStream()) {
37+
Properties properties = new Properties();
38+
properties.load(input);
39+
String version = properties.getProperty("version");
40+
if (version == null || version.trim().isEmpty()) {
41+
throw new IllegalStateException("No version property found in version.properties");
42+
}
43+
return version.trim();
44+
} catch (IOException e) {
45+
throw new IllegalStateException("Failed to read version from properties file", e);
46+
}
47+
}
48+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package edu.kit.datamanager.ro_crate.util;
2+
3+
public interface VersionProvider {
4+
/**
5+
* Returns the version of the ro-crate-java library.
6+
*
7+
* @return The version string.
8+
*/
9+
String getVersion();
10+
}

src/main/java/edu/kit/datamanager/ro_crate/writer/ProvenanceManager.java

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22

33
import edu.kit.datamanager.ro_crate.Crate;
44
import edu.kit.datamanager.ro_crate.entities.contextual.ContextualEntity;
5+
import edu.kit.datamanager.ro_crate.util.ClasspathPropertiesVersionProvider;
6+
import edu.kit.datamanager.ro_crate.util.VersionProvider;
57

6-
import java.io.IOException;
7-
import java.io.InputStream;
8-
import java.net.URL;
98
import java.time.Instant;
10-
import java.util.Properties;
119
import java.util.UUID;
1210

1311
import static edu.kit.datamanager.ro_crate.entities.contextual.ContextualEntity.ContextualEntityBuilder;
@@ -19,6 +17,24 @@
1917
class ProvenanceManager {
2018
private static final String RO_CRATE_JAVA_ID = "#ro-crate-java";
2119

20+
protected VersionProvider versionProvider;
21+
22+
/**
23+
* Constructs a ProvenanceManager with the default ClasspathPropertiesVersionProvider.
24+
*/
25+
public ProvenanceManager() {
26+
this(new ClasspathPropertiesVersionProvider());
27+
}
28+
29+
/**
30+
* Constructs a ProvenanceManager with a specified VersionProvider.
31+
*
32+
* @param versionProvider The VersionProvider to use for retrieving the version of ro-crate-java.
33+
*/
34+
public ProvenanceManager(VersionProvider versionProvider) {
35+
this.versionProvider = versionProvider;
36+
}
37+
2238
void addProvenanceInformation(Crate crate) {
2339
// Determine if this is the first write
2440
boolean isFirstWrite = !crate.getJsonMetadata().contains(RO_CRATE_JAVA_ID) && !crate.isImported();
@@ -53,7 +69,7 @@ private ContextualEntity buildRoCrateJavaEntity(
5369
.filter(contextualEntity -> RO_CRATE_JAVA_ID.equals(contextualEntity.getId()))
5470
.findFirst()
5571
.orElseGet(() -> {
56-
String version = loadVersionFromProperties();
72+
String version = this.versionProvider.getVersion();
5773
return new ContextualEntityBuilder()
5874
.setId(RO_CRATE_JAVA_ID)
5975
.addType("SoftwareApplication")
@@ -71,23 +87,4 @@ private ContextualEntity buildRoCrateJavaEntity(
7187
self.addIdProperty("Action", newActionId);
7288
return self;
7389
}
74-
75-
private String loadVersionFromProperties() {
76-
URL resource = this.getClass().getResource("/version.properties");
77-
if (resource == null) {
78-
throw new IllegalStateException("version.properties not found in classpath. This indicates a build configuration issue.");
79-
}
80-
81-
try (InputStream input = resource.openStream()) {
82-
Properties properties = new Properties();
83-
properties.load(input);
84-
String version = properties.getProperty("version");
85-
if (version == null || version.trim().isEmpty()) {
86-
throw new IllegalStateException("No version property found in version.properties");
87-
}
88-
return version.trim();
89-
} catch (IOException e) {
90-
throw new IllegalStateException("Failed to read version from properties file", e);
91-
}
92-
}
9390
}

0 commit comments

Comments
 (0)