Skip to content

Commit

Permalink
Merge pull request #17488 from WhiteCat22/resteasy_cdi_spi
Browse files Browse the repository at this point in the history
update the restfulWS-3.0 feature to use the CDIExtensionMetadata SPI
  • Loading branch information
WhiteCat22 authored Jun 28, 2021
2 parents 0dace9f + b3f12dc commit 5208f3a
Show file tree
Hide file tree
Showing 16 changed files with 350 additions and 59 deletions.
4 changes: 3 additions & 1 deletion dev/com.ibm.ws.jaxrs.2.0.cdi.1.2_fat/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
<classpathentry kind="src" path="fat/src"/>
<classpathentry kind="src" path="test-applications/basic/src"/>
<classpathentry kind="src" path="test-applications/beanvalidation/src"/>
<classpathentry kind="src" path="test-applications/cdiinjectintoapp/src"/>
<classpathentry kind="src" path="test-applications/complex/src"/>
<classpathentry kind="src" path="test-applications/contextandCDI/src"/>
<classpathentry kind="src" path="test-applications/contextandCDI/src"/>
<classpathentry kind="src" path="test-applications/contextandclient/src"/>
<classpathentry kind="src" path="test-applications/disable/src"/>
<classpathentry kind="src" path="test-applications/lifecyclemethod/src"/>
<classpathentry kind="src" path="test-applications/lifecyclemismatch/src"/>
<classpathentry kind="src" path="test-applications/loadonstartup/src"/>
<classpathentry kind="src" path="test-applications/resourceInfoAtStartup/src"/>
<classpathentry kind="src" path="test-applications/interceptor/src"/>
<classpathentry kind="src" path="test-applications/injectIntoApp/src"/>
<classpathentry kind="con" path="aQute.bnd.classpath.container"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="output" path="bin"/>
Expand Down
1 change: 1 addition & 0 deletions dev/com.ibm.ws.jaxrs.2.0.cdi.1.2_fat/bnd.bnd
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ src: \
fat/src,\
test-applications/basic/src,\
test-applications/beanvalidation/src,\
test-applications/cdiinjectintoapp/src,\
test-applications/complex/src,\
test-applications/contextandclient/src,\
test-applications/contextandCDI/src,\
Expand Down
1 change: 0 additions & 1 deletion dev/com.ibm.ws.jaxrs.2.0.cdi.1.2_fat/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@
*******************************************************************************/
apply from: '../wlp-gradle/subprojects/maven-central-mirror.gradle'


addRequiredLibraries.dependsOn addJakartaTransformer
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.ibm.ws.jaxrs20.cdi12.fat.test.Basic12Test;
import com.ibm.ws.jaxrs20.cdi12.fat.test.BeanValidation12Test;
import com.ibm.ws.jaxrs20.cdi12.fat.test.CDIInjectIntoAppTest;
import com.ibm.ws.jaxrs20.cdi12.fat.test.Complex12Test;
import com.ibm.ws.jaxrs20.cdi12.fat.test.ContextAndClientTest;
import com.ibm.ws.jaxrs20.cdi12.fat.test.ContextandCDI12Test;
Expand All @@ -37,6 +38,7 @@
AlwaysPassesTest.class,
Basic12Test.class, // Skip for JakartaEE9
BeanValidation12Test.class, // Skip for JakartaEE9
CDIInjectIntoAppTest.class,
Complex12Test.class, // Skip for JakartaEE9
ContextAndClientTest.class,
ContextandCDI12Test.class, // Skip for JakartaEE9
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*******************************************************************************
* Copyright (c) 2021 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package com.ibm.ws.jaxrs20.cdi12.fat.test;

import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;

import com.ibm.websphere.simplicity.ShrinkHelper;
import com.ibm.ws.jaxrs20.cdi12.fat.cdiinjectintoapp.CDIInjectIntoAppTestServlet;

import componenttest.annotation.Server;
import componenttest.annotation.SkipForRepeat;
import componenttest.annotation.TestServlet;
import componenttest.custom.junit.runner.FATRunner;
import componenttest.topology.impl.LibertyServer;
import componenttest.topology.utils.FATServletClient;

@RunWith(FATRunner.class)
@SkipForRepeat("EE9_FEATURES") // skip because cdi injection in Application subclasses require the @ApplicationPath annotation in our EE9 implementation
public class CDIInjectIntoAppTest extends FATServletClient {

public static final String APP_NAME = "cdiinjectintoapp";
public static final String SERVER_NAME = APP_NAME;

@Server(SERVER_NAME)
@TestServlet(servlet = CDIInjectIntoAppTestServlet.class, contextRoot = APP_NAME)
public static LibertyServer server;

@BeforeClass
public static void setUp() throws Exception {
WebArchive war = ShrinkHelper.defaultDropinApp(server, APP_NAME, "com.ibm.ws.jaxrs20.cdi12.fat.cdiinjectintoapp");

server.startServer();
}

@AfterClass
public static void tearDown() throws Exception {
server.stopServer();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
bootstrap.include=../testports.properties
#com.ibm.ws.logging.max.files=1
#com.ibm.ws.logging.trace.specification=LogService=all:RESTfulWS=all
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<server>
<featureManager>
<feature>componenttest-1.0</feature>
<feature>jaxrs-2.0</feature>
<feature>cdi-1.2</feature>
</featureManager>
<include location="../fatTestPorts.xml"/>

<javaPermission className="java.util.PropertyPermission" name="*" actions="read" />
<javaPermission className="java.net.URLPermission" name="http://localhost:8010/cdiinjectintoapp/app/-" actions="GET:"/>
<javaPermission className="java.net.SocketPermission" name="*" actions="connect,resolve"/>
</server>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="HelloworldServlet" version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<servlet>
<servlet-name>com.ibm.ws.jaxrs20.cdi12.fat.cdiinjectintoapp.MyApplication</servlet-name>
</servlet>
<!-- SERVLET MAPPINGS -->
<servlet-mapping>
<servlet-name>com.ibm.ws.jaxrs20.cdi12.fat.cdiinjectintoapp.MyApplication</servlet-name>
<url-pattern>/app/*</url-pattern>
</servlet-mapping>
</web-app>
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*******************************************************************************
* Copyright (c) 2021 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package com.ibm.ws.jaxrs20.cdi12.fat.cdiinjectintoapp;

import static org.junit.Assert.assertEquals;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URI;

import org.junit.Test;

import componenttest.app.FATServlet;
import javax.servlet.annotation.WebServlet;

@SuppressWarnings("serial")
@WebServlet("/CDIInjectIntoAppTestServlet")
public class CDIInjectIntoAppTestServlet extends FATServlet {

@Test
public void testCanInjectIntoAppAndAppIntoResource() throws Exception {
assertEquals("SUCCESS", testResource("checkAppInjection"));
}

@Test
public void testInjectedBeansHaveCorrectScopes() throws Exception {
assertEquals("1 - 1", testResource("1"));
assertEquals("2 - 2", testResource("1"));
assertEquals("1 - 3", testResource("2"));
assertEquals("3 - 4", testResource("1"));
assertEquals("2 - 5", testResource("2"));
}

private String testResource(String resourcePath) throws Exception {
URI uri = URI.create("http://localhost:" + System.getProperty("bvt.prop.HTTP_default") + "/cdiinjectintoapp/app/resource/" + resourcePath);
HttpURLConnection conn = (HttpURLConnection) uri.toURL().openConnection();
assertEquals(200, conn.getResponseCode());
return readEntity(conn.getInputStream());
}

private String readEntity(InputStream is) throws Exception {
StringBuilder sb = new StringBuilder();
byte[] b = new byte[256];
int i = is.read(b);
while (i > 0) {
sb.append(new String(b, 0, i));
i = is.read(b);
}
return sb.toString().trim();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*******************************************************************************
* Copyright (c) 2021 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package com.ibm.ws.jaxrs20.cdi12.fat.cdiinjectintoapp;

public interface InvocationCounter {

int invoke();

int getInvocations();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*******************************************************************************
* Copyright (c) 2021 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package com.ibm.ws.jaxrs20.cdi12.fat.cdiinjectintoapp;

import java.util.concurrent.atomic.AtomicInteger;

import javax.enterprise.context.ApplicationScoped;

@ApplicationScoped
public class InvocationCounterImpl implements InvocationCounter {

AtomicInteger count = new AtomicInteger(0);

@Override
public int invoke() {
return count.incrementAndGet();
}

@Override
public int getInvocations() {
return count.get();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*******************************************************************************
* Copyright (c) 2021 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package com.ibm.ws.jaxrs20.cdi12.fat.cdiinjectintoapp;

import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.inject.Inject;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

//Test that CDI injects without the @ApplicationPath annotation, use web.xml instead
//@ApplicationPath("/app")
public class MyApplication extends Application {

@Inject
InvocationCounter counter;

@Override
public Map<String, Object> getProperties() {
System.out.println("counter=" + counter);
return Collections.singletonMap("counter", counter);
}

InvocationCounter getCounter() {
return counter;
}

@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> classes = new HashSet<Class<?>>();
classes.add(MyResource.class);
return classes;

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*******************************************************************************
* Copyright (c) 2021 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package com.ibm.ws.jaxrs20.cdi12.fat.cdiinjectintoapp;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.Context;

@Path("/resource")
public class MyResource {

static Map<String,AtomicInteger> resourceCounterMap = new HashMap<>();

static {
resourceCounterMap.put("1", new AtomicInteger(0));
resourceCounterMap.put("2", new AtomicInteger(0));
}

@Context
Application app;

@GET
@Path("checkAppInjection")
public String checkAppInjectionNotNull() {
// Do not update any counters for this method.
if (app == null) {
return "Failed to inject app into MyResource";
}
Map<String, Object> props = app.getProperties();
if (props == null) {
return "Application#getProperties is null";
}
Object counter = props.get("counter");
if (counter == null) {
return "Counter that is supposed to be injected into MyApplication is null";
}
return "SUCCESS";
}

@GET
@Path("1")
public String one() {
int resourceCount = resourceCounterMap.get("1").incrementAndGet();
int appCount = ((InvocationCounter) app.getProperties().get("counter")).invoke();
return "" + resourceCount + " - " + appCount;
}

@GET
@Path("2")
public String two() {
int resourceCount = resourceCounterMap.get("2").incrementAndGet();
int appCount = ((InvocationCounter) app.getProperties().get("counter")).invoke();
return "" + resourceCount + " - " + appCount;
}
}
2 changes: 1 addition & 1 deletion dev/io.openliberty.org.jboss.resteasy.server/bnd.bnd
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ src: src, resources
-cdiannotations:

-dsannotations: \
io.openliberty.org.jboss.resteasy.common.component.LibertyResteasyCdiExtension, \
io.openliberty.org.jboss.resteasy.common.component.ResteasyCDIExtensionMetadata, \
io.openliberty.org.jboss.resteasy.common.component.ResteasyInjectionClassListCollaborator
-dsannotations-inherit: true

Expand Down
Loading

0 comments on commit 5208f3a

Please sign in to comment.