|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.apache.wicket.proxy.LazyInitProxyFactory
public class LazyInitProxyFactory
A factory class that creates lazy init proxies given a type and a IProxyTargetLocator
used to retrieve the object the proxy will represent.
A lazy init proxy waits until the first method invocation before it uses the
IProxyTargetLocator
to retrieve the object to which the method invocation will be
forwarded.
This factory creates two kinds of proxies: A standard dynamic proxy when the specified type is an interface, and a CGLib proxy when the specified type is a concrete class.
The general use case for such a proxy is to represent a dependency that should not be serialized
with a wicket page or IModel
. The solution is to serialize the proxy and the
IProxyTargetLocator
instead of the dependency, and be able to look up the target object
again when the proxy is deserialized and accessed. A good strategy for achieving this is to have
a static lookup in the IProxyTargetLocator
, this keeps its size small and makes it safe
to serialize.
Example:
class UserServiceLocator implements IProxyTargetLocator { public static final IProxyTargetLocator INSTANCE = new UserServiceLocator(); Object locateProxyObject() { MyApplication app = (MyApplication)Application.get(); return app.getUserService(); } } class UserDetachableModel extends LoadableModel { private UserService svc; private long userId; public UserDetachableModel(long userId, UserService svc) { this.userId = userId; this.svc = svc; } public Object load() { return svc.loadUser(userId); } } UserService service = LazyInitProxyFactory.createProxy(UserService.class, UserServiceLocator.INSTANCE); UserDetachableModel model = new UserDetachableModel(10, service);The detachable model in the example above follows to good citizen pattern and is easy to unit test. These are the advantages gained through the use of the lazy init proxies.
Nested Class Summary | |
---|---|
protected static interface |
LazyInitProxyFactory.IWriteReplace
This interface is used to make the proxy forward writeReplace() call to the handler instead of invoking it on itself. |
Constructor Summary | |
---|---|
LazyInitProxyFactory()
|
Method Summary | |
---|---|
static Object |
createProxy(Class<?> type,
IProxyTargetLocator locator)
Create a lazy init proxy for the specified type. |
protected static boolean |
isEqualsMethod(Method method)
Checks if the method is derived from Object.equals() |
protected static boolean |
isFinalizeMethod(Method method)
Checks if the method is derived from Object.finalize() |
protected static boolean |
isHashCodeMethod(Method method)
Checks if the method is derived from Object.hashCode() |
protected static boolean |
isToStringMethod(Method method)
Checks if the method is derived from Object.toString() |
protected static boolean |
isWriteReplaceMethod(Method method)
Checks if the method is the writeReplace method |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public LazyInitProxyFactory()
Method Detail |
---|
public static Object createProxy(Class<?> type, IProxyTargetLocator locator)
type
- type that proxy will representlocator
- object locator that will locate the object the proxy represents
protected static boolean isEqualsMethod(Method method)
method
- method being tested
protected static boolean isHashCodeMethod(Method method)
method
- method being tested
protected static boolean isToStringMethod(Method method)
method
- method being tested
protected static boolean isFinalizeMethod(Method method)
method
- method being tested
protected static boolean isWriteReplaceMethod(Method method)
method
- method being tested
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |