Coverage Report - com.ctrip.framework.apollo.internals.SimpleConfig
 
Classes in this File Line Coverage Branch Coverage Complexity
SimpleConfig
96%
28/29
75%
3/4
2
SimpleConfig$1
100%
2/2
N/A
2
 
 1  
 package com.ctrip.framework.apollo.internals;
 2  
 
 3  
 import com.google.common.base.Function;
 4  
 import com.google.common.collect.Maps;
 5  
 
 6  
 import com.ctrip.framework.apollo.model.ConfigChange;
 7  
 import com.ctrip.framework.apollo.model.ConfigChangeEvent;
 8  
 import com.ctrip.framework.apollo.util.ExceptionUtil;
 9  
 import com.dianping.cat.Cat;
 10  
 
 11  
 import org.slf4j.Logger;
 12  
 import org.slf4j.LoggerFactory;
 13  
 
 14  
 import java.util.List;
 15  
 import java.util.Map;
 16  
 import java.util.Properties;
 17  
 
 18  
 /**
 19  
  * @author Jason Song(song_s@ctrip.com)
 20  
  */
 21  
 public class SimpleConfig extends AbstractConfig implements RepositoryChangeListener {
 22  1
   private static final Logger logger = LoggerFactory.getLogger(SimpleConfig.class);
 23  
   private final String m_namespace;
 24  
   private final ConfigRepository m_configRepository;
 25  
   private volatile Properties m_configProperties;
 26  
 
 27  
   /**
 28  
    * Constructor.
 29  
    *
 30  
    * @param namespace        the namespace for this config instance
 31  
    * @param configRepository the config repository for this config instance
 32  
    */
 33  3
   public SimpleConfig(String namespace, ConfigRepository configRepository) {
 34  3
     m_namespace = namespace;
 35  3
     m_configRepository = configRepository;
 36  3
     this.initialize();
 37  3
   }
 38  
 
 39  
   private void initialize() {
 40  
     try {
 41  3
       m_configProperties = m_configRepository.getConfig();
 42  1
     } catch (Throwable ex) {
 43  1
       Cat.logError(ex);
 44  2
       logger.warn("Init Apollo Simple Config failed - namespace: {}, reason: {}", m_namespace,
 45  1
           ExceptionUtil.getDetailMessage(ex));
 46  
     } finally {
 47  
       //register the change listener no matter config repository is working or not
 48  
       //so that whenever config repository is recovered, config could get changed
 49  3
       m_configRepository.addChangeListener(this);
 50  3
     }
 51  3
   }
 52  
 
 53  
   @Override
 54  
   public String getProperty(String key, String defaultValue) {
 55  2
     if (m_configProperties == null) {
 56  1
       logger.error("Config initialization failed, always return default value!");
 57  1
       return defaultValue;
 58  
     }
 59  1
     return this.m_configProperties.getProperty(key, defaultValue);
 60  
   }
 61  
 
 62  
   @Override
 63  
   public synchronized void onRepositoryChange(String namespace, Properties newProperties) {
 64  1
     if (newProperties.equals(m_configProperties)) {
 65  0
       return;
 66  
     }
 67  1
     Properties newConfigProperties = new Properties();
 68  1
     newConfigProperties.putAll(newProperties);
 69  
 
 70  
     List<ConfigChange>
 71  1
         changes =
 72  1
         calcPropertyChanges(namespace, m_configProperties, newConfigProperties);
 73  1
     Map<String, ConfigChange> changeMap = Maps.uniqueIndex(changes,
 74  4
         new Function<ConfigChange, String>() {
 75  
           @Override
 76  
           public String apply(ConfigChange input) {
 77  3
             return input.getPropertyName();
 78  
           }
 79  
         });
 80  
 
 81  1
     m_configProperties = newConfigProperties;
 82  
 
 83  1
     this.fireConfigChange(new ConfigChangeEvent(m_namespace, changeMap));
 84  
 
 85  1
     Cat.logEvent("Apollo.Client.ConfigChanges", m_namespace);
 86  1
   }
 87  
 }