Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get specific component version at runtime #203

Merged
merged 10 commits into from
Feb 20, 2024
31 changes: 18 additions & 13 deletions qanary_component-template/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -106,20 +106,25 @@
<artifactId>log4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- https://mvnrepository.com/artifact/org.reflections/reflections -->
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.10.2</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.apache.jena/jena-arq -->
<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>jena-arq</artifactId>
<version>4.9.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.jena/jena-core -->
<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>jena-core</artifactId>
<version>4.9.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.jena/jena-arq -->
<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>jena-arq</artifactId>
<version>4.9.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.jena/jena-core -->
<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>jena-core</artifactId>
<version>4.9.0</version>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.reflections.Reflections;
import org.reflections.scanners.Scanners;
import org.reflections.util.ConfigurationBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -121,8 +125,8 @@ public String showDescriptionOnGetRequestOnRoot(HttpServletResponse response, Mo
String sessionDataValue = env.getProperty(name, "This text is shown as the property " + name + " is not defined (e.g., in application.properties).");
session.setAttribute(sessionDataName, sessionDataValue);
logger.info("session | {} -> {}={}", name, sessionDataName, session.getAttribute(sessionDataName));
}
}

Map<String,String> envImportantPropertyNameValue = new HashMap<>();
envImportantPropertyNameValue.put("component_description_url", QanaryConfiguration.description);
envImportantPropertyNameValue.put("component_description_file", QanaryConfiguration.description_file);
Expand All @@ -135,13 +139,48 @@ public String showDescriptionOnGetRequestOnRoot(HttpServletResponse response, Mo
envImportantPropertyNameValue.put("SpecificationVendor", getClass().getPackage().getSpecificationVendor());
envImportantPropertyNameValue.put("SpecificationVersion", getClass().getPackage().getSpecificationVersion());

try {
Class<? extends QanaryComponent> extendingComponent = getExtendingComponent();
envImportantPropertyNameValue.put("componentImplementationVersion", extendingComponent.getPackage().getImplementationVersion());
envImportantPropertyNameValue.put("componentImplementationTitle", extendingComponent.getPackage().getImplementationTitle());
envImportantPropertyNameValue.put("componentImplementationVendor", extendingComponent.getPackage().getSpecificationVendor());

} catch (Exception e) {
logger.warn("No class implementing QanaryComponent could be found during runtime!");
logger.warn(e.getMessage());
}

for (Map.Entry<String, String> entry : envImportantPropertyNameValue.entrySet()) {
String key = entry.getKey();
String val = entry.getValue();
String key = entry.getKey();
String val = entry.getValue();
session.setAttribute(key, val);
logger.info("session | {}={}", key, session.getAttribute(key));
}
}

return QanaryConfiguration.description_file;
}

// TODO: add documentation
heinpa marked this conversation as resolved.
Show resolved Hide resolved
private Class<? extends QanaryComponent> getExtendingComponent() throws Exception {
Reflections reflections = new Reflections( //
new ConfigurationBuilder() //
.addScanners(Scanners.SubTypes.filterResultsBy(s->true)) //
.forPackages("eu.wdaqua.qanary.component"));
// TODO: caution: what about custom components outside of this classpath?
Set<Class<? extends QanaryComponent>> classes = reflections.getSubTypesOf(QanaryComponent.class);
// exactly one class is expected
if (classes.size() == 1) {
logger.debug("Found class: {}", classes.iterator().next().getName());
logger.debug("version: {}", classes.iterator().next().getPackage().getImplementationVersion());
return classes.iterator().next();
} else if (classes.size() == 0) {
logger.warn("no extending component (classes.size() == 0)");
//throw new NoExtendingComponentClassException();
throw new Exception("no extending component");
} else {
logger.warn("ambiguous extending component");
//throw new AmbiguousExtendingComponentClassException();
throw new Exception("ambiguous extending component");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
</style>
</head>
<body>
<h1>Qanary component: <span th:text=${session.spring_application_name} th:unless="${session == null}" style="font-style: italic"></span><span th:unless="${session == null}">, v<span th:text=${session.ImplementationVersion} th:unless="${session.ImplementationVersion == null}" ></span></span></h1>
<h1>Qanary component: <span th:text=${session.spring_application_name} th:unless="${session == null}" style="font-style: italic"></span><span th:unless="${session == null}">, v<span th:text=${session.componentImplementationVersion} th:unless="${session.componentImplementationVersion == null}" ></span></span></h1>
<h2>(<span th:unless="${session == null}">based on <span th:text=${session.ImplementationTitle}></span> v<span th:text=${session.ImplementationVersion} th:unless="${session.ImplementationVersion == null}" ></span></span>)</h2>
<p th:text=${session.spring_application_description} th:unless="${session == null}"/>
<p>Given the current configuration, this component should connect to a Qanary system at <a th:text="${session.spring_boot_admin_url}" th:href="${session.spring_boot_admin_url}"></a>.</p>
<p>API docs (OpenAPI/Swagger) are available at <a th:text="${session.springdoc_swagger_ui_path}" th:href="${session.springdoc_swagger_ui_path}" th:unless="${session == null}"></a>.</p>
Expand Down