提交 2145f21c 编写于 作者: A Allen Wang

code refactoring.

上级 a7b7ae9d
package com.netflix.loadbalancer;
// like synchronizedMap, but with support for explicit re-entrant
// locking
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock;
import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock;
public class LockMap<K,V> extends HashMap<K,V> {
protected ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
protected ReadLock rl = rwl.readLock();
protected WriteLock wl = rwl.writeLock();
public LockMap() {
super();
}
public LockMap(int initialCapacity) {
super(initialCapacity);
}
public LockMap(Map<? extends K, ? extends V> m) {
super(m);
}
public LockMap(int initialCapacity, float loadFactor) {
super(initialCapacity, loadFactor);
}
public void readLock() {
rl.lock();
}
public void readUnlock() {
rl.unlock();
}
public void writeLock() {
wl.lock();
}
public void writeUnlock() {
wl.unlock();
}
@Override public void clear() {
try {
wl.lock();
super.clear();
} finally {
wl.unlock();
}
}
@Override public boolean containsKey(Object key) {
try {
rl.lock();
return super.containsKey(key);
} finally {
rl.unlock();
}
}
@Override public boolean containsValue(Object value) {
try {
rl.lock();
return super.containsValue(value);
} finally {
rl.unlock();
}
}
@Override public Set<Map.Entry<K,V>> entrySet() {
try {
rl.lock();
return super.entrySet();
} finally {
rl.unlock();
}
}
@Override public V get(Object key) {
try {
rl.lock();
return super.get(key);
} finally {
rl.unlock();
}
}
@Override public boolean isEmpty() {
try {
rl.lock();
return super.isEmpty();
} finally {
rl.unlock();
}
}
@Override public Set<K> keySet() {
try {
rl.lock();
return super.keySet();
} finally {
rl.unlock();
}
}
@Override public V put(K key, V value) {
try {
wl.lock();
return super.put(key, value);
} finally {
wl.unlock();
}
}
@Override public void putAll(Map<? extends K, ? extends V> m) {
try {
wl.lock();
super.putAll(m);
} finally {
wl.unlock();
}
}
@Override public V remove(Object key) {
try {
wl.lock();
return super.remove(key);
} finally {
wl.unlock();
}
}
@Override public int size() {
try {
rl.lock();
return super.size();
} finally {
rl.unlock();
}
}
@Override public Collection<V> values() {
try {
rl.lock();
return super.values();
} finally {
rl.unlock();
}
}
@Override public Object clone() {
try {
rl.lock();
LockMap map = new LockMap();
map.putAll(this);
return map;
} finally {
rl.unlock();
}
}
}
......@@ -18,8 +18,8 @@ public class ClientFactory {
private static Logger logger = LoggerFactory.getLogger(ClientFactory.class);
private static synchronized AbstractClient registerClientFromProperties(String restClientName, NiwsClientConfig niwsClientConfig) {
AbstractClient client = null;
private static synchronized AbstractLoadBalancerAwareClient registerClientFromProperties(String restClientName, NiwsClientConfig niwsClientConfig) {
AbstractLoadBalancerAwareClient client = null;
AbstractLoadBalancer loadBalancer = null;
try {
if (simpleClientMap.get(restClientName) != null) {
......@@ -29,7 +29,7 @@ public class ClientFactory {
}
try {
String clientClassName = (String) niwsClientConfig.getProperty(NiwsClientConfigKey.ClientClassName);
client = (AbstractClient) instantiateNiwsConfigAwareClassInstance(clientClassName, niwsClientConfig);
client = (AbstractLoadBalancerAwareClient) instantiateNiwsConfigAwareClassInstance(clientClassName, niwsClientConfig);
boolean initializeNFLoadBalancer = Boolean.valueOf("" + niwsClientConfig.getProperty(
NiwsClientConfigKey.InitializeNFLoadBalancer,
Boolean.valueOf(NiwsClientConfig.DEFAULT_ENABLE_LOADBALANCER)));
......
......@@ -16,6 +16,13 @@ public class ClientRequest implements Cloneable {
this.uri = uri;
}
public ClientRequest(URI uri, Object loadBalancerKey, boolean isRetriable, NiwsClientConfig overrideConfig) {
this.uri = uri;
this.loadBalancerKey = loadBalancerKey;
this.isRetriable = isRetriable;
this.overrideConfig = overrideConfig;
}
public ClientRequest(ClientRequest request) {
this.uri = request.uri;
this.loadBalancerKey = request.loadBalancerKey;
......@@ -28,7 +35,7 @@ public class ClientRequest implements Cloneable {
}
public final ClientRequest setUri(URI uri) {
protected final ClientRequest setUri(URI uri) {
this.uri = uri;
return this;
}
......@@ -37,7 +44,7 @@ public class ClientRequest implements Cloneable {
return loadBalancerKey;
}
public final ClientRequest setLoadBalancerKey(Object loadBalancerKey) {
protected final ClientRequest setLoadBalancerKey(Object loadBalancerKey) {
this.loadBalancerKey = loadBalancerKey;
return this;
}
......@@ -46,7 +53,7 @@ public class ClientRequest implements Cloneable {
return isRetriable;
}
public final ClientRequest setRetriable(boolean isRetriable) {
protected final ClientRequest setRetriable(boolean isRetriable) {
this.isRetriable = isRetriable;
return this;
}
......@@ -55,12 +62,12 @@ public class ClientRequest implements Cloneable {
return overrideConfig;
}
public final ClientRequest setOverrideConfig(NiwsClientConfig overrideConfig) {
protected final ClientRequest setOverrideConfig(NiwsClientConfig overrideConfig) {
this.overrideConfig = overrideConfig;
return this;
}
public ClientRequest createWithNewUri(URI newURI) {
public ClientRequest replaceUri(URI newURI) {
ClientRequest req;
try {
req = (ClientRequest) this.clone();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册