-
-
Notifications
You must be signed in to change notification settings - Fork 10.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow users to inject customized instance via ApolloInjectorCustomizer
- Loading branch information
Showing
6 changed files
with
78 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package com.ctrip.framework.apollo.internals; | ||
|
||
import com.ctrip.framework.apollo.exceptions.ApolloConfigException; | ||
import com.ctrip.framework.apollo.spi.ApolloInjectorCustomizer; | ||
import com.ctrip.framework.apollo.spi.ConfigFactory; | ||
import com.ctrip.framework.apollo.spi.ConfigFactoryManager; | ||
import com.ctrip.framework.apollo.spi.ConfigRegistry; | ||
|
@@ -14,20 +15,24 @@ | |
import com.ctrip.framework.apollo.util.http.HttpUtil; | ||
|
||
import com.ctrip.framework.apollo.util.yaml.YamlParser; | ||
import com.ctrip.framework.foundation.internals.ServiceBootstrap; | ||
import com.google.inject.AbstractModule; | ||
import com.google.inject.Guice; | ||
import com.google.inject.Singleton; | ||
import java.util.List; | ||
|
||
/** | ||
* Guice injector | ||
* @author Jason Song([email protected]) | ||
*/ | ||
public class DefaultInjector implements Injector { | ||
private com.google.inject.Injector m_injector; | ||
private final com.google.inject.Injector m_injector; | ||
private final List<ApolloInjectorCustomizer> m_customizers; | ||
|
||
public DefaultInjector() { | ||
try { | ||
m_injector = Guice.createInjector(new ApolloModule()); | ||
m_customizers = ServiceBootstrap.loadAllOrdered(ApolloInjectorCustomizer.class); | ||
} catch (Throwable ex) { | ||
ApolloConfigException exception = new ApolloConfigException("Unable to initialize Guice Injector!", ex); | ||
Tracer.logError(exception); | ||
|
@@ -38,6 +43,12 @@ public DefaultInjector() { | |
@Override | ||
public <T> T getInstance(Class<T> clazz) { | ||
try { | ||
for (ApolloInjectorCustomizer customizer : m_customizers) { | ||
T instance = customizer.getInstance(clazz); | ||
if (instance != null) { | ||
return instance; | ||
} | ||
} | ||
return m_injector.getInstance(clazz); | ||
} catch (Throwable ex) { | ||
Tracer.logError(ex); | ||
|
@@ -48,8 +59,20 @@ public <T> T getInstance(Class<T> clazz) { | |
|
||
@Override | ||
public <T> T getInstance(Class<T> clazz, String name) { | ||
//Guice does not support get instance by type and name | ||
return null; | ||
try { | ||
for (ApolloInjectorCustomizer customizer : m_customizers) { | ||
T instance = customizer.getInstance(clazz, name); | ||
if (instance != null) { | ||
return instance; | ||
} | ||
} | ||
//Guice does not support get instance by type and name | ||
return null; | ||
} catch (Throwable ex) { | ||
Tracer.logError(ex); | ||
throw new ApolloConfigException( | ||
String.format("Unable to load instance for %s with name %s!", clazz.getName(), name), ex); | ||
} | ||
} | ||
|
||
private static class ApolloModule extends AbstractModule { | ||
|
12 changes: 12 additions & 0 deletions
12
apollo-client/src/main/java/com/ctrip/framework/apollo/spi/ApolloInjectorCustomizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.ctrip.framework.apollo.spi; | ||
|
||
import com.ctrip.framework.apollo.core.spi.Ordered; | ||
import com.ctrip.framework.apollo.internals.DefaultInjector; | ||
import com.ctrip.framework.apollo.internals.Injector; | ||
|
||
/** | ||
* Allow users to inject customized instances, see {@link DefaultInjector#getInstance(java.lang.Class)} | ||
*/ | ||
public interface ApolloInjectorCustomizer extends Injector, Ordered { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,24 @@ | ||
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.ctrip.framework.apollo.spi.ApolloInjectorCustomizer; | ||
import com.google.common.collect.HashBasedTable; | ||
import com.google.common.collect.Maps; | ||
import com.google.common.collect.Table; | ||
import java.util.Map; | ||
|
||
/** | ||
* @author Jason Song([email protected]) | ||
*/ | ||
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); | ||
} | ||
|
@@ -32,11 +28,6 @@ public <T> T getInstance(Class<T> clazz) { | |
|
||
@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); | ||
} | ||
|
@@ -61,4 +52,22 @@ public static void reset() { | |
classTable.clear(); | ||
delegate = new DefaultInjector(); | ||
} | ||
|
||
public static class InjectCustomizer implements ApolloInjectorCustomizer { | ||
|
||
@Override | ||
public <T> T getInstance(Class<T> clazz) { | ||
return (T) classMap.get(clazz); | ||
} | ||
|
||
@Override | ||
public <T> T getInstance(Class<T> clazz, String name) { | ||
return (T) classTable.get(clazz, name); | ||
} | ||
|
||
@Override | ||
public int getOrder() { | ||
return 0; | ||
} | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
.../test/resources/META-INF/services/com.ctrip.framework.apollo.spi.ApolloInjectorCustomizer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
com.ctrip.framework.apollo.build.MockInjector$InjectCustomizer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters