rzo1 commented on code in PR #321:
URL: https://github.com/apache/openjpa/pull/321#discussion_r4684683052
##########
openjpa-kernel/pom.xml:
##########
@@ -90,6 +90,10 @@
<scope>provided</scope>
<optional>true</optional>
</dependency>
+ <dependency>
+ <groupId>jakarta.enterprise</groupId>
+ <artifactId>jakarta.enterprise.cdi-api</artifactId>
Review Comment:
The `<optional>true</optional>` in the root `dependencyManagement` does not
reach the module. In the effective pom of openjpa-kernel,
`jakarta.enterprise.cdi-api` 4.1.0 is a plain compile dependency, so every
OpenJPA user, including plain Java SE applications without CDI, now gets
cdi-api plus lang-model, el-api, interceptor-api, inject-api and annotation-api.
Is that acceptable for the SE use case? I see two options:
1. Accept it. The jars are small API jars and it keeps the code simple, but
then it should be mentioned in the migration notes for 4.3.0.
2. Make it optional in `openjpa-kernel/pom.xml` itself (`provided` +
`optional`, like the other optional APIs there), and let the default
`EntityListenerFactory` use the CDI implementation only when
`jakarta.enterprise.inject.spi.BeanManager` is on the class path, falling back
to `EntityListenerCachedFactory` otherwise. Right now
`EntityListenerFactoryImpl` references `CDI` and `BeanManager` directly, so
without the API on the class path an entity with a listener fails with
`NoClassDefFoundError`.
I would prefer option 2, so that SE users keep the dependencies they have
today. What do you think?
##########
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:
If the callback throws (e.g. a validation in `@PrePersist`), `dispose()` is
not called, so the instance and its creational context, including its
`@Dependent` beans, are never released. Could this go into a `finally` block?
##########
openjpa-kernel/src/main/java/org/apache/openjpa/cdi/EntityListenerFactoryImpl.java:
##########
@@ -0,0 +1,174 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.openjpa.cdi;
+
+import java.lang.annotation.Annotation;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+
+import org.apache.openjpa.lib.util.J2DoPrivHelper;
+import org.apache.openjpa.lib.util.Localizer;
+import org.apache.openjpa.util.UserException;
+
+import jakarta.enterprise.context.spi.CreationalContext;
+import jakarta.enterprise.inject.Instance;
+import jakarta.enterprise.inject.spi.AnnotatedType;
+import jakarta.enterprise.inject.spi.Bean;
+import jakarta.enterprise.inject.spi.BeanManager;
+import jakarta.enterprise.inject.spi.CDI;
+import jakarta.enterprise.inject.spi.InjectionPoint;
+import jakarta.enterprise.inject.spi.InjectionTarget;
+
+public class EntityListenerFactoryImpl implements EntityListenerFactory {
+
+ private static final Localizer _loc =
Localizer.forPackage(EntityListenerFactoryImpl.class);
+
+ private static final int NORMAL_BEAN_CHECK_DEPTH = 2;
+
+ private BeanManager beanManager;
+
+ private Map<Class<?>, EntityListenerWrapper<?>> cache = new HashMap<>();
Review Comment:
The factory is shared by all brokers of the EMF and `getWrapper()` is called
from the callbacks, so several threads can call `containsKey`/`put` at the same
time on this `HashMap`. Two threads can also both create a cacheable listener,
and the one that is overwritten is never disposed. A `ConcurrentHashMap` (as
Romain suggested earlier) plus `putIfAbsent`, disposing the instance that lost,
would cover both.
##########
openjpa-kernel/src/main/java/org/apache/openjpa/cdi/EntityListenerCachedFactory.java:
##########
@@ -0,0 +1,49 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.openjpa.cdi;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.openjpa.lib.util.J2DoPrivHelper;
+import org.apache.openjpa.lib.util.Localizer;
+import org.apache.openjpa.util.UserException;
+
+public class EntityListenerCachedFactory implements EntityListenerFactory {
+
+ private static final Localizer _loc =
Localizer.forPackage(EntityListenerFactoryImpl.class);
+
+ private Map<Class<?>, EntityListenerWrapper<?>> cache = new HashMap<>();
Review Comment:
Same here: the listener used to be created in the constructor of
`BeanLifecycleCallbacks`, now it is created lazily on the first callback, which
can happen on several threads at once. `computeIfAbsent` on a `HashMap` is not
safe for that, a `ConcurrentHashMap` would be.
--
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]