You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by rz...@apache.org on 2024/07/15 17:03:47 UTC
(tomee) branch main updated: [TOMEE-4355] Try to workaround Mojarra+OWB integration (#1278)
This is an automated email from the ASF dual-hosted git repository.
rzo1 pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tomee.git
The following commit(s) were added to refs/heads/main by this push:
new 841f8518e5 [TOMEE-4355] Try to workaround Mojarra+OWB integration (#1278)
841f8518e5 is described below
commit 841f8518e5a1f85813fa9be45b98faa5c68801af
Author: Markus Jung <ju...@apache.org>
AuthorDate: Mon Jul 15 19:03:41 2024 +0200
[TOMEE-4355] Try to workaround Mojarra+OWB integration (#1278)
* Try to workaround Mojarra + OWB integration
* Move MojarraAfterBeanDiscoveryDecorator out of inner class
* Programmatically replace CDI extension instead of config which can conflict with user
* Retrieve WebBeansContext from beanManager
* Fix missing license header
* Inject WebBeansContext from assembler into additional/replacement extension when needed
* Register extension replacement globally instead of per deployed Application
---
.../openejb/assembler/classic/Assembler.java | 5 +-
.../apache/openejb/cdi/OptimizedLoaderService.java | 51 ++++++++++++++-
.../org/apache/tomee/catalina/TomcatLoader.java | 1 +
.../apache/tomee/mojarra/TomEEMojarraService.java | 35 ++++++++++
.../owb/MojarraAfterBeanDiscoveryDecorator.java | 76 ++++++++++++++++++++++
...ojarraPassivationCapableThirdPartyBeanImpl.java | 50 ++++++++++++++
.../mojarra/owb/OwbCompatibleCdiExtension.java | 38 +++++++++++
7 files changed, 252 insertions(+), 4 deletions(-)
diff --git a/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/Assembler.java b/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/Assembler.java
index 8d9c1a5e97..72bf4ca06a 100644
--- a/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/Assembler.java
+++ b/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/Assembler.java
@@ -1789,7 +1789,9 @@ public class Assembler extends AssemblerTool implements org.apache.openejb.spi.A
services.put(ScannerService.class, new CdiScanner());
services.put(BeanArchiveService.class, new OpenEJBBeanInfoService());
services.put(ELAdaptor.class, new CustomELAdapter(appContext));
- services.put(LoaderService.class, new OptimizedLoaderService(appContext.getProperties()));
+
+ OptimizedLoaderService loaderService = new OptimizedLoaderService(appContext.getProperties());
+ services.put(LoaderService.class, loaderService);
final Properties properties = new Properties();
properties.setProperty(org.apache.webbeans.spi.SecurityService.class.getName(), ManagedSecurityService.class.getName());
@@ -1805,6 +1807,7 @@ public class Assembler extends AssemblerTool implements org.apache.openejb.spi.A
}
webBeansContext = new WebBeansContext(services, properties);
+ loaderService.setWebBeansContext(webBeansContext);
appContext.setCdiEnabled(false);
appContext.set(WebBeansContext.class, webBeansContext);
diff --git a/container/openejb-core/src/main/java/org/apache/openejb/cdi/OptimizedLoaderService.java b/container/openejb-core/src/main/java/org/apache/openejb/cdi/OptimizedLoaderService.java
index c83275c461..f9705a3615 100644
--- a/container/openejb-core/src/main/java/org/apache/openejb/cdi/OptimizedLoaderService.java
+++ b/container/openejb-core/src/main/java/org/apache/openejb/cdi/OptimizedLoaderService.java
@@ -22,6 +22,7 @@ import org.apache.openejb.loader.SystemInstance;
import org.apache.openejb.resource.activemq.jms2.cdi.JMS2CDIExtension;
import org.apache.openejb.util.LogCategory;
import org.apache.openejb.util.Logger;
+import org.apache.webbeans.config.WebBeansContext;
import org.apache.webbeans.service.DefaultLoaderService;
import org.apache.webbeans.spi.LoaderService;
import org.apache.webbeans.spi.plugins.OpenWebBeansPlugin;
@@ -36,8 +37,10 @@ import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Enumeration;
+import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
+import java.util.Map;
import java.util.Properties;
/**
@@ -47,11 +50,15 @@ public class OptimizedLoaderService implements LoaderService {
private static final Logger log = Logger.getInstance(LogCategory.OPENEJB.createChild("cdi"), OptimizedLoaderService.class);
- public static final ThreadLocal<Collection<String>> ADDITIONAL_EXTENSIONS = new ThreadLocal<Collection<String>>();
+ public static final Map<String, String> EXTENSION_REPLACEMENTS = new HashMap<>();
+
+ public static final ThreadLocal<Collection<String>> ADDITIONAL_EXTENSIONS = new ThreadLocal<>();
private final LoaderService loaderService;
private final Properties config;
+ private WebBeansContext webBeansContext;
+
public OptimizedLoaderService(final Properties appConfig) {
this(
is("openejb.cdi.ignore-not-loadable-extensions", appConfig, SystemInstance.get().getProperties(), "true") ?
@@ -85,11 +92,29 @@ public class OptimizedLoaderService implements LoaderService {
protected List<? extends Extension> loadExtensions(final ClassLoader classLoader) {
final List<Extension> list = loaderService.load(Extension.class, classLoader);
- final Collection<String> additional = ADDITIONAL_EXTENSIONS.get();
+ Collection<String> additional = ADDITIONAL_EXTENSIONS.get();
+
+ if (!EXTENSION_REPLACEMENTS.isEmpty()) {
+ if (additional == null) {
+ additional = new ArrayList<>();
+ }
+
+ applyExtensionReplacements(list, additional);
+ }
+
if (additional != null) {
for (final String name : additional) {
try {
- list.add(Extension.class.cast(classLoader.loadClass(name).newInstance()));
+ Class<? extends Extension> extensionClazz = classLoader.loadClass(name).asSubclass(Extension.class);
+ Extension instance;
+
+ try {
+ instance = extensionClazz.getConstructor(WebBeansContext.class).newInstance(webBeansContext);
+ } catch (final Exception ignored) {
+ instance = extensionClazz.getConstructor().newInstance();
+ }
+
+ list.add(instance);
} catch (final Exception ignored) {
// no-op
}
@@ -194,6 +219,22 @@ public class OptimizedLoaderService implements LoaderService {
return false;
}
+ private void applyExtensionReplacements(List<Extension> extensions, Collection<String> additional) {
+ Iterator<Extension> iterator = extensions.iterator();
+ while (iterator.hasNext()) {
+ Extension current = iterator.next();
+
+ String currentName = current.getClass().getName();
+ String replacementName = EXTENSION_REPLACEMENTS.get(current.getClass().getName());
+ if (replacementName != null) {
+ log.info("Replacing portable CDI Extension " + currentName + " with " + replacementName);
+
+ iterator.remove();
+ additional.add(replacementName);
+ }
+ }
+ }
+
private List<? extends OpenWebBeansPlugin> loadWebBeansPlugins(final ClassLoader loader) {
final List<OpenWebBeansPlugin> list = new ArrayList<>(2);
list.add(new CdiPlugin());
@@ -311,4 +352,8 @@ public class OptimizedLoaderService implements LoaderService {
}
}
}
+
+ public void setWebBeansContext(WebBeansContext webBeansContext) {
+ this.webBeansContext = webBeansContext;
+ }
}
diff --git a/tomee/tomee-catalina/src/main/java/org/apache/tomee/catalina/TomcatLoader.java b/tomee/tomee-catalina/src/main/java/org/apache/tomee/catalina/TomcatLoader.java
index 541d4fada3..a535614d87 100644
--- a/tomee/tomee-catalina/src/main/java/org/apache/tomee/catalina/TomcatLoader.java
+++ b/tomee/tomee-catalina/src/main/java/org/apache/tomee/catalina/TomcatLoader.java
@@ -218,6 +218,7 @@ public class TomcatLoader implements Loader {
SystemInstance.get().setComponent(WebDeploymentListeners.class, new WebDeploymentListeners());
optionalService(properties, "org.apache.tomee.microprofile.TomEEMicroProfileService");
+ optionalService(properties, "org.apache.tomee.mojarra.TomEEMojarraService");
// tomee webapp enricher
final TomEEClassLoaderEnricher classLoaderEnricher = new TomEEClassLoaderEnricher();
diff --git a/tomee/tomee-mojarra/src/main/java/org/apache/tomee/mojarra/TomEEMojarraService.java b/tomee/tomee-mojarra/src/main/java/org/apache/tomee/mojarra/TomEEMojarraService.java
new file mode 100644
index 0000000000..8f50ddcb26
--- /dev/null
+++ b/tomee/tomee-mojarra/src/main/java/org/apache/tomee/mojarra/TomEEMojarraService.java
@@ -0,0 +1,35 @@
+/**
+ * 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.tomee.mojarra;
+
+import com.sun.faces.cdi.CdiExtension;
+import org.apache.openejb.cdi.OptimizedLoaderService;
+import org.apache.openejb.spi.Service;
+import org.apache.tomee.mojarra.owb.OwbCompatibleCdiExtension;
+
+import java.util.Properties;
+
+// This is just a workaround until we have a permanent solution
+// Remove this optional service in TomcatLoader again when deleting this service
+public class TomEEMojarraService implements Service {
+ @Override
+ public void init(Properties props) throws Exception {
+ // Replace Mojarra's CDI extension because it registers beans OWB can't proxy since mojarra 4.0.1
+ // See https://github.com/eclipse-ee4j/mojarra/issues/5457
+ OptimizedLoaderService.EXTENSION_REPLACEMENTS.put(CdiExtension.class.getName(), OwbCompatibleCdiExtension.class.getName());
+ }
+}
diff --git a/tomee/tomee-mojarra/src/main/java/org/apache/tomee/mojarra/owb/MojarraAfterBeanDiscoveryDecorator.java b/tomee/tomee-mojarra/src/main/java/org/apache/tomee/mojarra/owb/MojarraAfterBeanDiscoveryDecorator.java
new file mode 100644
index 0000000000..7717eb5153
--- /dev/null
+++ b/tomee/tomee-mojarra/src/main/java/org/apache/tomee/mojarra/owb/MojarraAfterBeanDiscoveryDecorator.java
@@ -0,0 +1,76 @@
+/**
+ * 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.tomee.mojarra.owb;
+
+import jakarta.enterprise.context.spi.Context;
+import jakarta.enterprise.inject.spi.AfterBeanDiscovery;
+import jakarta.enterprise.inject.spi.AnnotatedType;
+import jakarta.enterprise.inject.spi.Bean;
+import jakarta.enterprise.inject.spi.ObserverMethod;
+import jakarta.enterprise.inject.spi.configurator.BeanConfigurator;
+import jakarta.enterprise.inject.spi.configurator.ObserverMethodConfigurator;
+import org.apache.webbeans.config.WebBeansContext;
+
+public class MojarraAfterBeanDiscoveryDecorator implements AfterBeanDiscovery {
+ private final AfterBeanDiscovery delegate;
+ private final WebBeansContext webBeansContext;
+
+ public MojarraAfterBeanDiscoveryDecorator(final AfterBeanDiscovery delegate, WebBeansContext webBeansContext) {
+ this.delegate = delegate;
+ this.webBeansContext = webBeansContext;
+ }
+
+ @Override
+ public void addDefinitionError(Throwable t) {
+ delegate.addDefinitionError(t);
+ }
+
+ @Override
+ public void addBean(Bean<?> bean) {
+ delegate.addBean(new MojarraPassivationCapableThirdPartyBeanImpl<>(webBeansContext, bean));
+ }
+
+ @Override
+ public <T> BeanConfigurator<T> addBean() {
+ return delegate.addBean();
+ }
+
+ @Override
+ public void addObserverMethod(ObserverMethod<?> observerMethod) {
+ delegate.addObserverMethod(observerMethod);
+ }
+
+ @Override
+ public <T> ObserverMethodConfigurator<T> addObserverMethod() {
+ return delegate.addObserverMethod();
+ }
+
+ @Override
+ public void addContext(Context context) {
+ delegate.addContext(context);
+ }
+
+ @Override
+ public <T> AnnotatedType<T> getAnnotatedType(Class<T> type, String id) {
+ return delegate.getAnnotatedType(type, id);
+ }
+
+ @Override
+ public <T> Iterable<AnnotatedType<T>> getAnnotatedTypes(Class<T> type) {
+ return delegate.getAnnotatedTypes(type);
+ }
+}
diff --git a/tomee/tomee-mojarra/src/main/java/org/apache/tomee/mojarra/owb/MojarraPassivationCapableThirdPartyBeanImpl.java b/tomee/tomee-mojarra/src/main/java/org/apache/tomee/mojarra/owb/MojarraPassivationCapableThirdPartyBeanImpl.java
new file mode 100644
index 0000000000..f7cee2dda4
--- /dev/null
+++ b/tomee/tomee-mojarra/src/main/java/org/apache/tomee/mojarra/owb/MojarraPassivationCapableThirdPartyBeanImpl.java
@@ -0,0 +1,50 @@
+/**
+ * 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.tomee.mojarra.owb;
+
+import jakarta.enterprise.inject.spi.Bean;
+import org.apache.webbeans.component.third.PassivationCapableThirdpartyBeanImpl;
+import org.apache.webbeans.config.WebBeansContext;
+
+import java.lang.reflect.Type;
+
+public class MojarraPassivationCapableThirdPartyBeanImpl<T> extends PassivationCapableThirdpartyBeanImpl<T> {
+ private final Class<T> returnType;
+
+ public MojarraPassivationCapableThirdPartyBeanImpl(WebBeansContext webBeansContext, Bean<T> bean) {
+ super(webBeansContext, bean);
+
+ Class<?> tempReturnType = Object.class;
+ for (Type type : bean.getTypes()) {
+ if (!(type instanceof Class<?> clazz)) {
+ continue;
+ }
+
+ if (tempReturnType.isAssignableFrom(clazz))
+ {
+ tempReturnType = clazz;
+ }
+ }
+
+ this.returnType = (Class<T>) tempReturnType;
+ }
+
+ @Override
+ public Class<T> getReturnType() {
+ return returnType;
+ }
+}
diff --git a/tomee/tomee-mojarra/src/main/java/org/apache/tomee/mojarra/owb/OwbCompatibleCdiExtension.java b/tomee/tomee-mojarra/src/main/java/org/apache/tomee/mojarra/owb/OwbCompatibleCdiExtension.java
new file mode 100644
index 0000000000..2b8b141789
--- /dev/null
+++ b/tomee/tomee-mojarra/src/main/java/org/apache/tomee/mojarra/owb/OwbCompatibleCdiExtension.java
@@ -0,0 +1,38 @@
+/**
+ * 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.tomee.mojarra.owb;
+
+import com.sun.faces.cdi.CdiExtension;
+import jakarta.enterprise.event.Observes;
+import jakarta.enterprise.inject.spi.AfterBeanDiscovery;
+import jakarta.enterprise.inject.spi.BeanManager;
+import org.apache.webbeans.config.WebBeansContext;
+
+public class OwbCompatibleCdiExtension extends CdiExtension {
+ private final WebBeansContext webBeansContext;
+
+ // no-arg constructor is omitted on purpose, WebBeansContext is injected by OptimizedLoaderService
+ // when replacing the Mojarra Extension with this one
+ public OwbCompatibleCdiExtension(WebBeansContext webBeansContext) {
+ this.webBeansContext = webBeansContext;
+ }
+
+ @Override
+ public void afterBeanDiscovery(@Observes AfterBeanDiscovery afterBeanDiscovery, BeanManager beanManager) {
+ super.afterBeanDiscovery(new MojarraAfterBeanDiscoveryDecorator(afterBeanDiscovery, webBeansContext), beanManager);
+ }
+}