cristof commented on code in PR #321:
URL: https://github.com/apache/openjpa/pull/321#discussion_r4689549682
##########
openjpa-kernel/src/main/java/org/apache/openjpa/event/BeanLifecycleCallbacks.java:
##########
@@ -23,73 +23,78 @@
import java.io.ObjectOutput;
import java.lang.reflect.Method;
-import org.apache.openjpa.lib.util.J2DoPrivHelper;
-import org.apache.openjpa.lib.util.Localizer;
-import org.apache.openjpa.util.UserException;
+import org.apache.openjpa.cdi.EntityListenerCachedFactory;
+import org.apache.openjpa.cdi.EntityListenerFactory;
+import org.apache.openjpa.cdi.EntityListenerWrapper;
/**
- * Performs a callback method on a cached bean instance.
+ * Performs a callback method on a entity listener factory provided bean
instance.
*
* @author Steve Kim
*/
public class BeanLifecycleCallbacks
extends MethodLifecycleCallbacks {
- private static final Localizer _loc = Localizer.forPackage
- (BeanLifecycleCallbacks.class);
-
- private transient Object _listener;
-
+ private final Class<?> listenerClass;
+
+ private final EntityListenerFactory factory;
+
/**
* Constructor. Make the callback on an instance of the given type.
*
* @param arg whether another argument is expected such as AfterDetach
*/
- public BeanLifecycleCallbacks(Class<?> cls, String method, boolean arg,
- Class<?> type) {
- this(cls, getMethod(cls, method, arg ? new Class[]{ Object.class,
- type } : new Class[]{ type }), arg);
+ public BeanLifecycleCallbacks(Class<?> cls, String method, boolean arg,
Class<?> type) {
+ this(cls, getMethod(cls, method, arg ? new Class[]{ Object.class, type
} : new Class[]{ type }), arg, null);
+ }
+
+ public BeanLifecycleCallbacks(Class<?> cls, String method, boolean arg,
Class<?> type, EntityListenerFactory factory) {
+ this(cls, getMethod(cls, method, arg ? new Class[]{ Object.class, type
} : new Class[]{ type }), arg, factory);
}
/**
* Constructor. Make the callback on an instance of the given type.
*/
public BeanLifecycleCallbacks(Class<?> cls, Method method, boolean arg) {
- super(method, arg);
- _listener = newListener(cls);
+ this(cls, method, arg, null);
+ }
+
+ public BeanLifecycleCallbacks(Class<?> cls, Method method, boolean arg,
EntityListenerFactory factory) {
+ super(method, arg);
+ listenerClass = cls;
+ this.factory = factory != null ? factory : new
EntityListenerCachedFactory();
}
- private Object newListener(Class<?> cls) {
- try {
- return J2DoPrivHelper.newInstance(cls);
- } catch (Throwable t) {
- throw new UserException(_loc.get("bean-constructor",
cls.getName()), t);
- }
+ private EntityListenerWrapper<?> getListenerWrapper() {
+ return factory.getWrapper(listenerClass);
}
@Override
public void makeCallback(Object obj, Object rel, int eventType) throws
Exception {
Method callback = getCallbackMethod();
- if (!callback.canAccess(_listener))
+ EntityListenerWrapper<?> wrapper = getListenerWrapper();
+ if (!callback.canAccess(wrapper.listener()))
callback.setAccessible(true);
if (requiresArgument())
- callback.invoke(_listener, new Object[]{ obj, rel });
+ callback.invoke(wrapper.listener(), new Object[]{ obj, rel });
else
- callback.invoke(_listener, new Object[]{ obj });
+ callback.invoke(wrapper.listener(), new Object[]{ obj });
+ if (!wrapper.cacheable()) {
+ wrapper.dispose();
Review Comment:
Added try-finally block.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]