Class LazyInitProxyFactory
- java.lang.Object
-
- org.apache.wicket.proxy.LazyInitProxyFactory
-
public class LazyInitProxyFactory extends java.lang.ObjectA factory class that creates lazy init proxies given a type and aIProxyTargetLocatorused to retrieve the object the proxy will represent.A lazy init proxy waits until the first method invocation before it uses the
IProxyTargetLocatorto 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 theIProxyTargetLocatorinstead 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 theIProxyTargetLocator, 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 LoadableDetachableModel { 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.- Author:
- Igor Vaynberg (ivaynberg)
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classLazyInitProxyFactory.AbstractCGLibInterceptorMethod interceptor for proxies representing concrete object not backed by an interface.protected static classLazyInitProxyFactory.CGLibInterceptorMethod interceptor for proxies representing concrete object not backed by an interface.static interfaceLazyInitProxyFactory.IWriteReplaceThis interface is used to make the proxy forward writeReplace() call to the handler instead of invoking it on itself.static classLazyInitProxyFactory.SerializableNoOpCallbackSerializable implementation of the NoOp callback.static classLazyInitProxyFactory.WicketNamingPolicy
-
Constructor Summary
Constructors Constructor Description LazyInitProxyFactory()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.lang.ObjectcreateProxy(java.lang.Class<?> type, IProxyTargetLocator locator)Create a lazy init proxy for the specified type.static booleanisEqualsMethod(java.lang.reflect.Method method)Checks if the method is derived from Object.equals()static booleanisFinalizeMethod(java.lang.reflect.Method method)Checks if the method is derived from Object.finalize()static booleanisHashCodeMethod(java.lang.reflect.Method method)Checks if the method is derived from Object.hashCode()static booleanisToStringMethod(java.lang.reflect.Method method)Checks if the method is derived from Object.toString()static booleanisWriteReplaceMethod(java.lang.reflect.Method method)Checks if the method is the writeReplace method
-
-
-
Method Detail
-
createProxy
public static java.lang.Object createProxy(java.lang.Class<?> type, IProxyTargetLocator locator)Create a lazy init proxy for the specified type. The target object will be located using the provided locator upon first method invocation.- Parameters:
type- type that proxy will representlocator- object locator that will locate the object the proxy represents- Returns:
- lazily initializable proxy
-
isEqualsMethod
public static boolean isEqualsMethod(java.lang.reflect.Method method)
Checks if the method is derived from Object.equals()- Parameters:
method- method being tested- Returns:
- true if the method is derived from Object.equals(), false otherwise
-
isHashCodeMethod
public static boolean isHashCodeMethod(java.lang.reflect.Method method)
Checks if the method is derived from Object.hashCode()- Parameters:
method- method being tested- Returns:
- true if the method is defined from Object.hashCode(), false otherwise
-
isToStringMethod
public static boolean isToStringMethod(java.lang.reflect.Method method)
Checks if the method is derived from Object.toString()- Parameters:
method- method being tested- Returns:
- true if the method is defined from Object.toString(), false otherwise
-
isFinalizeMethod
public static boolean isFinalizeMethod(java.lang.reflect.Method method)
Checks if the method is derived from Object.finalize()- Parameters:
method- method being tested- Returns:
- true if the method is defined from Object.finalize(), false otherwise
-
isWriteReplaceMethod
public static boolean isWriteReplaceMethod(java.lang.reflect.Method method)
Checks if the method is the writeReplace method- Parameters:
method- method being tested- Returns:
- true if the method is the writeReplace method, false otherwise
-
-