فهرست منبع

add txt file format

Jason Song 5 سال پیش
والد
کامیت
a5f5092f2a

+ 15 - 0
apollo-client/src/main/java/com/ctrip/framework/apollo/internals/TxtConfigFile.java

@@ -0,0 +1,15 @@
+package com.ctrip.framework.apollo.internals;
+
+import com.ctrip.framework.apollo.core.enums.ConfigFileFormat;
+
+public class TxtConfigFile extends PlainTextConfigFile {
+
+  public TxtConfigFile(String namespace, ConfigRepository configRepository) {
+    super(namespace, configRepository);
+  }
+
+  @Override
+  public ConfigFileFormat getConfigFileFormat() {
+    return ConfigFileFormat.TXT;
+  }
+}

+ 3 - 0
apollo-client/src/main/java/com/ctrip/framework/apollo/spi/DefaultConfigFactory.java

@@ -3,6 +3,7 @@ package com.ctrip.framework.apollo.spi;
 import com.ctrip.framework.apollo.ConfigService;
 import com.ctrip.framework.apollo.PropertiesCompatibleConfigFile;
 import com.ctrip.framework.apollo.internals.PropertiesCompatibleFileConfigRepository;
+import com.ctrip.framework.apollo.internals.TxtConfigFile;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -55,6 +56,8 @@ public class DefaultConfigFactory implements ConfigFactory {
         return new YamlConfigFile(namespace, configRepository);
       case YML:
         return new YmlConfigFile(namespace, configRepository);
+      case TXT:
+        return new TxtConfigFile(namespace, configRepository);
     }
 
     return null;

+ 43 - 0
apollo-client/src/test/java/com/ctrip/framework/apollo/internals/TxtConfigFileTest.java

@@ -0,0 +1,43 @@
+package com.ctrip.framework.apollo.internals;
+
+import static org.junit.Assert.*;
+import static org.mockito.Mockito.when;
+
+import com.ctrip.framework.apollo.core.ConfigConsts;
+import com.ctrip.framework.apollo.core.enums.ConfigFileFormat;
+import java.util.Properties;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.runners.MockitoJUnitRunner;
+
+@RunWith(MockitoJUnitRunner.class)
+public class TxtConfigFileTest {
+
+  private String someNamespace;
+  @Mock
+  private ConfigRepository configRepository;
+
+  @Before
+  public void setUp() throws Exception {
+    someNamespace = "someName";
+  }
+
+  @Test
+  public void testWhenHasContent() throws Exception {
+    Properties someProperties = new Properties();
+    String key = ConfigConsts.CONFIG_FILE_CONTENT_KEY;
+    String someValue = "someValue";
+    someProperties.setProperty(key, someValue);
+
+    when(configRepository.getConfig()).thenReturn(someProperties);
+
+    TxtConfigFile configFile = new TxtConfigFile(someNamespace, configRepository);
+
+    assertEquals(ConfigFileFormat.TXT, configFile.getConfigFileFormat());
+    assertEquals(someNamespace, configFile.getNamespace());
+    assertTrue(configFile.hasContent());
+    assertEquals(someValue, configFile.getContent());
+  }
+}

+ 4 - 2
apollo-core/src/main/java/com/ctrip/framework/apollo/core/enums/ConfigFileFormat.java

@@ -6,7 +6,7 @@ import com.ctrip.framework.apollo.core.utils.StringUtils;
  * @author Jason Song(song_s@ctrip.com)
  */
 public enum ConfigFileFormat {
-  Properties("properties"), XML("xml"), JSON("json"), YML("yml"), YAML("yaml");
+  Properties("properties"), XML("xml"), JSON("json"), YML("yml"), YAML("yaml"), TXT("txt");
 
   private String value;
 
@@ -22,7 +22,7 @@ public enum ConfigFileFormat {
     if (StringUtils.isEmpty(value)) {
       throw new IllegalArgumentException("value can not be empty");
     }
-    switch (value) {
+    switch (value.toLowerCase()) {
       case "properties":
         return Properties;
       case "xml":
@@ -33,6 +33,8 @@ public enum ConfigFileFormat {
         return YML;
       case "yaml":
         return YAML;
+      case "txt":
+        return TXT;
     }
     throw new IllegalArgumentException(value + " can not map enum");
   }

+ 2 - 1
apollo-portal/src/main/resources/static/namespace.html

@@ -57,7 +57,7 @@
                             <li>
                                 通过创建一个私有的Namespace可以实现分组管理配置
                             </li>
-                            <li>私有Namespace的格式可以是xml、yml、yaml、json. 您可以通过apollo-client中ConfigFile接口来获取非properties格式Namespace的内容</li>
+                            <li>私有Namespace的格式可以是xml、yml、yaml、json、txt. 您可以通过apollo-client中ConfigFile接口来获取非properties格式Namespace的内容</li>
                             <li>1.3.0及以上版本的apollo-client针对yaml/yml提供了更好的支持,可以通过ConfigService.getConfig("someNamespace.yml")直接获取Config对象,也可以通过@EnableApolloConfig("someNamespace.yml")或apollo.bootstrap.namespaces=someNamespace.yml注入yml配置到Spring/Spring Boot中去</li>
                         </ul>
                     </div>
@@ -113,6 +113,7 @@
                                     <option value="json">json</option>
                                     <option value="yml">yml</option>
                                     <option value="yaml">yaml</option>
+                                    <option value="txt">txt</option>
                                 </select>
                             </div>
 

+ 1 - 1
apollo-portal/src/main/resources/static/scripts/directive/namespace-panel-directive.js

@@ -92,7 +92,7 @@ function directive($window, toastr, AppUtil, EventManager, PermissionService, Na
                 //namespace view name hide suffix
                 namespace.viewName = namespace.baseInfo.namespaceName.replace(".xml", "").replace(
                             ".properties", "").replace(".json", "").replace(".yml", "")
-                            .replace(".yaml", "");
+                            .replace(".yaml", "").replace(".txt", "");
             }
 
             function init() {