X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-binding-broker%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fsal%2Fbinding%2Fimpl%2FRpcProviderRegistryImpl.java;h=c61ec4926a65f58f31fd03657162b0af70b23c0e;hb=1e1a4a1ce88fa1ef00df2f765a236e7139c2a0b4;hp=952d84d885c41e73fe2312bf22c723f62cc44b35;hpb=75f9ebff528344841c86e08d302340638db20858;p=controller.git diff --git a/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/impl/RpcProviderRegistryImpl.java b/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/impl/RpcProviderRegistryImpl.java index 952d84d885..c61ec4926a 100644 --- a/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/impl/RpcProviderRegistryImpl.java +++ b/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/impl/RpcProviderRegistryImpl.java @@ -9,6 +9,10 @@ package org.opendaylight.controller.sal.binding.impl; import static com.google.common.base.Preconditions.checkState; +import com.google.common.cache.CacheBuilder; +import com.google.common.cache.CacheLoader; +import com.google.common.cache.LoadingCache; + import java.util.EventListener; import java.util.HashMap; import java.util.Map; @@ -37,14 +41,21 @@ import org.opendaylight.yangtools.yang.binding.RpcService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class RpcProviderRegistryImpl implements // - RpcProviderRegistry, // - RouteChangePublisher> { +public class RpcProviderRegistryImpl implements RpcProviderRegistry, RouteChangePublisher> { private RuntimeCodeGenerator rpcFactory = SingletonHolder.RPC_GENERATOR_IMPL; - // publicProxies is a cache of proxy objects where each value in the map corresponds to a specific RpcService - private final Map, RpcService> publicProxies = new WeakHashMap<>(); + // cache of proxy objects where each value in the map corresponds to a specific RpcService + private final LoadingCache, RpcService> publicProxies = CacheBuilder.newBuilder().weakKeys(). + build(new CacheLoader, RpcService>() { + @Override + public RpcService load(final Class type) { + final RpcService proxy = rpcFactory.getDirectProxyFor(type); + LOG.debug("Created {} as public proxy for {} in {}", proxy, type.getSimpleName(), this); + return proxy; + } + }); + private final Map, RpcRouter> rpcRouters = new WeakHashMap<>(); private final ListenerRegistry>> routeChangeListeners = ListenerRegistry .create(); @@ -93,26 +104,7 @@ public class RpcProviderRegistryImpl implements // @SuppressWarnings("unchecked") @Override public final T getRpcService(final Class type) { - - T potentialProxy = (T) publicProxies.get(type); - if (potentialProxy != null) { - return potentialProxy; - } - synchronized (this) { - /** - * Potential proxy could be instantiated by other thread while we - * were waiting for the lock. - */ - - potentialProxy = (T) publicProxies.get(type); - if (potentialProxy != null) { - return potentialProxy; - } - T proxy = rpcFactory.getDirectProxyFor(type); - LOG.debug("Created {} as public proxy for {} in {}", proxy, type.getSimpleName(), this); - publicProxies.put(type, proxy); - return proxy; - } + return (T) publicProxies.getUnchecked(type); } @SuppressWarnings({ "unchecked", "rawtypes" }) @@ -205,8 +197,7 @@ public class RpcProviderRegistryImpl implements // } - private class RouteChangeForwarder implements - RouteChangeListener, InstanceIdentifier> { + private class RouteChangeForwarder implements RouteChangeListener, InstanceIdentifier> { private final Class type; @@ -240,8 +231,7 @@ public class RpcProviderRegistryImpl implements // } } - public static class RpcProxyRegistration extends AbstractObjectRegistration implements - RpcRegistration { + public static class RpcProxyRegistration extends AbstractObjectRegistration implements RpcRegistration { private final Class serviceType; private RpcProviderRegistryImpl registry;