com.sap.cloud.yaas.servicesdk.apiconsole.utils.AppInfoReader Maven / Gradle / Ivy
/*
* © 2016 SAP SE or an SAP affiliate company.
* All rights reserved.
* Please see http://www.sap.com/corporate-en/legal/copyright/index.epx for additional trademark information and
* notices.
*/
package com.sap.cloud.yaas.servicesdk.apiconsole.utils;
import com.sap.cloud.yaas.servicesdk.apiconsole.model.AppInfo;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Locale;
import java.util.Properties;
import java.util.Set;
import java.util.jar.Manifest;
import javax.servlet.ServletContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* This class provide functionality to read application info from pom.properties file.
*/
public class AppInfoReader
{
/**
* Environment variable key identifying the deployed application name.
*/
private static final String APPLICATION_NAME = "APP_NAME";
/**
* Environment variable key identifying the deployed application version.
*/
private static final String APPLICATION_VERSION = "APP_VERSION";
/**
* Environment variable key identifying the deployed application build time.
*/
private static final String BUILD_TIME = "BUILD_TIME";
private static final Logger LOG = LoggerFactory.getLogger(AppInfoReader.class);
/**
* Line number for line from pom.properties containing built time.
*/
private static final int BUILD_TIME_LINE_NUMBER = 2;
/**
* Read and return information about application from pom.properties files.
*
* @param context - context.
* @return Information about application from pom.properties files
*/
public AppInfo getAppVersion(final ServletContext context)
{
if (context == null)
{
return null; // no context - probably test environment
}
AppInfo result = resolveInfoFromEnvVariables();
if (result == null)
{
result = resolveInfoFromManifest(context);
}
if (result == null)
{
result = resolveInfoFromPomProperties(context);
}
return result;
}
private AppInfo resolveInfoFromEnvVariables()
{
final String appName = getEnvVariable(APPLICATION_NAME);
final String appVersion = getEnvVariable(APPLICATION_VERSION);
final String buildTime = getEnvVariable(BUILD_TIME);
if (appName == null || appVersion == null)
{
return null;
}
final AppInfo result = new AppInfo();
result.setArtifactId(appName);
result.setVersion(appVersion);
result.setBuildTime(buildTime);
return result;
}
private AppInfo resolveInfoFromManifest(final ServletContext context)
{
try (final InputStream manifestStream = context.getResourceAsStream("/META-INF/MANIFEST.MF");)
{
if (manifestStream != null)
{
final AppInfo appVersion = new AppInfo();
final Manifest manifest = new Manifest(manifestStream);
appVersion.setArtifactId(manifest.getMainAttributes().getValue("Implementation-Title"));
appVersion.setGroupId(manifest.getMainAttributes().getValue("Implementation-Vendor-Id"));
appVersion.setVersion(manifest.getMainAttributes().getValue("Implementation-Version"));
appVersion.setBuildTime(manifest.getMainAttributes().getValue("Build-Time"));
return appVersion;
}
else
{
LOG.warn("Manifest file not found at /META-INF/MANIFEST.MF");
}
}
catch (final IOException e)
{
LOG.warn("Error reading manifest file", e);
}
return null;
}
private AppInfo resolveInfoFromPomProperties(final ServletContext context)
{
final String pomFilePath = findPomProperties(context, "/META-INF/maven");
if (pomFilePath == null)
{
return null;
}
final AppInfo appVersion = new AppInfo();
try (final InputStream is = context.getResourceAsStream(pomFilePath))
{
if (is == null)
{
throw new IOException("File not found.");
}
final Properties properties = new Properties();
properties.load(is);
appVersion.setArtifactId(properties.getProperty("artifactId"));
appVersion.setGroupId(properties.getProperty("groupId"));
appVersion.setVersion(properties.getProperty("version"));
}
catch (final IOException ex)
{
LOG.error("Error reading pom.properties file", ex);
return null;
}
try (BufferedReader in = new BufferedReader(new InputStreamReader(context.getResourceAsStream(pomFilePath),
StandardCharsets.UTF_8)))
{
String line;
int lineNumber = 0;
while ((line = in.readLine()) != null)
{
lineNumber = lineNumber + 1;
if (lineNumber == BUILD_TIME_LINE_NUMBER)
{
appVersion.setBuildTime(line.substring("#".length()));
break;
}
}
}
catch (final IOException ex)
{
LOG.error("Error reading pom.properties file", ex);
}
return appVersion;
}
private String findPomProperties(final ServletContext context, final String root)
{
if (root.toLowerCase(Locale.getDefault()).endsWith("pom.properties"))
{
return root;
}
final Set paths = context.getResourcePaths(root);
if (paths != null)
{
for (final String path : paths)
{
final String pomPath = findPomProperties(context, path);
if (pomPath != null)
{
return pomPath;
}
}
}
return null;
}
/**
* Returns environment variable with given name. If a system property with same name
* is set, it will return the system property.
*
* @param varName the name of system property or environment variable to retrieve
* @return the value of the system property or environment variable
*/
private String getEnvVariable(final String varName)
{
final String systemProperty = System.getProperty(varName);
if (systemProperty == null)
{
return System.getenv(varName);
}
else
{
return systemProperty;
}
}
}