| 1 | |
package com.ctrip.apollo.spi; |
| 2 | |
|
| 3 | |
import com.ctrip.apollo.Config; |
| 4 | |
import com.ctrip.apollo.core.utils.ClassLoaderUtil; |
| 5 | |
import com.ctrip.apollo.internals.DefaultConfig; |
| 6 | |
import com.ctrip.apollo.internals.LocalFileConfigRepository; |
| 7 | |
import com.ctrip.apollo.internals.RemoteConfigRepository; |
| 8 | |
import com.ctrip.apollo.util.ExceptionUtil; |
| 9 | |
import com.dianping.cat.Cat; |
| 10 | |
import com.dianping.cat.message.Message; |
| 11 | |
import com.dianping.cat.message.Transaction; |
| 12 | |
|
| 13 | |
import org.slf4j.Logger; |
| 14 | |
import org.slf4j.LoggerFactory; |
| 15 | |
import org.unidal.lookup.annotation.Named; |
| 16 | |
|
| 17 | |
import java.io.File; |
| 18 | |
import java.io.IOException; |
| 19 | |
import java.nio.file.Files; |
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
@Named(type = ConfigFactory.class, value = "default") |
| 25 | |
public class DefaultConfigFactory implements ConfigFactory { |
| 26 | 1 | private static final Logger logger = LoggerFactory.getLogger(DefaultConfigFactory.class); |
| 27 | |
private static final String CONFIG_DIR = "/config-cache"; |
| 28 | |
private File m_baseDir; |
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | 9 | public DefaultConfigFactory() { |
| 34 | 9 | m_baseDir = new File(ClassLoaderUtil.getClassPath() + CONFIG_DIR); |
| 35 | 9 | this.checkLocalConfigCacheDir(m_baseDir); |
| 36 | 9 | } |
| 37 | |
|
| 38 | |
private void checkLocalConfigCacheDir(File baseDir) { |
| 39 | 9 | if (baseDir.exists()) { |
| 40 | 8 | return; |
| 41 | |
} |
| 42 | 1 | Transaction transaction = Cat.newTransaction("Apollo.ConfigService", "createLocalConfigDir"); |
| 43 | 1 | transaction.addData("BaseDir", baseDir.getAbsolutePath()); |
| 44 | |
try { |
| 45 | 1 | Files.createDirectory(baseDir.toPath()); |
| 46 | 1 | transaction.setStatus(Message.SUCCESS); |
| 47 | 0 | } catch (IOException ex) { |
| 48 | 0 | Cat.logError(ex); |
| 49 | 0 | transaction.setStatus(ex); |
| 50 | 0 | logger.warn( |
| 51 | |
"Unable to create local config cache directory {}, reason: {}. Will not able to cache config file.", |
| 52 | 0 | baseDir, ExceptionUtil.getDetailMessage(ex)); |
| 53 | |
} finally { |
| 54 | 1 | transaction.complete(); |
| 55 | 1 | } |
| 56 | 1 | } |
| 57 | |
|
| 58 | |
@Override |
| 59 | |
public Config create(String namespace) { |
| 60 | 9 | DefaultConfig defaultConfig = |
| 61 | 9 | new DefaultConfig(namespace, createLocalConfigRepository(namespace)); |
| 62 | 9 | return defaultConfig; |
| 63 | |
} |
| 64 | |
|
| 65 | |
LocalFileConfigRepository createLocalConfigRepository(String namespace) { |
| 66 | 8 | LocalFileConfigRepository localFileConfigRepository = |
| 67 | |
new LocalFileConfigRepository(m_baseDir, namespace); |
| 68 | 8 | localFileConfigRepository.setFallback(createRemoteConfigRepository(namespace)); |
| 69 | 8 | return localFileConfigRepository; |
| 70 | |
} |
| 71 | |
|
| 72 | |
RemoteConfigRepository createRemoteConfigRepository(String namespace) { |
| 73 | 8 | return new RemoteConfigRepository(namespace); |
| 74 | |
} |
| 75 | |
} |