Coverage Report - com.ctrip.framework.apollo.ConfigService
 
Classes in this File Line Coverage Branch Coverage Complexity
ConfigService
70%
19/27
N/A
1.462
ConfigService$1
66%
2/3
N/A
1.462
 
 1  
 package com.ctrip.framework.apollo;
 2  
 
 3  
 import com.ctrip.framework.apollo.core.ConfigConsts;
 4  
 import com.ctrip.framework.apollo.core.enums.ConfigFileFormat;
 5  
 import com.ctrip.framework.apollo.exceptions.ApolloConfigException;
 6  
 import com.ctrip.framework.apollo.internals.ConfigManager;
 7  
 import com.ctrip.framework.apollo.spi.ConfigFactory;
 8  
 import com.ctrip.framework.apollo.spi.ConfigRegistry;
 9  
 import com.dianping.cat.Cat;
 10  
 
 11  
 import org.codehaus.plexus.PlexusContainer;
 12  
 import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
 13  
 import org.unidal.lookup.ContainerLoader;
 14  
 
 15  
 /**
 16  
  * Entry point for client config use
 17  
  * @author Jason Song(song_s@ctrip.com)
 18  
  */
 19  
 public class ConfigService {
 20  1
   private static final ConfigService s_instance = new ConfigService();
 21  
 
 22  
   private PlexusContainer m_container;
 23  
 
 24  1
   private ConfigService() {
 25  1
     m_container = ContainerLoader.getDefaultContainer();
 26  1
   }
 27  
 
 28  
   /**
 29  
    * Get Application's config instance.
 30  
    * @return config instance
 31  
    */
 32  
   public static Config getAppConfig() {
 33  10
     return getConfig(ConfigConsts.NAMESPACE_APPLICATION);
 34  
   }
 35  
 
 36  
   /**
 37  
    * Get the config instance for the namespace.
 38  
    * @param namespace the namespace of the config
 39  
    * @return config instance
 40  
    */
 41  
   public static Config getConfig(String namespace) {
 42  11
     return getManager().getConfig(namespace);
 43  
   }
 44  
 
 45  
   public static ConfigFile getConfigFile(String namespace, ConfigFileFormat configFileFormat) {
 46  1
     return getManager().getConfigFile(namespace, configFileFormat);
 47  
   }
 48  
 
 49  
   private static ConfigManager getManager() {
 50  
     try {
 51  12
       return s_instance.m_container.lookup(ConfigManager.class);
 52  0
     } catch (ComponentLookupException ex) {
 53  0
       ApolloConfigException exception = new ApolloConfigException("Unable to load ConfigManager!", ex);
 54  0
       Cat.logError(exception);
 55  0
       throw exception;
 56  
     }
 57  
   }
 58  
 
 59  
   private static ConfigRegistry getRegistry() {
 60  
     try {
 61  2
       return s_instance.m_container.lookup(ConfigRegistry.class);
 62  0
     } catch (ComponentLookupException ex) {
 63  0
       ApolloConfigException exception = new ApolloConfigException("Unable to load ConfigRegistry!", ex);
 64  0
       Cat.logError(exception);
 65  0
       throw exception;
 66  
     }
 67  
   }
 68  
 
 69  
   static void setConfig(Config config) {
 70  1
     setConfig(ConfigConsts.NAMESPACE_APPLICATION, config);
 71  1
   }
 72  
 
 73  
   /**
 74  
    * Manually set the config for the namespace specified, use with caution.
 75  
    * @param namespace the namespace
 76  
    * @param config the config instance
 77  
    */
 78  
   static void setConfig(String namespace, final Config config) {
 79  1
     getRegistry().register(namespace, new ConfigFactory() {
 80  
       @Override
 81  
       public Config create(String namespace) {
 82  1
         return config;
 83  
       }
 84  
 
 85  
       @Override
 86  
       public ConfigFile createConfigFile(String namespace, ConfigFileFormat configFileFormat) {
 87  0
         return null;
 88  
       }
 89  
 
 90  
     });
 91  1
   }
 92  
 
 93  
   static void setConfigFactory(ConfigFactory factory) {
 94  1
     setConfigFactory(ConfigConsts.NAMESPACE_APPLICATION, factory);
 95  1
   }
 96  
 
 97  
   /**
 98  
    * Manually set the config factory for the namespace specified, use with caution.
 99  
    * @param namespace the namespace
 100  
    * @param factory the factory instance
 101  
    */
 102  
   static void setConfigFactory(String namespace, ConfigFactory factory) {
 103  1
     getRegistry().register(namespace, factory);
 104  1
   }
 105  
 
 106  
   // for test only
 107  
   static void setContainer(PlexusContainer m_container) {
 108  12
     s_instance.m_container = m_container;
 109  12
   }
 110  
 }