提交 bc0e4846 编写于 作者: 张乐 提交者: GitHub

Merge pull request #599 from nobodyiam/use-guice-as-client-ioc

use guice as apollo-client ioc mechanism
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
<parent> <parent>
<groupId>com.ctrip.framework.apollo</groupId> <groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo</artifactId> <artifactId>apollo</artifactId>
<version>0.6.4-SNAPSHOT</version> <version>0.7.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath> <relativePath>../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
<parent> <parent>
<groupId>com.ctrip.framework.apollo</groupId> <groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo</artifactId> <artifactId>apollo</artifactId>
<version>0.6.4-SNAPSHOT</version> <version>0.7.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath> <relativePath>../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
<parent> <parent>
<groupId>com.ctrip.framework.apollo</groupId> <groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo</artifactId> <artifactId>apollo</artifactId>
<version>0.6.4-SNAPSHOT</version> <version>0.7.0-SNAPSHOT</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>apollo-biz</artifactId> <artifactId>apollo-biz</artifactId>
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
<parent> <parent>
<groupId>com.ctrip.framework.apollo</groupId> <groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo</artifactId> <artifactId>apollo</artifactId>
<version>0.6.4-SNAPSHOT</version> <version>0.7.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath> <relativePath>../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
......
...@@ -88,7 +88,7 @@ If you need this functionality, you could specify the cluster as follows: ...@@ -88,7 +88,7 @@ If you need this functionality, you could specify the cluster as follows:
<dependency> <dependency>
<groupId>com.ctrip.framework.apollo</groupId> <groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-client</artifactId> <artifactId>apollo-client</artifactId>
<version>0.6.4</version> <version>0.7.0</version>
</dependency> </dependency>
## III. Client Usage ## III. Client Usage
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
<parent> <parent>
<groupId>com.ctrip.framework.apollo</groupId> <groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo</artifactId> <artifactId>apollo</artifactId>
<version>0.6.4-SNAPSHOT</version> <version>0.7.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath> <relativePath>../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
...@@ -21,12 +21,12 @@ ...@@ -21,12 +21,12 @@
<artifactId>apollo-core</artifactId> <artifactId>apollo-core</artifactId>
</dependency> </dependency>
<!-- end of apollo --> <!-- end of apollo -->
<!-- foundation service --> <!-- guice -->
<dependency> <dependency>
<groupId>org.unidal.framework</groupId> <groupId>com.google.inject</groupId>
<artifactId>foundation-service</artifactId> <artifactId>guice</artifactId>
</dependency> </dependency>
<!-- end of foundation service --> <!-- end of guice -->
<!-- log --> <!-- log -->
<dependency> <dependency>
<groupId>org.slf4j</groupId> <groupId>org.slf4j</groupId>
...@@ -61,19 +61,5 @@ ...@@ -61,19 +61,5 @@
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<!-- end of test --> <!-- end of test -->
<!-- dal-jdbc -->
<dependency>
<groupId>org.unidal.framework</groupId>
<artifactId>dal-jdbc</artifactId>
<version>2.4.0</version>
<optional>true</optional>
<exclusions>
<exclusion>
<groupId>com.dianping.cat</groupId>
<artifactId>cat-client</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- end of dal-jdbc -->
</dependencies> </dependencies>
</project> </project>
package com.ctrip.framework.apollo; package com.ctrip.framework.apollo;
import com.ctrip.framework.apollo.build.ApolloInjector;
import com.ctrip.framework.apollo.core.ConfigConsts; import com.ctrip.framework.apollo.core.ConfigConsts;
import com.ctrip.framework.apollo.core.enums.ConfigFileFormat; import com.ctrip.framework.apollo.core.enums.ConfigFileFormat;
import com.ctrip.framework.apollo.exceptions.ApolloConfigException;
import com.ctrip.framework.apollo.internals.ConfigManager; import com.ctrip.framework.apollo.internals.ConfigManager;
import com.ctrip.framework.apollo.spi.ConfigFactory; import com.ctrip.framework.apollo.spi.ConfigFactory;
import com.ctrip.framework.apollo.spi.ConfigRegistry; import com.ctrip.framework.apollo.spi.ConfigRegistry;
import com.ctrip.framework.apollo.tracer.Tracer;
import org.codehaus.plexus.PlexusContainer;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
import org.unidal.lookup.ContainerLoader;
/** /**
* Entry point for client config use * Entry point for client config use
...@@ -20,25 +15,14 @@ import org.unidal.lookup.ContainerLoader; ...@@ -20,25 +15,14 @@ import org.unidal.lookup.ContainerLoader;
public class ConfigService { public class ConfigService {
private static final ConfigService s_instance = new ConfigService(); private static final ConfigService s_instance = new ConfigService();
private PlexusContainer m_container;
private volatile ConfigManager m_configManager; private volatile ConfigManager m_configManager;
private volatile ConfigRegistry m_configRegistry; private volatile ConfigRegistry m_configRegistry;
private ConfigService() {
m_container = ContainerLoader.getDefaultContainer();
}
private ConfigManager getManager() { private ConfigManager getManager() {
if (m_configManager == null) { if (m_configManager == null) {
synchronized (this) { synchronized (this) {
if (m_configManager == null) { if (m_configManager == null) {
try { m_configManager = ApolloInjector.getInstance(ConfigManager.class);
m_configManager = m_container.lookup(ConfigManager.class);
} catch (ComponentLookupException ex) {
ApolloConfigException exception = new ApolloConfigException("Unable to load ConfigManager!", ex);
Tracer.logError(exception);
throw exception;
}
} }
} }
} }
...@@ -50,13 +34,7 @@ public class ConfigService { ...@@ -50,13 +34,7 @@ public class ConfigService {
if (m_configRegistry == null) { if (m_configRegistry == null) {
synchronized (this) { synchronized (this) {
if (m_configRegistry == null) { if (m_configRegistry == null) {
try { m_configRegistry = ApolloInjector.getInstance(ConfigRegistry.class);
m_configRegistry = m_container.lookup(ConfigRegistry.class);
} catch (ComponentLookupException ex) {
ApolloConfigException exception = new ApolloConfigException("Unable to load ConfigRegistry!", ex);
Tracer.logError(exception);
throw exception;
}
} }
} }
} }
...@@ -127,9 +105,8 @@ public class ConfigService { ...@@ -127,9 +105,8 @@ public class ConfigService {
} }
// for test only // for test only
static void setContainer(PlexusContainer m_container) { static void reset() {
synchronized (s_instance) { synchronized (s_instance) {
s_instance.m_container = m_container;
s_instance.m_configManager = null; s_instance.m_configManager = null;
s_instance.m_configRegistry = null; s_instance.m_configRegistry = null;
} }
......
package com.ctrip.framework.apollo.build;
import com.ctrip.framework.apollo.exceptions.ApolloConfigException;
import com.ctrip.framework.apollo.internals.Injector;
import com.ctrip.framework.apollo.tracer.Tracer;
import com.ctrip.framework.foundation.internals.ServiceBootstrap;
/**
* @author Jason Song(song_s@ctrip.com)
*/
public class ApolloInjector {
private static Injector s_injector;
private static Object lock = new Object();
private static Injector getInjector() {
if (s_injector == null) {
synchronized (lock) {
if (s_injector == null) {
try {
s_injector = ServiceBootstrap.loadFirst(Injector.class);
} catch (Throwable ex) {
ApolloConfigException exception = new ApolloConfigException("Unable to initialize Apollo Injector!", ex);
Tracer.logError(exception);
throw exception;
}
}
}
}
return s_injector;
}
public static <T> T getInstance(Class<T> clazz) {
try {
return getInjector().getInstance(clazz);
} catch (Throwable ex) {
Tracer.logError(ex);
throw new ApolloConfigException(String.format("Unable to load instance for type %s!", clazz.getName()), ex);
}
}
public static <T> T getInstance(Class<T> clazz, String name) {
try {
return getInjector().getInstance(clazz, name);
} catch (Throwable ex) {
Tracer.logError(ex);
throw new ApolloConfigException(
String.format("Unable to load instance for type %s and name %s !", clazz.getName(), name), ex);
}
}
}
package com.ctrip.framework.apollo.build;
import com.ctrip.framework.apollo.internals.ConfigServiceLocator;
import com.ctrip.framework.apollo.internals.DefaultConfigManager;
import com.ctrip.framework.apollo.internals.RemoteConfigLongPollService;
import com.ctrip.framework.apollo.spi.DefaultConfigFactory;
import com.ctrip.framework.apollo.spi.DefaultConfigFactoryManager;
import com.ctrip.framework.apollo.spi.DefaultConfigRegistry;
import com.ctrip.framework.apollo.util.ConfigUtil;
import com.ctrip.framework.apollo.util.http.HttpUtil;
import org.unidal.lookup.configuration.AbstractResourceConfigurator;
import org.unidal.lookup.configuration.Component;
import java.util.ArrayList;
import java.util.List;
/**
* @author Jason Song(song_s@ctrip.com)
*/
public class ComponentConfigurator extends AbstractResourceConfigurator {
public static void main(String[] args) {
generatePlexusComponentsXmlFile(new ComponentConfigurator());
}
@Override
public List<Component> defineComponents() {
List<Component> all = new ArrayList<>();
all.add(A(DefaultConfigManager.class));
all.add(A(DefaultConfigFactory.class));
all.add(A(DefaultConfigRegistry.class));
all.add(A(DefaultConfigFactoryManager.class));
all.add(A(ConfigUtil.class));
all.add(A(HttpUtil.class));
all.add(A(ConfigServiceLocator.class));
all.add(A(RemoteConfigLongPollService.class));
return all;
}
}
package com.ctrip.framework.apollo.ds;
import com.ctrip.framework.apollo.ConfigFile;
import com.ctrip.framework.apollo.ConfigService;
import com.ctrip.framework.apollo.core.enums.ConfigFileFormat;
import com.ctrip.framework.foundation.Foundation;
import org.codehaus.plexus.logging.LogEnabled;
import org.codehaus.plexus.logging.Logger;
import org.unidal.dal.jdbc.datasource.DataSourceProvider;
import org.unidal.dal.jdbc.datasource.model.entity.DataSourcesDef;
import org.unidal.dal.jdbc.datasource.model.transform.DefaultSaxParser;
import org.unidal.lookup.annotation.Named;
/**
* Data source provider based on Apollo configuration service.
* <p>
*
* Use following component definition to replace default
* <code>DataSourceProvider</code>:
* <p>
* <code><pre>
* public List<Component> defineComponents() {
* List<Component> all = new ArrayList<>();
*
* all.add(A(ApolloDataSourceProvider.class));
*
* return all;
* }
* </pre></code>
*
* <b>WARNING:</b> all defined <code>DataSourceProvider</code> components will
* be taken affect. DO NOT define unused <code>DataSourceProvider</code>
* component.
*/
@Named(type = DataSourceProvider.class, value = "apollo")
public class ApolloDataSourceProvider implements DataSourceProvider, LogEnabled {
private Logger m_logger;
private DataSourcesDef m_def;
@Override
public DataSourcesDef defineDatasources() {
if (m_def == null) {
ConfigFile file = ConfigService.getConfigFile("datasources", ConfigFileFormat.XML);
String appId = Foundation.app().getAppId();
String envType = Foundation.server().getEnvType();
if (file != null && file.hasContent()) {
String content = file.getContent();
m_logger.info(String.format("Found datasources.xml from Apollo(env=%s, app.id=%s)!", envType, appId));
try {
m_def = DefaultSaxParser.parse(content);
} catch (Exception e) {
throw new IllegalStateException(String.format("Error when parsing datasources.xml from Apollo(env=%s, app.id=%s)!", envType, appId), e);
}
} else {
m_logger.warn(String.format("Can't get datasources.xml from Apollo(env=%s, app.id=%s)!", envType, appId));
m_def = new DataSourcesDef();
}
}
return m_def;
}
@Override
public void enableLogging(Logger logger) {
m_logger = logger;
}
}
package com.ctrip.framework.apollo.internals; package com.ctrip.framework.apollo.internals;
import com.google.common.base.Function; import java.util.Date;
import com.google.common.base.Objects; import java.util.List;
import com.google.common.cache.Cache; import java.util.Locale;
import com.google.common.cache.CacheBuilder; import java.util.Map;
import com.google.common.collect.Lists; import java.util.Properties;
import com.google.common.collect.Maps; import java.util.Set;
import com.google.common.collect.Sets; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicLong;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.ctrip.framework.apollo.Config; import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigChangeListener; import com.ctrip.framework.apollo.ConfigChangeListener;
import com.ctrip.framework.apollo.build.ApolloInjector;
import com.ctrip.framework.apollo.core.utils.ApolloThreadFactory; import com.ctrip.framework.apollo.core.utils.ApolloThreadFactory;
import com.ctrip.framework.apollo.enums.PropertyChangeType; import com.ctrip.framework.apollo.enums.PropertyChangeType;
import com.ctrip.framework.apollo.exceptions.ApolloConfigException; import com.ctrip.framework.apollo.exceptions.ApolloConfigException;
...@@ -20,21 +26,13 @@ import com.ctrip.framework.apollo.tracer.spi.Transaction; ...@@ -20,21 +26,13 @@ import com.ctrip.framework.apollo.tracer.spi.Transaction;
import com.ctrip.framework.apollo.util.ConfigUtil; import com.ctrip.framework.apollo.util.ConfigUtil;
import com.ctrip.framework.apollo.util.function.Functions; import com.ctrip.framework.apollo.util.function.Functions;
import com.ctrip.framework.apollo.util.parser.Parsers; import com.ctrip.framework.apollo.util.parser.Parsers;
import com.google.common.base.Function;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException; import com.google.common.base.Objects;
import org.slf4j.Logger; import com.google.common.cache.Cache;
import org.slf4j.LoggerFactory; import com.google.common.cache.CacheBuilder;
import org.unidal.lookup.ContainerLoader; import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import java.util.Date; import com.google.common.collect.Sets;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicLong;
/** /**
* @author Jason Song(song_s@ctrip.com) * @author Jason Song(song_s@ctrip.com)
...@@ -65,15 +63,10 @@ public abstract class AbstractConfig implements Config { ...@@ -65,15 +63,10 @@ public abstract class AbstractConfig implements Config {
} }
public AbstractConfig() { public AbstractConfig() {
try { m_configUtil = ApolloInjector.getInstance(ConfigUtil.class);
m_configUtil = ContainerLoader.getDefaultContainer().lookup(ConfigUtil.class);
m_configVersion = new AtomicLong(); m_configVersion = new AtomicLong();
m_arrayCache = Maps.newConcurrentMap(); m_arrayCache = Maps.newConcurrentMap();
allCaches = Lists.newArrayList(); allCaches = Lists.newArrayList();
} catch (ComponentLookupException ex) {
Tracer.logError(ex);
throw new ApolloConfigException("Unable to load component!", ex);
}
} }
@Override @Override
......
package com.ctrip.framework.apollo.internals; package com.ctrip.framework.apollo.internals;
import com.ctrip.framework.apollo.ConfigFile; import java.util.Properties;
import com.ctrip.framework.apollo.tracer.Tracer; import java.util.concurrent.atomic.AtomicReference;
import com.ctrip.framework.apollo.util.ExceptionUtil;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.util.Properties; import com.ctrip.framework.apollo.ConfigFile;
import java.util.concurrent.atomic.AtomicReference; import com.ctrip.framework.apollo.tracer.Tracer;
import com.ctrip.framework.apollo.util.ExceptionUtil;
/** /**
* @author Jason Song(song_s@ctrip.com) * @author Jason Song(song_s@ctrip.com)
......
package com.ctrip.framework.apollo.internals; package com.ctrip.framework.apollo.internals;
import com.google.common.collect.Lists; import java.util.List;
import java.util.Properties;
import com.ctrip.framework.apollo.tracer.Tracer;
import com.ctrip.framework.apollo.util.ExceptionUtil;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.util.List; import com.ctrip.framework.apollo.tracer.Tracer;
import java.util.Properties; import com.ctrip.framework.apollo.util.ExceptionUtil;
import com.google.common.collect.Lists;
/** /**
* @author Jason Song(song_s@ctrip.com) * @author Jason Song(song_s@ctrip.com)
......
package com.ctrip.framework.apollo.internals; package com.ctrip.framework.apollo.internals;
import com.google.common.base.Joiner; import java.lang.reflect.Type;
import com.google.common.base.Strings; import java.util.List;
import com.google.common.collect.Lists; import java.util.Map;
import com.google.common.collect.Maps; import java.util.concurrent.Executors;
import com.google.common.escape.Escaper; import java.util.concurrent.ScheduledExecutorService;
import com.google.common.net.UrlEscapers; import java.util.concurrent.atomic.AtomicReference;
import com.google.gson.reflect.TypeToken;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.ctrip.framework.apollo.build.ApolloInjector;
import com.ctrip.framework.apollo.core.dto.ServiceDTO; import com.ctrip.framework.apollo.core.dto.ServiceDTO;
import com.ctrip.framework.apollo.core.utils.ApolloThreadFactory; import com.ctrip.framework.apollo.core.utils.ApolloThreadFactory;
import com.ctrip.framework.apollo.exceptions.ApolloConfigException; import com.ctrip.framework.apollo.exceptions.ApolloConfigException;
...@@ -18,28 +21,17 @@ import com.ctrip.framework.apollo.util.ExceptionUtil; ...@@ -18,28 +21,17 @@ import com.ctrip.framework.apollo.util.ExceptionUtil;
import com.ctrip.framework.apollo.util.http.HttpRequest; import com.ctrip.framework.apollo.util.http.HttpRequest;
import com.ctrip.framework.apollo.util.http.HttpResponse; import com.ctrip.framework.apollo.util.http.HttpResponse;
import com.ctrip.framework.apollo.util.http.HttpUtil; import com.ctrip.framework.apollo.util.http.HttpUtil;
import com.google.common.base.Joiner;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.escape.Escaper;
import com.google.common.net.UrlEscapers;
import com.google.gson.reflect.TypeToken;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable; public class ConfigServiceLocator {
import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.unidal.lookup.annotation.Inject;
import org.unidal.lookup.annotation.Named;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
@Named(type = ConfigServiceLocator.class)
public class ConfigServiceLocator implements Initializable {
private static final Logger logger = LoggerFactory.getLogger(ConfigServiceLocator.class); private static final Logger logger = LoggerFactory.getLogger(ConfigServiceLocator.class);
@Inject
private HttpUtil m_httpUtil; private HttpUtil m_httpUtil;
@Inject
private ConfigUtil m_configUtil; private ConfigUtil m_configUtil;
private AtomicReference<List<ServiceDTO>> m_configServices; private AtomicReference<List<ServiceDTO>> m_configServices;
private Type m_responseType; private Type m_responseType;
...@@ -55,12 +47,10 @@ public class ConfigServiceLocator implements Initializable { ...@@ -55,12 +47,10 @@ public class ConfigServiceLocator implements Initializable {
m_configServices = new AtomicReference<>(initial); m_configServices = new AtomicReference<>(initial);
m_responseType = new TypeToken<List<ServiceDTO>>() { m_responseType = new TypeToken<List<ServiceDTO>>() {
}.getType(); }.getType();
m_httpUtil = ApolloInjector.getInstance(HttpUtil.class);
m_configUtil = ApolloInjector.getInstance(ConfigUtil.class);
this.m_executorService = Executors.newScheduledThreadPool(1, this.m_executorService = Executors.newScheduledThreadPool(1,
ApolloThreadFactory.create("ConfigServiceLocator", true)); ApolloThreadFactory.create("ConfigServiceLocator", true));
}
@Override
public void initialize() throws InitializationException {
this.tryUpdateConfigServices(); this.tryUpdateConfigServices();
this.schedulePeriodicRefresh(); this.schedulePeriodicRefresh();
} }
......
package com.ctrip.framework.apollo.internals; package com.ctrip.framework.apollo.internals;
import com.google.common.collect.ImmutableMap;
import com.ctrip.framework.apollo.core.utils.ClassLoaderUtil;
import com.ctrip.framework.apollo.enums.PropertyChangeType;
import com.ctrip.framework.apollo.model.ConfigChange;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.tracer.Tracer;
import com.ctrip.framework.apollo.util.ExceptionUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.Collections; import java.util.Collections;
...@@ -22,6 +10,17 @@ import java.util.Properties; ...@@ -22,6 +10,17 @@ import java.util.Properties;
import java.util.Set; import java.util.Set;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.ctrip.framework.apollo.core.utils.ClassLoaderUtil;
import com.ctrip.framework.apollo.enums.PropertyChangeType;
import com.ctrip.framework.apollo.model.ConfigChange;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.tracer.Tracer;
import com.ctrip.framework.apollo.util.ExceptionUtil;
import com.google.common.collect.ImmutableMap;
/** /**
* @author Jason Song(song_s@ctrip.com) * @author Jason Song(song_s@ctrip.com)
......
package com.ctrip.framework.apollo.internals; package com.ctrip.framework.apollo.internals;
import com.google.common.collect.Maps; import java.util.Map;
import com.ctrip.framework.apollo.Config; import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigFile; import com.ctrip.framework.apollo.ConfigFile;
import com.ctrip.framework.apollo.build.ApolloInjector;
import com.ctrip.framework.apollo.core.enums.ConfigFileFormat; import com.ctrip.framework.apollo.core.enums.ConfigFileFormat;
import com.ctrip.framework.apollo.spi.ConfigFactory; import com.ctrip.framework.apollo.spi.ConfigFactory;
import com.ctrip.framework.apollo.spi.ConfigFactoryManager; import com.ctrip.framework.apollo.spi.ConfigFactoryManager;
import com.google.common.collect.Maps;
import org.unidal.lookup.annotation.Inject;
import org.unidal.lookup.annotation.Named;
import java.util.Map;
/** /**
* @author Jason Song(song_s@ctrip.com) * @author Jason Song(song_s@ctrip.com)
*/ */
@Named(type = ConfigManager.class)
public class DefaultConfigManager implements ConfigManager { public class DefaultConfigManager implements ConfigManager {
@Inject
private ConfigFactoryManager m_factoryManager; private ConfigFactoryManager m_factoryManager;
private Map<String, Config> m_configs = Maps.newConcurrentMap(); private Map<String, Config> m_configs = Maps.newConcurrentMap();
private Map<String, ConfigFile> m_configFiles = Maps.newConcurrentMap(); private Map<String, ConfigFile> m_configFiles = Maps.newConcurrentMap();
public DefaultConfigManager() {
m_factoryManager = ApolloInjector.getInstance(ConfigFactoryManager.class);
}
@Override @Override
public Config getConfig(String namespace) { public Config getConfig(String namespace) {
Config config = m_configs.get(namespace); Config config = m_configs.get(namespace);
......
package com.ctrip.framework.apollo.internals;
import com.ctrip.framework.apollo.exceptions.ApolloConfigException;
import com.ctrip.framework.apollo.spi.ConfigFactory;
import com.ctrip.framework.apollo.spi.ConfigFactoryManager;
import com.ctrip.framework.apollo.spi.ConfigRegistry;
import com.ctrip.framework.apollo.spi.DefaultConfigFactory;
import com.ctrip.framework.apollo.spi.DefaultConfigFactoryManager;
import com.ctrip.framework.apollo.spi.DefaultConfigRegistry;
import com.ctrip.framework.apollo.tracer.Tracer;
import com.ctrip.framework.apollo.util.ConfigUtil;
import com.ctrip.framework.apollo.util.http.HttpUtil;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Singleton;
/**
* Guice injector
* @author Jason Song(song_s@ctrip.com)
*/
public class DefaultInjector implements Injector {
private com.google.inject.Injector m_injector;
public DefaultInjector() {
try {
m_injector = Guice.createInjector(new ApolloModule());
} catch (Throwable ex) {
ApolloConfigException exception = new ApolloConfigException("Unable to initialize Guice Injector!", ex);
Tracer.logError(exception);
throw exception;
}
}
@Override
public <T> T getInstance(Class<T> clazz) {
try {
return m_injector.getInstance(clazz);
} catch (Throwable ex) {
Tracer.logError(ex);
throw new ApolloConfigException(
String.format("Unable to load instance for %s!", clazz.getName()), ex);
}
}
@Override
public <T> T getInstance(Class<T> clazz, String name) {
//Guice does not support get instance by type and name
return null;
}
private static class ApolloModule extends AbstractModule {
@Override
protected void configure() {
bind(ConfigManager.class).to(DefaultConfigManager.class).in(Singleton.class);
bind(ConfigFactoryManager.class).to(DefaultConfigFactoryManager.class).in(Singleton.class);
bind(ConfigRegistry.class).to(DefaultConfigRegistry.class).in(Singleton.class);
bind(ConfigFactory.class).to(DefaultConfigFactory.class).in(Singleton.class);
bind(ConfigUtil.class).in(Singleton.class);
bind(HttpUtil.class).in(Singleton.class);
bind(ConfigServiceLocator.class).in(Singleton.class);
bind(RemoteConfigLongPollService.class).in(Singleton.class);
}
}
}
package com.ctrip.framework.apollo.internals;
/**
* @author Jason Song(song_s@ctrip.com)
*/
public interface Injector {
/**
* Returns the appropriate instance for the given injection type
*/
<T> T getInstance(Class<T> clazz);
/**
* Returns the appropriate instance for the given injection type and name
*/
<T> T getInstance(Class<T> clazz, String name);
}
package com.ctrip.framework.apollo.internals; package com.ctrip.framework.apollo.internals;
import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.ctrip.framework.apollo.core.ConfigConsts;
import com.ctrip.framework.apollo.core.utils.ClassLoaderUtil;
import com.ctrip.framework.apollo.exceptions.ApolloConfigException;
import com.ctrip.framework.apollo.tracer.Tracer;
import com.ctrip.framework.apollo.tracer.spi.Transaction;
import com.ctrip.framework.apollo.util.ConfigUtil;
import com.ctrip.framework.apollo.util.ExceptionUtil;
import org.codehaus.plexus.PlexusContainer;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.unidal.lookup.ContainerLoader;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
...@@ -28,6 +11,20 @@ import java.nio.file.Path; ...@@ -28,6 +11,20 @@ import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.Properties; import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.ctrip.framework.apollo.build.ApolloInjector;
import com.ctrip.framework.apollo.core.ConfigConsts;
import com.ctrip.framework.apollo.core.utils.ClassLoaderUtil;
import com.ctrip.framework.apollo.exceptions.ApolloConfigException;
import com.ctrip.framework.apollo.tracer.Tracer;
import com.ctrip.framework.apollo.tracer.spi.Transaction;
import com.ctrip.framework.apollo.util.ConfigUtil;
import com.ctrip.framework.apollo.util.ExceptionUtil;
import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
/** /**
* @author Jason Song(song_s@ctrip.com) * @author Jason Song(song_s@ctrip.com)
*/ */
...@@ -35,7 +32,6 @@ public class LocalFileConfigRepository extends AbstractConfigRepository ...@@ -35,7 +32,6 @@ public class LocalFileConfigRepository extends AbstractConfigRepository
implements RepositoryChangeListener { implements RepositoryChangeListener {
private static final Logger logger = LoggerFactory.getLogger(LocalFileConfigRepository.class); private static final Logger logger = LoggerFactory.getLogger(LocalFileConfigRepository.class);
private static final String CONFIG_DIR = "/config-cache"; private static final String CONFIG_DIR = "/config-cache";
private final PlexusContainer m_container;
private final String m_namespace; private final String m_namespace;
private File m_baseDir; private File m_baseDir;
private final ConfigUtil m_configUtil; private final ConfigUtil m_configUtil;
...@@ -53,13 +49,7 @@ public class LocalFileConfigRepository extends AbstractConfigRepository ...@@ -53,13 +49,7 @@ public class LocalFileConfigRepository extends AbstractConfigRepository
public LocalFileConfigRepository(String namespace, ConfigRepository upstream) { public LocalFileConfigRepository(String namespace, ConfigRepository upstream) {
m_namespace = namespace; m_namespace = namespace;
m_container = ContainerLoader.getDefaultContainer(); m_configUtil = ApolloInjector.getInstance(ConfigUtil.class);
try {
m_configUtil = m_container.lookup(ConfigUtil.class);
} catch (ComponentLookupException ex) {
Tracer.logError(ex);
throw new ApolloConfigException("Unable to load component!", ex);
}
this.setLocalCacheDir(findLocalCacheDir(), false); this.setLocalCacheDir(findLocalCacheDir(), false);
this.setUpstreamRepository(upstream); this.setUpstreamRepository(upstream);
this.trySync(); this.trySync();
......
package com.ctrip.framework.apollo.internals; package com.ctrip.framework.apollo.internals;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.ctrip.framework.apollo.core.enums.ConfigFileFormat; import com.ctrip.framework.apollo.core.enums.ConfigFileFormat;
import com.ctrip.framework.apollo.core.utils.PropertiesUtil; import com.ctrip.framework.apollo.core.utils.PropertiesUtil;
import com.ctrip.framework.apollo.exceptions.ApolloConfigException; import com.ctrip.framework.apollo.exceptions.ApolloConfigException;
import com.ctrip.framework.apollo.tracer.Tracer; import com.ctrip.framework.apollo.tracer.Tracer;
import com.ctrip.framework.apollo.util.ExceptionUtil; import com.ctrip.framework.apollo.util.ExceptionUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicReference;
/** /**
* @author Jason Song(song_s@ctrip.com) * @author Jason Song(song_s@ctrip.com)
*/ */
......
package com.ctrip.framework.apollo.internals; package com.ctrip.framework.apollo.internals;
import com.google.common.base.Joiner; import java.lang.reflect.Type;
import com.google.common.base.Strings; import java.util.List;
import com.google.common.collect.HashMultimap; import java.util.Map;
import com.google.common.collect.Lists; import java.util.Random;
import com.google.common.collect.Maps; import java.util.concurrent.ConcurrentMap;
import com.google.common.collect.Multimap; import java.util.concurrent.ExecutorService;
import com.google.common.collect.Multimaps; import java.util.concurrent.Executors;
import com.google.common.escape.Escaper; import java.util.concurrent.TimeUnit;
import com.google.common.net.UrlEscapers; import java.util.concurrent.atomic.AtomicBoolean;
import com.google.common.reflect.TypeToken;
import com.google.common.util.concurrent.RateLimiter; import org.slf4j.Logger;
import com.google.gson.Gson; import org.slf4j.LoggerFactory;
import com.ctrip.framework.apollo.build.ApolloInjector;
import com.ctrip.framework.apollo.core.ConfigConsts; import com.ctrip.framework.apollo.core.ConfigConsts;
import com.ctrip.framework.apollo.core.dto.ApolloConfigNotification; import com.ctrip.framework.apollo.core.dto.ApolloConfigNotification;
import com.ctrip.framework.apollo.core.dto.ServiceDTO; import com.ctrip.framework.apollo.core.dto.ServiceDTO;
...@@ -28,29 +29,23 @@ import com.ctrip.framework.apollo.util.ExceptionUtil; ...@@ -28,29 +29,23 @@ import com.ctrip.framework.apollo.util.ExceptionUtil;
import com.ctrip.framework.apollo.util.http.HttpRequest; import com.ctrip.framework.apollo.util.http.HttpRequest;
import com.ctrip.framework.apollo.util.http.HttpResponse; import com.ctrip.framework.apollo.util.http.HttpResponse;
import com.ctrip.framework.apollo.util.http.HttpUtil; import com.ctrip.framework.apollo.util.http.HttpUtil;
import com.google.common.base.Joiner;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable; import com.google.common.base.Strings;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException; import com.google.common.collect.HashMultimap;
import org.slf4j.Logger; import com.google.common.collect.Lists;
import org.slf4j.LoggerFactory; import com.google.common.collect.Maps;
import org.unidal.lookup.annotation.Inject; import com.google.common.collect.Multimap;
import org.unidal.lookup.annotation.Named; import com.google.common.collect.Multimaps;
import com.google.common.escape.Escaper;
import java.lang.reflect.Type; import com.google.common.net.UrlEscapers;
import java.util.List; import com.google.common.reflect.TypeToken;
import java.util.Map; import com.google.common.util.concurrent.RateLimiter;
import java.util.Random; import com.google.gson.Gson;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
/** /**
* @author Jason Song(song_s@ctrip.com) * @author Jason Song(song_s@ctrip.com)
*/ */
@Named(type = RemoteConfigLongPollService.class) public class RemoteConfigLongPollService {
public class RemoteConfigLongPollService implements Initializable {
private static final Logger logger = LoggerFactory.getLogger(RemoteConfigLongPollService.class); private static final Logger logger = LoggerFactory.getLogger(RemoteConfigLongPollService.class);
private static final Joiner STRING_JOINER = Joiner.on(ConfigConsts.CLUSTER_NAMESPACE_SEPARATOR); private static final Joiner STRING_JOINER = Joiner.on(ConfigConsts.CLUSTER_NAMESPACE_SEPARATOR);
private static final Joiner.MapJoiner MAP_JOINER = Joiner.on("&").withKeyValueSeparator("="); private static final Joiner.MapJoiner MAP_JOINER = Joiner.on("&").withKeyValueSeparator("=");
...@@ -65,11 +60,8 @@ public class RemoteConfigLongPollService implements Initializable { ...@@ -65,11 +60,8 @@ public class RemoteConfigLongPollService implements Initializable {
private final ConcurrentMap<String, Long> m_notifications; private final ConcurrentMap<String, Long> m_notifications;
private Type m_responseType; private Type m_responseType;
private Gson gson; private Gson gson;
@Inject
private ConfigUtil m_configUtil; private ConfigUtil m_configUtil;
@Inject
private HttpUtil m_httpUtil; private HttpUtil m_httpUtil;
@Inject
private ConfigServiceLocator m_serviceLocator; private ConfigServiceLocator m_serviceLocator;
/** /**
...@@ -87,10 +79,9 @@ public class RemoteConfigLongPollService implements Initializable { ...@@ -87,10 +79,9 @@ public class RemoteConfigLongPollService implements Initializable {
m_responseType = new TypeToken<List<ApolloConfigNotification>>() { m_responseType = new TypeToken<List<ApolloConfigNotification>>() {
}.getType(); }.getType();
gson = new Gson(); gson = new Gson();
} m_configUtil = ApolloInjector.getInstance(ConfigUtil.class);
m_httpUtil = ApolloInjector.getInstance(HttpUtil.class);
@Override m_serviceLocator = ApolloInjector.getInstance(ConfigServiceLocator.class);
public void initialize() throws InitializationException {
m_longPollRateLimiter = RateLimiter.create(m_configUtil.getLongPollQPS()); m_longPollRateLimiter = RateLimiter.create(m_configUtil.getLongPollQPS());
} }
......
package com.ctrip.framework.apollo.internals; package com.ctrip.framework.apollo.internals;
import com.google.common.base.Joiner; import java.util.Collections;
import com.google.common.base.Strings; import java.util.List;
import com.google.common.collect.Lists; import java.util.Map;
import com.google.common.collect.Maps; import java.util.Properties;
import com.google.common.escape.Escaper; import java.util.concurrent.Executors;
import com.google.common.net.UrlEscapers; import java.util.concurrent.ScheduledExecutorService;
import com.google.common.util.concurrent.RateLimiter; import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.ctrip.framework.apollo.Apollo; import com.ctrip.framework.apollo.Apollo;
import com.ctrip.framework.apollo.build.ApolloInjector;
import com.ctrip.framework.apollo.core.ConfigConsts; import com.ctrip.framework.apollo.core.ConfigConsts;
import com.ctrip.framework.apollo.core.dto.ApolloConfig; import com.ctrip.framework.apollo.core.dto.ApolloConfig;
import com.ctrip.framework.apollo.core.dto.ServiceDTO; import com.ctrip.framework.apollo.core.dto.ServiceDTO;
...@@ -22,21 +27,13 @@ import com.ctrip.framework.apollo.util.ExceptionUtil; ...@@ -22,21 +27,13 @@ import com.ctrip.framework.apollo.util.ExceptionUtil;
import com.ctrip.framework.apollo.util.http.HttpRequest; import com.ctrip.framework.apollo.util.http.HttpRequest;
import com.ctrip.framework.apollo.util.http.HttpResponse; import com.ctrip.framework.apollo.util.http.HttpResponse;
import com.ctrip.framework.apollo.util.http.HttpUtil; import com.ctrip.framework.apollo.util.http.HttpUtil;
import com.google.common.base.Joiner;
import org.codehaus.plexus.PlexusContainer; import com.google.common.base.Strings;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException; import com.google.common.collect.Lists;
import org.slf4j.Logger; import com.google.common.collect.Maps;
import org.slf4j.LoggerFactory; import com.google.common.escape.Escaper;
import org.unidal.lookup.ContainerLoader; import com.google.common.net.UrlEscapers;
import com.google.common.util.concurrent.RateLimiter;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
/** /**
* @author Jason Song(song_s@ctrip.com) * @author Jason Song(song_s@ctrip.com)
...@@ -45,11 +42,10 @@ public class RemoteConfigRepository extends AbstractConfigRepository { ...@@ -45,11 +42,10 @@ public class RemoteConfigRepository extends AbstractConfigRepository {
private static final Logger logger = LoggerFactory.getLogger(RemoteConfigRepository.class); private static final Logger logger = LoggerFactory.getLogger(RemoteConfigRepository.class);
private static final Joiner STRING_JOINER = Joiner.on(ConfigConsts.CLUSTER_NAMESPACE_SEPARATOR); private static final Joiner STRING_JOINER = Joiner.on(ConfigConsts.CLUSTER_NAMESPACE_SEPARATOR);
private static final Joiner.MapJoiner MAP_JOINER = Joiner.on("&").withKeyValueSeparator("="); private static final Joiner.MapJoiner MAP_JOINER = Joiner.on("&").withKeyValueSeparator("=");
private PlexusContainer m_container; private ConfigServiceLocator m_serviceLocator;
private final ConfigServiceLocator m_serviceLocator; private HttpUtil m_httpUtil;
private final HttpUtil m_httpUtil; private ConfigUtil m_configUtil;
private final ConfigUtil m_configUtil; private RemoteConfigLongPollService remoteConfigLongPollService;
private final RemoteConfigLongPollService remoteConfigLongPollService;
private volatile AtomicReference<ApolloConfig> m_configCache; private volatile AtomicReference<ApolloConfig> m_configCache;
private final String m_namespace; private final String m_namespace;
private final static ScheduledExecutorService m_executorService; private final static ScheduledExecutorService m_executorService;
...@@ -71,16 +67,10 @@ public class RemoteConfigRepository extends AbstractConfigRepository { ...@@ -71,16 +67,10 @@ public class RemoteConfigRepository extends AbstractConfigRepository {
public RemoteConfigRepository(String namespace) { public RemoteConfigRepository(String namespace) {
m_namespace = namespace; m_namespace = namespace;
m_configCache = new AtomicReference<>(); m_configCache = new AtomicReference<>();
m_container = ContainerLoader.getDefaultContainer(); m_configUtil = ApolloInjector.getInstance(ConfigUtil.class);
try { m_httpUtil = ApolloInjector.getInstance(HttpUtil.class);
m_configUtil = m_container.lookup(ConfigUtil.class); m_serviceLocator = ApolloInjector.getInstance(ConfigServiceLocator.class);
m_httpUtil = m_container.lookup(HttpUtil.class); remoteConfigLongPollService = ApolloInjector.getInstance(RemoteConfigLongPollService.class);
m_serviceLocator = m_container.lookup(ConfigServiceLocator.class);
remoteConfigLongPollService = m_container.lookup(RemoteConfigLongPollService.class);
} catch (ComponentLookupException ex) {
Tracer.logError(ex);
throw new ApolloConfigException("Unable to load component!", ex);
}
m_longPollServiceDto = new AtomicReference<>(); m_longPollServiceDto = new AtomicReference<>();
m_loadConfigRateLimiter = RateLimiter.create(m_configUtil.getLoadConfigQPS()); m_loadConfigRateLimiter = RateLimiter.create(m_configUtil.getLoadConfigQPS());
this.trySync(); this.trySync();
......
package com.ctrip.framework.apollo.internals; package com.ctrip.framework.apollo.internals;
import com.google.common.base.Function;
import com.google.common.collect.Maps;
import com.ctrip.framework.apollo.model.ConfigChange;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.tracer.Tracer;
import com.ctrip.framework.apollo.util.ExceptionUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
import java.util.Set; import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.ctrip.framework.apollo.model.ConfigChange;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.tracer.Tracer;
import com.ctrip.framework.apollo.util.ExceptionUtil;
import com.google.common.base.Function;
import com.google.common.collect.Maps;
/** /**
* @author Jason Song(song_s@ctrip.com) * @author Jason Song(song_s@ctrip.com)
*/ */
......
package com.ctrip.framework.apollo.spi; package com.ctrip.framework.apollo.spi;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.ctrip.framework.apollo.Config; import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigFile; import com.ctrip.framework.apollo.ConfigFile;
import com.ctrip.framework.apollo.build.ApolloInjector;
import com.ctrip.framework.apollo.core.enums.ConfigFileFormat; import com.ctrip.framework.apollo.core.enums.ConfigFileFormat;
import com.ctrip.framework.apollo.internals.ConfigRepository; import com.ctrip.framework.apollo.internals.ConfigRepository;
import com.ctrip.framework.apollo.internals.DefaultConfig; import com.ctrip.framework.apollo.internals.DefaultConfig;
...@@ -14,20 +18,17 @@ import com.ctrip.framework.apollo.internals.YamlConfigFile; ...@@ -14,20 +18,17 @@ import com.ctrip.framework.apollo.internals.YamlConfigFile;
import com.ctrip.framework.apollo.internals.YmlConfigFile; import com.ctrip.framework.apollo.internals.YmlConfigFile;
import com.ctrip.framework.apollo.util.ConfigUtil; import com.ctrip.framework.apollo.util.ConfigUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.unidal.lookup.annotation.Inject;
import org.unidal.lookup.annotation.Named;
/** /**
* @author Jason Song(song_s@ctrip.com) * @author Jason Song(song_s@ctrip.com)
*/ */
@Named(type = ConfigFactory.class)
public class DefaultConfigFactory implements ConfigFactory { public class DefaultConfigFactory implements ConfigFactory {
private static final Logger logger = LoggerFactory.getLogger(DefaultConfigFactory.class); private static final Logger logger = LoggerFactory.getLogger(DefaultConfigFactory.class);
@Inject
private ConfigUtil m_configUtil; private ConfigUtil m_configUtil;
public DefaultConfigFactory() {
m_configUtil = ApolloInjector.getInstance(ConfigUtil.class);
}
@Override @Override
public Config create(String namespace) { public Config create(String namespace) {
DefaultConfig defaultConfig = DefaultConfig defaultConfig =
......
package com.ctrip.framework.apollo.spi; package com.ctrip.framework.apollo.spi;
import com.google.common.collect.Maps;
import org.unidal.lookup.ContainerHolder;
import org.unidal.lookup.LookupException;
import org.unidal.lookup.annotation.Inject;
import org.unidal.lookup.annotation.Named;
import java.util.Map; import java.util.Map;
import com.ctrip.framework.apollo.build.ApolloInjector;
import com.google.common.collect.Maps;
/** /**
* @author Jason Song(song_s@ctrip.com) * @author Jason Song(song_s@ctrip.com)
*/ */
@Named(type = ConfigFactoryManager.class) public class DefaultConfigFactoryManager implements ConfigFactoryManager {
public class DefaultConfigFactoryManager extends ContainerHolder implements ConfigFactoryManager {
@Inject
private ConfigRegistry m_registry; private ConfigRegistry m_registry;
private Map<String, ConfigFactory> m_factories = Maps.newConcurrentMap(); private Map<String, ConfigFactory> m_factories = Maps.newConcurrentMap();
public DefaultConfigFactoryManager() {
m_registry = ApolloInjector.getInstance(ConfigRegistry.class);
}
@Override @Override
public ConfigFactory getFactory(String namespace) { public ConfigFactory getFactory(String namespace) {
// step 1: check hacked factory // step 1: check hacked factory
...@@ -36,16 +34,14 @@ public class DefaultConfigFactoryManager extends ContainerHolder implements Conf ...@@ -36,16 +34,14 @@ public class DefaultConfigFactoryManager extends ContainerHolder implements Conf
} }
// step 3: check declared config factory // step 3: check declared config factory
try { factory = ApolloInjector.getInstance(ConfigFactory.class, namespace);
factory = lookup(ConfigFactory.class, namespace);
} catch (LookupException ex) { if (factory != null) {
// ignore it return factory;
} }
// step 4: check default config factory // step 4: check default config factory
if (factory == null) { factory = ApolloInjector.getInstance(ConfigFactory.class);
factory = lookup(ConfigFactory.class);
}
m_factories.put(namespace, factory); m_factories.put(namespace, factory);
......
package com.ctrip.framework.apollo.spi; package com.ctrip.framework.apollo.spi;
import com.google.common.collect.Maps; import java.util.Map;
import org.codehaus.plexus.logging.LogEnabled; import org.slf4j.Logger;
import org.codehaus.plexus.logging.Logger; import org.slf4j.LoggerFactory;
import org.unidal.lookup.annotation.Named;
import java.util.Map; import com.google.common.collect.Maps;
/** /**
* @author Jason Song(song_s@ctrip.com) * @author Jason Song(song_s@ctrip.com)
*/ */
@Named(type = ConfigRegistry.class) public class DefaultConfigRegistry implements ConfigRegistry {
public class DefaultConfigRegistry implements ConfigRegistry, LogEnabled { private static final Logger s_logger = LoggerFactory.getLogger(DefaultConfigRegistry.class);
private Map<String, ConfigFactory> m_instances = Maps.newConcurrentMap(); private Map<String, ConfigFactory> m_instances = Maps.newConcurrentMap();
private Logger m_logger;
@Override @Override
public void register(String namespace, ConfigFactory factory) { public void register(String namespace, ConfigFactory factory) {
if (m_instances.containsKey(namespace)) { if (m_instances.containsKey(namespace)) {
m_logger.warn( s_logger.warn("ConfigFactory({}) is overridden by {}!", namespace, factory.getClass());
String.format("ConfigFactory(%s) is overridden by %s!", namespace, factory.getClass()));
} }
m_instances.put(namespace, factory); m_instances.put(namespace, factory);
...@@ -33,9 +29,4 @@ public class DefaultConfigRegistry implements ConfigRegistry, LogEnabled { ...@@ -33,9 +29,4 @@ public class DefaultConfigRegistry implements ConfigRegistry, LogEnabled {
return config; return config;
} }
@Override
public void enableLogging(Logger logger) {
m_logger = logger;
}
} }
package com.ctrip.framework.apollo.spring.annotation; package com.ctrip.framework.apollo.spring.annotation;
import com.google.common.base.Preconditions; import java.lang.reflect.Field;
import java.lang.reflect.Method;
import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigChangeListener;
import com.ctrip.framework.apollo.ConfigService;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.beans.factory.config.BeanPostProcessor;
...@@ -14,8 +10,11 @@ import org.springframework.core.PriorityOrdered; ...@@ -14,8 +10,11 @@ import org.springframework.core.PriorityOrdered;
import org.springframework.core.annotation.AnnotationUtils; import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.util.ReflectionUtils; import org.springframework.util.ReflectionUtils;
import java.lang.reflect.Field; import com.ctrip.framework.apollo.Config;
import java.lang.reflect.Method; import com.ctrip.framework.apollo.ConfigChangeListener;
import com.ctrip.framework.apollo.ConfigService;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.google.common.base.Preconditions;
/** /**
* Apollo Annotation Processor for Spring Application * Apollo Annotation Processor for Spring Application
......
package com.ctrip.framework.apollo.spring.annotation; package com.ctrip.framework.apollo.spring.annotation;
import com.ctrip.framework.apollo.core.ConfigConsts;
import java.lang.annotation.Documented; import java.lang.annotation.Documented;
import java.lang.annotation.ElementType; import java.lang.annotation.ElementType;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import java.lang.annotation.Target;
import com.ctrip.framework.apollo.core.ConfigConsts;
/** /**
* Use this annotation to inject Apollo Config Instance. * Use this annotation to inject Apollo Config Instance.
* *
......
package com.ctrip.framework.apollo.spring.annotation; package com.ctrip.framework.apollo.spring.annotation;
import com.ctrip.framework.apollo.core.ConfigConsts;
import java.lang.annotation.Documented; import java.lang.annotation.Documented;
import java.lang.annotation.ElementType; import java.lang.annotation.ElementType;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import java.lang.annotation.Target;
import com.ctrip.framework.apollo.core.ConfigConsts;
/** /**
* Use this annotation to register Apollo ConfigChangeListener. * Use this annotation to register Apollo ConfigChangeListener.
* *
......
package com.ctrip.framework.apollo.spring.annotation; package com.ctrip.framework.apollo.spring.annotation;
import com.google.common.collect.Lists;
import com.ctrip.framework.apollo.spring.config.PropertySourcesProcessor;
import com.ctrip.framework.apollo.spring.util.BeanRegistrationUtil;
import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.annotation.AnnotationAttributes; import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotationMetadata; import org.springframework.core.type.AnnotationMetadata;
import com.ctrip.framework.apollo.spring.config.PropertySourcesProcessor;
import com.ctrip.framework.apollo.spring.util.BeanRegistrationUtil;
import com.google.common.collect.Lists;
/** /**
* @author Jason Song(song_s@ctrip.com) * @author Jason Song(song_s@ctrip.com)
*/ */
......
package com.ctrip.framework.apollo.spring.annotation; package com.ctrip.framework.apollo.spring.annotation;
import com.ctrip.framework.apollo.core.ConfigConsts;
import org.springframework.context.annotation.Import;
import org.springframework.core.Ordered;
import java.lang.annotation.Documented; import java.lang.annotation.Documented;
import java.lang.annotation.ElementType; import java.lang.annotation.ElementType;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import java.lang.annotation.Target;
import org.springframework.context.annotation.Import;
import org.springframework.core.Ordered;
import com.ctrip.framework.apollo.core.ConfigConsts;
/** /**
* Use this annotation to register Apollo property sources when using Java Config. * Use this annotation to register Apollo property sources when using Java Config.
* *
......
package com.ctrip.framework.apollo.spring.config; package com.ctrip.framework.apollo.spring.config;
import java.util.Set; import java.util.Set;
import org.springframework.core.env.EnumerablePropertySource; import org.springframework.core.env.EnumerablePropertySource;
import com.ctrip.framework.apollo.Config; import com.ctrip.framework.apollo.Config;
......
package com.ctrip.framework.apollo.spring.config; package com.ctrip.framework.apollo.spring.config;
import com.ctrip.framework.apollo.spring.annotation.ApolloAnnotationProcessor;
import com.ctrip.framework.apollo.spring.util.BeanRegistrationUtil;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor; import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import com.ctrip.framework.apollo.spring.annotation.ApolloAnnotationProcessor;
import com.ctrip.framework.apollo.spring.util.BeanRegistrationUtil;
/** /**
* Apollo Property Sources processor for Spring XML Based Application * Apollo Property Sources processor for Spring XML Based Application
* *
......
package com.ctrip.framework.apollo.spring.config; package com.ctrip.framework.apollo.spring.config;
import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import com.ctrip.framework.apollo.core.ConfigConsts;
import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser; import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.beans.factory.xml.NamespaceHandlerSupport; import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.w3c.dom.Element; import org.w3c.dom.Element;
import com.ctrip.framework.apollo.core.ConfigConsts;
import com.google.common.base.Splitter;
import com.google.common.base.Strings;
/** /**
* @author Jason Song(song_s@ctrip.com) * @author Jason Song(song_s@ctrip.com)
*/ */
......
package com.ctrip.framework.apollo.spring.config; package com.ctrip.framework.apollo.spring.config;
import com.google.common.collect.HashMultimap; import java.util.Collection;
import com.google.common.collect.ImmutableSortedSet; import java.util.Iterator;
import com.google.common.collect.Multimap;
import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigService;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor; import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
...@@ -17,9 +13,11 @@ import org.springframework.core.env.CompositePropertySource; ...@@ -17,9 +13,11 @@ import org.springframework.core.env.CompositePropertySource;
import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import java.util.Collection; import com.ctrip.framework.apollo.Config;
import java.util.Iterator; import com.ctrip.framework.apollo.ConfigService;
import java.util.concurrent.atomic.AtomicBoolean; import com.google.common.collect.HashMultimap;
import com.google.common.collect.ImmutableSortedSet;
import com.google.common.collect.Multimap;
/** /**
* Apollo Property Sources processor for Spring Annotation Based Application * Apollo Property Sources processor for Spring Annotation Based Application
......
package com.ctrip.framework.apollo.spring.util; package com.ctrip.framework.apollo.spring.util;
import java.util.Objects;
import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import java.util.Objects;
/** /**
* @author Jason Song(song_s@ctrip.com) * @author Jason Song(song_s@ctrip.com)
*/ */
......
package com.ctrip.framework.apollo.util; package com.ctrip.framework.apollo.util;
import com.google.common.base.Strings; import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.ctrip.framework.apollo.core.ConfigConsts; import com.ctrip.framework.apollo.core.ConfigConsts;
import com.ctrip.framework.apollo.core.MetaDomainConsts; import com.ctrip.framework.apollo.core.MetaDomainConsts;
...@@ -8,17 +11,11 @@ import com.ctrip.framework.apollo.core.enums.Env; ...@@ -8,17 +11,11 @@ import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.core.enums.EnvUtils; import com.ctrip.framework.apollo.core.enums.EnvUtils;
import com.ctrip.framework.apollo.exceptions.ApolloConfigException; import com.ctrip.framework.apollo.exceptions.ApolloConfigException;
import com.ctrip.framework.foundation.Foundation; import com.ctrip.framework.foundation.Foundation;
import com.google.common.base.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.unidal.lookup.annotation.Named;
import java.util.concurrent.TimeUnit;
/** /**
* @author Jason Song(song_s@ctrip.com) * @author Jason Song(song_s@ctrip.com)
*/ */
@Named(type = ConfigUtil.class)
public class ConfigUtil { public class ConfigUtil {
private static final Logger logger = LoggerFactory.getLogger(ConfigUtil.class); private static final Logger logger = LoggerFactory.getLogger(ConfigUtil.class);
private static final String TOOLING_CLUSTER = "tooling"; private static final String TOOLING_CLUSTER = "tooling";
......
package com.ctrip.framework.apollo.util; package com.ctrip.framework.apollo.util;
import java.util.List;
import com.google.common.base.Strings; import com.google.common.base.Strings;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import java.util.List;
/** /**
* @author Jason Song(song_s@ctrip.com) * @author Jason Song(song_s@ctrip.com)
*/ */
......
package com.ctrip.framework.apollo.util.function; package com.ctrip.framework.apollo.util.function;
import com.google.common.base.Function; import java.util.Date;
import com.ctrip.framework.apollo.exceptions.ApolloConfigException; import com.ctrip.framework.apollo.exceptions.ApolloConfigException;
import com.ctrip.framework.apollo.util.parser.ParserException; import com.ctrip.framework.apollo.util.parser.ParserException;
import com.ctrip.framework.apollo.util.parser.Parsers; import com.ctrip.framework.apollo.util.parser.Parsers;
import com.google.common.base.Function;
import java.util.Date;
/** /**
* @author Jason Song(song_s@ctrip.com) * @author Jason Song(song_s@ctrip.com)
......
package com.ctrip.framework.apollo.util.http; package com.ctrip.framework.apollo.util.http;
import com.google.common.base.Charsets;
import com.google.common.base.Function;
import com.google.common.io.BaseEncoding;
import com.google.gson.Gson;
import com.ctrip.framework.apollo.exceptions.ApolloConfigException;
import com.ctrip.framework.apollo.exceptions.ApolloConfigStatusCodeException;
import com.ctrip.framework.apollo.util.ConfigUtil;
import org.unidal.helper.Files;
import org.unidal.lookup.annotation.Inject;
import org.unidal.lookup.annotation.Named;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import com.ctrip.framework.apollo.build.ApolloInjector;
import com.ctrip.framework.apollo.exceptions.ApolloConfigException;
import com.ctrip.framework.apollo.exceptions.ApolloConfigStatusCodeException;
import com.ctrip.framework.apollo.util.ConfigUtil;
import com.google.common.base.Function;
import com.google.common.io.BaseEncoding;
import com.google.common.io.CharStreams;
import com.google.gson.Gson;
/** /**
* @author Jason Song(song_s@ctrip.com) * @author Jason Song(song_s@ctrip.com)
*/ */
@Named(type = HttpUtil.class)
public class HttpUtil { public class HttpUtil {
@Inject
private ConfigUtil m_configUtil; private ConfigUtil m_configUtil;
private Gson gson; private Gson gson;
private String basicAuth; private String basicAuth;
...@@ -34,6 +29,7 @@ public class HttpUtil { ...@@ -34,6 +29,7 @@ public class HttpUtil {
* Constructor. * Constructor.
*/ */
public HttpUtil() { public HttpUtil() {
m_configUtil = ApolloInjector.getInstance(ConfigUtil.class);
gson = new Gson(); gson = new Gson();
try { try {
basicAuth = "Basic " + BaseEncoding.base64().encode("user:".getBytes("UTF-8")); basicAuth = "Basic " + BaseEncoding.base64().encode("user:".getBytes("UTF-8"));
...@@ -82,7 +78,7 @@ public class HttpUtil { ...@@ -82,7 +78,7 @@ public class HttpUtil {
private <T> HttpResponse<T> doGetWithSerializeFunction(HttpRequest httpRequest, private <T> HttpResponse<T> doGetWithSerializeFunction(HttpRequest httpRequest,
Function<String, T> serializeFunction) { Function<String, T> serializeFunction) {
InputStream is = null; InputStreamReader isr = null;
int statusCode; int statusCode;
try { try {
HttpURLConnection conn = (HttpURLConnection) new URL(httpRequest.getUrl()).openConnection(); HttpURLConnection conn = (HttpURLConnection) new URL(httpRequest.getUrl()).openConnection();
...@@ -108,8 +104,8 @@ public class HttpUtil { ...@@ -108,8 +104,8 @@ public class HttpUtil {
statusCode = conn.getResponseCode(); statusCode = conn.getResponseCode();
if (statusCode == 200) { if (statusCode == 200) {
is = conn.getInputStream(); isr = new InputStreamReader(conn.getInputStream());
String content = Files.IO.INSTANCE.readFrom(is, Charsets.UTF_8.name()); String content = CharStreams.toString(isr);
return new HttpResponse<>(statusCode, serializeFunction.apply(content)); return new HttpResponse<>(statusCode, serializeFunction.apply(content));
} }
...@@ -120,10 +116,10 @@ public class HttpUtil { ...@@ -120,10 +116,10 @@ public class HttpUtil {
} catch (Throwable ex) { } catch (Throwable ex) {
throw new ApolloConfigException("Could not complete get operation", ex); throw new ApolloConfigException("Could not complete get operation", ex);
} finally { } finally {
if (is != null) { if (isr != null) {
try { try {
is.close(); isr.close();
} catch (IOException ex) { } catch (IOException e) {
// ignore // ignore
} }
} }
......
<plexus>
<components>
<component>
<role>com.ctrip.framework.apollo.internals.ConfigManager</role>
<implementation>com.ctrip.framework.apollo.internals.DefaultConfigManager</implementation>
<requirements>
<requirement>
<role>com.ctrip.framework.apollo.spi.ConfigFactoryManager</role>
</requirement>
</requirements>
</component>
<component>
<role>com.ctrip.framework.apollo.spi.ConfigFactory</role>
<implementation>com.ctrip.framework.apollo.spi.DefaultConfigFactory</implementation>
<requirements>
<requirement>
<role>com.ctrip.framework.apollo.util.ConfigUtil</role>
</requirement>
</requirements>
</component>
<component>
<role>com.ctrip.framework.apollo.spi.ConfigRegistry</role>
<implementation>com.ctrip.framework.apollo.spi.DefaultConfigRegistry</implementation>
</component>
<component>
<role>com.ctrip.framework.apollo.spi.ConfigFactoryManager</role>
<implementation>com.ctrip.framework.apollo.spi.DefaultConfigFactoryManager</implementation>
<requirements>
<requirement>
<role>com.ctrip.framework.apollo.spi.ConfigRegistry</role>
</requirement>
</requirements>
</component>
<component>
<role>com.ctrip.framework.apollo.util.ConfigUtil</role>
<implementation>com.ctrip.framework.apollo.util.ConfigUtil</implementation>
</component>
<component>
<role>com.ctrip.framework.apollo.util.http.HttpUtil</role>
<implementation>com.ctrip.framework.apollo.util.http.HttpUtil</implementation>
<requirements>
<requirement>
<role>com.ctrip.framework.apollo.util.ConfigUtil</role>
</requirement>
</requirements>
</component>
<component>
<role>com.ctrip.framework.apollo.internals.ConfigServiceLocator</role>
<implementation>com.ctrip.framework.apollo.internals.ConfigServiceLocator</implementation>
<requirements>
<requirement>
<role>com.ctrip.framework.apollo.util.http.HttpUtil</role>
</requirement>
<requirement>
<role>com.ctrip.framework.apollo.util.ConfigUtil</role>
</requirement>
</requirements>
</component>
<component>
<role>com.ctrip.framework.apollo.internals.RemoteConfigLongPollService</role>
<implementation>com.ctrip.framework.apollo.internals.RemoteConfigLongPollService</implementation>
<requirements>
<requirement>
<role>com.ctrip.framework.apollo.util.ConfigUtil</role>
</requirement>
<requirement>
<role>com.ctrip.framework.apollo.util.http.HttpUtil</role>
</requirement>
<requirement>
<role>com.ctrip.framework.apollo.internals.ConfigServiceLocator</role>
</requirement>
</requirements>
</component>
</components>
</plexus>
com.ctrip.framework.apollo.internals.DefaultInjector
\ No newline at end of file
package com.ctrip.framework.apollo; package com.ctrip.framework.apollo;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
import com.ctrip.framework.apollo.integration.ConfigIntegrationTest; import com.ctrip.framework.apollo.integration.ConfigIntegrationTest;
import com.ctrip.framework.apollo.internals.DefaultConfigManagerTest; import com.ctrip.framework.apollo.internals.DefaultConfigManagerTest;
import com.ctrip.framework.apollo.internals.DefaultConfigTest; import com.ctrip.framework.apollo.internals.DefaultConfigTest;
...@@ -22,10 +26,6 @@ import com.ctrip.framework.apollo.util.ExceptionUtilTest; ...@@ -22,10 +26,6 @@ import com.ctrip.framework.apollo.util.ExceptionUtilTest;
import com.ctrip.framework.apollo.util.parser.DateParserTest; import com.ctrip.framework.apollo.util.parser.DateParserTest;
import com.ctrip.framework.apollo.util.parser.DurationParserTest; import com.ctrip.framework.apollo.util.parser.DurationParserTest;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class) @RunWith(Suite.class)
@SuiteClasses({ @SuiteClasses({
ConfigServiceTest.class, DefaultConfigRegistryTest.class, DefaultConfigFactoryManagerTest.class, ConfigServiceTest.class, DefaultConfigRegistryTest.class, DefaultConfigFactoryManagerTest.class,
......
package com.ctrip.framework.apollo; package com.ctrip.framework.apollo;
import com.google.common.base.Charsets; import java.io.File;
import com.google.common.collect.Lists; import java.io.IOException;
import com.google.common.io.Files; import java.net.ServerSocket;
import com.google.gson.Gson; import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import com.ctrip.framework.apollo.core.dto.ServiceDTO; import javax.servlet.ServletException;
import com.ctrip.framework.apollo.core.enums.Env; import javax.servlet.http.HttpServletRequest;
import com.ctrip.framework.apollo.core.utils.ClassLoaderUtil; import javax.servlet.http.HttpServletResponse;
import com.ctrip.framework.apollo.util.ConfigUtil;
import org.eclipse.jetty.server.Request; import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.Server;
...@@ -18,22 +18,22 @@ import org.eclipse.jetty.server.handler.ContextHandlerCollection; ...@@ -18,22 +18,22 @@ import org.eclipse.jetty.server.handler.ContextHandlerCollection;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.unidal.lookup.ComponentTestCase;
import java.io.File; import com.ctrip.framework.apollo.build.MockInjector;
import java.io.IOException; import com.ctrip.framework.apollo.core.dto.ServiceDTO;
import java.net.ServerSocket; import com.ctrip.framework.apollo.core.enums.Env;
import java.util.concurrent.TimeUnit; import com.ctrip.framework.apollo.core.utils.ClassLoaderUtil;
import java.util.concurrent.atomic.AtomicInteger; import com.ctrip.framework.apollo.internals.DefaultInjector;
import com.ctrip.framework.apollo.util.ConfigUtil;
import javax.servlet.ServletException; import com.google.common.base.Charsets;
import javax.servlet.http.HttpServletRequest; import com.google.common.collect.Lists;
import javax.servlet.http.HttpServletResponse; import com.google.common.io.Files;
import com.google.gson.Gson;
/** /**
* @author Jason Song(song_s@ctrip.com) * @author Jason Song(song_s@ctrip.com)
*/ */
public abstract class BaseIntegrationTest extends ComponentTestCase { public abstract class BaseIntegrationTest{
private static final int PORT = findFreePort(); private static final int PORT = findFreePort();
private static final String metaServiceUrl = "http://localhost:" + PORT; private static final String metaServiceUrl = "http://localhost:" + PORT;
private static final String someAppName = "someAppName"; private static final String someAppName = "someAppName";
...@@ -56,8 +56,6 @@ public abstract class BaseIntegrationTest extends ComponentTestCase { ...@@ -56,8 +56,6 @@ public abstract class BaseIntegrationTest extends ComponentTestCase {
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
super.tearDown();//clear the container
super.setUp();
someAppId = "1003171"; someAppId = "1003171";
someClusterName = "someClusterName"; someClusterName = "someClusterName";
someDataCenter = "someDC"; someDataCenter = "someDC";
...@@ -65,9 +63,11 @@ public abstract class BaseIntegrationTest extends ComponentTestCase { ...@@ -65,9 +63,11 @@ public abstract class BaseIntegrationTest extends ComponentTestCase {
refreshTimeUnit = TimeUnit.MINUTES; refreshTimeUnit = TimeUnit.MINUTES;
//as ConfigService is singleton, so we must manually clear its container //as ConfigService is singleton, so we must manually clear its container
ConfigService.setContainer(getContainer()); ConfigService.reset();
MockInjector.reset();
MockInjector.setDelegate(new DefaultInjector());
defineComponent(ConfigUtil.class, MockConfigUtil.class); MockInjector.setInstance(ConfigUtil.class, new MockConfigUtil());
} }
/** /**
...@@ -93,7 +93,6 @@ public abstract class BaseIntegrationTest extends ComponentTestCase { ...@@ -93,7 +93,6 @@ public abstract class BaseIntegrationTest extends ComponentTestCase {
if (server != null && server.isStarted()) { if (server != null && server.isStarted()) {
server.stop(); server.stop();
} }
super.tearDown();
} }
protected ContextHandler mockMetaServerHandler() { protected ContextHandler mockMetaServerHandler() {
......
package com.ctrip.framework.apollo; package com.ctrip.framework.apollo;
import static org.junit.Assert.assertEquals;
import java.util.Set;
import org.junit.Before;
import org.junit.Test;
import com.ctrip.framework.apollo.build.MockInjector;
import com.ctrip.framework.apollo.core.ConfigConsts; import com.ctrip.framework.apollo.core.ConfigConsts;
import com.ctrip.framework.apollo.core.enums.ConfigFileFormat; import com.ctrip.framework.apollo.core.enums.ConfigFileFormat;
import com.ctrip.framework.apollo.internals.AbstractConfig; import com.ctrip.framework.apollo.internals.AbstractConfig;
import com.ctrip.framework.apollo.internals.DefaultInjector;
import com.ctrip.framework.apollo.spi.ConfigFactory; import com.ctrip.framework.apollo.spi.ConfigFactory;
import com.ctrip.framework.apollo.util.ConfigUtil; import com.ctrip.framework.apollo.util.ConfigUtil;
import org.junit.Before;
import org.junit.Test;
import org.unidal.lookup.ComponentTestCase;
import java.util.Set;
import static org.junit.Assert.assertEquals;
/** /**
* @author Jason Song(song_s@ctrip.com) * @author Jason Song(song_s@ctrip.com)
*/ */
public class ConfigServiceTest extends ComponentTestCase { public class ConfigServiceTest {
private static String someAppId; private static String someAppId;
@Override
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
super.tearDown();//clear the container
super.setUp();
someAppId = "someAppId"; someAppId = "someAppId";
//as ConfigService is singleton, so we must manually clear its container //as ConfigService is singleton, so we must manually clear its container
ConfigService.setContainer(getContainer()); ConfigService.reset();
defineComponent(ConfigUtil.class, MockConfigUtil.class); MockInjector.reset();
MockInjector.setDelegate(new DefaultInjector());
MockInjector.setInstance(ConfigUtil.class, new MockConfigUtil());
} }
@Test @Test
...@@ -58,7 +59,7 @@ public class ConfigServiceTest extends ComponentTestCase { ...@@ -58,7 +59,7 @@ public class ConfigServiceTest extends ComponentTestCase {
public void testMockConfigFactory() throws Exception { public void testMockConfigFactory() throws Exception {
String someNamespace = "mock"; String someNamespace = "mock";
String someKey = "someKey"; String someKey = "someKey";
defineComponent(ConfigFactory.class, someNamespace, MockConfigFactory.class); MockInjector.setInstance(ConfigFactory.class, someNamespace, new MockConfigFactory());
Config config = ConfigService.getConfig(someNamespace); Config config = ConfigService.getConfig(someNamespace);
...@@ -72,7 +73,7 @@ public class ConfigServiceTest extends ComponentTestCase { ...@@ -72,7 +73,7 @@ public class ConfigServiceTest extends ComponentTestCase {
ConfigFileFormat someConfigFileFormat = ConfigFileFormat.Properties; ConfigFileFormat someConfigFileFormat = ConfigFileFormat.Properties;
String someNamespaceFileName = String someNamespaceFileName =
String.format("%s.%s", someNamespace, someConfigFileFormat.getValue()); String.format("%s.%s", someNamespace, someConfigFileFormat.getValue());
defineComponent(ConfigFactory.class, someNamespaceFileName, MockConfigFactory.class); MockInjector.setInstance(ConfigFactory.class, someNamespaceFileName, new MockConfigFactory());
ConfigFile configFile = ConfigService.getConfigFile(someNamespace, someConfigFileFormat); ConfigFile configFile = ConfigService.getConfigFile(someNamespace, someConfigFileFormat);
......
package com.ctrip.framework.apollo.build;
import java.util.Map;
import com.ctrip.framework.apollo.internals.DefaultInjector;
import com.ctrip.framework.apollo.internals.Injector;
import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Maps;
import com.google.common.collect.Table;
/**
* @author Jason Song(song_s@ctrip.com)
*/
public class MockInjector implements Injector {
private static Map<Class, Object> classMap = Maps.newHashMap();
private static Table<Class, String, Object> classTable = HashBasedTable.create();
private static Injector delegate = new DefaultInjector();
@Override
public <T> T getInstance(Class<T> clazz) {
T o = (T) classMap.get(clazz);
if (o != null) {
return o;
}
if (delegate != null) {
return delegate.getInstance(clazz);
}
return null;
}
@Override
public <T> T getInstance(Class<T> clazz, String name) {
T o = (T) classTable.get(clazz, name);
if (o != null) {
return o;
}
if (delegate != null) {
return delegate.getInstance(clazz, name);
}
return null;
}
public static void setInstance(Class clazz, Object o) {
classMap.put(clazz, o);
}
public static void setInstance(Class clazz, String name, Object o) {
classTable.put(clazz, name, o);
}
public static void setDelegate(Injector delegateInjector) {
delegate = delegateInjector;
}
public static void reset() {
classMap.clear();
classTable.clear();
delegate = null;
}
}
package com.ctrip.framework.apollo.integration; package com.ctrip.framework.apollo.integration;
import com.google.common.base.Joiner; import static org.hamcrest.core.IsEqual.equalTo;
import com.google.common.collect.ImmutableMap; import static org.junit.Assert.assertEquals;
import com.google.common.collect.Lists; import static org.junit.Assert.assertThat;
import com.google.common.collect.Maps; import static org.junit.Assert.assertTrue;
import com.google.common.util.concurrent.SettableFuture;
import com.ctrip.framework.apollo.BaseIntegrationTest;
import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigChangeListener;
import com.ctrip.framework.apollo.ConfigService;
import com.ctrip.framework.apollo.core.ConfigConsts;
import com.ctrip.framework.apollo.core.dto.ApolloConfig;
import com.ctrip.framework.apollo.core.dto.ApolloConfigNotification;
import com.ctrip.framework.apollo.core.utils.ClassLoaderUtil;
import com.ctrip.framework.apollo.internals.RemoteConfigLongPollService;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.test.util.ReflectionTestUtils;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
...@@ -39,10 +19,30 @@ import javax.servlet.ServletException; ...@@ -39,10 +19,30 @@ import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import static org.hamcrest.core.IsEqual.equalTo; import org.eclipse.jetty.server.Request;
import static org.junit.Assert.assertEquals; import org.eclipse.jetty.server.handler.AbstractHandler;
import static org.junit.Assert.assertThat; import org.eclipse.jetty.server.handler.ContextHandler;
import static org.junit.Assert.assertTrue; import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.test.util.ReflectionTestUtils;
import com.ctrip.framework.apollo.BaseIntegrationTest;
import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigChangeListener;
import com.ctrip.framework.apollo.ConfigService;
import com.ctrip.framework.apollo.build.ApolloInjector;
import com.ctrip.framework.apollo.core.ConfigConsts;
import com.ctrip.framework.apollo.core.dto.ApolloConfig;
import com.ctrip.framework.apollo.core.dto.ApolloConfigNotification;
import com.ctrip.framework.apollo.core.utils.ClassLoaderUtil;
import com.ctrip.framework.apollo.internals.RemoteConfigLongPollService;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.util.concurrent.SettableFuture;
/** /**
* @author Jason Song(song_s@ctrip.com) * @author Jason Song(song_s@ctrip.com)
...@@ -66,7 +66,7 @@ public class ConfigIntegrationTest extends BaseIntegrationTest { ...@@ -66,7 +66,7 @@ public class ConfigIntegrationTest extends BaseIntegrationTest {
configDir.delete(); configDir.delete();
} }
configDir.mkdirs(); configDir.mkdirs();
remoteConfigLongPollService = lookup(RemoteConfigLongPollService.class); remoteConfigLongPollService = ApolloInjector.getInstance(RemoteConfigLongPollService.class);
} }
@Override @Override
......
package com.ctrip.framework.apollo.internals; package com.ctrip.framework.apollo.internals;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import java.util.Set;
import org.junit.Before;
import org.junit.Test;
import com.ctrip.framework.apollo.Config; import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigFile; import com.ctrip.framework.apollo.ConfigFile;
import com.ctrip.framework.apollo.build.MockInjector;
import com.ctrip.framework.apollo.core.enums.ConfigFileFormat; import com.ctrip.framework.apollo.core.enums.ConfigFileFormat;
import com.ctrip.framework.apollo.spi.ConfigFactory; import com.ctrip.framework.apollo.spi.ConfigFactory;
import com.ctrip.framework.apollo.spi.ConfigFactoryManager; import com.ctrip.framework.apollo.spi.ConfigFactoryManager;
import com.ctrip.framework.apollo.util.ConfigUtil;
import org.junit.Before;
import org.junit.Test;
import org.unidal.lookup.ComponentTestCase;
import java.util.Set;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
/** /**
* @author Jason Song(song_s@ctrip.com) * @author Jason Song(song_s@ctrip.com)
*/ */
public class DefaultConfigManagerTest extends ComponentTestCase { public class DefaultConfigManagerTest {
private DefaultConfigManager defaultConfigManager; private DefaultConfigManager defaultConfigManager;
private static String someConfigContent; private static String someConfigContent;
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
super.tearDown();//clear the container MockInjector.reset();
super.setUp(); MockInjector.setInstance(ConfigFactoryManager.class, new MockConfigFactoryManager());
defineComponent(ConfigFactoryManager.class, MockConfigFactoryManager.class); MockInjector.setInstance(ConfigUtil.class, new ConfigUtil());
defaultConfigManager = (DefaultConfigManager) lookup(ConfigManager.class); defaultConfigManager = new DefaultConfigManager();
someConfigContent = "someContent"; someConfigContent = "someContent";
} }
......
package com.ctrip.framework.apollo.internals; package com.ctrip.framework.apollo.internals;
import com.google.common.base.Charsets; import static org.junit.Assert.assertArrayEquals;
import com.google.common.base.Joiner; import static org.junit.Assert.assertEquals;
import com.google.common.collect.ImmutableMap; import static org.mockito.Mockito.mock;
import com.google.common.io.Files; import static org.mockito.Mockito.times;
import com.google.common.util.concurrent.SettableFuture; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.io.File;
import java.util.Calendar;
import java.util.Date;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.ctrip.framework.apollo.Config; import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigChangeListener; import com.ctrip.framework.apollo.ConfigChangeListener;
import com.ctrip.framework.apollo.build.MockInjector;
import com.ctrip.framework.apollo.core.utils.ClassLoaderUtil; import com.ctrip.framework.apollo.core.utils.ClassLoaderUtil;
import com.ctrip.framework.apollo.enums.PropertyChangeType; import com.ctrip.framework.apollo.enums.PropertyChangeType;
import com.ctrip.framework.apollo.model.ConfigChange; import com.ctrip.framework.apollo.model.ConfigChange;
import com.ctrip.framework.apollo.model.ConfigChangeEvent; import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.util.ConfigUtil; import com.ctrip.framework.apollo.util.ConfigUtil;
import com.google.common.base.Charsets;
import org.junit.After; import com.google.common.base.Joiner;
import org.junit.Before; import com.google.common.collect.ImmutableMap;
import org.junit.Test; import com.google.common.io.Files;
import org.unidal.lookup.ComponentTestCase; import com.google.common.util.concurrent.SettableFuture;
import java.io.File;
import java.util.Calendar;
import java.util.Date;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/** /**
* @author Jason Song(song_s@ctrip.com) * @author Jason Song(song_s@ctrip.com)
*/ */
public class DefaultConfigTest extends ComponentTestCase { public class DefaultConfigTest {
private File someResourceDir; private File someResourceDir;
private String someNamespace; private String someNamespace;
private ConfigRepository configRepository; private ConfigRepository configRepository;
...@@ -43,9 +42,8 @@ public class DefaultConfigTest extends ComponentTestCase { ...@@ -43,9 +42,8 @@ public class DefaultConfigTest extends ComponentTestCase {
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
super.tearDown();//clear the container MockInjector.reset();
super.setUp(); MockInjector.setInstance(ConfigUtil.class, new MockConfigUtil());
defineComponent(ConfigUtil.class, MockConfigUtil.class);
someResourceDir = new File(ClassLoaderUtil.getClassPath() + "/META-INF/config"); someResourceDir = new File(ClassLoaderUtil.getClassPath() + "/META-INF/config");
someResourceDir.mkdirs(); someResourceDir.mkdirs();
...@@ -55,7 +53,6 @@ public class DefaultConfigTest extends ComponentTestCase { ...@@ -55,7 +53,6 @@ public class DefaultConfigTest extends ComponentTestCase {
@After @After
public void tearDown() throws Exception { public void tearDown() throws Exception {
super.tearDown();
recursiveDelete(someResourceDir); recursiveDelete(someResourceDir);
} }
...@@ -197,7 +194,7 @@ public class DefaultConfigTest extends ComponentTestCase { ...@@ -197,7 +194,7 @@ public class DefaultConfigTest extends ComponentTestCase {
Integer someDefaultValue = -1; Integer someDefaultValue = -1;
defineComponent(ConfigUtil.class, MockConfigUtilWithSmallCache.class); MockInjector.setInstance(ConfigUtil.class, new MockConfigUtilWithSmallCache());
//set up config repo //set up config repo
someProperties = mock(Properties.class); someProperties = mock(Properties.class);
...@@ -230,7 +227,7 @@ public class DefaultConfigTest extends ComponentTestCase { ...@@ -230,7 +227,7 @@ public class DefaultConfigTest extends ComponentTestCase {
Integer someDefaultValue = -1; Integer someDefaultValue = -1;
defineComponent(ConfigUtil.class, MockConfigUtilWithShortExpireTime.class); MockInjector.setInstance(ConfigUtil.class, new MockConfigUtilWithShortExpireTime());
//set up config repo //set up config repo
someProperties = mock(Properties.class); someProperties = mock(Properties.class);
......
package com.ctrip.framework.apollo.internals; package com.ctrip.framework.apollo.internals;
import com.ctrip.framework.apollo.core.ConfigConsts; import static org.junit.Assert.assertEquals;
import com.ctrip.framework.apollo.core.enums.ConfigFileFormat; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;
import java.util.Properties;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
...@@ -9,13 +14,8 @@ import org.junit.runner.RunWith; ...@@ -9,13 +14,8 @@ import org.junit.runner.RunWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner; import org.mockito.runners.MockitoJUnitRunner;
import java.util.Properties; import com.ctrip.framework.apollo.core.ConfigConsts;
import com.ctrip.framework.apollo.core.enums.ConfigFileFormat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;
/** /**
* @author Jason Song(song_s@ctrip.com) * @author Jason Song(song_s@ctrip.com)
......
package com.ctrip.framework.apollo.internals; package com.ctrip.framework.apollo.internals;
import com.google.common.base.Charsets;
import com.google.common.base.Joiner;
import com.google.common.io.Files;
import com.ctrip.framework.apollo.core.ConfigConsts;
import com.ctrip.framework.apollo.util.ConfigUtil;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.unidal.lookup.ComponentTestCase;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import static org.hamcrest.core.IsEqual.equalTo; import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
...@@ -27,10 +9,27 @@ import static org.mockito.Mockito.times; ...@@ -27,10 +9,27 @@ import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import com.ctrip.framework.apollo.build.MockInjector;
import com.ctrip.framework.apollo.core.ConfigConsts;
import com.ctrip.framework.apollo.util.ConfigUtil;
import com.google.common.base.Charsets;
import com.google.common.base.Joiner;
import com.google.common.io.Files;
/** /**
* Created by Jason on 4/9/16. * Created by Jason on 4/9/16.
*/ */
public class LocalFileConfigRepositoryTest extends ComponentTestCase { public class LocalFileConfigRepositoryTest {
private File someBaseDir; private File someBaseDir;
private String someNamespace; private String someNamespace;
private ConfigRepository upstreamRepo; private ConfigRepository upstreamRepo;
...@@ -42,8 +41,6 @@ public class LocalFileConfigRepositoryTest extends ComponentTestCase { ...@@ -42,8 +41,6 @@ public class LocalFileConfigRepositoryTest extends ComponentTestCase {
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
super.tearDown();//clear the container
super.setUp();
someBaseDir = new File("src/test/resources/config-cache"); someBaseDir = new File("src/test/resources/config-cache");
someBaseDir.mkdir(); someBaseDir.mkdir();
...@@ -55,12 +52,12 @@ public class LocalFileConfigRepositoryTest extends ComponentTestCase { ...@@ -55,12 +52,12 @@ public class LocalFileConfigRepositoryTest extends ComponentTestCase {
upstreamRepo = mock(ConfigRepository.class); upstreamRepo = mock(ConfigRepository.class);
when(upstreamRepo.getConfig()).thenReturn(someProperties); when(upstreamRepo.getConfig()).thenReturn(someProperties);
defineComponent(ConfigUtil.class, MockConfigUtil.class); MockInjector.reset();
MockInjector.setInstance(ConfigUtil.class, new MockConfigUtil());
} }
@After @After
public void tearDown() throws Exception { public void tearDown() throws Exception {
super.tearDown();
recursiveDelete(someBaseDir); recursiveDelete(someBaseDir);
} }
......
package com.ctrip.framework.apollo.internals; package com.ctrip.framework.apollo.internals;
import com.ctrip.framework.apollo.core.enums.ConfigFileFormat; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;
import java.util.Properties;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
...@@ -8,13 +14,7 @@ import org.junit.runner.RunWith; ...@@ -8,13 +14,7 @@ import org.junit.runner.RunWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner; import org.mockito.runners.MockitoJUnitRunner;
import java.util.Properties; import com.ctrip.framework.apollo.core.enums.ConfigFileFormat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;
/** /**
* @author Jason Song(song_s@ctrip.com) * @author Jason Song(song_s@ctrip.com)
......
package com.ctrip.framework.apollo.internals; package com.ctrip.framework.apollo.internals;
import com.google.common.collect.ImmutableMap; import static org.junit.Assert.assertTrue;
import com.google.common.collect.Lists; import static org.mockito.Matchers.any;
import com.google.common.util.concurrent.SettableFuture; import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import com.ctrip.framework.apollo.core.dto.ApolloConfigNotification; import java.lang.reflect.Type;
import com.ctrip.framework.apollo.core.dto.ServiceDTO; import java.util.List;
import com.ctrip.framework.apollo.util.ConfigUtil; import java.util.Map;
import com.ctrip.framework.apollo.util.http.HttpRequest; import java.util.concurrent.TimeUnit;
import com.ctrip.framework.apollo.util.http.HttpResponse; import java.util.concurrent.atomic.AtomicInteger;
import com.ctrip.framework.apollo.util.http.HttpUtil;
import javax.servlet.http.HttpServletResponse;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
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;
...@@ -20,36 +26,30 @@ import org.mockito.invocation.InvocationOnMock; ...@@ -20,36 +26,30 @@ import org.mockito.invocation.InvocationOnMock;
import org.mockito.runners.MockitoJUnitRunner; import org.mockito.runners.MockitoJUnitRunner;
import org.mockito.stubbing.Answer; import org.mockito.stubbing.Answer;
import org.springframework.test.util.ReflectionTestUtils; import org.springframework.test.util.ReflectionTestUtils;
import org.unidal.lookup.ComponentTestCase;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import javax.servlet.http.HttpServletResponse; import com.ctrip.framework.apollo.build.MockInjector;
import com.ctrip.framework.apollo.core.dto.ApolloConfigNotification;
import static org.junit.Assert.assertTrue; import com.ctrip.framework.apollo.core.dto.ServiceDTO;
import static org.mockito.Matchers.any; import com.ctrip.framework.apollo.util.ConfigUtil;
import static org.mockito.Matchers.eq; import com.ctrip.framework.apollo.util.http.HttpRequest;
import static org.mockito.Mockito.doAnswer; import com.ctrip.framework.apollo.util.http.HttpResponse;
import static org.mockito.Mockito.mock; import com.ctrip.framework.apollo.util.http.HttpUtil;
import static org.mockito.Mockito.never; import com.google.common.collect.ImmutableMap;
import static org.mockito.Mockito.times; import com.google.common.collect.Lists;
import static org.mockito.Mockito.verify; import com.google.common.util.concurrent.SettableFuture;
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 RemoteConfigLongPollServiceTest extends ComponentTestCase { public class RemoteConfigLongPollServiceTest {
private RemoteConfigLongPollService remoteConfigLongPollService; private RemoteConfigLongPollService remoteConfigLongPollService;
@Mock @Mock
private HttpResponse<List<ApolloConfigNotification>> pollResponse; private HttpResponse<List<ApolloConfigNotification>> pollResponse;
@Mock @Mock
private HttpUtil httpUtil; private HttpUtil httpUtil;
@Mock
private ConfigServiceLocator configServiceLocator;
private Type responseType; private Type responseType;
private static String someServerUrl; private static String someServerUrl;
...@@ -58,15 +58,19 @@ public class RemoteConfigLongPollServiceTest extends ComponentTestCase { ...@@ -58,15 +58,19 @@ public class RemoteConfigLongPollServiceTest extends ComponentTestCase {
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
super.tearDown();//clear the container MockInjector.reset();
super.setUp();
MockInjector.setInstance(HttpUtil.class, httpUtil);
ServiceDTO serviceDTO = mock(ServiceDTO.class);
when(serviceDTO.getHomepageUrl()).thenReturn(someServerUrl);
when(configServiceLocator.getConfigServices()).thenReturn(Lists.newArrayList(serviceDTO));
MockInjector.setInstance(ConfigServiceLocator.class, configServiceLocator);
defineComponent(ConfigUtil.class, MockConfigUtil.class); MockInjector.setInstance(ConfigUtil.class, new MockConfigUtil());
defineComponent(ConfigServiceLocator.class, MockConfigServiceLocator.class);
remoteConfigLongPollService = lookup(RemoteConfigLongPollService.class); remoteConfigLongPollService = new RemoteConfigLongPollService();
ReflectionTestUtils.setField(remoteConfigLongPollService, "m_httpUtil", httpUtil);
responseType = responseType =
(Type) ReflectionTestUtils.getField(remoteConfigLongPollService, "m_responseType"); (Type) ReflectionTestUtils.getField(remoteConfigLongPollService, "m_responseType");
...@@ -365,18 +369,4 @@ public class RemoteConfigLongPollServiceTest extends ComponentTestCase { ...@@ -365,18 +369,4 @@ public class RemoteConfigLongPollServiceTest extends ComponentTestCase {
} }
} }
public static class MockConfigServiceLocator extends ConfigServiceLocator {
@Override
public List<ServiceDTO> getConfigServices() {
ServiceDTO serviceDTO = mock(ServiceDTO.class);
when(serviceDTO.getHomepageUrl()).thenReturn(someServerUrl);
return Lists.newArrayList(serviceDTO);
}
@Override
public void initialize() throws InitializationException {
//do nothing
}
}
} }
package com.ctrip.framework.apollo.internals; package com.ctrip.framework.apollo.internals;
import com.google.common.collect.ImmutableMap; import static org.junit.Assert.assertEquals;
import com.google.common.collect.Lists; import static org.junit.Assert.assertTrue;
import com.google.common.collect.Maps; import static org.mockito.Matchers.eq;
import com.google.common.util.concurrent.SettableFuture; import static org.mockito.Mockito.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import com.ctrip.framework.apollo.core.dto.ApolloConfig; import java.lang.reflect.Type;
import com.ctrip.framework.apollo.core.dto.ApolloConfigNotification; import java.util.List;
import com.ctrip.framework.apollo.core.dto.ServiceDTO; import java.util.Map;
import com.ctrip.framework.apollo.exceptions.ApolloConfigException; import java.util.Properties;
import com.ctrip.framework.apollo.util.ConfigUtil; import java.util.concurrent.TimeUnit;
import com.ctrip.framework.apollo.util.http.HttpRequest;
import com.ctrip.framework.apollo.util.http.HttpResponse; import javax.servlet.http.HttpServletResponse;
import com.ctrip.framework.apollo.util.http.HttpUtil;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
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;
...@@ -23,31 +26,26 @@ import org.mockito.Mock; ...@@ -23,31 +26,26 @@ import org.mockito.Mock;
import org.mockito.invocation.InvocationOnMock; import org.mockito.invocation.InvocationOnMock;
import org.mockito.runners.MockitoJUnitRunner; import org.mockito.runners.MockitoJUnitRunner;
import org.mockito.stubbing.Answer; import org.mockito.stubbing.Answer;
import org.unidal.lookup.ComponentTestCase;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import javax.servlet.http.HttpServletResponse;
import static org.junit.Assert.assertEquals; import com.ctrip.framework.apollo.build.MockInjector;
import static org.junit.Assert.assertTrue; import com.ctrip.framework.apollo.core.dto.ApolloConfig;
import static org.mockito.Matchers.eq; import com.ctrip.framework.apollo.core.dto.ApolloConfigNotification;
import static org.mockito.Mockito.any; import com.ctrip.framework.apollo.core.dto.ServiceDTO;
import static org.mockito.Mockito.doAnswer; import com.ctrip.framework.apollo.exceptions.ApolloConfigException;
import static org.mockito.Mockito.mock; import com.ctrip.framework.apollo.util.ConfigUtil;
import static org.mockito.Mockito.times; import com.ctrip.framework.apollo.util.http.HttpRequest;
import static org.mockito.Mockito.verify; import com.ctrip.framework.apollo.util.http.HttpResponse;
import static org.mockito.Mockito.when; import com.ctrip.framework.apollo.util.http.HttpUtil;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.util.concurrent.SettableFuture;
/** /**
* Created by Jason on 4/9/16. * Created by Jason on 4/9/16.
*/ */
@RunWith(MockitoJUnitRunner.class) @RunWith(MockitoJUnitRunner.class)
public class RemoteConfigRepositoryTest extends ComponentTestCase { public class RemoteConfigRepositoryTest {
@Mock @Mock
private ConfigServiceLocator configServiceLocator; private ConfigServiceLocator configServiceLocator;
private String someNamespace; private String someNamespace;
...@@ -59,17 +57,26 @@ public class RemoteConfigRepositoryTest extends ComponentTestCase { ...@@ -59,17 +57,26 @@ public class RemoteConfigRepositoryTest extends ComponentTestCase {
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
super.tearDown();//clear the container
super.setUp();
someNamespace = "someName"; someNamespace = "someName";
when(pollResponse.getStatusCode()).thenReturn(HttpServletResponse.SC_NOT_MODIFIED); when(pollResponse.getStatusCode()).thenReturn(HttpServletResponse.SC_NOT_MODIFIED);
defineComponent(ConfigUtil.class, MockConfigUtil.class); MockInjector.reset();
defineComponent(ConfigServiceLocator.class, MockConfigServiceLocator.class); MockInjector.setInstance(ConfigUtil.class, new MockConfigUtil());
defineComponent(HttpUtil.class, MockHttpUtil.class);
String someServerUrl = "http://someServer";
ServiceDTO serviceDTO = mock(ServiceDTO.class);
remoteConfigLongPollService = lookup(RemoteConfigLongPollService.class); when(serviceDTO.getHomepageUrl()).thenReturn(someServerUrl);
when(configServiceLocator.getConfigServices()).thenReturn(Lists.newArrayList(serviceDTO));
MockInjector.setInstance(ConfigServiceLocator.class, configServiceLocator);
MockInjector.setInstance(HttpUtil.class, new MockHttpUtil());
remoteConfigLongPollService = new RemoteConfigLongPollService();
MockInjector.setInstance(RemoteConfigLongPollService.class, remoteConfigLongPollService);
} }
@Test @Test
...@@ -245,23 +252,6 @@ public class RemoteConfigRepositoryTest extends ComponentTestCase { ...@@ -245,23 +252,6 @@ public class RemoteConfigRepositoryTest extends ComponentTestCase {
} }
} }
public static class MockConfigServiceLocator extends ConfigServiceLocator {
@Override
public List<ServiceDTO> getConfigServices() {
String someServerUrl = "http://someServer";
ServiceDTO serviceDTO = mock(ServiceDTO.class);
when(serviceDTO.getHomepageUrl()).thenReturn(someServerUrl);
return Lists.newArrayList(serviceDTO);
}
@Override
public void initialize() throws InitializationException {
//do nothing
}
}
public static class MockHttpUtil extends HttpUtil { public static class MockHttpUtil extends HttpUtil {
@Override @Override
public <T> HttpResponse<T> doGet(HttpRequest httpRequest, Class<T> responseType) { public <T> HttpResponse<T> doGet(HttpRequest httpRequest, Class<T> responseType) {
......
package com.ctrip.framework.apollo.internals; package com.ctrip.framework.apollo.internals;
import com.google.common.collect.ImmutableMap; import static org.junit.Assert.assertEquals;
import com.google.common.util.concurrent.SettableFuture; import static org.mockito.Mockito.when;
import com.ctrip.framework.apollo.Config; import java.util.Properties;
import com.ctrip.framework.apollo.ConfigChangeListener; import java.util.concurrent.TimeUnit;
import com.ctrip.framework.apollo.enums.PropertyChangeType;
import com.ctrip.framework.apollo.model.ConfigChange;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
...@@ -15,11 +12,13 @@ import org.junit.runner.RunWith; ...@@ -15,11 +12,13 @@ import org.junit.runner.RunWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner; import org.mockito.runners.MockitoJUnitRunner;
import java.util.Properties; import com.ctrip.framework.apollo.Config;
import java.util.concurrent.TimeUnit; import com.ctrip.framework.apollo.ConfigChangeListener;
import com.ctrip.framework.apollo.enums.PropertyChangeType;
import static org.junit.Assert.assertEquals; import com.ctrip.framework.apollo.model.ConfigChange;
import static org.mockito.Mockito.when; import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.google.common.collect.ImmutableMap;
import com.google.common.util.concurrent.SettableFuture;
/** /**
* @author Jason Song(song_s@ctrip.com) * @author Jason Song(song_s@ctrip.com)
......
package com.ctrip.framework.apollo.internals; package com.ctrip.framework.apollo.internals;
import com.ctrip.framework.apollo.core.ConfigConsts; import static org.junit.Assert.assertEquals;
import com.ctrip.framework.apollo.core.enums.ConfigFileFormat; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;
import java.util.Properties;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
...@@ -9,13 +14,8 @@ import org.junit.runner.RunWith; ...@@ -9,13 +14,8 @@ import org.junit.runner.RunWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner; import org.mockito.runners.MockitoJUnitRunner;
import java.util.Properties; import com.ctrip.framework.apollo.core.ConfigConsts;
import com.ctrip.framework.apollo.core.enums.ConfigFileFormat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;
/** /**
* @author Jason Song(song_s@ctrip.com) * @author Jason Song(song_s@ctrip.com)
......
package com.ctrip.framework.apollo.spi; package com.ctrip.framework.apollo.spi;
import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigFile;
import com.ctrip.framework.apollo.core.enums.ConfigFileFormat;
import org.junit.Before;
import org.junit.Test;
import org.unidal.lookup.ComponentTestCase;
import static org.hamcrest.core.IsEqual.equalTo; import static org.hamcrest.core.IsEqual.equalTo;
import static org.hamcrest.core.IsInstanceOf.instanceOf; import static org.hamcrest.core.IsInstanceOf.instanceOf;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import org.junit.Before;
import org.junit.Test;
import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigFile;
import com.ctrip.framework.apollo.build.MockInjector;
import com.ctrip.framework.apollo.core.enums.ConfigFileFormat;
/** /**
* @author Jason Song(song_s@ctrip.com) * @author Jason Song(song_s@ctrip.com)
*/ */
public class DefaultConfigFactoryManagerTest extends ComponentTestCase { public class DefaultConfigFactoryManagerTest {
private DefaultConfigFactoryManager defaultConfigFactoryManager; private DefaultConfigFactoryManager defaultConfigFactoryManager;
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
super.tearDown();//clear the container MockInjector.reset();
super.setUp(); MockInjector.setInstance(ConfigRegistry.class, new MockConfigRegistry());
defineComponent(ConfigRegistry.class, MockConfigRegistry.class); defaultConfigFactoryManager = new DefaultConfigFactoryManager();
defaultConfigFactoryManager =
(DefaultConfigFactoryManager) lookup(ConfigFactoryManager.class);
} }
@Test @Test
...@@ -39,7 +37,7 @@ public class DefaultConfigFactoryManagerTest extends ComponentTestCase { ...@@ -39,7 +37,7 @@ public class DefaultConfigFactoryManagerTest extends ComponentTestCase {
@Test @Test
public void testGetFactoryFromNamespace() throws Exception { public void testGetFactoryFromNamespace() throws Exception {
String someNamespace = "someName"; String someNamespace = "someName";
defineComponent(ConfigFactory.class, someNamespace, SomeConfigFactory.class); MockInjector.setInstance(ConfigFactory.class, someNamespace, new SomeConfigFactory());
ConfigFactory result = defaultConfigFactoryManager.getFactory(someNamespace); ConfigFactory result = defaultConfigFactoryManager.getFactory(someNamespace);
...@@ -50,7 +48,7 @@ public class DefaultConfigFactoryManagerTest extends ComponentTestCase { ...@@ -50,7 +48,7 @@ public class DefaultConfigFactoryManagerTest extends ComponentTestCase {
@Test @Test
public void testGetFactoryFromNamespaceMultipleTimes() throws Exception { public void testGetFactoryFromNamespaceMultipleTimes() throws Exception {
String someNamespace = "someName"; String someNamespace = "someName";
defineComponent(ConfigFactory.class, someNamespace, SomeConfigFactory.class); MockInjector.setInstance(ConfigFactory.class, someNamespace, new SomeConfigFactory());
ConfigFactory result = defaultConfigFactoryManager.getFactory(someNamespace); ConfigFactory result = defaultConfigFactoryManager.getFactory(someNamespace);
ConfigFactory anotherResult = defaultConfigFactoryManager.getFactory(someNamespace); ConfigFactory anotherResult = defaultConfigFactoryManager.getFactory(someNamespace);
...@@ -63,7 +61,7 @@ public class DefaultConfigFactoryManagerTest extends ComponentTestCase { ...@@ -63,7 +61,7 @@ public class DefaultConfigFactoryManagerTest extends ComponentTestCase {
@Test @Test
public void testGetFactoryFromDefault() throws Exception { public void testGetFactoryFromDefault() throws Exception {
String someNamespace = "someName"; String someNamespace = "someName";
defineComponent(ConfigFactory.class, AnotherConfigFactory.class); MockInjector.setInstance(ConfigFactory.class, new AnotherConfigFactory());
ConfigFactory result = defaultConfigFactoryManager.getFactory(someNamespace); ConfigFactory result = defaultConfigFactoryManager.getFactory(someNamespace);
......
package com.ctrip.framework.apollo.spi; package com.ctrip.framework.apollo.spi;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsInstanceOf.instanceOf;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import java.util.Properties;
import org.junit.Before;
import org.junit.Test;
import org.springframework.test.util.ReflectionTestUtils;
import com.ctrip.framework.apollo.Config; import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigFile; import com.ctrip.framework.apollo.ConfigFile;
import com.ctrip.framework.apollo.build.MockInjector;
import com.ctrip.framework.apollo.core.enums.ConfigFileFormat; import com.ctrip.framework.apollo.core.enums.ConfigFileFormat;
import com.ctrip.framework.apollo.core.enums.Env; import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.internals.DefaultConfig; import com.ctrip.framework.apollo.internals.DefaultConfig;
...@@ -13,39 +30,21 @@ import com.ctrip.framework.apollo.internals.YamlConfigFile; ...@@ -13,39 +30,21 @@ import com.ctrip.framework.apollo.internals.YamlConfigFile;
import com.ctrip.framework.apollo.internals.YmlConfigFile; import com.ctrip.framework.apollo.internals.YmlConfigFile;
import com.ctrip.framework.apollo.util.ConfigUtil; import com.ctrip.framework.apollo.util.ConfigUtil;
import org.junit.Before;
import org.junit.Test;
import org.springframework.test.util.ReflectionTestUtils;
import org.unidal.lookup.ComponentTestCase;
import java.util.Properties;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsInstanceOf.instanceOf;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
/** /**
* @author Jason Song(song_s@ctrip.com) * @author Jason Song(song_s@ctrip.com)
*/ */
public class DefaultConfigFactoryTest extends ComponentTestCase { public class DefaultConfigFactoryTest {
private DefaultConfigFactory defaultConfigFactory; private DefaultConfigFactory defaultConfigFactory;
private static String someAppId; private static String someAppId;
private static Env someEnv; private static Env someEnv;
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
super.tearDown();//clear the container
super.setUp();
someAppId = "someId"; someAppId = "someId";
someEnv = Env.DEV; someEnv = Env.DEV;
defineComponent(ConfigUtil.class, MockConfigUtil.class); MockInjector.reset();
defaultConfigFactory = spy((DefaultConfigFactory) lookup(ConfigFactory.class)); MockInjector.setInstance(ConfigUtil.class, new MockConfigUtil());
defaultConfigFactory = spy(new DefaultConfigFactory());
} }
@Test @Test
......
package com.ctrip.framework.apollo.spi; package com.ctrip.framework.apollo.spi;
import com.ctrip.framework.apollo.Config; import static org.hamcrest.core.IsEqual.equalTo;
import com.ctrip.framework.apollo.ConfigFile; import static org.junit.Assert.assertNull;
import com.ctrip.framework.apollo.core.enums.ConfigFileFormat; import static org.junit.Assert.assertThat;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.unidal.lookup.ComponentTestCase;
import static org.hamcrest.core.IsEqual.equalTo; import com.ctrip.framework.apollo.Config;
import static org.junit.Assert.assertNull; import com.ctrip.framework.apollo.ConfigFile;
import static org.junit.Assert.assertThat; import com.ctrip.framework.apollo.build.MockInjector;
import com.ctrip.framework.apollo.core.enums.ConfigFileFormat;
/** /**
* @author Jason Song(song_s@ctrip.com) * @author Jason Song(song_s@ctrip.com)
*/ */
public class DefaultConfigRegistryTest extends ComponentTestCase { public class DefaultConfigRegistryTest {
private DefaultConfigRegistry defaultConfigRegistry; private DefaultConfigRegistry defaultConfigRegistry;
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
super.tearDown();//clear the container MockInjector.reset();
super.setUp(); defaultConfigRegistry = new DefaultConfigRegistry();
defaultConfigRegistry = (DefaultConfigRegistry) lookup(ConfigRegistry.class);
} }
@Test @Test
......
package com.ctrip.framework.apollo.spring; package com.ctrip.framework.apollo.spring;
import com.google.common.collect.Maps; import java.lang.reflect.Method;
import java.util.Map;
import org.junit.After;
import org.junit.Before;
import org.springframework.util.ReflectionUtils;
import com.ctrip.framework.apollo.Config; import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigFile; import com.ctrip.framework.apollo.ConfigFile;
import com.ctrip.framework.apollo.ConfigService; import com.ctrip.framework.apollo.ConfigService;
import com.ctrip.framework.apollo.build.MockInjector;
import com.ctrip.framework.apollo.core.enums.ConfigFileFormat; import com.ctrip.framework.apollo.core.enums.ConfigFileFormat;
import com.ctrip.framework.apollo.internals.ConfigManager; import com.ctrip.framework.apollo.internals.ConfigManager;
import com.ctrip.framework.apollo.spring.config.PropertySourcesProcessor; import com.ctrip.framework.apollo.spring.config.PropertySourcesProcessor;
import com.google.common.collect.Maps;
import org.codehaus.plexus.PlexusContainer;
import org.junit.After;
import org.junit.Before;
import org.springframework.util.ReflectionUtils;
import org.unidal.lookup.ComponentTestCase;
import java.lang.reflect.Method;
import java.util.Map;
/** /**
* @author Jason Song(song_s@ctrip.com) * @author Jason Song(song_s@ctrip.com)
*/ */
public abstract class AbstractSpringIntegrationTest extends ComponentTestCase { public abstract class AbstractSpringIntegrationTest {
private static final Map<String, Config> CONFIG_REGISTRY = Maps.newHashMap(); private static final Map<String, Config> CONFIG_REGISTRY = Maps.newHashMap();
private static Method PROPERTY_SOURCES_PROCESSOR_CLEAR; private static Method PROPERTY_SOURCES_PROCESSOR_CLEAR;
private static Method CONFIG_SERVICE_SET_CONTAINER; private static Method CONFIG_SERVICE_RESET;
static { static {
try { try {
PROPERTY_SOURCES_PROCESSOR_CLEAR = PropertySourcesProcessor.class.getDeclaredMethod("reset"); PROPERTY_SOURCES_PROCESSOR_CLEAR = PropertySourcesProcessor.class.getDeclaredMethod("reset");
ReflectionUtils.makeAccessible(PROPERTY_SOURCES_PROCESSOR_CLEAR); ReflectionUtils.makeAccessible(PROPERTY_SOURCES_PROCESSOR_CLEAR);
CONFIG_SERVICE_SET_CONTAINER = ConfigService.class.getDeclaredMethod("setContainer", PlexusContainer.class); CONFIG_SERVICE_RESET = ConfigService.class.getDeclaredMethod("reset");
ReflectionUtils.makeAccessible(CONFIG_SERVICE_SET_CONTAINER); ReflectionUtils.makeAccessible(CONFIG_SERVICE_RESET);
} catch (NoSuchMethodException e) { } catch (NoSuchMethodException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
@Override
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
super.tearDown();//clear the container
super.setUp();
//as PropertySourcesProcessor has some static states, so we must manually clear its state //as PropertySourcesProcessor has some static states, so we must manually clear its state
ReflectionUtils.invokeMethod(PROPERTY_SOURCES_PROCESSOR_CLEAR, null); ReflectionUtils.invokeMethod(PROPERTY_SOURCES_PROCESSOR_CLEAR, null);
//as ConfigService is singleton, so we must manually clear its container //as ConfigService is singleton, so we must manually clear its container
ReflectionUtils.invokeMethod(CONFIG_SERVICE_SET_CONTAINER, null, getContainer()); ReflectionUtils.invokeMethod(CONFIG_SERVICE_RESET, null);
MockInjector.reset();
defineComponent(ConfigManager.class, MockConfigManager.class); MockInjector.setInstance(ConfigManager.class, new MockConfigManager());
} }
@Override
@After @After
public void tearDown() throws Exception { public void tearDown() throws Exception {
super.tearDown();
CONFIG_REGISTRY.clear(); CONFIG_REGISTRY.clear();
} }
......
package com.ctrip.framework.apollo.spring; package com.ctrip.framework.apollo.spring;
import com.google.common.collect.Lists; import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import com.ctrip.framework.apollo.Config; import java.util.List;
import com.ctrip.framework.apollo.ConfigChangeListener;
import com.ctrip.framework.apollo.core.ConfigConsts;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfig;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig;
import org.junit.Test; import org.junit.Test;
import org.mockito.invocation.InvocationOnMock; import org.mockito.invocation.InvocationOnMock;
...@@ -18,12 +15,14 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext ...@@ -18,12 +15,14 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import java.util.List; import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigChangeListener;
import static org.junit.Assert.assertEquals; import com.ctrip.framework.apollo.core.ConfigConsts;
import static org.mockito.Matchers.any; import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import static org.mockito.Mockito.doAnswer; import com.ctrip.framework.apollo.spring.annotation.ApolloConfig;
import static org.mockito.Mockito.mock; import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig;
import com.google.common.collect.Lists;
/** /**
* @author Jason Song(song_s@ctrip.com) * @author Jason Song(song_s@ctrip.com)
......
package com.ctrip.framework.apollo.spring; package com.ctrip.framework.apollo.spring;
import com.ctrip.framework.apollo.Config; import static org.junit.Assert.assertEquals;
import com.ctrip.framework.apollo.core.ConfigConsts; import static org.mockito.Matchers.anyString;
import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig; import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
...@@ -11,11 +13,9 @@ import org.springframework.context.annotation.Bean; ...@@ -11,11 +13,9 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import static org.junit.Assert.assertEquals; import com.ctrip.framework.apollo.Config;
import static org.mockito.Matchers.anyString; import com.ctrip.framework.apollo.core.ConfigConsts;
import static org.mockito.Matchers.eq; import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/** /**
* @author Jason Song(song_s@ctrip.com) * @author Jason Song(song_s@ctrip.com)
......
package com.ctrip.framework.apollo.spring; package com.ctrip.framework.apollo.spring;
import com.google.common.collect.Lists; import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import com.ctrip.framework.apollo.Config; import java.util.List;
import com.ctrip.framework.apollo.ConfigChangeListener;
import com.ctrip.framework.apollo.core.ConfigConsts;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfig;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
import org.junit.Test; import org.junit.Test;
import org.mockito.invocation.InvocationOnMock; import org.mockito.invocation.InvocationOnMock;
...@@ -15,12 +13,13 @@ import org.mockito.stubbing.Answer; ...@@ -15,12 +13,13 @@ import org.mockito.stubbing.Answer;
import org.springframework.beans.factory.BeanCreationException; import org.springframework.beans.factory.BeanCreationException;
import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List; import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigChangeListener;
import static org.junit.Assert.assertEquals; import com.ctrip.framework.apollo.core.ConfigConsts;
import static org.mockito.Matchers.any; import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import static org.mockito.Mockito.doAnswer; import com.ctrip.framework.apollo.spring.annotation.ApolloConfig;
import static org.mockito.Mockito.mock; import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
import com.google.common.collect.Lists;
/** /**
* @author Jason Song(song_s@ctrip.com) * @author Jason Song(song_s@ctrip.com)
......
package com.ctrip.framework.apollo.spring; package com.ctrip.framework.apollo.spring;
import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.core.ConfigConsts;
import org.junit.Test;
import org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.anyString; import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq; import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import org.junit.Test;
import org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.core.ConfigConsts;
/** /**
* @author Jason Song(song_s@ctrip.com) * @author Jason Song(song_s@ctrip.com)
*/ */
......
package com.ctrip.framework.apollo.util; package com.ctrip.framework.apollo.util;
import org.junit.Test;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import org.junit.Test;
/** /**
* @author Jason Song(song_s@ctrip.com) * @author Jason Song(song_s@ctrip.com)
*/ */
......
package com.ctrip.framework.apollo.util.parser; package com.ctrip.framework.apollo.util.parser;
import org.junit.Test; import static org.junit.Assert.assertEquals;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.Locale; import java.util.Locale;
import static org.junit.Assert.assertEquals; import org.junit.Test;
/** /**
* @author Jason Song(song_s@ctrip.com) * @author Jason Song(song_s@ctrip.com)
......
package com.ctrip.framework.apollo.util.parser; package com.ctrip.framework.apollo.util.parser;
import org.junit.Test;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class DurationParserTest { public class DurationParserTest {
private Parsers.DurationParser durationParser = Parsers.forDuration(); private Parsers.DurationParser durationParser = Parsers.forDuration();
......
com.ctrip.framework.apollo.build.MockInjector
\ No newline at end of file
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
<parent> <parent>
<groupId>com.ctrip.framework.apollo</groupId> <groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo</artifactId> <artifactId>apollo</artifactId>
<version>0.6.4-SNAPSHOT</version> <version>0.7.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath> <relativePath>../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
<parent> <parent>
<groupId>com.ctrip.framework.apollo</groupId> <groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo</artifactId> <artifactId>apollo</artifactId>
<version>0.6.4-SNAPSHOT</version> <version>0.7.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath> <relativePath>../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
<parent> <parent>
<groupId>com.ctrip.framework.apollo</groupId> <groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo</artifactId> <artifactId>apollo</artifactId>
<version>0.6.4-SNAPSHOT</version> <version>0.7.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath> <relativePath>../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
<parent> <parent>
<artifactId>apollo</artifactId> <artifactId>apollo</artifactId>
<groupId>com.ctrip.framework.apollo</groupId> <groupId>com.ctrip.framework.apollo</groupId>
<version>0.6.4-SNAPSHOT</version> <version>0.7.0-SNAPSHOT</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>apollo-demo</artifactId> <artifactId>apollo-demo</artifactId>
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
<parent> <parent>
<groupId>com.ctrip.framework.apollo</groupId> <groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo</artifactId> <artifactId>apollo</artifactId>
<version>0.6.4-SNAPSHOT</version> <version>0.7.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath> <relativePath>../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
<groupId>com.ctrip.framework.apollo</groupId> <groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo</artifactId> <artifactId>apollo</artifactId>
<version>0.6.4-SNAPSHOT</version> <version>0.7.0-SNAPSHOT</version>
<name>Apollo</name> <name>Apollo</name>
<packaging>pom</packaging> <packaging>pom</packaging>
<description>Ctrip Configuration Center</description> <description>Ctrip Configuration Center</description>
...@@ -263,9 +263,9 @@ ...@@ -263,9 +263,9 @@
<version>5.1.39</version> <version>5.1.39</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.unidal.framework</groupId> <groupId>com.google.inject</groupId>
<artifactId>foundation-service</artifactId> <artifactId>guice</artifactId>
<version>2.5.6</version> <version>4.1.0</version>
</dependency> </dependency>
<!--for test --> <!--for test -->
<dependency> <dependency>
...@@ -583,12 +583,6 @@ ...@@ -583,12 +583,6 @@
</plugin> </plugin>
</plugins> </plugins>
</build> </build>
<repositories>
<repository>
<id>internal.repo</id>
<url>https://raw.github.com/ctripcorp/apollo/mvn-repo/</url>
</repository>
</repositories>
</profile> </profile>
<profile> <profile>
<!-- for open source usage --> <!-- for open source usage -->
...@@ -599,12 +593,6 @@ ...@@ -599,12 +593,6 @@
<activation> <activation>
<activeByDefault>true</activeByDefault> <activeByDefault>true</activeByDefault>
</activation> </activation>
<repositories>
<repository>
<id>internal.repo</id>
<url>https://raw.github.com/ctripcorp/apollo/mvn-repo/</url>
</repository>
</repositories>
</profile> </profile>
<profile> <profile>
<!-- for ctrip development --> <!-- for ctrip development -->
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册