diff --git a/apollo-client/src/main/java/com/ctrip/apollo/client/env/ClientEnvironment.java b/apollo-client/src/main/java/com/ctrip/apollo/client/env/ClientEnvironment.java index f57851a5605c19752823dedfa80f3516aff16500..602313be1d1ad90bcbc20a224ce8b16b66103921 100644 --- a/apollo-client/src/main/java/com/ctrip/apollo/client/env/ClientEnvironment.java +++ b/apollo-client/src/main/java/com/ctrip/apollo/client/env/ClientEnvironment.java @@ -4,18 +4,12 @@ import com.ctrip.apollo.Apollo; import com.ctrip.apollo.Apollo.Env; import com.ctrip.apollo.client.constants.Constants; import com.ctrip.apollo.core.MetaDomainConsts; +import com.ctrip.apollo.core.utils.ResourceUtils; import com.ctrip.apollo.core.utils.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.Enumeration; import java.util.Properties; import java.util.concurrent.atomic.AtomicReference; @@ -41,7 +35,7 @@ public class ClientEnvironment { if (env.get() == null) { Env resultEnv = Apollo.getEnv(); Properties apolloProperties = null; - apolloProperties = readConfigFile(DEFAULT_FILE, null); + apolloProperties = ResourceUtils.readConfigFile(DEFAULT_FILE, null); if (apolloProperties != null) { String strEnv = apolloProperties.getProperty(Constants.ENV); if (!StringUtils.isBlank(strEnv)) { @@ -62,47 +56,4 @@ public class ClientEnvironment { return MetaDomainConsts.getDomain(getEnv()); } - @SuppressWarnings("unchecked") - private Properties readConfigFile(String configPath, Properties defaults) { - InputStream in = this.getClass().getResourceAsStream(configPath); - logger.info("Reading config from resource {}", configPath); - Properties props = new Properties(); - try { - if (in == null) { - // load outside resource under current user path - Path path = new File(System.getProperty("user.dir") + configPath).toPath(); - if (Files.isReadable(path)) { - in = new FileInputStream(path.toFile()); - logger.info("Reading config from file {} ", path); - } - } - if (defaults != null) { - props.putAll(defaults); - } - - if (in != null) { - props.load(in); - in.close(); - } - } catch (Exception ex) { - logger.warn("Reading config failed: {}", ex.getMessage()); - } finally { - if (in != null) { - try { - in.close(); - } catch (IOException ex) { - logger.warn("Close config failed: {}", ex.getMessage()); - } - } - } - StringBuilder sb = new StringBuilder(); - for (Enumeration e = (Enumeration) props.propertyNames(); e - .hasMoreElements(); ) { - String key = e.nextElement(); - String val = (String) props.getProperty(key); - sb.append(key).append('=').append(val).append('\n'); - } - logger.info("Reading properties: \n" + sb.toString()); - return props; - } } diff --git a/apollo-core/src/main/java/com/ctrip/apollo/core/MetaDomainConsts.java b/apollo-core/src/main/java/com/ctrip/apollo/core/MetaDomainConsts.java index 33a44460b3cca78226f299242d47bade01706e51..72107a0f286a606b2ce6822cfcface3465da170d 100644 --- a/apollo-core/src/main/java/com/ctrip/apollo/core/MetaDomainConsts.java +++ b/apollo-core/src/main/java/com/ctrip/apollo/core/MetaDomainConsts.java @@ -1,47 +1,27 @@ package com.ctrip.apollo.core; -import com.ctrip.apollo.Apollo.Env; - import java.util.HashMap; import java.util.Map; +import java.util.Properties; -public class MetaDomainConsts { - - public static final String DEFAULT_PORT = "8080"; - - public static final String LOCAL = "http://localhost" + ":" + DEFAULT_PORT; - - public static final String DEV = - "http://10.3.2.56" + ":" + DEFAULT_PORT; - - public static final String FAT = - "http://ws.meta.apollo.fx.fat.nt.ctripcorp.com" + ":" + DEFAULT_PORT; - - public static final String FWS = - "http://ws.meta.apollo.fx.fws.nt.ctripcorp.com" + ":" + DEFAULT_PORT; - - public static final String UAT = - "http://ws.meta.apollo.fx.uat.nt.ctripcorp.com" + ":" + DEFAULT_PORT; - - public static final String LPT = - "http://ws.meta.apollo.fx.lpt.nt.ctripcorp.com" + ":" + DEFAULT_PORT; - - public static final String TOOLS = - "http://ws.meta.apollo.fx.tools.ctripcorp.com" + ":" + DEFAULT_PORT; +import com.ctrip.apollo.Apollo.Env; +import com.ctrip.apollo.core.utils.ResourceUtils; - public static final String PRO = "http://ws.meta.apollo.fx.ctripcorp.com" + ":" + DEFAULT_PORT; +public class MetaDomainConsts { private static Map domains = new HashMap<>(); static { - domains.put(Env.LOCAL, LOCAL); - domains.put(Env.DEV, DEV); - domains.put(Env.FAT, FAT); - domains.put(Env.FWS, FWS); - domains.put(Env.UAT, UAT); - domains.put(Env.LPT, LPT); - domains.put(Env.TOOLS, TOOLS); - domains.put(Env.PRO, PRO); + Properties prop = new Properties(); + prop = ResourceUtils.readConfigFile("apollo-env.properties", prop); + domains.put(Env.LOCAL, prop.getProperty("local.meta", "http://localhost:8080")); + domains.put(Env.DEV, prop.getProperty("dev.meta")); + domains.put(Env.FAT, prop.getProperty("fat.meta")); + domains.put(Env.FWS, prop.getProperty("fws.meta")); + domains.put(Env.UAT, prop.getProperty("uat.meta")); + domains.put(Env.LPT, prop.getProperty("lpt.meta")); + domains.put(Env.TOOLS, prop.getProperty("tools.meta")); + domains.put(Env.PRO, prop.getProperty("pro.meta")); } public static String getDomain(Env env) { diff --git a/apollo-core/src/main/java/com/ctrip/apollo/core/utils/ResourceUtils.java b/apollo-core/src/main/java/com/ctrip/apollo/core/utils/ResourceUtils.java new file mode 100644 index 0000000000000000000000000000000000000000..859182df8f4facb3ba2e81db276eaf58b3c2acd9 --- /dev/null +++ b/apollo-core/src/main/java/com/ctrip/apollo/core/utils/ResourceUtils.java @@ -0,0 +1,62 @@ +package com.ctrip.apollo.core.utils; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Enumeration; +import java.util.Properties; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ResourceUtils { + + private static final Logger logger = LoggerFactory.getLogger(ResourceUtils.class); + + @SuppressWarnings("unchecked") + public static Properties readConfigFile(String configPath, Properties defaults) { + InputStream in = ClassLoader.getSystemResourceAsStream(configPath); + logger.info("Reading config from resource {}", configPath); + Properties props = new Properties(); + try { + if (in == null) { + // load outside resource under current user path + Path path = new File(System.getProperty("user.dir") + configPath).toPath(); + if (Files.isReadable(path)) { + in = new FileInputStream(path.toFile()); + logger.info("Reading config from file {} ", path); + } + } + if (defaults != null) { + props.putAll(defaults); + } + + if (in != null) { + props.load(in); + in.close(); + } + } catch (Exception ex) { + logger.warn("Reading config failed: {}", ex.getMessage()); + } finally { + if (in != null) { + try { + in.close(); + } catch (IOException ex) { + logger.warn("Close config failed: {}", ex.getMessage()); + } + } + } + StringBuilder sb = new StringBuilder(); + for (Enumeration e = (Enumeration) props.propertyNames(); e + .hasMoreElements(); ) { + String key = e.nextElement(); + String val = (String) props.getProperty(key); + sb.append(key).append('=').append(val).append('\n'); + } + logger.info("Reading properties: \n" + sb.toString()); + return props; + } +} diff --git a/apollo-core/src/main/resources/apollo-env.properties b/apollo-core/src/main/resources/apollo-env.properties new file mode 100644 index 0000000000000000000000000000000000000000..53889034c0c017efa291ba48ca2052582cb9c1d3 --- /dev/null +++ b/apollo-core/src/main/resources/apollo-env.properties @@ -0,0 +1 @@ +local.meta=http://localhost:8090 \ No newline at end of file diff --git a/apollo-core/src/test/java/com/ctrip/apollo/core/MetaDomainTest.java b/apollo-core/src/test/java/com/ctrip/apollo/core/MetaDomainTest.java new file mode 100644 index 0000000000000000000000000000000000000000..8ebf800f16c17bf005ac6a02e9506a4910a47892 --- /dev/null +++ b/apollo-core/src/test/java/com/ctrip/apollo/core/MetaDomainTest.java @@ -0,0 +1,15 @@ +package com.ctrip.apollo.core; + +import org.junit.Test; +import org.springframework.util.Assert; + +import com.ctrip.apollo.Apollo.Env; + +public class MetaDomainTest { + + @Test + public void testDefaultDomain() { + Assert.notNull(MetaDomainConsts.getDomain(Env.LOCAL)); + Assert.isNull(MetaDomainConsts.getDomain(Env.PRO)); + } +} diff --git a/apollo-core/src/test/resources/apollo-env.properties b/apollo-core/src/test/resources/apollo-env.properties new file mode 100644 index 0000000000000000000000000000000000000000..53889034c0c017efa291ba48ca2052582cb9c1d3 --- /dev/null +++ b/apollo-core/src/test/resources/apollo-env.properties @@ -0,0 +1 @@ +local.meta=http://localhost:8090 \ No newline at end of file