| 1 | |
package com.ctrip.framework.apollo.internals; |
| 2 | |
|
| 3 | |
import com.ctrip.framework.apollo.core.enums.ConfigFileFormat; |
| 4 | |
import com.ctrip.framework.apollo.exceptions.ApolloConfigException; |
| 5 | |
import com.ctrip.framework.apollo.util.ExceptionUtil; |
| 6 | |
import com.dianping.cat.Cat; |
| 7 | |
|
| 8 | |
import org.slf4j.Logger; |
| 9 | |
import org.slf4j.LoggerFactory; |
| 10 | |
|
| 11 | |
import java.io.IOException; |
| 12 | |
import java.io.StringWriter; |
| 13 | |
import java.util.Properties; |
| 14 | |
import java.util.concurrent.atomic.AtomicReference; |
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
public class PropertiesConfigFile extends AbstractConfigFile { |
| 20 | 1 | private static final Logger logger = LoggerFactory.getLogger(PropertiesConfigFile.class); |
| 21 | |
protected AtomicReference<String> m_contentCache; |
| 22 | |
|
| 23 | |
public PropertiesConfigFile(String namespace, |
| 24 | |
ConfigRepository configRepository) { |
| 25 | 6 | super(namespace, configRepository); |
| 26 | 6 | m_contentCache = new AtomicReference<>(); |
| 27 | 6 | } |
| 28 | |
|
| 29 | |
@Override |
| 30 | |
public String getContent() { |
| 31 | 8 | if (m_contentCache.get() == null) { |
| 32 | 7 | m_contentCache.set(doGetContent()); |
| 33 | |
} |
| 34 | 8 | return m_contentCache.get(); |
| 35 | |
} |
| 36 | |
|
| 37 | |
String doGetContent() { |
| 38 | 7 | if (m_configProperties.get() == null) { |
| 39 | 3 | return null; |
| 40 | |
} |
| 41 | 4 | StringWriter writer = new StringWriter(); |
| 42 | |
try { |
| 43 | 4 | m_configProperties.get().store(writer, null); |
| 44 | 4 | StringBuffer stringBuffer = writer.getBuffer(); |
| 45 | 4 | filterPropertiesComment(stringBuffer); |
| 46 | 4 | return stringBuffer.toString(); |
| 47 | 0 | } catch (IOException ex) { |
| 48 | 0 | ApolloConfigException exception = |
| 49 | |
new ApolloConfigException(String |
| 50 | 0 | .format("Parse properties file content failed for namespace: %s, cause: %s", |
| 51 | 0 | m_namespace, ExceptionUtil.getDetailMessage(ex))); |
| 52 | 0 | Cat.logError(exception); |
| 53 | 0 | throw exception; |
| 54 | |
} |
| 55 | |
} |
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
boolean filterPropertiesComment(StringBuffer stringBuffer) { |
| 63 | |
|
| 64 | 4 | if (stringBuffer.charAt(0) != '#') { |
| 65 | 0 | return false; |
| 66 | |
} |
| 67 | 4 | int commentLineIndex = stringBuffer.indexOf("\n"); |
| 68 | 4 | if (commentLineIndex == -1) { |
| 69 | 0 | return false; |
| 70 | |
} |
| 71 | 4 | stringBuffer.delete(0, commentLineIndex + 1); |
| 72 | 4 | return true; |
| 73 | |
} |
| 74 | |
|
| 75 | |
|
| 76 | |
@Override |
| 77 | |
public boolean hasContent() { |
| 78 | 5 | return m_configProperties.get() != null && !m_configProperties.get().isEmpty(); |
| 79 | |
} |
| 80 | |
|
| 81 | |
@Override |
| 82 | |
public ConfigFileFormat getConfigFileFormat() { |
| 83 | 1 | return ConfigFileFormat.Properties; |
| 84 | |
} |
| 85 | |
|
| 86 | |
@Override |
| 87 | |
public synchronized void onRepositoryChange(String namespace, Properties newProperties) { |
| 88 | 2 | super.onRepositoryChange(namespace, newProperties); |
| 89 | 2 | m_contentCache.set(null); |
| 90 | 2 | } |
| 91 | |
} |