use of com.ctrip.framework.apollo.exceptions.ApolloConfigException in project apollo by ctripcorp.
the class ConfigUtil method getApolloEnv.
/**
* Get the current environment.
*
* @return the env
* @throws ApolloConfigException if env is set
*/
public Env getApolloEnv() {
Env env = EnvUtils.transformEnv(Foundation.server().getEnvType());
if (env == null) {
String path = isOSWindows() ? "C:\\opt\\settings\\server.properties" : "/opt/settings/server.properties";
String message = String.format("env is not set, please make sure it is set in %s!", path);
logger.error(message);
throw new ApolloConfigException(message);
}
return env;
}
use of com.ctrip.framework.apollo.exceptions.ApolloConfigException in project apollo by apolloconfig.
the class YamlConfigFileTest method testWhenInvalidYamlContent.
@Test
public void testWhenInvalidYamlContent() throws Exception {
Properties someProperties = new Properties();
String key = ConfigConsts.CONFIG_FILE_CONTENT_KEY;
String someInvalidContent = ",";
someProperties.setProperty(key, someInvalidContent);
someSourceType = ConfigSourceType.LOCAL;
when(configRepository.getConfig()).thenReturn(someProperties);
when(configRepository.getSourceType()).thenReturn(someSourceType);
when(yamlParser.yamlToProperties(someInvalidContent)).thenThrow(new RuntimeException("some exception"));
YamlConfigFile configFile = new YamlConfigFile(someNamespace, configRepository);
assertSame(someInvalidContent, configFile.getContent());
Throwable exceptionThrown = null;
try {
configFile.asProperties();
} catch (Throwable ex) {
exceptionThrown = ex;
}
assertTrue(exceptionThrown instanceof ApolloConfigException);
assertNotNull(exceptionThrown.getCause());
}
use of com.ctrip.framework.apollo.exceptions.ApolloConfigException in project apollo by apolloconfig.
the class LocalFileConfigRepository method checkLocalConfigCacheDir.
private void checkLocalConfigCacheDir(File baseDir) {
if (baseDir.exists()) {
return;
}
Transaction transaction = Tracer.newTransaction("Apollo.ConfigService", "createLocalConfigDir");
transaction.addData("BaseDir", baseDir.getAbsolutePath());
try {
Files.createDirectory(baseDir.toPath());
transaction.setStatus(Transaction.SUCCESS);
} catch (IOException ex) {
ApolloConfigException exception = new ApolloConfigException(String.format("Create local config directory %s failed", baseDir.getAbsolutePath()), ex);
Tracer.logError(exception);
transaction.setStatus(exception);
logger.warn("Unable to create local config cache directory {}, reason: {}. Will not able to cache config file.", baseDir.getAbsolutePath(), ExceptionUtil.getDetailMessage(ex));
} finally {
transaction.complete();
}
}
use of com.ctrip.framework.apollo.exceptions.ApolloConfigException in project apollo by apolloconfig.
the class LocalFileConfigRepository method sync.
@Override
protected void sync() {
// sync with upstream immediately
boolean syncFromUpstreamResultSuccess = trySyncFromUpstream();
if (syncFromUpstreamResultSuccess) {
return;
}
Transaction transaction = Tracer.newTransaction("Apollo.ConfigService", "syncLocalConfig");
Throwable exception = null;
try {
transaction.addData("Basedir", m_baseDir.getAbsolutePath());
m_fileProperties = this.loadFromLocalCacheFile(m_baseDir, m_namespace);
m_sourceType = ConfigSourceType.LOCAL;
transaction.setStatus(Transaction.SUCCESS);
} catch (Throwable ex) {
Tracer.logEvent("ApolloConfigException", ExceptionUtil.getDetailMessage(ex));
transaction.setStatus(ex);
exception = ex;
// ignore
} finally {
transaction.complete();
}
if (m_fileProperties == null) {
m_sourceType = ConfigSourceType.NONE;
throw new ApolloConfigException("Load config from local config failed!", exception);
}
}
use of com.ctrip.framework.apollo.exceptions.ApolloConfigException in project apollo by ctripcorp.
the class LocalFileConfigRepository method persistLocalCacheFile.
void persistLocalCacheFile(File baseDir, String namespace) {
if (baseDir == null) {
return;
}
File file = assembleLocalCacheFile(baseDir, namespace);
OutputStream out = null;
Transaction transaction = Tracer.newTransaction("Apollo.ConfigService", "persistLocalConfigFile");
transaction.addData("LocalConfigFile", file.getAbsolutePath());
try {
out = new FileOutputStream(file);
m_fileProperties.store(out, "Persisted by DefaultConfig");
transaction.setStatus(Transaction.SUCCESS);
} catch (IOException ex) {
ApolloConfigException exception = new ApolloConfigException(String.format("Persist local cache file %s failed", file.getAbsolutePath()), ex);
Tracer.logError(exception);
transaction.setStatus(exception);
logger.warn("Persist local cache file {} failed, reason: {}.", file.getAbsolutePath(), ExceptionUtil.getDetailMessage(ex));
} finally {
if (out != null) {
try {
out.close();
} catch (IOException ex) {
// ignore
}
}
transaction.complete();
}
}
Aggregations