Skip to content

Commit

Permalink
Merge pull request #24 from Coreoz/fix/ApplicationInfoProvider
Browse files Browse the repository at this point in the history
Fix ApplicationInfoProvider
  • Loading branch information
amanteaux authored Jul 27, 2023
2 parents 01487f4 + bdc604e commit 60d161c
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 7 deletions.
8 changes: 8 additions & 0 deletions plume-framework-dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
<commons-validator.version>1.7</commons-validator.version>
<javax-annotation.version>1.3.2</javax-annotation.version>
<dropwizard-metrics.version>4.1.2</dropwizard-metrics.version>
<classgraph.version>4.8.161</classgraph.version>
<maven-model.version>3.3.9</maven-model.version>

<junit.version>4.13.2</junit.version>
Expand Down Expand Up @@ -316,6 +317,13 @@
<version>${maven-model.version}</version>
</dependency>

<!-- Resource reader -->
<dependency>
<groupId>io.github.classgraph</groupId>
<artifactId>classgraph</artifactId>
<version>${classgraph.version}</version>
</dependency>

<!-- Tests -->
<dependency>
<groupId>junit</groupId>
Expand Down
6 changes: 6 additions & 0 deletions plume-web-jersey-monitoring/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@
<artifactId>maven-model</artifactId>
</dependency>

<!-- Resource reader -->
<dependency>
<groupId>io.github.classgraph</groupId>
<artifactId>classgraph</artifactId>
</dependency>

<!-- dependency injection -->
<dependency>
<groupId>com.google.inject</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,92 @@

import com.coreoz.plume.jersey.monitoring.configuration.JerseyMonitoringConfigurationService;
import com.coreoz.plume.jersey.monitoring.utils.info.beans.ApplicationInfo;
import io.github.classgraph.ClassGraph;
import io.github.classgraph.ResourceList;
import io.github.classgraph.ScanResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;

import javax.inject.Inject;
import javax.inject.Provider;
import javax.inject.Singleton;
import javax.validation.constraints.Negative;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map;

@Slf4j
@Singleton
public class ApplicationInfoProvider implements Provider<ApplicationInfo> {
private static final String POM_FILE_NAME = "pom.xml";
private final JerseyMonitoringConfigurationService configurationService;
private final ApplicationInfo applicationInfo;

@Inject
private ApplicationInfoProvider(JerseyMonitoringConfigurationService configurationService) throws IOException, XmlPullParserException {
MavenXpp3Reader reader = new MavenXpp3Reader();
Model model = reader.read(new FileReader("pom.xml"));
private ApplicationInfoProvider(JerseyMonitoringConfigurationService configurationService) {
this.configurationService = configurationService;
this.applicationInfo = this.fetchApplicationInfo();
}

@Override
public ApplicationInfo get() {
return this.applicationInfo;
}

/* PRIVATE */
private ApplicationInfo fetchApplicationInfo() {
Model model = this.readPom();

if (model == null) {
model = this.readMetaInfPom();
}

if (model == null) {
log.warn("Failed to read pom.xml file on root project and in META-INF folder");
model = new Model();
}

Map<String, Object> additionalInformation = configurationService.getCustomInfo();
this.applicationInfo = new ApplicationInfo(
return new ApplicationInfo(
model.getName(),
model.getDescription(),
model.getVersion(),
additionalInformation.isEmpty() ? null : additionalInformation
);
}

@Override
public ApplicationInfo get() {
return this.applicationInfo;
private Model readPom() {
MavenXpp3Reader reader = new MavenXpp3Reader();
if ((new File(POM_FILE_NAME)).exists()) {
try {
return reader.read(new FileReader(POM_FILE_NAME));
} catch (Exception e) {
log.error("Failed to read {}", POM_FILE_NAME, e);
return null;
}
}
return null;
}

private Model readMetaInfPom() {
MavenXpp3Reader reader = new MavenXpp3Reader();

try (ScanResult scanResult = new ClassGraph().acceptPaths("META-INF/maven").scan()) {
ResourceList resourceList = scanResult.getResourcesWithLeafName(POM_FILE_NAME);

if (resourceList == null || resourceList.isEmpty()) {
throw new RuntimeException("Could not find " + POM_FILE_NAME);
}

return reader.read(new InputStreamReader(resourceList.get(0).open()));
} catch (Exception e) {
log.error("Failed to read {} from META-INF folder", POM_FILE_NAME, e);
return null;
}
}
}

Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package com.coreoz.plume.jersey.monitoring.utils.info.beans;

import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import lombok.Value;

import java.util.Map;

@Value
@AllArgsConstructor
@NoArgsConstructor(force = true)
public class ApplicationInfo {
String name;
String description;
Expand Down

0 comments on commit 60d161c

Please sign in to comment.