Commit b271bebe by Yiming Liu

Merge pull request #55 from nobodyiam/refactor

refactor
parents 7692e147 5efb7c4e
......@@ -27,4 +27,5 @@ target
dev
fat
uat
prd
\ No newline at end of file
prd
apollo-env.properties
package com.ctrip.apollo.adminservice.controller;
import com.ctrip.apollo.biz.service.AdminConfigService;
import com.ctrip.apollo.biz.service.AdminReleaseService;
import com.ctrip.apollo.core.dto.ConfigItemDTO;
import com.ctrip.apollo.core.dto.ReleaseSnapshotDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
......@@ -11,19 +13,18 @@ import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import javax.annotation.Resource;
@RestController
@RequestMapping("/configs")
public class ConfigController {
@Resource(name = "adminConfigService")
@Autowired
private AdminConfigService adminConfigService;
@Autowired
private AdminReleaseService adminReleaseService;
@RequestMapping("/release/{releaseId}")
public List<ReleaseSnapshotDTO> getRelaseSnapshot(@PathVariable long releaseId) {
return adminConfigService.findReleaseSnapshotByReleaseId(releaseId);
return adminReleaseService.findReleaseSnapshotByReleaseId(releaseId);
}
@RequestMapping("/latest")
......
package com.ctrip.apollo.adminservice.controller;
import com.ctrip.apollo.biz.service.AdminConfigService;
import com.ctrip.apollo.biz.service.AdminReleaseService;
import com.ctrip.apollo.core.dto.VersionDTO;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -15,18 +15,15 @@ import java.util.List;
public class VersionController {
@Autowired
private AdminConfigService adminConfigService;
private AdminReleaseService adminReleaseService;
@RequestMapping("/app/{appId}")
public List<VersionDTO> versions(@PathVariable String appId) {
return adminConfigService.findVersionsByApp(appId);
return adminReleaseService.findVersionsByApp(appId);
}
@RequestMapping("/{versionId}")
public VersionDTO version(@PathVariable long versionId) {
return adminConfigService.loadVersionById(versionId);
return adminReleaseService.loadVersionById(versionId);
}
}
package com.ctrip.apollo.biz.service;
import com.google.common.base.Strings;
import com.ctrip.apollo.biz.entity.Cluster;
import com.ctrip.apollo.biz.entity.ConfigItem;
import com.ctrip.apollo.biz.repository.ClusterRepository;
import com.ctrip.apollo.biz.repository.ConfigItemRepository;
import com.ctrip.apollo.biz.utils.ApolloBeanUtils;
import com.ctrip.apollo.core.dto.ClusterDTO;
import com.ctrip.apollo.core.dto.ConfigItemDTO;
import com.ctrip.apollo.core.dto.ReleaseSnapshotDTO;
import com.ctrip.apollo.core.dto.VersionDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Collections;
import java.util.List;
/**
* config service for admin
*/
public interface AdminConfigService {
List<ReleaseSnapshotDTO> findReleaseSnapshotByReleaseId(long releaseId);
List<VersionDTO> findVersionsByApp(String appId);
VersionDTO loadVersionById(long versionId);
List<ClusterDTO> findClustersByApp(String appId);
List<ConfigItemDTO> findConfigItemsByClusters(List<Long> clusterIds);
@Service("adminConfigService")
public class AdminConfigService {
@Autowired
private ClusterRepository clusterRepository;
@Autowired
private ConfigItemRepository configItemRepository;
public List<ClusterDTO> findClustersByApp(String appId) {
if (Strings.isNullOrEmpty(appId)) {
return Collections.EMPTY_LIST;
}
List<Cluster> clusters = clusterRepository.findByAppId(appId);
if (clusters == null || clusters.size() == 0) {
return Collections.EMPTY_LIST;
}
return ApolloBeanUtils.batchTransform(ClusterDTO.class, clusters);
}
public List<ConfigItemDTO> findConfigItemsByClusters(List<Long> clusterIds) {
if (clusterIds == null || clusterIds.size() == 0) {
return Collections.EMPTY_LIST;
}
List<ConfigItem> configItems = configItemRepository.findByClusterIdIsIn(clusterIds);
if (configItems == null || configItems.size() == 0) {
return Collections.EMPTY_LIST;
}
return ApolloBeanUtils.batchTransform(ConfigItemDTO.class, configItems);
}
}
package com.ctrip.apollo.biz.service.impl;
package com.ctrip.apollo.biz.service;
import com.google.common.base.Strings;
import com.ctrip.apollo.biz.entity.Cluster;
import com.ctrip.apollo.biz.entity.ConfigItem;
import com.ctrip.apollo.biz.entity.ReleaseSnapshot;
import com.ctrip.apollo.biz.entity.Version;
import com.ctrip.apollo.biz.repository.ClusterRepository;
import com.ctrip.apollo.biz.repository.ConfigItemRepository;
import com.ctrip.apollo.biz.repository.ReleaseSnapShotRepository;
import com.ctrip.apollo.biz.repository.VersionRepository;
import com.ctrip.apollo.biz.service.AdminConfigService;
import com.ctrip.apollo.biz.utils.ApolloBeanUtils;
import com.ctrip.apollo.core.dto.ClusterDTO;
import com.ctrip.apollo.core.dto.ConfigItemDTO;
import com.ctrip.apollo.core.dto.ReleaseSnapshotDTO;
import com.ctrip.apollo.core.dto.VersionDTO;
......@@ -23,19 +16,16 @@ import org.springframework.stereotype.Service;
import java.util.Collections;
import java.util.List;
@Service("adminConfigService")
public class AdminConfigServiceImpl implements AdminConfigService {
@Autowired
private VersionRepository versionRepository;
/**
* @author Jason Song(song_s@ctrip.com)
*/
@Service("adminReleaseService")
public class AdminReleaseService {
@Autowired
private ReleaseSnapShotRepository releaseSnapShotRepository;
@Autowired
private ClusterRepository clusterRepository;
@Autowired
private ConfigItemRepository configItemRepository;
private VersionRepository versionRepository;
@Override
public List<ReleaseSnapshotDTO> findReleaseSnapshotByReleaseId(long releaseId) {
if (releaseId <= 0) {
return Collections.EMPTY_LIST;
......@@ -50,8 +40,6 @@ public class AdminConfigServiceImpl implements AdminConfigService {
return ApolloBeanUtils.batchTransform(ReleaseSnapshotDTO.class, releaseSnapShots);
}
@Override
public List<VersionDTO> findVersionsByApp(String appId) {
if (Strings.isNullOrEmpty(appId)) {
return Collections.EMPTY_LIST;
......@@ -65,43 +53,15 @@ public class AdminConfigServiceImpl implements AdminConfigService {
return ApolloBeanUtils.batchTransform(VersionDTO.class, versions);
}
@Override
public VersionDTO loadVersionById(long versionId) {
if (versionId <= 0) {
return null;
}
Version version = versionRepository.findById(versionId);
if (version == null){
if (version == null) {
return null;
}
VersionDTO dto = ApolloBeanUtils.transfrom(VersionDTO.class, version);
return dto;
}
@Override
public List<ClusterDTO> findClustersByApp(String appId) {
if (Strings.isNullOrEmpty(appId)) {
return Collections.EMPTY_LIST;
}
List<Cluster> clusters = clusterRepository.findByAppId(appId);
if (clusters == null || clusters.size() == 0) {
return Collections.EMPTY_LIST;
}
return ApolloBeanUtils.batchTransform(ClusterDTO.class, clusters);
}
@Override
public List<ConfigItemDTO> findConfigItemsByClusters(List<Long> clusterIds) {
if (clusterIds == null || clusterIds.size() == 0) {
return Collections.EMPTY_LIST;
}
List<ConfigItem> configItems = configItemRepository.findByClusterIdIsIn(clusterIds);
if (configItems == null || configItems.size() == 0) {
return Collections.EMPTY_LIST;
}
return ApolloBeanUtils.batchTransform(ConfigItemDTO.class, configItems);
}
}
package com.ctrip.apollo.biz.service;
import com.google.common.collect.Maps;
import com.ctrip.apollo.biz.entity.ReleaseSnapshot;
import com.ctrip.apollo.biz.entity.Version;
import com.ctrip.apollo.biz.repository.ReleaseSnapShotRepository;
import com.ctrip.apollo.biz.repository.VersionRepository;
import com.ctrip.apollo.core.dto.ApolloConfig;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.Map;
/**
* Config Service
*
* @author Jason Song(song_s@ctrip.com)
*/
public interface ConfigService {
@Service("configService")
public class ConfigService {
@Autowired
private VersionRepository versionRepository;
@Autowired
private ReleaseSnapShotRepository releaseSnapShotRepository;
@Autowired
private ObjectMapper objectMapper;
private TypeReference<Map<String, Object>> configurationTypeReference =
new TypeReference<Map<String, Object>>() {
};
/**
* Load configuration from database
*/
ApolloConfig loadConfig(String appId, String clusterName, String versionName);
public ApolloConfig loadConfig(String appId, String clusterName, String versionName) {
Version version = loadVersionByAppIdAndVersionName(appId, versionName);
if (version == null) {
return null;
}
return loadConfigByVersionAndClusterName(version, clusterName);
}
/**
* Load Version by appId and versionName from database
*/
Version loadVersionByAppIdAndVersionName(String appId, String versionName);
public Version loadVersionByAppIdAndVersionName(String appId, String versionName) {
return versionRepository.findByAppIdAndName(appId, versionName);
}
/**
* Load Config by version and clusterName from database
*/
ApolloConfig loadConfigByVersionAndClusterName(Version version, String clusterName);
public ApolloConfig loadConfigByVersionAndClusterName(Version version, String clusterName) {
ReleaseSnapshot releaseSnapShot =
releaseSnapShotRepository
.findByReleaseIdAndClusterName(version.getReleaseId(), clusterName);
if (releaseSnapShot == null) {
return null;
}
return assembleConfig(version, releaseSnapShot);
}
private ApolloConfig assembleConfig(Version version, ReleaseSnapshot releaseSnapShot) {
ApolloConfig config =
new ApolloConfig(version.getAppId(), releaseSnapShot.getClusterName(), version.getName(),
version.getReleaseId());
config.setConfigurations(transformConfigurationToMap(releaseSnapShot.getConfigurations()));
return config;
}
Map<String, Object> transformConfigurationToMap(String configurations) {
try {
return objectMapper.readValue(configurations, configurationTypeReference);
} catch (IOException e) {
e.printStackTrace();
return Maps.newHashMap();
}
}
}
package com.ctrip.apollo.biz.service.impl;
import com.google.common.collect.Maps;
import com.ctrip.apollo.biz.entity.ReleaseSnapshot;
import com.ctrip.apollo.biz.entity.Version;
import com.ctrip.apollo.biz.repository.ReleaseSnapShotRepository;
import com.ctrip.apollo.biz.repository.VersionRepository;
import com.ctrip.apollo.biz.service.ConfigService;
import com.ctrip.apollo.core.dto.ApolloConfig;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.Map;
/**
* @author Jason Song(song_s@ctrip.com)
*/
@Service("configService")
public class ConfigServiceImpl implements ConfigService {
@Autowired
private VersionRepository versionRepository;
@Autowired
private ReleaseSnapShotRepository releaseSnapShotRepository;
@Autowired
private ObjectMapper objectMapper;
private TypeReference<Map<String, Object>> configurationTypeReference =
new TypeReference<Map<String, Object>>() {
};
@Override
public ApolloConfig loadConfig(String appId, String clusterName, String versionName) {
Version version = loadVersionByAppIdAndVersionName(appId, versionName);
if (version == null) {
return null;
}
return loadConfigByVersionAndClusterName(version, clusterName);
}
@Override
public Version loadVersionByAppIdAndVersionName(String appId, String versionName) {
return versionRepository.findByAppIdAndName(appId, versionName);
}
@Override
public ApolloConfig loadConfigByVersionAndClusterName(Version version, String clusterName) {
ReleaseSnapshot releaseSnapShot =
releaseSnapShotRepository
.findByReleaseIdAndClusterName(version.getReleaseId(), clusterName);
if (releaseSnapShot == null) {
return null;
}
return assembleConfig(version, releaseSnapShot);
}
private ApolloConfig assembleConfig(Version version, ReleaseSnapshot releaseSnapShot) {
ApolloConfig config =
new ApolloConfig(version.getAppId(), releaseSnapShot.getClusterName(), version.getName(),
version.getReleaseId());
config.setConfigurations(transformConfigurationToMap(releaseSnapShot.getConfigurations()));
return config;
}
Map<String, Object> transformConfigurationToMap(String configurations) {
try {
return objectMapper.readValue(configurations, configurationTypeReference);
} catch (IOException e) {
e.printStackTrace();
return Maps.newHashMap();
}
}
}
package com.ctrip.apollo.biz;
import com.ctrip.apollo.biz.service.impl.ConfigServiceImplTest;
import com.ctrip.apollo.biz.service.ConfigServiceTest;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
......@@ -8,7 +8,7 @@ import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class)
@SuiteClasses({
ConfigServiceImplTest.class})
ConfigServiceTest.class})
public class AllTests {
}
package com.ctrip.apollo.biz.service.impl;
package com.ctrip.apollo.biz.service;
import com.google.common.collect.Maps;
......@@ -6,6 +6,7 @@ import com.ctrip.apollo.biz.entity.ReleaseSnapshot;
import com.ctrip.apollo.biz.entity.Version;
import com.ctrip.apollo.biz.repository.ReleaseSnapShotRepository;
import com.ctrip.apollo.biz.repository.VersionRepository;
import com.ctrip.apollo.biz.service.ConfigService;
import com.ctrip.apollo.core.dto.ApolloConfig;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
......@@ -33,18 +34,18 @@ import static org.mockito.Mockito.when;
* @author Jason Song(song_s@ctrip.com)
*/
@RunWith(MockitoJUnitRunner.class)
public class ConfigServiceImplTest {
public class ConfigServiceTest {
@Mock
private VersionRepository versionRepository;
@Mock
private ReleaseSnapShotRepository releaseSnapShotRepository;
@Mock
private ObjectMapper objectMapper;
private ConfigServiceImpl configService;
private ConfigService configService;
@Before
public void setUp() throws Exception {
configService = new ConfigServiceImpl();
configService = new ConfigService();
ReflectionTestUtils.setField(configService, "versionRepository", versionRepository);
ReflectionTestUtils
.setField(configService, "releaseSnapShotRepository", releaseSnapShotRepository);
......
......@@ -68,7 +68,6 @@ public class ApolloConfigManager
applicationContext.getClass().getName()));
}
this.applicationContext = (ConfigurableApplicationContext) applicationContext;
this.configUtil.setApplicationContext(applicationContext);
}
/**
......
......@@ -6,13 +6,13 @@ import com.google.common.collect.FluentIterable;
import com.ctrip.apollo.client.constants.Constants;
import com.ctrip.apollo.client.model.ApolloRegistry;
import com.ctrip.apollo.core.utils.ClassLoaderUtil;
import org.springframework.context.ApplicationContext;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Collections;
import java.util.List;
......@@ -23,13 +23,13 @@ import java.util.concurrent.TimeUnit;
* @author Jason Song(song_s@ctrip.com)
*/
public class ConfigUtil {
private static final Logger logger = LoggerFactory.getLogger(ConfigUtil.class);
public static final String APOLLO_PROPERTY = "apollo.properties";
//TODO read from config?
private static final int refreshInterval = 5;
private static final TimeUnit refreshIntervalTimeUnit = TimeUnit.MINUTES;
private static ConfigUtil configUtil = new ConfigUtil();
private ApplicationContext applicationContext;
private ConfigUtil() {
}
......@@ -43,10 +43,6 @@ public class ConfigUtil {
return "default";
}
public void setApplicationContext(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
public int getRefreshInterval() {
return refreshInterval;
}
......@@ -77,14 +73,13 @@ public class ConfigUtil {
}
Properties loadPropertiesFromResourceURL(URL resourceUrl) {
Resource resource = applicationContext.getResource(resourceUrl.toExternalForm());
if (resource == null || !resource.exists()) {
return null;
}
try {
return PropertiesLoaderUtils.loadProperties(new EncodedResource(resource, "UTF-8"));
InputStream inputStream = resourceUrl.openStream();
Properties prop = new Properties();
prop.load(inputStream);
return prop;
} catch (IOException ex) {
ex.printStackTrace();
logger.error("Load properties from {} failed", resourceUrl.toExternalForm(), ex);
}
return null;
}
......
......@@ -39,6 +39,7 @@ public class RemoteConfigLoaderTest {
private RemoteConfigLoader remoteConfigLoader;
@Mock
private RestTemplate restTemplate;
@Mock
private ConfigUtil configUtil;
@Mock
private ConfigServiceLocator serviceLocater;
......@@ -47,7 +48,6 @@ public class RemoteConfigLoaderTest {
@Before
public void setUp() {
configUtil = spy(ConfigUtil.getInstance());
remoteConfigLoader = spy(new RemoteConfigLoader(restTemplate, configUtil, serviceLocater));
}
......
......@@ -4,13 +4,13 @@ import com.google.common.collect.Lists;
import com.ctrip.apollo.client.constants.Constants;
import com.ctrip.apollo.client.model.ApolloRegistry;
import com.ctrip.apollo.core.utils.ClassLoaderUtil;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.test.util.ReflectionTestUtils;
import java.io.IOException;
import java.net.URL;
......@@ -34,14 +34,10 @@ import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public class ConfigUtilTest {
private ConfigUtil configUtil;
@Mock
private ConfigurableApplicationContext applicationContext;
@Before
public void setUp() throws Exception {
configUtil = spy(ConfigUtil.getInstance());
configUtil.setApplicationContext(applicationContext);
}
@Test
......@@ -75,7 +71,7 @@ public class ConfigUtilTest {
private void preparePropertiesFromLocalResource(Properties someProperties) throws IOException {
ClassLoader someClassLoader = mock(ClassLoader.class);
Thread.currentThread().setContextClassLoader(someClassLoader);
ReflectionTestUtils.setField(ClassLoaderUtil.class, "loader", someClassLoader);
URL someUrl = new URL("http", "somepath/", "someFile");
Enumeration<URL> someResourceUrls = Collections.enumeration(Lists.newArrayList(someUrl));
......
......@@ -4,6 +4,7 @@ import com.ctrip.apollo.biz.entity.Version;
import com.ctrip.apollo.biz.service.ConfigService;
import com.ctrip.apollo.core.dto.ApolloConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
......@@ -12,7 +13,6 @@ import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
/**
......@@ -21,7 +21,7 @@ import javax.servlet.http.HttpServletResponse;
@RestController
@RequestMapping("/config")
public class ConfigController {
@Resource(name = "configService")
@Autowired
private ConfigService configService;
@RequestMapping(value = "/{appId}/{clusterName}/{versionName:.*}", method = RequestMethod.GET)
......
package com.ctrip.apollo.client.util;
package com.ctrip.apollo.core.utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......
......@@ -18,7 +18,7 @@ public class ResourceUtils {
@SuppressWarnings("unchecked")
public static Properties readConfigFile(String configPath, Properties defaults) {
InputStream in = ClassLoader.getSystemResourceAsStream(configPath);
InputStream in = ClassLoaderUtil.getLoader().getResourceAsStream(configPath);
logger.info("Reading config from resource {}", configPath);
Properties props = new Properties();
try {
......
local.meta=http://localhost:8080
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment