diff options
author | Marc R. Hoffmann <hoffmann@mountainminds.com> | 2012-12-28 13:54:23 +0100 |
---|---|---|
committer | Marc R. Hoffmann <hoffmann@mountainminds.com> | 2012-12-28 13:54:23 +0100 |
commit | 47990a4a02bb738eedd7dce6a2f2dd10d5f28cc4 (patch) | |
tree | 6413669212ce5c34c5c8068038e50b1481df6743 /org.jacoco.agent.rt | |
parent | 9c1b63dafd9be7316d3661aaa18aa3815160d19c (diff) | |
download | jacoco-47990a4a02bb738eedd7dce6a2f2dd10d5f28cc4.tar.gz |
Additional way to provide configuration options via classpath resource.
Diffstat (limited to 'org.jacoco.agent.rt')
-rw-r--r-- | org.jacoco.agent.rt/src/org/jacoco/agent/rt/ConfigLoader.java | 57 | ||||
-rw-r--r-- | org.jacoco.agent.rt/src/org/jacoco/agent/rt/RT.java | 8 |
2 files changed, 63 insertions, 2 deletions
diff --git a/org.jacoco.agent.rt/src/org/jacoco/agent/rt/ConfigLoader.java b/org.jacoco.agent.rt/src/org/jacoco/agent/rt/ConfigLoader.java new file mode 100644 index 00000000..977aac2f --- /dev/null +++ b/org.jacoco.agent.rt/src/org/jacoco/agent/rt/ConfigLoader.java @@ -0,0 +1,57 @@ +/******************************************************************************* + * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Marc R. Hoffmann - initial API and implementation + * + *******************************************************************************/ +package org.jacoco.agent.rt; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Map; +import java.util.Properties; + +/** + * Internal utility to load runtime configuration from a classpath resource and + * from system properties. System property keys are prefixed with + * <code>jacoco.</code>. If the same property is defined twice the system + * property takes precedence. + */ +final class ConfigLoader { + + private static final String SYS_PREFIX = "jacoco-agent."; + + static Properties load(final String resource, final Properties system) { + final Properties result = new Properties(); + + // 1. Try to load resource + final InputStream file = RT.class.getResourceAsStream(resource); + if (file != null) { + try { + result.load(file); + } catch (final IOException e) { + throw new RuntimeException(e); + } + } + + // 2. Override with system properties + for (final Map.Entry<Object, Object> entry : system.entrySet()) { + final String keystr = entry.getKey().toString(); + if (keystr.startsWith(SYS_PREFIX)) { + result.put(keystr.substring(SYS_PREFIX.length()), + entry.getValue()); + } + } + + return result; + } + + private ConfigLoader() { + } + +} diff --git a/org.jacoco.agent.rt/src/org/jacoco/agent/rt/RT.java b/org.jacoco.agent.rt/src/org/jacoco/agent/rt/RT.java index c760b0e2..53bca947 100644 --- a/org.jacoco.agent.rt/src/org/jacoco/agent/rt/RT.java +++ b/org.jacoco.agent.rt/src/org/jacoco/agent/rt/RT.java @@ -11,6 +11,8 @@ *******************************************************************************/ package org.jacoco.agent.rt; +import java.util.Properties; + import org.jacoco.core.runtime.AgentOptions; import org.jacoco.core.runtime.RuntimeData; @@ -21,10 +23,12 @@ import org.jacoco.core.runtime.RuntimeData; public class RT { private static final RuntimeData data; + private static final String CONFIG_RESOURCE = "/jacoco-agent.properties"; static { - final AgentOptions options = new AgentOptions(System.getProperties()); - data = Agent.getInstance(options).getData(); + final Properties config = ConfigLoader.load( + CONFIG_RESOURCE, System.getProperties()); + data = Agent.getInstance(new AgentOptions(config)).getData(); } /** |