use of com.teamscale.jacoco.agent.options.AgentOptionParseException in project teamscale-jacoco-agent by cqse.
the class AgentBase method premain.
/**
* Called by the actual premain method once the agent is isolated from the rest of the application.
*/
public static void premain(String options, Instrumentation instrumentation) throws Exception {
AgentOptions agentOptions;
DelayedLogger delayedLogger = new DelayedLogger();
try {
agentOptions = AgentOptionsParser.parse(options, delayedLogger);
} catch (AgentOptionParseException e) {
try (LoggingUtils.LoggingResources ignored = initializeFallbackLogging(options, delayedLogger)) {
delayedLogger.error("Failed to parse agent options: " + e.getMessage(), e);
System.err.println("Failed to parse agent options: " + e.getMessage());
// we perform actual logging output after writing to console to
// ensure the console is reached even in case of logging issues
// (see TS-23151). We use the Agent class here (same as below)
Logger logger = LoggingUtils.getLogger(Agent.class);
delayedLogger.logTo(logger);
throw e;
}
}
loggingResources = LoggingUtils.initializeLogging(agentOptions.getLoggingConfig());
Logger logger = LoggingUtils.getLogger(Agent.class);
delayedLogger.logTo(logger);
HttpUtils.setShouldValidateSsl(agentOptions.shouldValidateSsl());
logger.info("Starting JaCoCo's agent");
JacocoAgentBuilder agentBuilder = new JacocoAgentBuilder(agentOptions);
org.jacoco.agent.rt.internal_3570298.PreMain.premain(agentBuilder.createJacocoAgentOptions(), instrumentation);
AgentBase agent = agentBuilder.createAgent(instrumentation);
agent.registerShutdownHook();
}
use of com.teamscale.jacoco.agent.options.AgentOptionParseException in project teamscale-jacoco-agent by cqse.
the class TeamscaleConfig method getCommitFromManifest.
/**
* Reads `Branch` and `Timestamp` entries from the given jar/war file's manifest and builds a commit descriptor out
* of it.
*/
private CommitDescriptor getCommitFromManifest(File jarFile) throws AgentOptionParseException {
Manifest manifest = getManifestFromJarFile(jarFile);
String branch = manifest.getMainAttributes().getValue("Branch");
String timestamp = manifest.getMainAttributes().getValue("Timestamp");
if (StringUtils.isEmpty(branch)) {
throw new AgentOptionParseException("No entry 'Branch' in MANIFEST");
} else if (StringUtils.isEmpty(timestamp)) {
throw new AgentOptionParseException("No entry 'Timestamp' in MANIFEST");
}
logger.debug("Found commit " + branch + ":" + timestamp + " in file " + jarFile);
return new CommitDescriptor(branch, timestamp);
}
use of com.teamscale.jacoco.agent.options.AgentOptionParseException in project teamscale-jacoco-agent by cqse.
the class AgentBase method initializeFallbackLogging.
/**
* Initializes fallback logging in case of an error during the parsing of the options to {@link #premain(String,
* Instrumentation)} (see TS-23151). This tries to extract the logging configuration and use this and falls back to
* the default logger.
*/
private static LoggingResources initializeFallbackLogging(String premainOptions, DelayedLogger delayedLogger) {
for (String optionPart : premainOptions.split(",")) {
if (optionPart.startsWith(AgentOptionsParser.LOGGING_CONFIG_OPTION + "=")) {
return createFallbackLoggerFromConfig(optionPart.split("=", 2)[1], delayedLogger);
}
if (optionPart.startsWith(AgentOptionsParser.CONFIG_FILE_OPTION + "=")) {
String configFileValue = optionPart.split("=", 2)[1];
Optional<String> loggingConfigLine = Optional.empty();
try {
File configFile = new FilePatternResolver(delayedLogger).parsePath(AgentOptionsParser.CONFIG_FILE_OPTION, configFileValue).toFile();
loggingConfigLine = FileSystemUtils.readLinesUTF8(configFile).stream().filter(line -> line.startsWith(AgentOptionsParser.LOGGING_CONFIG_OPTION + "=")).findFirst();
} catch (IOException | AgentOptionParseException e) {
delayedLogger.error("Failed to load configuration from " + configFileValue + ": " + e.getMessage(), e);
}
if (loggingConfigLine.isPresent()) {
return createFallbackLoggerFromConfig(loggingConfigLine.get().split("=", 2)[1], delayedLogger);
}
}
}
return LoggingUtils.initializeDefaultLogging();
}
use of com.teamscale.jacoco.agent.options.AgentOptionParseException in project teamscale-jacoco-agent by cqse.
the class TeamscaleConfig method getRevisionFromManifest.
/**
* Reads `Git_Commit` entry from the given jar/war file's manifest and sets it as revision.
*/
private String getRevisionFromManifest(File jarFile) throws AgentOptionParseException {
Manifest manifest = getManifestFromJarFile(jarFile);
String revision = manifest.getMainAttributes().getValue("Revision");
if (StringUtils.isEmpty(revision)) {
// currently needed option for a customer
if (manifest.getAttributes("Git") != null) {
revision = manifest.getAttributes("Git").getValue("Git_Commit");
}
if (StringUtils.isEmpty(revision)) {
throw new AgentOptionParseException("No entry 'Revision' in MANIFEST");
}
}
logger.debug("Found revision " + revision + " in file " + jarFile);
return revision;
}
Aggregations