From 38f8f98b16d2340b0e4ea89211925ae70a6c2e19 Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Thu, 20 Sep 2012 15:25:21 -0700 Subject: [PATCH] removed static field-based state sharing when looking up delegate methods --- .../org/jclouds/rest/config/RestModule.java | 17 ++++++++++++++++- .../internal/RestAnnotationProcessor.java | 19 ++++++------------- .../rest/internal/SeedAnnotationCache.java | 6 ++++-- 3 files changed, 26 insertions(+), 16 deletions(-) diff --git a/core/src/main/java/org/jclouds/rest/config/RestModule.java b/core/src/main/java/org/jclouds/rest/config/RestModule.java index 99e12558b8..d7c0fa9090 100644 --- a/core/src/main/java/org/jclouds/rest/config/RestModule.java +++ b/core/src/main/java/org/jclouds/rest/config/RestModule.java @@ -20,6 +20,7 @@ package org.jclouds.rest.config; import static org.jclouds.Constants.PROPERTY_TIMEOUTS_PREFIX; +import java.lang.reflect.Method; import java.net.URI; import java.util.Map; import java.util.concurrent.atomic.AtomicReference; @@ -46,6 +47,7 @@ import org.jclouds.rest.HttpClient; import org.jclouds.rest.binders.BindToJsonPayloadWrappedWith; import org.jclouds.rest.internal.AsyncRestClientProxy; import org.jclouds.rest.internal.RestAnnotationProcessor; +import org.jclouds.rest.internal.RestAnnotationProcessor.MethodKey; import org.jclouds.rest.internal.SeedAnnotationCache; import org.jclouds.util.Maps2; import org.jclouds.util.Predicates2; @@ -53,6 +55,7 @@ import org.jclouds.util.Predicates2; import com.google.common.base.Function; import com.google.common.base.Predicate; import com.google.common.base.Supplier; +import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheLoader; import com.google.common.cache.LoadingCache; @@ -107,7 +110,19 @@ public class RestModule extends AbstractModule { }).to(FilterStringsBoundToInjectorByName.class); installLocations(); } - + + /** + * Shared for all types of rest clients. this is read-only in this class, and + * currently populated only by {@link SeedAnnotationCache} + * + * @see SeedAnnotationCache + */ + @Provides + @Singleton + protected Cache delegationMap(){ + return CacheBuilder.newBuilder().build(); + } + @Provides @Singleton @Named("TIMEOUTS") diff --git a/core/src/main/java/org/jclouds/rest/internal/RestAnnotationProcessor.java b/core/src/main/java/org/jclouds/rest/internal/RestAnnotationProcessor.java index 1a8376e682..38d2dbe0ff 100644 --- a/core/src/main/java/org/jclouds/rest/internal/RestAnnotationProcessor.java +++ b/core/src/main/java/org/jclouds/rest/internal/RestAnnotationProcessor.java @@ -146,6 +146,7 @@ import com.google.common.base.Predicate; import com.google.common.base.Predicates; import com.google.common.base.Supplier; import com.google.common.base.Throwables; +import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheLoader; import com.google.common.cache.LoadingCache; @@ -192,17 +193,7 @@ public class RestAnnotationProcessor { static final LoadingCache>> methodToIndexOfParamToPartParamAnnotations = createMethodToIndexOfParamToAnnotation(PartParam.class); static final LoadingCache>> methodToIndexOfParamToParamParserAnnotations = createMethodToIndexOfParamToAnnotation(ParamParser.class); - /** - * Shared for all types of rest clients. this is read-only in this class, and - * currently populated only by {@link SeedAnnotationCache} - * - * @see SeedAnnotationCache - */ - // TODO: change this to a private final LoadingCache - // supplied by guice. The CacheLoader can be refactored - // out from SeedAnnotationCache. Potentially, preseed the cache, but only if - // this is uncomplicated. - static final Map delegationMap = newHashMap(); + final Cache delegationMap; static LoadingCache>> createMethodToIndexOfParamToAnnotation( final Class annotation) { @@ -331,7 +322,7 @@ public class RestAnnotationProcessor { @SuppressWarnings("unchecked") @Inject - public RestAnnotationProcessor(Injector injector, LoadingCache, Boolean> seedAnnotationCache, + public RestAnnotationProcessor(Injector injector, LoadingCache, Boolean> seedAnnotationCache, Cache delegationMap, @ApiVersion String apiVersion, @BuildVersion String buildVersion, ParseSax.Factory parserFactory, HttpUtils utils, ContentMetadataCodec contentMetadataCodec, TypeLiteral typeLiteral) throws ExecutionException { this.declaring = (Class) typeLiteral.getRawType(); @@ -342,6 +333,7 @@ public class RestAnnotationProcessor { this.uriBuilderProvider = injector.getProvider(UriBuilder.class); this.seedAnnotationCache = seedAnnotationCache; seedAnnotationCache.get(declaring); + this.delegationMap = delegationMap; if (declaring.isAnnotationPresent(SkipEncoding.class)) { skips = declaring.getAnnotation(SkipEncoding.class).value(); } else { @@ -351,8 +343,9 @@ public class RestAnnotationProcessor { this.buildVersion = buildVersion; } + public Method getDelegateOrNull(Method in) { - return delegationMap.get(new MethodKey(in)); + return delegationMap.getIfPresent(new MethodKey(in)); } public static class MethodKey { diff --git a/core/src/main/java/org/jclouds/rest/internal/SeedAnnotationCache.java b/core/src/main/java/org/jclouds/rest/internal/SeedAnnotationCache.java index 486bf6f988..6318c24bcc 100644 --- a/core/src/main/java/org/jclouds/rest/internal/SeedAnnotationCache.java +++ b/core/src/main/java/org/jclouds/rest/internal/SeedAnnotationCache.java @@ -19,7 +19,6 @@ package org.jclouds.rest.internal; import static com.google.common.collect.Sets.difference; -import static org.jclouds.rest.internal.RestAnnotationProcessor.delegationMap; import static org.jclouds.rest.internal.RestAnnotationProcessor.getHttpMethods; import static org.jclouds.rest.internal.RestAnnotationProcessor.methodToIndexOfParamToBinderParamAnnotation; import static org.jclouds.rest.internal.RestAnnotationProcessor.methodToIndexOfParamToEndpointAnnotations; @@ -48,6 +47,7 @@ import org.jclouds.rest.RestContext; import org.jclouds.rest.annotations.Delegate; import org.jclouds.rest.internal.RestAnnotationProcessor.MethodKey; +import com.google.common.cache.Cache; import com.google.common.cache.CacheLoader; import com.google.common.collect.ImmutableSet; import com.google.inject.Inject; @@ -68,10 +68,12 @@ public class SeedAnnotationCache extends CacheLoader, Boolean> { protected Logger logger = Logger.NULL; protected final Injector injector; + protected final Cache delegationMap; @Inject - public SeedAnnotationCache(Injector injector) { + public SeedAnnotationCache(Injector injector, Cache delegationMap) { this.injector = injector; + this.delegationMap = delegationMap; } @Override