| 1 | |
package com.ctrip.framework.apollo.internals; |
| 2 | |
|
| 3 | |
import com.ctrip.framework.apollo.core.enums.ConfigFileFormat; |
| 4 | |
import com.ctrip.framework.apollo.core.utils.PropertiesUtil; |
| 5 | |
import com.ctrip.framework.apollo.exceptions.ApolloConfigException; |
| 6 | |
import com.ctrip.framework.apollo.util.ExceptionUtil; |
| 7 | |
import com.dianping.cat.Cat; |
| 8 | |
|
| 9 | |
import org.slf4j.Logger; |
| 10 | |
import org.slf4j.LoggerFactory; |
| 11 | |
|
| 12 | |
import java.util.Properties; |
| 13 | |
import java.util.concurrent.atomic.AtomicReference; |
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
public class PropertiesConfigFile extends AbstractConfigFile { |
| 19 | 1 | private static final Logger logger = LoggerFactory.getLogger(PropertiesConfigFile.class); |
| 20 | |
protected AtomicReference<String> m_contentCache; |
| 21 | |
|
| 22 | |
public PropertiesConfigFile(String namespace, |
| 23 | |
ConfigRepository configRepository) { |
| 24 | 6 | super(namespace, configRepository); |
| 25 | 6 | m_contentCache = new AtomicReference<>(); |
| 26 | 6 | } |
| 27 | |
|
| 28 | |
@Override |
| 29 | |
public String getContent() { |
| 30 | 8 | if (m_contentCache.get() == null) { |
| 31 | 7 | m_contentCache.set(doGetContent()); |
| 32 | |
} |
| 33 | 8 | return m_contentCache.get(); |
| 34 | |
} |
| 35 | |
|
| 36 | |
String doGetContent() { |
| 37 | 7 | if (m_configProperties.get() == null) { |
| 38 | 3 | return null; |
| 39 | |
} |
| 40 | |
|
| 41 | |
try { |
| 42 | 4 | return PropertiesUtil.toString(m_configProperties.get()); |
| 43 | 0 | } catch (Throwable ex) { |
| 44 | 0 | ApolloConfigException exception = |
| 45 | |
new ApolloConfigException(String |
| 46 | 0 | .format("Parse properties file content failed for namespace: %s, cause: %s", |
| 47 | 0 | m_namespace, ExceptionUtil.getDetailMessage(ex))); |
| 48 | 0 | Cat.logError(exception); |
| 49 | 0 | throw exception; |
| 50 | |
} |
| 51 | |
} |
| 52 | |
|
| 53 | |
@Override |
| 54 | |
public boolean hasContent() { |
| 55 | 5 | return m_configProperties.get() != null && !m_configProperties.get().isEmpty(); |
| 56 | |
} |
| 57 | |
|
| 58 | |
@Override |
| 59 | |
public ConfigFileFormat getConfigFileFormat() { |
| 60 | 1 | return ConfigFileFormat.Properties; |
| 61 | |
} |
| 62 | |
|
| 63 | |
@Override |
| 64 | |
public synchronized void onRepositoryChange(String namespace, Properties newProperties) { |
| 65 | 2 | super.onRepositoryChange(namespace, newProperties); |
| 66 | 2 | m_contentCache.set(null); |
| 67 | 2 | } |
| 68 | |
} |