Skip to content

Commit

Permalink
[Fixes #1622] Working Aries-CDI + JSF + Pax Web WAB scenario for Jett…
Browse files Browse the repository at this point in the history
…y, Tomcat and Undertow
  • Loading branch information
grgrzybek committed Feb 19, 2022
1 parent 82fce02 commit 44ee68b
Show file tree
Hide file tree
Showing 24 changed files with 558 additions and 225 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ public OsgiContextModel addingService(final ServiceReference<S> serviceReference
serviceId = 0L;
}
OsgiContextModel model = new OsgiContextModel(serviceReference.getBundle(), rank, serviceId, true);
model.setAsynchronusRegistration(true);

LOG.debug("Configuring OSGi context model from Whiteboard service {} (id={})", serviceReference, serviceId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ public T addingService(final ServiceReference<S> serviceReference) {
// turn a ServiceReference into ElementModel<R> that can be passed to HttpService/WebContainer
// and contains almost _everything_ needed to process it later (for example after WebContainer becomes available)
T webElement = createElementModel(serviceReference, rank, serviceId);
webElement.setAsynchronusRegistration(true);

return addingService(serviceReference, webElement);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,9 @@ protected Option[] ariesCdiAndMyfaces() {
.versionAsInProject().startLevel(START_LEVEL_TEST_BUNDLE - 1),
mavenBundle("org.ops4j.pax.web", "pax-web-compatibility-interceptor12")
.versionAsInProject().startLevel(START_LEVEL_TEST_BUNDLE - 2).noStart(),
// Aries CDI extension.servlet.weld and extension.el.jsp require JavaServlet 3.1 capability...
mavenBundle("org.ops4j.pax.web", "pax-web-compatibility-servlet31")
.versionAsInProject().startLevel(START_LEVEL_TEST_BUNDLE - 1).noStart(),

mavenBundle("org.osgi", "org.osgi.service.cdi")
.versionAsInProject().startLevel(START_LEVEL_TEST_BUNDLE - 1),
Expand Down Expand Up @@ -662,8 +665,6 @@ protected Option[] ariesCdiAndMyfaces() {
mavenBundle("jakarta.validation", "jakarta.validation-api")
.versionAsInProject().startLevel(START_LEVEL_TEST_BUNDLE - 1),
mavenBundle("org.jboss.classfilewriter", "jboss-classfilewriter")
.versionAsInProject().startLevel(START_LEVEL_TEST_BUNDLE - 1),
mavenBundle("org.jboss.weld", "weld-osgi-bundle")
.versionAsInProject().startLevel(START_LEVEL_TEST_BUNDLE - 1)
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright 2021 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.
*/
package org.ops4j.pax.web.itest.container.war.jsf;

import org.junit.Before;
import org.junit.Test;
import org.ops4j.pax.web.itest.container.AbstractContainerTestBase;
import org.ops4j.pax.web.itest.utils.client.HttpTestClientFactory;
import org.ops4j.pax.web.service.spi.model.events.EventListenerEventData;
import org.ops4j.pax.web.service.spi.model.events.WebElementEvent;

public class AbstractWarJSFCdiIntegrationTest extends AbstractContainerTestBase {

@Before
public void setUp() {
// simple waiting for deployment is not enough, because war-jsf23-cdi bundle is subject to CDI extension
// as well. And because org.apache.aries.cdi.extension.servlet.weld whiteboard-registers
// additional listener (org.jboss.weld.module.web.servlet.WeldInitialListener), the /war-jsf23-cdi
// will be restarted
configureAndWait(() -> {
installAndStartBundle(sampleWarURI("war-jsf23-cdi"));
}, events -> {
// when the WAB is starting, the context will be deployed together with all the listeners and SCIs
// but at the same time Aries CDI will be extending our WAB as well, registering a context listener
// we can assume tha
LOG.info("~~~ events: {}", events.size());
for (WebElementEvent ev : events) {
if (ev.getType() == WebElementEvent.State.DEPLOYED && ev.getData() instanceof EventListenerEventData) {
EventListenerEventData eled = (EventListenerEventData) ev.getData();
LOG.info("~~~ evd: {}/{} ({})", eled.getListener(), eled.getElementReference(), eled.getOriginBundle());
}
}
if (events.stream().filter(e -> e.getType() == WebElementEvent.State.DEPLOYED).count() >= 4) {
// give it one more second... After all, we've only detected listener registrations
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
LOG.error(e.getMessage(), e);
Thread.currentThread().interrupt();
throw new RuntimeException(e.getMessage(), e);
}
return true;
}
return false;
});
}

@Test
public void testCdi() throws Exception {
HttpTestClientFactory.createDefaultTestClient()
.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/");
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,18 @@
*/
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;
import org.ops4j.pax.exam.Option;
import org.ops4j.pax.exam.junit.PaxExam;
import org.ops4j.pax.web.itest.container.war.jsf.AbstractWarJsfCdiIntegrationTest;
import org.ops4j.pax.web.itest.utils.client.HttpTestClientFactory;
import org.ops4j.pax.web.itest.container.war.jsf.AbstractWarJSFCdiIntegrationTest;

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)
public class WarJSFCdiIntegrationTest extends AbstractWarJsfCdiIntegrationTest {
public class WarJSFCdiIntegrationTest extends AbstractWarJSFCdiIntegrationTest {

@Configuration
public Option[] configure() {
Expand All @@ -47,30 +43,4 @@ public Option[] configure() {
return combine(cdiOptions, paxWebExtenderWar());
}

@Before
public void setUp() throws Exception {
configureAndWaitForDeploymentUnlessInstalled("war-jsf23-cdi", () -> {
installAndStartBundle(sampleWarURI("war-jsf23-cdi"));
});
}

@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 '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/");
}

}
29 changes: 29 additions & 0 deletions pax-web-itest/pax-web-itest-container/pax-web-itest-tomcat/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,22 @@
<scope>runtime</scope>
</dependency>

<!-- OSGi -->

<dependency>
<!-- Can't be added to maven-failsafe-plugin classpath -->
<groupId>org.osgi</groupId>
<artifactId>org.osgi.service.cdi</artifactId>
<scope>runtime</scope>
</dependency>

<dependency>
<!-- Can't be added to maven-failsafe-plugin classpath -->
<groupId>org.apache.aries.cdi</groupId>
<artifactId>org.apache.aries.cdi.extender</artifactId>
<scope>runtime</scope>
</dependency>

<!-- JavaEE -->

<dependency>
Expand All @@ -102,6 +118,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>

</dependencies>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2021 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.
*/
package org.ops4j.pax.web.itest.tomcat.war.jsf;

import org.junit.runner.RunWith;
import org.ops4j.pax.exam.Configuration;
import org.ops4j.pax.exam.Option;
import org.ops4j.pax.exam.junit.PaxExam;
import org.ops4j.pax.web.itest.container.war.jsf.AbstractWarJSFCdiIntegrationTest;

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)
public class WarJSFCdiIntegrationTest extends AbstractWarJSFCdiIntegrationTest {

@Configuration
public Option[] configure() {
Option[] serverOptions = combine(baseConfigure(), paxWebTomcat());
// 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());
return combine(cdiOptions, paxWebExtenderWar());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,22 @@
<scope>runtime</scope>
</dependency>

<!-- OSGi -->

<dependency>
<!-- Can't be added to maven-failsafe-plugin classpath -->
<groupId>org.osgi</groupId>
<artifactId>org.osgi.service.cdi</artifactId>
<scope>runtime</scope>
</dependency>

<dependency>
<!-- Can't be added to maven-failsafe-plugin classpath -->
<groupId>org.apache.aries.cdi</groupId>
<artifactId>org.apache.aries.cdi.extender</artifactId>
<scope>runtime</scope>
</dependency>

<!-- JavaEE -->

<dependency>
Expand All @@ -112,6 +128,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>

<!-- Undertow -->

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2021 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.
*/
package org.ops4j.pax.web.itest.undertow.war.jsf;

import org.junit.runner.RunWith;
import org.ops4j.pax.exam.Configuration;
import org.ops4j.pax.exam.Option;
import org.ops4j.pax.exam.junit.PaxExam;
import org.ops4j.pax.web.itest.container.war.jsf.AbstractWarJSFCdiIntegrationTest;

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)
public class WarJSFCdiIntegrationTest extends AbstractWarJSFCdiIntegrationTest {

@Configuration
public Option[] configure() {
Option[] serverOptions = combine(baseConfigure(), paxWebUndertow());
// 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());
return combine(cdiOptions, paxWebExtenderWar());
}

}
Loading

0 comments on commit 44ee68b

Please sign in to comment.