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