| 1 | |
package com.ctrip.framework.apollo.internals; |
| 2 | |
|
| 3 | |
import com.google.common.collect.Maps; |
| 4 | |
|
| 5 | |
import com.ctrip.framework.apollo.Config; |
| 6 | |
import com.ctrip.framework.apollo.ConfigFile; |
| 7 | |
import com.ctrip.framework.apollo.core.enums.ConfigFileFormat; |
| 8 | |
import com.ctrip.framework.apollo.spi.ConfigFactory; |
| 9 | |
import com.ctrip.framework.apollo.spi.ConfigFactoryManager; |
| 10 | |
|
| 11 | |
import org.unidal.lookup.annotation.Inject; |
| 12 | |
import org.unidal.lookup.annotation.Named; |
| 13 | |
|
| 14 | |
import java.util.Map; |
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
@Named(type = ConfigManager.class) |
| 20 | 16 | public class DefaultConfigManager implements ConfigManager { |
| 21 | |
@Inject |
| 22 | |
private ConfigFactoryManager m_factoryManager; |
| 23 | |
|
| 24 | 16 | private Map<String, Config> m_configs = Maps.newConcurrentMap(); |
| 25 | 16 | private Map<String, ConfigFile> m_configFiles = Maps.newConcurrentMap(); |
| 26 | |
|
| 27 | |
@Override |
| 28 | |
public Config getConfig(String namespace) { |
| 29 | 15 | Config config = m_configs.get(namespace); |
| 30 | |
|
| 31 | 15 | if (config == null) { |
| 32 | 14 | synchronized (this) { |
| 33 | 14 | config = m_configs.get(namespace); |
| 34 | |
|
| 35 | 14 | if (config == null) { |
| 36 | 14 | ConfigFactory factory = m_factoryManager.getFactory(namespace); |
| 37 | |
|
| 38 | 14 | config = factory.create(namespace); |
| 39 | 14 | m_configs.put(namespace, config); |
| 40 | |
} |
| 41 | 14 | } |
| 42 | |
} |
| 43 | |
|
| 44 | 15 | return config; |
| 45 | |
} |
| 46 | |
|
| 47 | |
@Override |
| 48 | |
public ConfigFile getConfigFile(String namespace, ConfigFileFormat configFileFormat) { |
| 49 | 4 | String namespaceFileName = String.format("%s.%s", namespace, configFileFormat.getValue()); |
| 50 | 4 | ConfigFile configFile = m_configFiles.get(namespaceFileName); |
| 51 | |
|
| 52 | 4 | if (configFile == null) { |
| 53 | 3 | synchronized (this) { |
| 54 | 3 | configFile = m_configFiles.get(namespaceFileName); |
| 55 | |
|
| 56 | 3 | if (configFile == null) { |
| 57 | 3 | ConfigFactory factory = m_factoryManager.getFactory(namespaceFileName); |
| 58 | |
|
| 59 | 3 | configFile = factory.createConfigFile(namespaceFileName, configFileFormat); |
| 60 | 3 | m_configFiles.put(namespaceFileName, configFile); |
| 61 | |
} |
| 62 | 3 | } |
| 63 | |
} |
| 64 | |
|
| 65 | 4 | return configFile; |
| 66 | |
} |
| 67 | |
} |