git://wamblee.org
/
utils
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
performance optimization.
[utils]
/
support
/
general
/
src
/
main
/
java
/
org
/
wamblee
/
general
/
ThreadSpecificProxyFactory.java
diff --git
a/support/general/src/main/java/org/wamblee/general/ThreadSpecificProxyFactory.java
b/support/general/src/main/java/org/wamblee/general/ThreadSpecificProxyFactory.java
index f7087296796d8a60ae14fc83139aed7603c5ef2e..51d23abf46fd54484ff002f776c8a93c5f8df384 100644
(file)
--- a/
support/general/src/main/java/org/wamblee/general/ThreadSpecificProxyFactory.java
+++ b/
support/general/src/main/java/org/wamblee/general/ThreadSpecificProxyFactory.java
@@
-53,8
+53,9
@@
public class ThreadSpecificProxyFactory<T> {
}
}
}
}
- private ThreadLocal<T> svc
= new ThreadLocal<T>()
;
+ private ThreadLocal<T> svc;
private Class clazz;
private Class clazz;
+ private T proxy;
/**
* Constructs the factory.
/**
* Constructs the factory.
@@
-67,7
+68,9
@@
public class ThreadSpecificProxyFactory<T> {
throw new IllegalArgumentException("Class " + aClass.getName() +
" is not an interface");
}
throw new IllegalArgumentException("Class " + aClass.getName() +
" is not an interface");
}
+ svc = new ThreadLocal<T>();
clazz = aClass;
clazz = aClass;
+ proxy = createProxy();
}
/**
}
/**
@@
-81,7
+84,9
@@
public class ThreadSpecificProxyFactory<T> {
}
/**
}
/**
- * Gets the current thread-specific service.
+ * Gets the current thread-specific service.
+ * To get a contextual reference that can be used by any thread but delegates to a thread-specific
+ * instance, use {@link #getProxy()}.
* @return Service.
*/
public T get() {
* @return Service.
*/
public T get() {
@@
-95,15
+100,19
@@
public class ThreadSpecificProxyFactory<T> {
* @return Proxy.
*/
public T getProxy() {
* @return Proxy.
*/
public T getProxy() {
+ return proxy;
+ }
+
+ private T createProxy() {
InvocationHandler handler = new ThreadSpecificInvocationHandler();
Class proxyClass = Proxy.getProxyClass(clazz.getClassLoader(),
new Class[] { clazz });
InvocationHandler handler = new ThreadSpecificInvocationHandler();
Class proxyClass = Proxy.getProxyClass(clazz.getClassLoader(),
new Class[] { clazz });
- T proxy;
+ T proxy
Obj
;
try {
try {
- proxy = (T) proxyClass.getConstructor(
+ proxy
Obj
= (T) proxyClass.getConstructor(
new Class[] { InvocationHandler.class }).newInstance(
new Object[] { handler });
new Class[] { InvocationHandler.class }).newInstance(
new Object[] { handler });
- return proxy;
+ return proxy
Obj
;
} catch (Exception e) {
throw new RuntimeException("Could not create proxy for " +
clazz.getName(), e);
} catch (Exception e) {
throw new RuntimeException("Could not create proxy for " +
clazz.getName(), e);