Skip to content

Commit

Permalink
[JSF, CDI, test, #1622] The best possible (for now) test with Aries-C…
Browse files Browse the repository at this point in the history
…DI, MyFaces and Weld. Works only under debugger.
  • Loading branch information
grgrzybek committed Jul 8, 2021
1 parent dac8317 commit 71a7dd0
Show file tree
Hide file tree
Showing 15 changed files with 267 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,12 @@ private void deploy() {
}
}

// TODO: special situation for aries-cdi, which is also an extender. its two CDI extensions:
// - org.apache.aries.cdi.extension.servlet.weld.WeldServletExtension
// - org.apache.aries.cdi.extension.el.jsp.ELJSPExtension
// register two servlet context listeners via whiteboard
// We have to (somehow) wait for this extender before we actually register the web application...

state = deploymentState.get();
if (state == State.DEPLOYING) {
LOG.debug("Registering {} in WebContainer", contextPath);
Expand Down Expand Up @@ -1260,7 +1266,7 @@ private void buildModel() {
ocm.getContextParams().putAll(mainWebXml.getContextParams());
// TODO: do it consistently using runtime configuration of temp directory
File tmpLocation = new File(System.getProperty("java.io.tmpdir"), ocm.getTemporaryLocation());
if (!tmpLocation.mkdirs()) {
if (!tmpLocation.exists() && !tmpLocation.mkdirs()) {
LOG.warn("Can't create temporary directory for {}: {}", ocm, tmpLocation.getAbsolutePath());
}
ocm.getInitialContextAttributes().put(ServletContext.TEMPDIR, tmpLocation);
Expand Down
65 changes: 65 additions & 0 deletions pax-web-fragments/pax-web-fragment-myfaces-inject/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2020 OPS4J.
Licensed 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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.ops4j.pax.web</groupId>
<artifactId>pax-web-fragments</artifactId>
<version>8.0.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<groupId>org.ops4j.pax.web</groupId>
<artifactId>pax-web-fragment-myfaces-inject</artifactId>
<packaging>bundle</packaging>

<name>OPS4J Pax Web - myfaces-api tweak </name>

<description>
This fragment bundle adds javax.inject import to myfaces-api
</description>

<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Fragment-Host>org.apache.myfaces.core.api</Fragment-Host>
<Import-Package>javax.inject</Import-Package>
</instructions>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>

<dependency>
<groupId>org.apache.myfaces.core</groupId>
<artifactId>myfaces-impl</artifactId>
</dependency>

</dependencies>

</project>
1 change: 1 addition & 0 deletions pax-web-fragments/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
<module>pax-web-compatibility-interceptor12</module>
<module>pax-web-compatibility-annotation13</module>
<!-- These are other support fragments -->
<module>pax-web-fragment-myfaces-inject</module>
<module>pax-web-fragment-myfaces-spifly</module>
</modules>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ protected Option[] paxCdiAndMyfaces(String containerCdiArtifact) {
);
}

protected Option[] ariesCdiAndMyfaces(String containerCdiArtifact) {
protected Option[] ariesCdiAndMyfaces() {
return new Option[]{
mavenBundle("jakarta.websocket", "jakarta.websocket-api")
.versionAsInProject().startLevel(START_LEVEL_TEST_BUNDLE - 1),
Expand All @@ -492,6 +492,8 @@ protected Option[] ariesCdiAndMyfaces(String containerCdiArtifact) {
.versionAsInProject().startLevel(START_LEVEL_TEST_BUNDLE - 1),
mavenBundle("org.apache.myfaces.core", "myfaces-impl")
.versionAsInProject().startLevel(START_LEVEL_TEST_BUNDLE - 1),
mavenBundle("org.ops4j.pax.web", "pax-web-fragment-myfaces-inject")
.versionAsInProject().startLevel(START_LEVEL_TEST_BUNDLE - 2).noStart(),
mavenBundle("org.ops4j.pax.web", "pax-web-fragment-myfaces-spifly")
.versionAsInProject().startLevel(START_LEVEL_TEST_BUNDLE - 2).noStart(),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@
<groupId>org.ops4j.pax.web</groupId>
<artifactId>pax-web-compatibility-annotation13</artifactId>
</dependency>
<dependency>
<groupId>org.ops4j.pax.web</groupId>
<artifactId>pax-web-fragment-myfaces-inject</artifactId>
</dependency>
<dependency>
<groupId>org.ops4j.pax.web</groupId>
<artifactId>pax-web-fragment-myfaces-spifly</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@

public abstract class AbstractWarJsfCdiIntegrationTest extends AbstractContainerTestBase {

protected abstract String containerSpecificCdiBundle();

// only Jetty implementation for now, I need to prepare Pax CDI 1.2.x to integrate with new Pax Web...

}
13 changes: 13 additions & 0 deletions pax-web-itest/pax-web-itest-container/pax-web-itest-jetty/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,19 @@
<artifactId>jakarta.el-api</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<!-- Can't be added to maven-failsafe-plugin classpath -->
<groupId>jakarta.enterprise</groupId>
<artifactId>jakarta.enterprise.cdi-api</artifactId>
<scope>runtime</scope>
</dependency>

<dependency>
<!-- Can't be added to maven-failsafe-plugin classpath -->
<groupId>org.apache.myfaces.core</groupId>
<artifactId>myfaces-api</artifactId>
<scope>runtime</scope>
</dependency>

<!-- Jetty -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.ops4j.pax.web.itest.jetty.war.jsf;

import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.ops4j.pax.exam.Configuration;
Expand All @@ -25,6 +26,8 @@
import org.ops4j.pax.web.itest.utils.client.HttpTestClientFactory;
import org.osgi.framework.Bundle;

import static org.ops4j.pax.exam.CoreOptions.bootDelegationPackage;
import static org.ops4j.pax.exam.CoreOptions.systemPackage;
import static org.ops4j.pax.exam.OptionUtils.combine;

@RunWith(PaxExam.class)
Expand All @@ -35,10 +38,15 @@ public class WarJSFCdiIntegrationTest extends AbstractWarJsfCdiIntegrationTest {
@Configuration
public Option[] configure() {
Option[] serverOptions = combine(baseConfigure(), paxWebJetty());
Option[] osgiOptions = combine(serverOptions, configAdmin());
// myfaces-core-api doesn't import javax.inject and I'm getting
// WELD-001111: interface javax.faces.annotation.FlowMap defined on org.apache.myfaces.cdi.JsfArtifactFlowMapProducer@2143ca58 is not a qualifier
// if javax.inject is available only as bundle (== is excluded from maven-failsafe-plugin test classpath)
Option[] tweakOptions1 = combine(serverOptions, bootDelegationPackage("javax.inject"));
Option[] tweakOptions2 = combine(tweakOptions1, systemPackage("javax.inject;version=\"1.0\""));
Option[] osgiOptions = combine(tweakOptions2, configAdmin());
Option[] whiteboardOptions = combine(osgiOptions, paxWebExtenderWhiteboard());
Option[] jspOptions = combine(whiteboardOptions, paxWebJsp());
Option[] cdiOptions = combine(jspOptions, ariesCdiAndMyfaces(containerSpecificCdiBundle()));
Option[] cdiOptions = combine(jspOptions, ariesCdiAndMyfaces());
return combine(cdiOptions, paxWebExtenderWar());
}

Expand All @@ -50,16 +58,22 @@ public void setUp() throws Exception {
}

@Test
@Ignore
public void testCdi() throws Exception {
// This test works fine when:
// - ARIES-2053 is fixed
// - I manually (in debugger) order the WAB deployment and calls to:
// - org.apache.aries.cdi.extension.servlet.weld.WeldServletExtension.afterDeploymentValidation()
// - org.apache.aries.cdi.extension.el.jsp.ELJSPExtension.afterDeploymentValidation()
// so the important aries-cdi/weld listeners are registered after the WAB is deployed (and its
// OsgiContextModel)
// - TODO: the listeners added after the WAB context (real Jetty/Tomcat/Undertow) has started are called
// (ServletContextListener.contextInitialized()) in separate thread to not block Aries CCR thread and
// pax-web-config thread.
HttpTestClientFactory.createDefaultTestClient()
.withResponseAssertion("Response must contain '" + containerSpecificCdiBundle() + "'",
resp -> resp.contains(containerSpecificCdiBundle()))
.withResponseAssertion("Response must contain 'hello from working JSF 2.3/CDI 2.0 example, org.ops4j.pax.url.commons.handler.HandlerActivator$Handler'",
resp -> resp.contains("hello from working JSF 2.3/CDI 2.0 example, org.ops4j.pax.url.commons.handler.HandlerActivator$Handler"))
.doGETandExecuteTest("http://127.0.0.1:8181/war-jsf23-cdi/");
}

@Override
protected String containerSpecificCdiBundle() {
return "pax-cdi-jetty-weld";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@ public class ListenerAddedInWebXml implements ServletContextListener {

@Override
public void contextInitialized(ServletContextEvent sce) {
StaticList.EVENTS.add("Listener-from-web.xml initialized");
try {
sce.getServletContext().addListener(new ServletContextListener() {
});
} catch (UnsupportedOperationException ignored) {
// it should not be possible to add a listener from a listener
StaticList.EVENTS.add("Listener-from-web.xml initialized");
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class SCIThatAddsServletContextListener implements ServletContainerInitia

@Override
public void onStartup(Set<Class<?>> c, ServletContext ctx) throws ServletException {
// this is exactly how JasperInitializer (JSP SCI) adds context listeners found in TLDs
ctx.addListener(new ServletContextListener() {
@Override
public void contextInitialized(ServletContextEvent sce) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1305,18 +1305,16 @@ public void visit(EventListenerModelChange change) {

// add the listener to real context - even ServletContextAttributeListener (but only once - even
// if there are many OsgiServletContexts per ServletContext)
// boolean restartNeeded = false;
// if (servletContextHandler.isStarted()) {
// try {
// servletContextHandler.stop();
// restartNeeded = true;
// } catch (Exception e) {
// LOG.error(e.getMessage(), e);
// }
// }
servletContextHandler.addEventListener(eventListener);
// if (restartNeeded) {
// ensureServletContextStarted(servletContextHandler);

// calling javax.servlet.ServletContextListener.contextInitialized() when server (context) is
// already started and doing it in separate thread is a tweak to make Aries-CDI + extensions
// work with CDI/JSF sample. I definitely have to solve it differently.
// The probelms are summarized in https://github.com/ops4j/org.ops4j.pax.web/issues/1622
// if (servletContextHandler.isStarted() && ServletContextListener.class.isAssignableFrom(eventListener.getClass())) {
// new Thread(() -> {
// ((ServletContextListener) eventListener).contextInitialized(new ServletContextEvent(osgiServletContexts.get(context)));
// }).start();
// }
});
}
Expand Down
53 changes: 53 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@
<dependency.jakarta.cdi-api.version>2.0.2</dependency.jakarta.cdi-api.version>
<dependency.jakarta.el-api.version>3.0.3</dependency.jakarta.el-api.version>
<dependency.geronimo.el-api.version>1.1</dependency.geronimo.el-api.version>
<dependency.jakarta.inject-api>1.0.3</dependency.jakarta.inject-api>
<dependency.jakarta.interceptor-api.version>1.2.5</dependency.jakarta.interceptor-api.version>
<dependency.geronimo.interceptor-api.version>1.2</dependency.geronimo.interceptor-api.version>
<dependency.jakarta.security.auth.message-api.version>1.1.3</dependency.jakarta.security.auth.message-api.version>
Expand Down Expand Up @@ -815,6 +816,11 @@
<artifactId>pax-web-compatibility-annotation13</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.ops4j.pax.web</groupId>
<artifactId>pax-web-fragment-myfaces-inject</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.ops4j.pax.web</groupId>
<artifactId>pax-web-fragment-myfaces-spifly</artifactId>
Expand Down Expand Up @@ -1650,6 +1656,11 @@

<!-- JavaEE / JakartaEE -->

<dependency>
<groupId>jakarta.inject</groupId>
<artifactId>jakarta.inject-api</artifactId>
<version>${dependency.jakarta.inject-api}</version>
</dependency>
<dependency>
<groupId>com.sun.activation</groupId>
<artifactId>javax.activation</artifactId>
Expand Down Expand Up @@ -2663,16 +2674,34 @@
<groupId>org.glassfish.hk2</groupId>
<artifactId>hk2-api</artifactId>
<version>${dependency.org.glassfish.hk2}</version>
<exclusions>
<exclusion>
<groupId>org.glassfish.hk2.external</groupId>
<artifactId>jakarta.inject</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.glassfish.hk2</groupId>
<artifactId>hk2-locator</artifactId>
<version>${dependency.org.glassfish.hk2}</version>
<exclusions>
<exclusion>
<groupId>org.glassfish.hk2.external</groupId>
<artifactId>jakarta.inject</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.glassfish.hk2</groupId>
<artifactId>hk2-utils</artifactId>
<version>${dependency.org.glassfish.hk2}</version>
<exclusions>
<exclusion>
<groupId>org.glassfish.hk2.external</groupId>
<artifactId>jakarta.inject</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.glassfish.hk2</groupId>
Expand All @@ -2684,16 +2713,34 @@
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-common</artifactId>
<version>${dependency.jersey.version}</version>
<exclusions>
<exclusion>
<groupId>org.glassfish.hk2.external</groupId>
<artifactId>jakarta.inject</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>${dependency.jersey.version}</version>
<exclusions>
<exclusion>
<groupId>org.glassfish.hk2.external</groupId>
<artifactId>jakarta.inject</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>${dependency.jersey.version}</version>
<exclusions>
<exclusion>
<groupId>org.glassfish.hk2.external</groupId>
<artifactId>jakarta.inject</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
Expand All @@ -2704,6 +2751,12 @@
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>${dependency.jersey.version}</version>
<exclusions>
<exclusion>
<groupId>org.glassfish.hk2.external</groupId>
<artifactId>jakarta.inject</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
Expand Down
Loading

0 comments on commit 71a7dd0

Please sign in to comment.