Skip to content

Commit

Permalink
Use dto namespace instead of model and updated the demo page
Browse files Browse the repository at this point in the history
  • Loading branch information
nobodyiam committed Mar 22, 2016
1 parent 47478b4 commit dcdd4b0
Show file tree
Hide file tree
Showing 13 changed files with 93 additions and 46 deletions.
@@ -1,7 +1,7 @@
package com.ctrip.apollo.biz.service;

import com.ctrip.apollo.biz.entity.Version;
import com.ctrip.apollo.core.model.ApolloConfig;
import com.ctrip.apollo.core.dto.ApolloConfig;

/**
* Config Service
Expand Down
Expand Up @@ -5,7 +5,7 @@
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.model.ApolloConfig;
import com.ctrip.apollo.core.dto.ApolloConfig;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Maps;
Expand Down
Expand Up @@ -4,7 +4,7 @@
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.model.ApolloConfig;
import com.ctrip.apollo.core.dto.ApolloConfig;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Maps;
Expand Down
Expand Up @@ -19,19 +19,26 @@
import org.springframework.core.env.CompositePropertySource;
import org.springframework.core.env.MutablePropertySources;

import java.util.concurrent.atomic.AtomicReference;

/**
* Client side config manager
*
* @author Jason Song(song_s@ctrip.com)
*/
public class ApolloConfigManager implements BeanDefinitionRegistryPostProcessor, PriorityOrdered, ApplicationContextAware {
public static final String APOLLO_PROPERTY_SOURCE_NAME = "ApolloConfigProperties";
private static AtomicReference<ApolloConfigManager> singletonProtector = new AtomicReference<ApolloConfigManager>();

private ConfigLoader configLoader;

private ConfigurableApplicationContext applicationContext;

private CompositePropertySource currentPropertySource;

public ApolloConfigManager() {
if(!singletonProtector.compareAndSet(null, this)) {
throw new IllegalStateException("There should be only one ApolloConfigManager instance!");
}
this.configLoader = ConfigLoaderFactory.getInstance().getRemoteConfigLoader();
}

Expand All @@ -53,7 +60,7 @@ public void setApplicationContext(ApplicationContext applicationContext) throws
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
registerDependentBeans(registry);
preparePropertySource();
initializePropertySource();
}

/**
Expand Down Expand Up @@ -94,14 +101,19 @@ public int getOrder() {
* First try to load from remote
* If loading from remote failed, then fall back to local cached properties
*/
void preparePropertySource() {
void initializePropertySource() {
currentPropertySource = loadPropertySource();

MutablePropertySources currentPropertySources = applicationContext.getEnvironment().getPropertySources();
if (currentPropertySources.contains(APOLLO_PROPERTY_SOURCE_NAME)) {
currentPropertySources.remove(APOLLO_PROPERTY_SOURCE_NAME);
if (currentPropertySources.contains(currentPropertySource.getName())) {
currentPropertySources.remove(currentPropertySource.getName());
}
currentPropertySources.addFirst(currentPropertySource);
}

CompositePropertySource composite = new CompositePropertySource(APOLLO_PROPERTY_SOURCE_NAME);
composite.addPropertySource(configLoader.loadPropertySource());
currentPropertySources.addFirst(composite);
CompositePropertySource loadPropertySource() {
CompositePropertySource compositePropertySource = new CompositePropertySource(APOLLO_PROPERTY_SOURCE_NAME);
compositePropertySource.addPropertySource(configLoader.loadPropertySource());
return compositePropertySource;
}
}
@@ -1,15 +1,10 @@
package com.ctrip.apollo.client.loader.impl;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicLong;

import com.ctrip.apollo.client.loader.ConfigLoader;
import com.ctrip.apollo.client.model.ApolloRegistry;
import com.ctrip.apollo.client.util.ConfigUtil;
import com.ctrip.apollo.core.dto.ApolloConfig;
import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.env.CompositePropertySource;
Expand All @@ -21,11 +16,10 @@
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

import com.ctrip.apollo.client.loader.ConfigLoader;
import com.ctrip.apollo.client.model.ApolloRegistry;
import com.ctrip.apollo.client.util.ConfigUtil;
import com.ctrip.apollo.core.model.ApolloConfig;
import com.google.common.collect.Lists;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicLong;

/**
* Load config from remote config server
Expand Down
@@ -1,7 +1,9 @@
package com.ctrip.apollo.client;

import com.ctrip.apollo.client.loader.ConfigLoader;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
Expand All @@ -16,6 +18,8 @@
import org.springframework.core.env.MutablePropertySources;
import org.springframework.test.util.ReflectionTestUtils;

import java.util.concurrent.atomic.AtomicReference;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.*;
Expand Down Expand Up @@ -48,6 +52,13 @@ public void setUp() {
ReflectionTestUtils.setField(apolloConfigManager, "configLoader", configLoader);
}

@After
public void tearDown() throws Exception {
AtomicReference<ApolloConfigManager> singletonProtector =
(AtomicReference<ApolloConfigManager>)ReflectionTestUtils.getField(ApolloConfigManager.class, "singletonProtector");
singletonProtector.set(null);
}

@Test(expected = RuntimeException.class)
public void testInvalidApplicationContext() {
ApplicationContext someInvalidApplication = mock(ApplicationContext.class);
Expand All @@ -61,7 +72,7 @@ public void testPreparePropertySourceSuccessfully() {

when(configLoader.loadPropertySource()).thenReturn(somePropertySource);

apolloConfigManager.preparePropertySource();
apolloConfigManager.initializePropertySource();

verify(configLoader, times(1)).loadPropertySource();
verify(mutablePropertySources, times(1)).addFirst(captor.capture());
Expand All @@ -74,7 +85,7 @@ public void testPreparePropertySourceSuccessfully() {

@Test
public void testPostProcessBeanDefinitionRegistry() {
doNothing().when(apolloConfigManager).preparePropertySource();
doNothing().when(apolloConfigManager).initializePropertySource();

apolloConfigManager.postProcessBeanDefinitionRegistry(beanDefinitionRegistry);

Expand Down
Expand Up @@ -2,7 +2,7 @@

import com.ctrip.apollo.client.model.ApolloRegistry;
import com.ctrip.apollo.client.util.ConfigUtil;
import com.ctrip.apollo.core.model.ApolloConfig;
import com.ctrip.apollo.core.dto.ApolloConfig;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.junit.Before;
Expand Down
Expand Up @@ -2,7 +2,7 @@

import com.ctrip.apollo.biz.entity.Version;
import com.ctrip.apollo.biz.service.ConfigService;
import com.ctrip.apollo.core.model.ApolloConfig;
import com.ctrip.apollo.core.dto.ApolloConfig;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
Expand Down
Expand Up @@ -2,7 +2,7 @@

import com.ctrip.apollo.biz.entity.Version;
import com.ctrip.apollo.biz.service.ConfigService;
import com.ctrip.apollo.core.model.ApolloConfig;
import com.ctrip.apollo.core.dto.ApolloConfig;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down
@@ -1,4 +1,4 @@
package com.ctrip.apollo.core.model;
package com.ctrip.apollo.core.dto;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
Expand Down
Expand Up @@ -35,13 +35,12 @@ public class DemoController {

@RequestMapping(value = "/config/{configName:.*}", method = RequestMethod.GET)
public Config queryConfig(@PathVariable String configName) {
String value;
if (configName.equals("foo")) {
value = demoService.getFoo();
} else {
value = env.getProperty(configName, "undefined");
}
return new Config(configName, value);
return new Config(configName, env.getProperty(configName, "undefined"));
}

@RequestMapping(value = "/injected/config", method = RequestMethod.GET)
public Config queryInjectedConfig() {
return new Config("apollo.foo", demoService.getFoo());
}

@RequestMapping(value = "/client/registries", method = RequestMethod.GET)
Expand Down
11 changes: 11 additions & 0 deletions apollo-demo/src/main/webapp/s/scripts/app.js
Expand Up @@ -12,6 +12,7 @@
this.registries = {};
this.configQuery = {};
//this.refreshResult = NONE;
this.injectedConfigValue = '';

var self = this;

Expand All @@ -35,6 +36,16 @@
});
};

this.queryInjectedConfig = function () {
$http.get("demo/injected/config")
.success(function(data) {
self.injectedConfigValue = data.value;
})
.error(function(data, status) {
toastr.error((data && data.msg) || 'Load injected config failed');
});
};

//this.refreshConfig = function() {
// $http.post("refresh")
// .success(function(data) {
Expand Down
34 changes: 27 additions & 7 deletions apollo-demo/src/main/webapp/s/templates/list.html
Expand Up @@ -14,14 +14,15 @@ <h3>Current Client Side Registries:</h3>
<tr ng-repeat="item in demoCtrl.registries">
<td>{{item.appId}}</td>
<td>{{item.version}}</td>
</tr.>
</tr>
</tbody>
</table>
</div>

<div id="load-config-wrapper">
<h3>Load Config:</h3>
<form name="loadConfigForm" class="form-horizontal" novalidate ng-submit="demoCtrl.queryConfig()">
<form name="loadConfigForm" class="form-horizontal" novalidate
ng-submit="demoCtrl.queryConfig()">
<div class="form-group"
ng-class="{ 'has-error' : loadConfigForm.configName.$invalid && loadConfigForm.configName.$dirty}">
<div id="input-config-wrapper" class="clearfix">
Expand Down Expand Up @@ -50,12 +51,31 @@ <h3>Load Config:</h3>
</form>
</div>


<div>
<h3>Load Injected Config:</h3>
<form name="loadInjectedConfigForm" class="form-horizontal" novalidate
ng-submit="demoCtrl.queryInjectedConfig()">
<div class="form-group">
<div class="clearfix">
<label class="col-sm-2 control-label">apollo.foo</label>
<div class="col-sm-3">
<input type="text" name="injectedConfigValue" class="form-control"
ng-model="demoCtrl.injectedConfigValue" readonly/>
</div>

<input type="submit" class="btn btn-primary col-sm-1" value="query"/>
</div>
</div>
</form>
</div>

<!--<div id="refresh-config-wrapper">-->
<!--<h3>Refresh Config:</h3>-->
<!--<button type="button" class="btn btn-primary" ng-click="demoCtrl.refreshConfig()">Refresh Config</button>-->
<!--<div id="refresh-result">-->
<!--<strong>Changed Properties:</strong> {{demoCtrl.refreshResult}}-->
<!--</div>-->
<!--<h3>Refresh Config:</h3>-->
<!--<button type="button" class="btn btn-primary" ng-click="demoCtrl.refreshConfig()">Refresh Config</button>-->
<!--<div id="refresh-result">-->
<!--<strong>Changed Properties:</strong> {{demoCtrl.refreshResult}}-->
<!--</div>-->
<!--</div>-->

</div>

0 comments on commit dcdd4b0

Please sign in to comment.