##### Coverage Report - com.ctrip.framework.apollo.internals.AbstractConfigFile

Classes in this File | Line Coverage | Branch Coverage | Complexity
AbstractConfigFile |
95% | 22/23
50% | 1/2
| 1.75;1.75

1 |  |
     package com.ctrip.framework.apollo.internals;
2 |  |
3 |  |
     import com.ctrip.framework.apollo.ConfigFile;
4 |  |
     import com.ctrip.framework.apollo.util.ExceptionUtil;
5 |  |
     import com.dianping.cat.Cat;
6 |  |
7 |  |
     import org.slf4j.Logger;
8 |  |
     import org.slf4j.LoggerFactory;
9 |  |
10 |  |
     import java.util.Properties;
11 |  |
     import java.util.concurrent.atomic.AtomicReference;
12 |  |
13 |  |
     /**
14 |  |
      * @author Jason Song(song_s@ctrip.com)
15 |  |
      */
16 |  |
     public abstract class AbstractConfigFile implements ConfigFile, RepositoryChangeListener {
17 | 1 |
       private static final Logger logger = LoggerFactory.getLogger(AbstractConfigFile.class);
18 |  |
       protected ConfigRepository m_configRepository;
19 |  |
       protected String m_namespace;
20 |  |
       protected AtomicReference<Properties> m_configProperties;
21 |  |
22 | 14 |
       public AbstractConfigFile(String namespace, ConfigRepository configRepository) {
23 | 14 |
         m_configRepository = configRepository;
24 | 14 |
         m_namespace = namespace;
25 | 14 |
         m_configProperties = new AtomicReference<>();
26 | 14 |
         initialize();
27 | 14 |
       }
28 |  |
29 |  |
       private void initialize() {
30 |  |
         try {
31 | 14 |
           m_configProperties.set(m_configRepository.getConfig());
32 | 4 |
         } catch (Throwable ex) {
33 | 4 |
           Cat.logError(ex);
34 | 8 |
           logger.warn("Init Apollo Config File failed - namespace: {}, reason: {}.",
35 | 4 |
               m_namespace, ExceptionUtil.getDetailMessage(ex));
36 |  |
         } finally {
37 |  |
           //register the change listener no matter config repository is working or not
38 |  |
           //so that whenever config repository is recovered, config could get changed
39 | 14 |
           m_configRepository.addChangeListener(this);
40 | 14 |
         }
41 | 14 |
       }
42 |  |
43 |  |
       @Override
44 |  |
       public String getNamespace() {
45 | 4 |
         return m_namespace;
46 |  |
       }
47 |  |
48 |  |
       @Override
49 |  |
       public synchronized void onRepositoryChange(String namespace, Properties newProperties) {
50 | 4 |
         if (newProperties.equals(m_configProperties.get())) {
51 | 0 |
           return;
52 |  |
         }
53 | 4 |
         Properties newConfigProperties = new Properties();
54 | 4 |
         newConfigProperties.putAll(newProperties);
55 |  |
56 | 4 |
         m_configProperties.set(newConfigProperties);
57 |  |
58 | 4 |
         Cat.logEvent("Apollo.Client.ConfigChanges", m_namespace);
59 | 4 |
       }
60 |  |
61 |  |
     }