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
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
</manifest>
</archive>
</configuration>
</plugin>
<!-- https://github.com/spotify/dockerfile-maven -->
<!-- build Docker image: mvn dockerfile:build -->
<plugin>
Expand Down Expand Up @@ -98,6 +110,18 @@
</buildArgs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
46 changes: 41 additions & 5 deletions qanary_component-parent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,45 @@
</parent>
<properties>
<java.version>17</java.version>
<qanary.version>[3.9.0,4.0.0)</qanary.version>
<spring-boot-admin.version>2.7.18</spring-boot-admin.version>
<spring-boot-admin.version>2.7.11</spring-boot-admin.version>
<qanary.component.version>[3.9.2,4.0.0)</qanary.component.version>
<qanary.commons.version>[3.11.0,4.0.0)</qanary.commons.version>
<junit-jupiter.version>5.8.2</junit-jupiter.version>
<springdoc.version>1.7.0</springdoc.version>
<json-path.version>2.9.0</json-path.version>
</properties>

<!-- common dependencies that are implemented by all components -->

<dependencies>
<dependency>
<groupId>eu.wdaqua.qanary</groupId>
<artifactId>qa.component</artifactId>
<version>${qanary.version}</version>
<version>${qanary.component.version}</version>
<exclusions>
<exclusion>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>eu.wdaqua.qanary</groupId>
<artifactId>qa.commons</artifactId>
<version>${qanary.commons.version}</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
<version>2.20.0</version>
<exclusions>
<exclusion>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
Expand Down Expand Up @@ -80,7 +107,6 @@
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>${json-path.version}</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.jayway.jsonpath/json-path-assert -->
<dependency>
Expand Down Expand Up @@ -113,14 +139,24 @@
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.7.0</version>
<version>${springdoc.version}</version>
<exclusions>
<exclusion>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-annotations</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-webmvc-core</artifactId>
<version>${springdoc.version}</version>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-data-rest</artifactId>
<version>${springdoc.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.swagger.core.v3/swagger-core-jakarta -->
<dependency>
<groupId>io.swagger.core.v3</groupId>
Expand Down
123 changes: 64 additions & 59 deletions qanary_component-template/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
<modelVersion>4.0.0</modelVersion>
<groupId>eu.wdaqua.qanary</groupId>
<artifactId>qa.component</artifactId>
<version>3.9.2</version>
<version>3.9.3</version>

<parent>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.15</version>
Expand Down Expand Up @@ -97,29 +97,34 @@
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
<version>[2.19.0,3.)</version>
<exclusions>
<exclusion>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</exclusion>
</exclusions>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
<version>[2.19.0,3.)</version>
<exclusions>
<exclusion>
<groupId>org.apache.logging.log4j</groupId>
<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.10.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.jena/jena-core -->
<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>jena-core</artifactId>
<version>4.10.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.apache.jena/jena-arq -->
<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>jena-arq</artifactId>
<version>4.10.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.jena/jena-core -->
<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>jena-core</artifactId>
<version>4.10.0</version>
</dependency>
</dependencies>
<build>
<plugins>
Expand All @@ -129,42 +134,42 @@
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<executions>
<execution>
<id>enforce-banned-dependencies</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<bannedDependencies>
<excludes>
<exclude>org.apache.logging.log4j:log4j-to-slf4j</exclude>
</excludes>
<includes>
<include>org.apache.logging.log4j:log4j-to-slf4j:2.15.0</include>
</includes>
</bannedDependencies>
</rules>
<fail>true</fail>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
</manifest>
</archive>
</configuration>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<executions>
<execution>
<id>enforce-banned-dependencies</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<bannedDependencies>
<excludes>
<exclude>org.apache.logging.log4j:log4j-to-slf4j</exclude>
</excludes>
<includes>
<include>org.apache.logging.log4j:log4j-to-slf4j:2.15.0</include>
</includes>
</bannedDependencies>
</rules>
<fail>true</fail>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
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 All @@ -28,6 +32,8 @@
import eu.wdaqua.qanary.commons.QanaryMessage;
import eu.wdaqua.qanary.commons.QanaryUtils;
import eu.wdaqua.qanary.commons.config.QanaryConfiguration;
import eu.wdaqua.qanary.component.exceptions.AmbiguousExtendingComponentClass;
import eu.wdaqua.qanary.component.exceptions.NoExtendingComponentClass;
import io.swagger.v3.oas.annotations.Operation;


Expand Down Expand Up @@ -129,8 +135,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 @@ -143,13 +149,51 @@ public String showDescriptionOnGetRequestOnRoot(HttpServletResponse response, Mo
envImportantPropertyNameValue.put("SpecificationVendor", getClass().getPackage().getSpecificationVendor());
envImportantPropertyNameValue.put("SpecificationVersion", getClass().getPackage().getSpecificationVersion());

try {
//find the specific implemented Qanary component
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;
}

/**
* get the specific implemented component that extends eu.wdaqua.qanary.component
*
* @return
* @throws Exception
*/
private Class<? extends QanaryComponent> getExtendingComponent() throws Exception {
Reflections reflections = new Reflections(
new ConfigurationBuilder()
.addScanners(Scanners.SubTypes.filterResultsBy(s->true))
.forPackages("eu.wdaqua.qanary.component"));
// use reflections to find subtypes of eu.wdaqua.qanary.component
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 the first (and only) one
return classes.iterator().next();
} else if (classes.size() == 0) {
throw new NoExtendingComponentClass(QanaryComponent.class);
} else {
throw new AmbiguousExtendingComponentClass(QanaryComponent.class);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package eu.wdaqua.qanary.component.exceptions;

public class AmbiguousExtendingComponentClass extends Exception {

public AmbiguousExtendingComponentClass(Class<?> extendedClass) {
super("" //
+ "Found multiple classes extending " //
+ extendedClass.getName() //
+ " in the current classpath");
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package eu.wdaqua.qanary.component.exceptions;

public class NoExtendingComponentClass extends Exception {

public NoExtendingComponentClass(Class<?> extendedClass) {
super("" //
+ "Could not find any class extending " //
+ extendedClass.getName() //
+ " in the current classpath");
}
}
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