Skip to content

Commit

Permalink
use guice as apollo-client ioc mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
nobodyiam committed Apr 25, 2017
1 parent c219ee8 commit f048baf
Show file tree
Hide file tree
Showing 74 changed files with 834 additions and 985 deletions.
2 changes: 1 addition & 1 deletion apollo-adminservice/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo</artifactId>
<version>0.6.4-SNAPSHOT</version>
<version>0.7.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
Expand Down
2 changes: 1 addition & 1 deletion apollo-assembly/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo</artifactId>
<version>0.6.4-SNAPSHOT</version>
<version>0.7.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
Expand Down
2 changes: 1 addition & 1 deletion apollo-biz/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo</artifactId>
<version>0.6.4-SNAPSHOT</version>
<version>0.7.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>apollo-biz</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion apollo-buildtools/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo</artifactId>
<version>0.6.4-SNAPSHOT</version>
<version>0.7.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
Expand Down
2 changes: 1 addition & 1 deletion apollo-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ If you need this functionality, you could specify the cluster as follows:
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-client</artifactId>
<version>0.6.4</version>
<version>0.7.0</version>
</dependency>

## III. Client Usage
Expand Down
24 changes: 5 additions & 19 deletions apollo-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo</artifactId>
<version>0.6.4-SNAPSHOT</version>
<version>0.7.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
Expand All @@ -21,12 +21,12 @@
<artifactId>apollo-core</artifactId>
</dependency>
<!-- end of apollo -->
<!-- foundation service -->
<!-- guice -->
<dependency>
<groupId>org.unidal.framework</groupId>
<artifactId>foundation-service</artifactId>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
</dependency>
<!-- end of foundation service -->
<!-- end of guice -->
<!-- log -->
<dependency>
<groupId>org.slf4j</groupId>
Expand Down Expand Up @@ -61,19 +61,5 @@
<scope>test</scope>
</dependency>
<!-- 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>
</project>
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
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.enums.ConfigFileFormat;
import com.ctrip.framework.apollo.exceptions.ApolloConfigException;
import com.ctrip.framework.apollo.internals.ConfigManager;
import com.ctrip.framework.apollo.spi.ConfigFactory;
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
Expand All @@ -20,25 +15,14 @@
public class ConfigService {
private static final ConfigService s_instance = new ConfigService();

private PlexusContainer m_container;
private volatile ConfigManager m_configManager;
private volatile ConfigRegistry m_configRegistry;

private ConfigService() {
m_container = ContainerLoader.getDefaultContainer();
}

private ConfigManager getManager() {
if (m_configManager == null) {
synchronized (this) {
if (m_configManager == null) {
try {
m_configManager = m_container.lookup(ConfigManager.class);
} catch (ComponentLookupException ex) {
ApolloConfigException exception = new ApolloConfigException("Unable to load ConfigManager!", ex);
Tracer.logError(exception);
throw exception;
}
m_configManager = ApolloInjector.getInstance(ConfigManager.class);
}
}
}
Expand All @@ -50,13 +34,7 @@ private ConfigRegistry getRegistry() {
if (m_configRegistry == null) {
synchronized (this) {
if (m_configRegistry == null) {
try {
m_configRegistry = m_container.lookup(ConfigRegistry.class);
} catch (ComponentLookupException ex) {
ApolloConfigException exception = new ApolloConfigException("Unable to load ConfigRegistry!", ex);
Tracer.logError(exception);
throw exception;
}
m_configRegistry = ApolloInjector.getInstance(ConfigRegistry.class);
}
}
}
Expand Down Expand Up @@ -127,9 +105,8 @@ static void setConfigFactory(String namespace, ConfigFactory factory) {
}

// for test only
static void setContainer(PlexusContainer m_container) {
static void reset() {
synchronized (s_instance) {
s_instance.m_container = m_container;
s_instance.m_configManager = null;
s_instance.m_configRegistry = null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
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([email protected])
*/
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);
}
}
}

This file was deleted.

This file was deleted.

Loading

0 comments on commit f048baf

Please sign in to comment.