Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
95 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 14 additions & 34 deletions
48
apollo-core/src/main/java/com/ctrip/apollo/core/MetaDomainConsts.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
apollo-core/src/main/java/com/ctrip/apollo/core/utils/ResourceUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
local.meta=http://localhost:8090 |
15 changes: 15 additions & 0 deletions
15
apollo-core/src/test/java/com/ctrip/apollo/core/MetaDomainTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
local.meta=http://localhost:8090 |