Skip to content

Commit d8bc3b2

Browse files
author
Allen Wang
committed
Merge pull request #157 from allenxwang/pluggable-annotations
Fix Issue #156, #149 and #141
2 parents 4bd4aa3 + b3fc37a commit d8bc3b2

File tree

47 files changed

+838
-480
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+838
-480
lines changed

build.gradle

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@ project(':ribbon') {
146146

147147
dependencies {
148148
compile 'com.netflix.hystrix:hystrix-core:1.4.0-RC4'
149-
compile 'com.netflix.evcache:evcache-client:1.0.5'
150149
compile 'javax.inject:javax.inject:1'
151150
compile project(':ribbon-transport')
152151
testCompile project(':ribbon-test')
@@ -155,6 +154,15 @@ project(':ribbon') {
155154
}
156155
}
157156

157+
project(':ribbon-evcache') {
158+
dependencies {
159+
compile project(':ribbon')
160+
compile 'com.netflix.evcache:evcache-client:1.0.5'
161+
testCompile project(':ribbon-test')
162+
testCompile project(':ribbon').sourceSets.test.output
163+
}
164+
}
165+
158166
project(':ribbon-guice') {
159167
dependencies {
160168
compile project(':ribbon')

ribbon-core/src/main/java/com/netflix/client/config/ClientConfigFactory.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,8 @@ public static class DefaultClientConfigFactory implements ClientConfigFactory {
2626
@Override
2727
public IClientConfig newConfig() {
2828
IClientConfig config = new DefaultClientConfigImpl();
29-
config.loadDefaultValues();
3029
return config;
31-
32-
}
30+
}
3331
}
3432

3533
public static final ClientConfigFactory DEFAULT = new DefaultClientConfigFactory();

ribbon-core/src/main/java/com/netflix/client/config/CommonClientConfigKey.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*/
1818
package com.netflix.client.config;
1919

20-
import static com.google.common.base.Preconditions.checkArgument;
20+
import com.google.common.reflect.TypeToken;
2121

2222
import java.lang.reflect.Field;
2323
import java.lang.reflect.Modifier;
@@ -26,7 +26,7 @@
2626
import java.util.HashSet;
2727
import java.util.Set;
2828

29-
import com.google.common.reflect.TypeToken;
29+
import static com.google.common.base.Preconditions.checkArgument;
3030

3131
public abstract class CommonClientConfigKey<T> implements IClientConfigKey<T> {
3232

@@ -214,6 +214,25 @@ public static IClientConfigKey[] values() {
214214
public static Set<IClientConfigKey> keys() {
215215
return keys;
216216
}
217+
218+
public static IClientConfigKey valueOf(final String name) {
219+
for (IClientConfigKey key: keys()) {
220+
if (key.key().equals(name)) {
221+
return key;
222+
}
223+
}
224+
return new IClientConfigKey() {
225+
@Override
226+
public String key() {
227+
return name;
228+
}
229+
230+
@Override
231+
public Class type() {
232+
return String.class;
233+
}
234+
};
235+
}
217236

218237
private final String configKey;
219238
private final Class<T> type;
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package com.netflix.ribbon.proxy.processor;
2+
3+
import com.netflix.evcache.EVCacheTranscoder;
4+
import com.netflix.ribbon.ResourceGroup.GroupBuilder;
5+
import com.netflix.ribbon.ResourceGroup.TemplateBuilder;
6+
import com.netflix.ribbon.RibbonResourceFactory;
7+
import com.netflix.ribbon.evache.EvCacheOptions;
8+
import com.netflix.ribbon.evache.EvCacheProvider;
9+
import com.netflix.ribbon.proxy.ProxyAnnotationException;
10+
import com.netflix.ribbon.proxy.Utils;
11+
import com.netflix.ribbon.proxy.annotation.EvCache;
12+
13+
import java.lang.reflect.Method;
14+
import java.util.HashMap;
15+
import java.util.Map;
16+
17+
/**
18+
* @author Allen Wang
19+
*/
20+
public class EVCacheAnnotationProcessor implements AnnotationProcessor<GroupBuilder, TemplateBuilder> {
21+
22+
private static final class CacheId {
23+
private final String appName;
24+
private final String cacheName;
25+
26+
CacheId(String appName, String cacheName) {
27+
this.appName = appName;
28+
this.cacheName = cacheName;
29+
}
30+
31+
@Override
32+
public boolean equals(Object o) {
33+
if (this == o) {
34+
return true;
35+
}
36+
if (o == null || getClass() != o.getClass()) {
37+
return false;
38+
}
39+
40+
CacheId cacheId = (CacheId) o;
41+
42+
if (!appName.equals(cacheId.appName)) {
43+
return false;
44+
}
45+
return cacheName.equals(cacheId.cacheName);
46+
}
47+
48+
@Override
49+
public int hashCode() {
50+
int result = appName.hashCode();
51+
result = 31 * result + cacheName.hashCode();
52+
return result;
53+
}
54+
}
55+
56+
private Map<CacheId, EvCacheProvider<?>> evCacheProviderPool = new HashMap<CacheId, EvCacheProvider<?>>();
57+
58+
@Override
59+
public void process(String templateName, TemplateBuilder templateBuilder, Method method) {
60+
EvCache annotation = method.getAnnotation(EvCache.class);
61+
if (annotation == null) {
62+
return;
63+
}
64+
65+
Class<? extends EVCacheTranscoder<?>>[] transcoderClasses = annotation.transcoder();
66+
EVCacheTranscoder<?> transcoder;
67+
if (transcoderClasses.length == 0) {
68+
transcoder = null;
69+
} else if (transcoderClasses.length > 1) {
70+
throw new ProxyAnnotationException("Multiple transcoders defined on method " + method.getName());
71+
} else {
72+
transcoder = Utils.newInstance(transcoderClasses[0]);
73+
}
74+
75+
EvCacheOptions evCacheOptions = new EvCacheOptions(
76+
annotation.appName(),
77+
annotation.name(),
78+
annotation.enableZoneFallback(),
79+
annotation.ttl(),
80+
transcoder,
81+
annotation.key());
82+
if (evCacheOptions != null) {
83+
CacheId cacheId = new CacheId(evCacheOptions.getAppName(), evCacheOptions.getCacheName());
84+
EvCacheProvider<?> provider = evCacheProviderPool.get(cacheId);
85+
if (provider == null) {
86+
provider = new EvCacheProvider(evCacheOptions);
87+
evCacheProviderPool.put(cacheId, provider);
88+
}
89+
templateBuilder.withCacheProvider(evCacheOptions.getCacheKeyTemplate(), provider);
90+
}
91+
92+
}
93+
94+
@Override
95+
public void process(String groupName, GroupBuilder groupBuilder, RibbonResourceFactory factory, Class<?> interfaceClass) {
96+
}
97+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
com.netflix.ribbon.proxy.processor.EVCacheAnnotationProcessor

0 commit comments

Comments
 (0)