Skip to content

Commit e17a2f9

Browse files
authored
Merge pull request #477 from rnnr/config_dir_system_property
adding config dir system property
2 parents 2fc357e + 554ae2c commit e17a2f9

File tree

3 files changed

+49
-12
lines changed

3 files changed

+49
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
- Introducing FixedRate Sampling v2 Using Telemetry Processors
1818
- Fixed issue #436 (TraceTelemetry with Severity is not shown in UI). This fixes a regression issue with `TelemetryClient.trackTrace` and `TelemetryClient.trackException`.
1919
- Compilation now targets Java 1.7. Java 1.6 is no longer supported.
20+
- Adding system property `applicationinsights.configurationDirectory` to allow to explicitly set directory containing the config file.
2021

2122
## Version 1.0.10
2223
- `track()` method of 'com.microsoft.applicationinsights.TelemetryClient' is now modified. No longer performing pre-sanitization

core/src/main/java/com/microsoft/applicationinsights/internal/config/ConfigurationFileLocator.java

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@
4141
* Created by gupele on 5/25/2015.
4242
*/
4343
public final class ConfigurationFileLocator {
44+
45+
/** name of property containing path to directory with configuration file */
46+
public static final String CONFIG_DIR_PROPERTY = "applicationinsights.configurationDirectory";
47+
4448
private final String configurationFileName;
4549

4650
public ConfigurationFileLocator(String configurationFileName) {
@@ -49,22 +53,34 @@ public ConfigurationFileLocator(String configurationFileName) {
4953
}
5054

5155
public InputStream getConfigurationFile() {
52-
InputStream inputStream = ConfigurationFileLocator.class.getClassLoader().getResourceAsStream(configurationFileName);
53-
if (inputStream != null) {
54-
InternalLogger.INSTANCE.logAlways(InternalLogger.LoggingLevel.INFO, "Configuration file has been successfully found as resource");
55-
return inputStream;
56-
}
5756

58-
// Trying to load configuration as a resource.
59-
String configurationFile = getConfigurationFromCurrentClassLoader();
57+
String configurationFile;
58+
InputStream inputStream;
6059

61-
// If not found as a resource, trying to load from the executing jar directory
62-
if (configurationFile == null) {
63-
configurationFile = getConfigurationFromLibraryLocation();
60+
// first try to get from dir defined explicitly in system property insights.configurationFile
61+
String configDirFromProperty = System.getProperty(CONFIG_DIR_PROPERTY);
62+
if (configDirFromProperty != null) {
63+
configurationFile = getConfigurationAbsolutePath(configDirFromProperty);
64+
} else {
6465

65-
// If still not found try to get it from the class path
66+
inputStream = ConfigurationFileLocator.class.getClassLoader().getResourceAsStream(configurationFileName);
67+
if (inputStream != null) {
68+
InternalLogger.INSTANCE.logAlways(InternalLogger.LoggingLevel.INFO,
69+
"Configuration file has been successfully found as resource");
70+
return inputStream;
71+
}
72+
73+
// Trying to load configuration as a resource.
74+
configurationFile = getConfigurationFromCurrentClassLoader();
75+
76+
// If not found as a resource, trying to load from the executing jar directory
6677
if (configurationFile == null) {
67-
configurationFile = getConfFromClassPath();
78+
configurationFile = getConfigurationFromLibraryLocation();
79+
80+
// If still not found try to get it from the class path
81+
if (configurationFile == null) {
82+
configurationFile = getConfFromClassPath();
83+
}
6884
}
6985
}
7086

core/src/test/java/com/microsoft/applicationinsights/internal/config/ConfigurationFileLocatorTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,31 @@ public void testNoConfigurationFound() throws Exception {
5555
assertNull(resourceFile);
5656
}
5757

58+
@Test
59+
public void testGetConfigurationFileWhereDirIsFromProperty() throws Exception {
60+
System.setProperty(ConfigurationFileLocator.CONFIG_DIR_PROPERTY, "src/test/resources");
61+
62+
InputStream resourceFile = new ConfigurationFileLocator(EXISTING_CONF_TEST_FILE).getConfigurationFile();
63+
verifyFile(resourceFile);
64+
}
65+
66+
@Test
67+
public void testNoConfigurationFoundWhereDirPropertySetWrong() throws Exception {
68+
System.setProperty(ConfigurationFileLocator.CONFIG_DIR_PROPERTY, "this-directory-does-not-exist");
69+
70+
InputStream resourceFile = new ConfigurationFileLocator(EXISTING_CONF_TEST_FILE).getConfigurationFile();
71+
System.clearProperty(ConfigurationFileLocator.CONFIG_DIR_PROPERTY);
72+
73+
assertNull(resourceFile);
74+
}
75+
5876
@Test
5977
public void testGetConfigurationFileWhereFileIsResource() throws Exception {
6078
String configurationFileName = putConfigurationFileAsResourceInCurrentClassLoaderOnly();
6179

6280
InputStream resourceFile = new ConfigurationFileLocator(configurationFileName).getConfigurationFile();
81+
System.clearProperty(ConfigurationFileLocator.CONFIG_DIR_PROPERTY);
82+
6383
verifyFile(resourceFile);
6484
}
6585

0 commit comments

Comments
 (0)