| 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 | |
|
| 15 | |
|
| 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 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
public static Config getAppConfig() { |
| 31 | 10 | return getConfig(ConfigConsts.NAMESPACE_DEFAULT); |
| 32 | |
} |
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 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 | |
|
| 68 | |
|
| 69 | |
|
| 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 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
static void setConfigFactory(String namespace, ConfigFactory factory) { |
| 90 | 1 | getRegistry().register(namespace, factory); |
| 91 | 1 | } |
| 92 | |
|
| 93 | |
|
| 94 | |
static void setContainer(PlexusContainer m_container) { |
| 95 | 11 | s_instance.m_container = m_container; |
| 96 | 11 | } |
| 97 | |
} |