forked from apolloconfig/apollo-java
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConfigService.java
More file actions
170 lines (148 loc) · 5.1 KB
/
ConfigService.java
File metadata and controls
170 lines (148 loc) · 5.1 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
* Copyright 2022 Apollo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.ctrip.framework.apollo;
import com.ctrip.framework.apollo.build.ApolloInjector;
import com.ctrip.framework.apollo.core.ConfigConsts;
import com.ctrip.framework.apollo.core.enums.ConfigFileFormat;
import com.ctrip.framework.apollo.core.utils.StringUtils;
import com.ctrip.framework.apollo.internals.ConfigManager;
import com.ctrip.framework.apollo.internals.ConfigMonitorInitializer;
import com.ctrip.framework.apollo.monitor.api.ConfigMonitor;
import com.ctrip.framework.apollo.spi.ConfigFactory;
import com.ctrip.framework.apollo.spi.ConfigRegistry;
import com.ctrip.framework.apollo.util.ConfigUtil;
/**
* 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 volatile ConfigMonitor m_configMonitor;
private volatile ConfigManager m_configManager;
private volatile ConfigRegistry m_configRegistry;
private ConfigMonitor getMonitor() {
getManager();
if (m_configMonitor == null) {
synchronized (this) {
if (m_configMonitor == null) {
m_configMonitor = ApolloInjector.getInstance(ConfigMonitor.class);
}
}
}
return m_configMonitor;
}
private ConfigManager getManager() {
if (m_configManager == null) {
synchronized (this) {
if (m_configManager == null) {
m_configManager = ApolloInjector.getInstance(ConfigManager.class);
ConfigMonitorInitializer.initialize();
}
}
}
return m_configManager;
}
private ConfigRegistry getRegistry() {
if (m_configRegistry == null) {
synchronized (this) {
if (m_configRegistry == null) {
m_configRegistry = ApolloInjector.getInstance(ConfigRegistry.class);
}
}
}
return m_configRegistry;
}
/**
* Get Application's config instance.
*
* @return config instance
*/
public static Config getAppConfig() {
return getConfig(ConfigConsts.NAMESPACE_APPLICATION);
}
/**
* Get the config instance for the namespace.
*
* @param namespace the namespace of the config
* @return config instance
*/
public static Config getConfig(String namespace) {
return s_instance.getManager().getConfig(namespace);
}
public static Config getConfig(String appId, String namespace) {
return s_instance.getManager().getConfig(appId, namespace);
}
public static ConfigFile getConfigFile(String namespace, ConfigFileFormat configFileFormat) {
return s_instance.getManager().getConfigFile(namespace, configFileFormat);
}
public static ConfigMonitor getConfigMonitor(){
return s_instance.getMonitor();
}
static void setConfig(Config config) {
setConfig(ConfigConsts.NAMESPACE_APPLICATION, 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) {
s_instance.getRegistry().register(namespace, new ConfigFactory() {
private final ConfigUtil m_configUtil = ApolloInjector.getInstance(ConfigUtil.class);
@Override
public Config create(String namespace) {
return config;
}
@Override
public Config create(String appId, String namespace) {
if(!StringUtils.equals(appId, m_configUtil.getAppId())){
throw new IllegalArgumentException("Provided appId '" + appId + "' does not match the default appId '" + m_configUtil.getAppId() + "'");
}
return config;
}
@Override
public ConfigFile createConfigFile(String namespace, ConfigFileFormat configFileFormat) {
return null;
}
@Override
public ConfigFile createConfigFile(String appId, String namespace, ConfigFileFormat configFileFormat) {
return null;
}
});
}
static void setConfigFactory(ConfigFactory factory) {
setConfigFactory(ConfigConsts.NAMESPACE_APPLICATION, 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) {
s_instance.getRegistry().register(namespace, factory);
}
// for test only
static void reset() {
synchronized (s_instance) {
s_instance.m_configManager = null;
s_instance.m_configRegistry = null;
}
}
}