diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/ConfigService.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/ConfigService.java index 2886823b..97127f43 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/ConfigService.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/ConfigService.java @@ -101,6 +101,19 @@ public static ConfigFile getConfigFile(String namespace, ConfigFileFormat config return s_instance.getManager().getConfigFile(namespace, configFileFormat); } + /** + * Get the config file instance for the appId and namespace. + * + * @param appId the appId of the config + * @param namespace the namespace of the config without file extension, e.g. "application" + * @param configFileFormat the config file format + * @return config file instance + */ + public static ConfigFile getConfigFile(String appId, String namespace, + ConfigFileFormat configFileFormat) { + return s_instance.getManager().getConfigFile(appId, namespace, configFileFormat); + } + public static ConfigMonitor getConfigMonitor(){ return s_instance.getMonitor(); } diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/spi/DefaultConfigFactory.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/spi/DefaultConfigFactory.java index 6f896892..9e392810 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/spi/DefaultConfigFactory.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/spi/DefaultConfigFactory.java @@ -163,7 +163,7 @@ PropertiesCompatibleFileConfigRepository createPropertiesCompatibleFileConfigRep String appId, String namespace, ConfigFileFormat format) { String actualNamespaceName = trimNamespaceFormat(namespace, format); PropertiesCompatibleConfigFile configFile = (PropertiesCompatibleConfigFile) ConfigService - .getConfigFile(actualNamespaceName, format); + .getConfigFile(appId, actualNamespaceName, format); return new PropertiesCompatibleFileConfigRepository(configFile); } diff --git a/apollo-client/src/test/java/com/ctrip/framework/apollo/ConfigServiceTest.java b/apollo-client/src/test/java/com/ctrip/framework/apollo/ConfigServiceTest.java index 65f0c2f4..a5a4d70d 100644 --- a/apollo-client/src/test/java/com/ctrip/framework/apollo/ConfigServiceTest.java +++ b/apollo-client/src/test/java/com/ctrip/framework/apollo/ConfigServiceTest.java @@ -103,6 +103,34 @@ public void testMockConfigFactoryForConfigFile() throws Exception { assertEquals(someNamespaceFileName + ":" + someConfigFileFormat.getValue(), configFile.getContent()); } + @Test + public void testGetConfigWithCustomAppId() throws Exception { + String customAppId = "customAppId"; + String someNamespace = "mock"; + String someKey = "someKey"; + MockInjector.setInstance(ConfigFactory.class, someNamespace, new MockConfigFactory()); + + Config config = ConfigService.getConfig(customAppId, someNamespace); + + assertEquals(customAppId + ConfigConsts.CLUSTER_NAMESPACE_SEPARATOR + someNamespace + ":" + someKey, + config.getProperty(someKey, null)); + } + + @Test + public void testGetConfigFileWithCustomAppId() throws Exception { + String customAppId = "customAppId"; + String someNamespace = "mock"; + ConfigFileFormat someConfigFileFormat = ConfigFileFormat.YML; + String someNamespaceFileName = + String.format("%s.%s", someNamespace, someConfigFileFormat.getValue()); + MockInjector.setInstance(ConfigFactory.class, someNamespaceFileName, new MockConfigFactory()); + + ConfigFile configFile = ConfigService.getConfigFile(customAppId, someNamespace, someConfigFileFormat); + + assertEquals(customAppId, configFile.getAppId()); + assertEquals(someNamespaceFileName, configFile.getNamespace()); + } + private static class MockConfig extends AbstractConfig { private final String m_appId; private final String m_namespace;