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