Commit 5efb7c4e by Jason Song

refactor

parent 7692e147
...@@ -28,3 +28,4 @@ dev ...@@ -28,3 +28,4 @@ dev
fat fat
uat uat
prd prd
apollo-env.properties
package com.ctrip.apollo.adminservice.controller; package com.ctrip.apollo.adminservice.controller;
import com.ctrip.apollo.biz.service.AdminConfigService; 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.ConfigItemDTO;
import com.ctrip.apollo.core.dto.ReleaseSnapshotDTO; 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.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
...@@ -11,19 +13,18 @@ import org.springframework.web.bind.annotation.RestController; ...@@ -11,19 +13,18 @@ import org.springframework.web.bind.annotation.RestController;
import java.util.List; import java.util.List;
import javax.annotation.Resource;
@RestController @RestController
@RequestMapping("/configs") @RequestMapping("/configs")
public class ConfigController { public class ConfigController {
@Resource(name = "adminConfigService") @Autowired
private AdminConfigService adminConfigService; private AdminConfigService adminConfigService;
@Autowired
private AdminReleaseService adminReleaseService;
@RequestMapping("/release/{releaseId}") @RequestMapping("/release/{releaseId}")
public List<ReleaseSnapshotDTO> getRelaseSnapshot(@PathVariable long releaseId) { public List<ReleaseSnapshotDTO> getRelaseSnapshot(@PathVariable long releaseId) {
return adminConfigService.findReleaseSnapshotByReleaseId(releaseId); return adminReleaseService.findReleaseSnapshotByReleaseId(releaseId);
} }
@RequestMapping("/latest") @RequestMapping("/latest")
......
package com.ctrip.apollo.adminservice.controller; 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 com.ctrip.apollo.core.dto.VersionDTO;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -15,18 +15,15 @@ import java.util.List; ...@@ -15,18 +15,15 @@ import java.util.List;
public class VersionController { public class VersionController {
@Autowired @Autowired
private AdminConfigService adminConfigService; private AdminReleaseService adminReleaseService;
@RequestMapping("/app/{appId}") @RequestMapping("/app/{appId}")
public List<VersionDTO> versions(@PathVariable String appId) { public List<VersionDTO> versions(@PathVariable String appId) {
return adminReleaseService.findVersionsByApp(appId);
return adminConfigService.findVersionsByApp(appId);
} }
@RequestMapping("/{versionId}") @RequestMapping("/{versionId}")
public VersionDTO version(@PathVariable long versionId) { public VersionDTO version(@PathVariable long versionId) {
return adminConfigService.loadVersionById(versionId); return adminReleaseService.loadVersionById(versionId);
} }
} }
package com.ctrip.apollo.biz.service; 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.ClusterDTO;
import com.ctrip.apollo.core.dto.ConfigItemDTO; 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; import java.util.List;
/** /**
* config service for admin * config service for admin
*/ */
public interface AdminConfigService { @Service("adminConfigService")
public class AdminConfigService {
List<ReleaseSnapshotDTO> findReleaseSnapshotByReleaseId(long releaseId);
@Autowired
List<VersionDTO> findVersionsByApp(String appId); private ClusterRepository clusterRepository;
@Autowired
VersionDTO loadVersionById(long versionId); private ConfigItemRepository configItemRepository;
List<ClusterDTO> findClustersByApp(String appId);
public List<ClusterDTO> findClustersByApp(String appId) {
List<ConfigItemDTO> findConfigItemsByClusters(List<Long> clusterIds); 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.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.ReleaseSnapshot;
import com.ctrip.apollo.biz.entity.Version; 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.ReleaseSnapShotRepository;
import com.ctrip.apollo.biz.repository.VersionRepository; 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.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.ReleaseSnapshotDTO;
import com.ctrip.apollo.core.dto.VersionDTO; import com.ctrip.apollo.core.dto.VersionDTO;
...@@ -23,19 +16,16 @@ import org.springframework.stereotype.Service; ...@@ -23,19 +16,16 @@ import org.springframework.stereotype.Service;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@Service("adminConfigService") /**
public class AdminConfigServiceImpl implements AdminConfigService { * @author Jason Song(song_s@ctrip.com)
*/
@Autowired @Service("adminReleaseService")
private VersionRepository versionRepository; public class AdminReleaseService {
@Autowired @Autowired
private ReleaseSnapShotRepository releaseSnapShotRepository; private ReleaseSnapShotRepository releaseSnapShotRepository;
@Autowired @Autowired
private ClusterRepository clusterRepository; private VersionRepository versionRepository;
@Autowired
private ConfigItemRepository configItemRepository;
@Override
public List<ReleaseSnapshotDTO> findReleaseSnapshotByReleaseId(long releaseId) { public List<ReleaseSnapshotDTO> findReleaseSnapshotByReleaseId(long releaseId) {
if (releaseId <= 0) { if (releaseId <= 0) {
return Collections.EMPTY_LIST; return Collections.EMPTY_LIST;
...@@ -50,8 +40,6 @@ public class AdminConfigServiceImpl implements AdminConfigService { ...@@ -50,8 +40,6 @@ public class AdminConfigServiceImpl implements AdminConfigService {
return ApolloBeanUtils.batchTransform(ReleaseSnapshotDTO.class, releaseSnapShots); return ApolloBeanUtils.batchTransform(ReleaseSnapshotDTO.class, releaseSnapShots);
} }
@Override
public List<VersionDTO> findVersionsByApp(String appId) { public List<VersionDTO> findVersionsByApp(String appId) {
if (Strings.isNullOrEmpty(appId)) { if (Strings.isNullOrEmpty(appId)) {
return Collections.EMPTY_LIST; return Collections.EMPTY_LIST;
...@@ -65,43 +53,15 @@ public class AdminConfigServiceImpl implements AdminConfigService { ...@@ -65,43 +53,15 @@ public class AdminConfigServiceImpl implements AdminConfigService {
return ApolloBeanUtils.batchTransform(VersionDTO.class, versions); return ApolloBeanUtils.batchTransform(VersionDTO.class, versions);
} }
@Override
public VersionDTO loadVersionById(long versionId) { public VersionDTO loadVersionById(long versionId) {
if (versionId <= 0) { if (versionId <= 0) {
return null; return null;
} }
Version version = versionRepository.findById(versionId); Version version = versionRepository.findById(versionId);
if (version == null){ if (version == null) {
return null; return null;
} }
VersionDTO dto = ApolloBeanUtils.transfrom(VersionDTO.class, version); VersionDTO dto = ApolloBeanUtils.transfrom(VersionDTO.class, version);
return dto; 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; 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.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.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 * Config Service
* *
* @author Jason Song(song_s@ctrip.com) * @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 * 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 * 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 * 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; 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.runner.RunWith;
import org.junit.runners.Suite; import org.junit.runners.Suite;
...@@ -8,7 +8,7 @@ import org.junit.runners.Suite.SuiteClasses; ...@@ -8,7 +8,7 @@ import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class) @RunWith(Suite.class)
@SuiteClasses({ @SuiteClasses({
ConfigServiceImplTest.class}) ConfigServiceTest.class})
public class AllTests { public class AllTests {
} }
package com.ctrip.apollo.biz.service.impl; package com.ctrip.apollo.biz.service;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
...@@ -6,6 +6,7 @@ import com.ctrip.apollo.biz.entity.ReleaseSnapshot; ...@@ -6,6 +6,7 @@ import com.ctrip.apollo.biz.entity.ReleaseSnapshot;
import com.ctrip.apollo.biz.entity.Version; import com.ctrip.apollo.biz.entity.Version;
import com.ctrip.apollo.biz.repository.ReleaseSnapShotRepository; import com.ctrip.apollo.biz.repository.ReleaseSnapShotRepository;
import com.ctrip.apollo.biz.repository.VersionRepository; import com.ctrip.apollo.biz.repository.VersionRepository;
import com.ctrip.apollo.biz.service.ConfigService;
import com.ctrip.apollo.core.dto.ApolloConfig; import com.ctrip.apollo.core.dto.ApolloConfig;
import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
...@@ -33,18 +34,18 @@ import static org.mockito.Mockito.when; ...@@ -33,18 +34,18 @@ import static org.mockito.Mockito.when;
* @author Jason Song(song_s@ctrip.com) * @author Jason Song(song_s@ctrip.com)
*/ */
@RunWith(MockitoJUnitRunner.class) @RunWith(MockitoJUnitRunner.class)
public class ConfigServiceImplTest { public class ConfigServiceTest {
@Mock @Mock
private VersionRepository versionRepository; private VersionRepository versionRepository;
@Mock @Mock
private ReleaseSnapShotRepository releaseSnapShotRepository; private ReleaseSnapShotRepository releaseSnapShotRepository;
@Mock @Mock
private ObjectMapper objectMapper; private ObjectMapper objectMapper;
private ConfigServiceImpl configService; private ConfigService configService;
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
configService = new ConfigServiceImpl(); configService = new ConfigService();
ReflectionTestUtils.setField(configService, "versionRepository", versionRepository); ReflectionTestUtils.setField(configService, "versionRepository", versionRepository);
ReflectionTestUtils ReflectionTestUtils
.setField(configService, "releaseSnapShotRepository", releaseSnapShotRepository); .setField(configService, "releaseSnapShotRepository", releaseSnapShotRepository);
......
...@@ -68,7 +68,6 @@ public class ApolloConfigManager ...@@ -68,7 +68,6 @@ public class ApolloConfigManager
applicationContext.getClass().getName())); applicationContext.getClass().getName()));
} }
this.applicationContext = (ConfigurableApplicationContext) applicationContext; this.applicationContext = (ConfigurableApplicationContext) applicationContext;
this.configUtil.setApplicationContext(applicationContext);
} }
/** /**
......
...@@ -6,13 +6,13 @@ import com.google.common.collect.FluentIterable; ...@@ -6,13 +6,13 @@ import com.google.common.collect.FluentIterable;
import com.ctrip.apollo.client.constants.Constants; import com.ctrip.apollo.client.constants.Constants;
import com.ctrip.apollo.client.model.ApolloRegistry; import com.ctrip.apollo.client.model.ApolloRegistry;
import com.ctrip.apollo.core.utils.ClassLoaderUtil;
import org.springframework.context.ApplicationContext; import org.slf4j.Logger;
import org.springframework.core.io.Resource; import org.slf4j.LoggerFactory;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.net.URL; import java.net.URL;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
...@@ -23,13 +23,13 @@ import java.util.concurrent.TimeUnit; ...@@ -23,13 +23,13 @@ import java.util.concurrent.TimeUnit;
* @author Jason Song(song_s@ctrip.com) * @author Jason Song(song_s@ctrip.com)
*/ */
public class ConfigUtil { public class ConfigUtil {
private static final Logger logger = LoggerFactory.getLogger(ConfigUtil.class);
public static final String APOLLO_PROPERTY = "apollo.properties"; public static final String APOLLO_PROPERTY = "apollo.properties";
//TODO read from config? //TODO read from config?
private static final int refreshInterval = 5; private static final int refreshInterval = 5;
private static final TimeUnit refreshIntervalTimeUnit = TimeUnit.MINUTES; private static final TimeUnit refreshIntervalTimeUnit = TimeUnit.MINUTES;
private static ConfigUtil configUtil = new ConfigUtil(); private static ConfigUtil configUtil = new ConfigUtil();
private ApplicationContext applicationContext;
private ConfigUtil() { private ConfigUtil() {
} }
...@@ -43,10 +43,6 @@ public class ConfigUtil { ...@@ -43,10 +43,6 @@ public class ConfigUtil {
return "default"; return "default";
} }
public void setApplicationContext(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
public int getRefreshInterval() { public int getRefreshInterval() {
return refreshInterval; return refreshInterval;
} }
...@@ -77,14 +73,13 @@ public class ConfigUtil { ...@@ -77,14 +73,13 @@ public class ConfigUtil {
} }
Properties loadPropertiesFromResourceURL(URL resourceUrl) { Properties loadPropertiesFromResourceURL(URL resourceUrl) {
Resource resource = applicationContext.getResource(resourceUrl.toExternalForm());
if (resource == null || !resource.exists()) {
return null;
}
try { 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) { } catch (IOException ex) {
ex.printStackTrace(); logger.error("Load properties from {} failed", resourceUrl.toExternalForm(), ex);
} }
return null; return null;
} }
......
...@@ -39,6 +39,7 @@ public class RemoteConfigLoaderTest { ...@@ -39,6 +39,7 @@ public class RemoteConfigLoaderTest {
private RemoteConfigLoader remoteConfigLoader; private RemoteConfigLoader remoteConfigLoader;
@Mock @Mock
private RestTemplate restTemplate; private RestTemplate restTemplate;
@Mock
private ConfigUtil configUtil; private ConfigUtil configUtil;
@Mock @Mock
private ConfigServiceLocator serviceLocater; private ConfigServiceLocator serviceLocater;
...@@ -47,7 +48,6 @@ public class RemoteConfigLoaderTest { ...@@ -47,7 +48,6 @@ public class RemoteConfigLoaderTest {
@Before @Before
public void setUp() { public void setUp() {
configUtil = spy(ConfigUtil.getInstance());
remoteConfigLoader = spy(new RemoteConfigLoader(restTemplate, configUtil, serviceLocater)); remoteConfigLoader = spy(new RemoteConfigLoader(restTemplate, configUtil, serviceLocater));
} }
......
...@@ -4,13 +4,13 @@ import com.google.common.collect.Lists; ...@@ -4,13 +4,13 @@ import com.google.common.collect.Lists;
import com.ctrip.apollo.client.constants.Constants; import com.ctrip.apollo.client.constants.Constants;
import com.ctrip.apollo.client.model.ApolloRegistry; import com.ctrip.apollo.client.model.ApolloRegistry;
import com.ctrip.apollo.core.utils.ClassLoaderUtil;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner; import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.test.util.ReflectionTestUtils;
import java.io.IOException; import java.io.IOException;
import java.net.URL; import java.net.URL;
...@@ -34,14 +34,10 @@ import static org.mockito.Mockito.when; ...@@ -34,14 +34,10 @@ import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class) @RunWith(MockitoJUnitRunner.class)
public class ConfigUtilTest { public class ConfigUtilTest {
private ConfigUtil configUtil; private ConfigUtil configUtil;
@Mock
private ConfigurableApplicationContext applicationContext;
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
configUtil = spy(ConfigUtil.getInstance()); configUtil = spy(ConfigUtil.getInstance());
configUtil.setApplicationContext(applicationContext);
} }
@Test @Test
...@@ -75,7 +71,7 @@ public class ConfigUtilTest { ...@@ -75,7 +71,7 @@ public class ConfigUtilTest {
private void preparePropertiesFromLocalResource(Properties someProperties) throws IOException { private void preparePropertiesFromLocalResource(Properties someProperties) throws IOException {
ClassLoader someClassLoader = mock(ClassLoader.class); ClassLoader someClassLoader = mock(ClassLoader.class);
Thread.currentThread().setContextClassLoader(someClassLoader); ReflectionTestUtils.setField(ClassLoaderUtil.class, "loader", someClassLoader);
URL someUrl = new URL("http", "somepath/", "someFile"); URL someUrl = new URL("http", "somepath/", "someFile");
Enumeration<URL> someResourceUrls = Collections.enumeration(Lists.newArrayList(someUrl)); Enumeration<URL> someResourceUrls = Collections.enumeration(Lists.newArrayList(someUrl));
......
...@@ -4,6 +4,7 @@ import com.ctrip.apollo.biz.entity.Version; ...@@ -4,6 +4,7 @@ import com.ctrip.apollo.biz.entity.Version;
import com.ctrip.apollo.biz.service.ConfigService; import com.ctrip.apollo.biz.service.ConfigService;
import com.ctrip.apollo.core.dto.ApolloConfig; 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.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
...@@ -12,7 +13,6 @@ import org.springframework.web.bind.annotation.RestController; ...@@ -12,7 +13,6 @@ import org.springframework.web.bind.annotation.RestController;
import java.io.IOException; import java.io.IOException;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
/** /**
...@@ -21,7 +21,7 @@ import javax.servlet.http.HttpServletResponse; ...@@ -21,7 +21,7 @@ import javax.servlet.http.HttpServletResponse;
@RestController @RestController
@RequestMapping("/config") @RequestMapping("/config")
public class ConfigController { public class ConfigController {
@Resource(name = "configService") @Autowired
private ConfigService configService; private ConfigService configService;
@RequestMapping(value = "/{appId}/{clusterName}/{versionName:.*}", method = RequestMethod.GET) @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.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
......
...@@ -18,7 +18,7 @@ public class ResourceUtils { ...@@ -18,7 +18,7 @@ public class ResourceUtils {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static Properties readConfigFile(String configPath, Properties defaults) { 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); logger.info("Reading config from resource {}", configPath);
Properties props = new Properties(); Properties props = new Properties();
try { 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