Search in sources :

Example 11 with ApolloConfigException

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;
}
Also used : Env(com.ctrip.framework.apollo.core.enums.Env) ApolloConfigException(com.ctrip.framework.apollo.exceptions.ApolloConfigException)

Example 12 with ApolloConfigException

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());
}
Also used : Properties(java.util.Properties) OrderedProperties(com.ctrip.framework.apollo.util.OrderedProperties) ApolloConfigException(com.ctrip.framework.apollo.exceptions.ApolloConfigException) Test(org.junit.Test)

Example 13 with ApolloConfigException

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();
    }
}
Also used : Transaction(com.ctrip.framework.apollo.tracer.spi.Transaction) IOException(java.io.IOException) ApolloConfigException(com.ctrip.framework.apollo.exceptions.ApolloConfigException)

Example 14 with ApolloConfigException

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);
    }
}
Also used : Transaction(com.ctrip.framework.apollo.tracer.spi.Transaction) ApolloConfigException(com.ctrip.framework.apollo.exceptions.ApolloConfigException)

Example 15 with ApolloConfigException

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();
    }
}
Also used : Transaction(com.ctrip.framework.apollo.tracer.spi.Transaction) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) ApolloConfigException(com.ctrip.framework.apollo.exceptions.ApolloConfigException)

Aggregations

ApolloConfigException (com.ctrip.framework.apollo.exceptions.ApolloConfigException)20 Transaction (com.ctrip.framework.apollo.tracer.spi.Transaction)10 IOException (java.io.IOException)9 ApolloConfigStatusCodeException (com.ctrip.framework.apollo.exceptions.ApolloConfigStatusCodeException)5 InputStream (java.io.InputStream)5 ServiceDTO (com.ctrip.framework.apollo.core.dto.ServiceDTO)4 HttpRequest (com.ctrip.framework.apollo.util.http.HttpRequest)4 File (java.io.File)4 List (java.util.List)4 Map (java.util.Map)4 Properties (java.util.Properties)4 InputStreamReader (java.io.InputStreamReader)3 HttpURLConnection (java.net.HttpURLConnection)3 URL (java.net.URL)3 ApolloConfig (com.ctrip.framework.apollo.core.dto.ApolloConfig)2 OrderedProperties (com.ctrip.framework.apollo.util.OrderedProperties)2 HttpResponse (com.ctrip.framework.apollo.util.http.HttpResponse)2 FileInputStream (java.io.FileInputStream)2 FileOutputStream (java.io.FileOutputStream)2 OutputStream (java.io.OutputStream)2