Skip to content

Commit

Permalink
Extract domain conf into files
Browse files Browse the repository at this point in the history
  • Loading branch information
yiming187 committed Mar 28, 2016
1 parent 1d3cecb commit fd11033
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 85 deletions.
Expand Up @@ -4,18 +4,12 @@
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;

Expand All @@ -41,7 +35,7 @@ public Env getEnv() {
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)) {
Expand All @@ -62,47 +56,4 @@ public String getMetaServerDomainName() {
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<String> e = (Enumeration<String>) 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;
}
}
@@ -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<Env, String> 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) {
Expand Down
@@ -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<String> e = (Enumeration<String>) 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;
}
}
1 change: 1 addition & 0 deletions apollo-core/src/main/resources/apollo-env.properties
@@ -0,0 +1 @@
local.meta=http://localhost:8090
@@ -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));
}
}
1 change: 1 addition & 0 deletions apollo-core/src/test/resources/apollo-env.properties
@@ -0,0 +1 @@
local.meta=http://localhost:8090

0 comments on commit fd11033

Please sign in to comment.