Skip to content

Commit

Permalink
Client integration test and integrate foundation framework
Browse files Browse the repository at this point in the history
  • Loading branch information
nobodyiam committed Apr 15, 2016
1 parent 3607666 commit c6b4dd5
Show file tree
Hide file tree
Showing 55 changed files with 912 additions and 288 deletions.
119 changes: 66 additions & 53 deletions apollo-client/pom.xml
@@ -1,56 +1,69 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<parent>
<groupId>com.ctrip.apollo</groupId>
<artifactId>apollo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>apollo-client</artifactId>
<name>Apollo Client</name>
<properties>
<java.version>1.7</java.version>
</properties>
<dependencies>
<!-- apollo -->
<dependency>
<groupId>com.ctrip.apollo</groupId>
<artifactId>apollo-core</artifactId>
</dependency>
<!-- end of apollo -->
<dependency>
<groupId>com.ctrip.framework</groupId>
<artifactId>framework-foundation</artifactId>
</dependency>
<dependency>
<groupId>com.dianping.cat</groupId>
<artifactId>cat-client</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</exclusion>
</exclusions>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<parent>
<groupId>com.ctrip.apollo</groupId>
<artifactId>apollo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>apollo-client</artifactId>
<name>Apollo Client</name>
<properties>
<java.version>1.7</java.version>
</properties>
<dependencies>
<!-- apollo -->
<dependency>
<groupId>com.ctrip.apollo</groupId>
<artifactId>apollo-core</artifactId>
</dependency>
<!-- end of apollo -->
<!-- foundation service -->
<dependency>
<groupId>com.ctrip.framework</groupId>
<artifactId>framework-foundation</artifactId>
</dependency>
<!-- end of foundation service -->
<!-- cat -->
<dependency>
<groupId>com.dianping.cat</groupId>
<artifactId>cat-client</artifactId>
</dependency>
<!-- end of cat -->
<!-- log -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</exclusion>
</exclusions>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<scope>provided</scope>
</dependency>
<!-- end of log -->
<!-- test -->
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<scope>test</scope>
</dependency>
<!-- end of test -->
</dependencies>
</project>
4 changes: 4 additions & 0 deletions apollo-client/src/main/java/com/ctrip/apollo/Config.java
Expand Up @@ -13,5 +13,9 @@ public interface Config {
*/
public String getProperty(String key, String defaultValue);

/**
* Add change listener to this config instance.
* @param listener the config change listener
*/
public void addChangeListener(ConfigChangeListener listener);
}
Expand Up @@ -6,5 +6,9 @@
* @author Jason Song(song_s@ctrip.com)
*/
public interface ConfigChangeListener {
/**
* Invoked when there is any config change for the namespace.
* @param changeEvent the event for this change
*/
public void onChange(ConfigChangeEvent changeEvent);
}
22 changes: 16 additions & 6 deletions apollo-client/src/main/java/com/ctrip/apollo/ConfigService.java
Expand Up @@ -23,15 +23,15 @@ private ConfigService() {
}

/**
* Get the config instance with default namespace
* Get the config instance with default namespace.
* @return config instance
*/
public static Config getConfig() {
return getConfig(ConfigConsts.NAMESPACE_APPLICATION);
}

/**
* Get the config instance for the namespace
* Get the config instance for the namespace.
* @param namespace the namespace of the config
* @return config instance
*/
Expand All @@ -42,23 +42,28 @@ public static Config getConfig(String namespace) {
private static ConfigManager getManager() {
try {
return s_instance.m_container.lookup(ConfigManager.class);
} catch (ComponentLookupException e) {
throw new IllegalStateException("Unable to load ConfigManager!", e);
} catch (ComponentLookupException ex) {
throw new IllegalStateException("Unable to load ConfigManager!", ex);
}
}

private static ConfigRegistry getRegistry() {
try {
return s_instance.m_container.lookup(ConfigRegistry.class);
} catch (ComponentLookupException e) {
throw new IllegalStateException("Unable to load ConfigRegistry!", e);
} catch (ComponentLookupException ex) {
throw new IllegalStateException("Unable to load ConfigRegistry!", ex);
}
}

public static void setConfig(Config config) {
setConfig(ConfigConsts.NAMESPACE_APPLICATION, config);
}

/**
* Manually set the config for the namespace specified, use with caution!
* @param namespace the namespace
* @param config the config instance
*/
public static void setConfig(String namespace, final Config config) {
getRegistry().register(namespace, new ConfigFactory() {
@Override
Expand All @@ -72,6 +77,11 @@ public static void setConfigFactory(ConfigFactory factory) {
setConfigFactory(ConfigConsts.NAMESPACE_APPLICATION, factory);
}

/**
* Manually set the config factory for the namespace specified, use with caution!
* @param namespace the namespace
* @param factory the factory instance
*/
public static void setConfigFactory(String namespace, ConfigFactory factory) {
getRegistry().register(namespace, factory);
}
Expand Down

This file was deleted.

62 changes: 62 additions & 0 deletions apollo-client/src/main/java/com/ctrip/apollo/env/Apollo.java
@@ -0,0 +1,62 @@
package com.ctrip.apollo.env;

import com.google.common.base.Strings;

import com.ctrip.apollo.core.enums.Env;
import com.ctrip.framework.foundation.Foundation;

public class Apollo {
private static Env s_env;
private static String s_appId;
private static String s_cluster;

static {
setEnv(Foundation.server().getEnvType());

s_appId = Foundation.app().getAppId();

s_cluster = System.getProperty("apollo.cluster");
}

public static String getAppId() {
return s_appId;
}

public static Env getEnv() {
return s_env;
}

public static String getCluster() {
return s_cluster;
}

private static void setEnv(String envName) {
if (Strings.isNullOrEmpty(envName)) {
return;
}
switch (envName.toUpperCase()) {
case "LPT":
s_env = Env.LPT;
break;
case "FAT":
s_env = Env.FAT;
break;
case "UAT":
s_env = Env.UAT;
break;
case "PRO":
s_env = Env.PRO;
break;
case "DEV":
s_env = Env.DEV;
break;
case "LOCAL":
s_env = Env.LOCAL;
break;
default:
//do nothing
break;
}
}

}

This file was deleted.

Expand Up @@ -35,8 +35,8 @@ protected void fireConfigChange(ConfigChangeEvent changeEvent) {
for (ConfigChangeListener listener : m_listeners) {
try {
listener.onChange(changeEvent);
} catch (Throwable t) {
logger.error("Failed to invoke config change listener {}", listener.getClass(), t);
} catch (Throwable ex) {
logger.error("Failed to invoke config change listener {}", listener.getClass(), ex);
}
}
}
Expand All @@ -61,7 +61,8 @@ List<ConfigChange> calcPropertyChanges(Properties previous,
List<ConfigChange> changes = Lists.newArrayList();

for (String newKey : newKeys) {
changes.add(new ConfigChange(newKey, null, current.getProperty(newKey), PropertyChangeType.NEW));
changes
.add(new ConfigChange(newKey, null, current.getProperty(newKey), PropertyChangeType.NEW));
}

for (String removedKey : removedKeys) {
Expand Down
Expand Up @@ -15,6 +15,16 @@ public abstract class AbstractConfigRepository implements ConfigRepository {
private static final Logger logger = LoggerFactory.getLogger(AbstractConfigRepository.class);
private List<RepositoryChangeListener> m_listeners = Lists.newCopyOnWriteArrayList();

protected void trySync() {
try {
sync();
} catch (Throwable ex) {
logger.error("Sync config failed with repository {}", this.getClass(), ex);
}
}

protected abstract void sync();

@Override
public void addChangeListener(RepositoryChangeListener listener) {
if (!m_listeners.contains(listener)) {
Expand All @@ -31,8 +41,8 @@ protected void fireRepositoryChange(String namespace, Properties newProperties)
for (RepositoryChangeListener listener : m_listeners) {
try {
listener.onRepositoryChange(namespace, newProperties);
} catch (Throwable t) {
logger.error("Failed to invoke repository change listener {}", listener.getClass(), t);
} catch (Throwable ex) {
logger.error("Failed to invoke repository change listener {}", listener.getClass(), ex);
}
}
}
Expand Down
Expand Up @@ -6,5 +6,10 @@
* @author Jason Song(song_s@ctrip.com)
*/
public interface ConfigManager {
public Config getConfig(String namespace);
/**
* Get the config instance for the namespace specified.
* @param namespace the namespace
* @return the config instance for the namespace
*/
public Config getConfig(String namespace);
}

0 comments on commit c6b4dd5

Please sign in to comment.