diff --git a/ext/cdi/jersey-cdi1x/src/main/java/org/glassfish/jersey/ext/cdi1x/internal/CdiComponentProvider.java b/ext/cdi/jersey-cdi1x/src/main/java/org/glassfish/jersey/ext/cdi1x/internal/CdiComponentProvider.java index 01a2d8effe..5e5c4b0a70 100644 --- a/ext/cdi/jersey-cdi1x/src/main/java/org/glassfish/jersey/ext/cdi1x/internal/CdiComponentProvider.java +++ b/ext/cdi/jersey-cdi1x/src/main/java/org/glassfish/jersey/ext/cdi1x/internal/CdiComponentProvider.java @@ -826,7 +826,7 @@ private void bindHk2ClassAnalyzer() { int skippedElements = methodsToSkip.size() + fieldsToSkip.size(); ClassAnalyzer customizedClassAnalyzer = skippedElements > 0 - ? new InjecteeSkippingAnalyzer(defaultClassAnalyzer, methodsToSkip, fieldsToSkip) + ? new InjecteeSkippingAnalyzer(defaultClassAnalyzer, methodsToSkip, fieldsToSkip, beanManager) : defaultClassAnalyzer; Binder binder = new AbstractBinder() { diff --git a/ext/cdi/jersey-cdi1x/src/main/java/org/glassfish/jersey/ext/cdi1x/internal/InjecteeSkippingAnalyzer.java b/ext/cdi/jersey-cdi1x/src/main/java/org/glassfish/jersey/ext/cdi1x/internal/InjecteeSkippingAnalyzer.java index 591b45835d..edddf8d29f 100644 --- a/ext/cdi/jersey-cdi1x/src/main/java/org/glassfish/jersey/ext/cdi1x/internal/InjecteeSkippingAnalyzer.java +++ b/ext/cdi/jersey-cdi1x/src/main/java/org/glassfish/jersey/ext/cdi1x/internal/InjecteeSkippingAnalyzer.java @@ -30,6 +30,9 @@ import org.glassfish.hk2.api.ClassAnalyzer; import org.glassfish.hk2.api.MultiException; +import javax.enterprise.inject.spi.BeanManager; +import javax.inject.Inject; + /** * Class analyzer that ignores given injection points. * Used for CDI integration, where we need to avoid HK2 replacing CDI injected entities. @@ -41,13 +44,16 @@ public final class InjecteeSkippingAnalyzer implements ClassAnalyzer { private final ClassAnalyzer defaultAnalyzer; private final Map, Set> methodsToSkip; private final Map, Set> fieldsToSkip; + private final BeanManager beanManager; public InjecteeSkippingAnalyzer(ClassAnalyzer defaultAnalyzer, Map, Set> methodsToSkip, - Map, Set> fieldsToSkip) { + Map, Set> fieldsToSkip, + BeanManager beanManager) { this.defaultAnalyzer = defaultAnalyzer; this.methodsToSkip = methodsToSkip; this.fieldsToSkip = fieldsToSkip; + this.beanManager = beanManager; } @Override @@ -66,6 +72,7 @@ public Set getInitializerMethods(Class type) throws MultiExceptio public Set getFields(Class type) throws MultiException { final Set originalFields = defaultAnalyzer.getFields(type); final Set skippedFields = getMembersToSkip(type, fieldsToSkip); + addCdiInjectedFieldsToSkip(skippedFields, originalFields); return Views.setDiffView(originalFields, skippedFields); } @@ -98,4 +105,12 @@ private Set getMembersToSkip(final Class type, final Ma return compositeResult; } + + private void addCdiInjectedFieldsToSkip(Set skippedFields, Set originalFields) { + for (Field field : originalFields) { + if (field.getAnnotation(Inject.class) != null) { + skippedFields.add(field); + } + } + } } diff --git a/tests/integration/cdi-integration/cdi-manually-bound/pom.xml b/tests/integration/cdi-integration/cdi-manually-bound/pom.xml new file mode 100644 index 0000000000..d5821bf991 --- /dev/null +++ b/tests/integration/cdi-integration/cdi-manually-bound/pom.xml @@ -0,0 +1,82 @@ + + + + + cdi-integration-project + org.glassfish.jersey.tests.integration.cdi + 2.30-SNAPSHOT + + 4.0.0 + + cdi-manually-bound + + + + jakarta.ws.rs + jakarta.ws.rs-api + provided + + + jakarta.annotation + jakarta.annotation-api + provided + + + javax.enterprise + cdi-api + 2.0 + + + org.jboss.weld.se + weld-se-core + 3.0.3.Final + + + org.glassfish.jersey.test-framework + jersey-test-framework-util + provided + + + org.glassfish.jersey.test-framework.providers + jersey-test-framework-provider-bundle + pom + test + + + org.glassfish.jersey.ext.cdi + jersey-weld2-se + + + javax.enterprise + cdi-api + + + org.jboss.weld.se + weld-se-core + + + test + + + + + + \ No newline at end of file diff --git a/tests/integration/cdi-integration/cdi-manually-bound/src/main/java/org/glassfish/jersey/tests/cdi/manuallybound/CdiService.java b/tests/integration/cdi-integration/cdi-manually-bound/src/main/java/org/glassfish/jersey/tests/cdi/manuallybound/CdiService.java new file mode 100644 index 0000000000..9ac9b0c912 --- /dev/null +++ b/tests/integration/cdi-integration/cdi-manually-bound/src/main/java/org/glassfish/jersey/tests/cdi/manuallybound/CdiService.java @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0, which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +package org.glassfish.jersey.tests.cdi.manuallybound; + +public interface CdiService { + void doService(T t); +} diff --git a/tests/integration/cdi-integration/cdi-manually-bound/src/main/java/org/glassfish/jersey/tests/cdi/manuallybound/CdiServiceExtension.java b/tests/integration/cdi-integration/cdi-manually-bound/src/main/java/org/glassfish/jersey/tests/cdi/manuallybound/CdiServiceExtension.java new file mode 100644 index 0000000000..efb064ebf4 --- /dev/null +++ b/tests/integration/cdi-integration/cdi-manually-bound/src/main/java/org/glassfish/jersey/tests/cdi/manuallybound/CdiServiceExtension.java @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0, which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +package org.glassfish.jersey.tests.cdi.manuallybound; + +import javax.enterprise.context.ApplicationScoped; +import javax.enterprise.event.Observes; +import javax.enterprise.inject.spi.AfterBeanDiscovery; +import javax.enterprise.inject.spi.Extension; +import java.io.IOException; + +public class CdiServiceExtension implements Extension { + public void observe(@Observes AfterBeanDiscovery event) throws IOException, ClassNotFoundException { + event.addBean() + .addType(CdiService.class) + .beanClass(CdiService.class) + .scope(ApplicationScoped.class) + .createWith(context -> new CdiServiceImpl()); + } + +} diff --git a/tests/integration/cdi-integration/cdi-manually-bound/src/main/java/org/glassfish/jersey/tests/cdi/manuallybound/CdiServiceImpl.java b/tests/integration/cdi-integration/cdi-manually-bound/src/main/java/org/glassfish/jersey/tests/cdi/manuallybound/CdiServiceImpl.java new file mode 100644 index 0000000000..cd05236299 --- /dev/null +++ b/tests/integration/cdi-integration/cdi-manually-bound/src/main/java/org/glassfish/jersey/tests/cdi/manuallybound/CdiServiceImpl.java @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0, which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +package org.glassfish.jersey.tests.cdi.manuallybound; + +public class CdiServiceImpl implements CdiService { + + @Override + public void doService(StringBuilder sb) { + sb.append(getClass().getSimpleName()); + } +} diff --git a/tests/integration/cdi-integration/cdi-manually-bound/src/main/java/org/glassfish/jersey/tests/cdi/manuallybound/NoBeanDefiningAnnotationContainerFilter.java b/tests/integration/cdi-integration/cdi-manually-bound/src/main/java/org/glassfish/jersey/tests/cdi/manuallybound/NoBeanDefiningAnnotationContainerFilter.java new file mode 100644 index 0000000000..4f700ced81 --- /dev/null +++ b/tests/integration/cdi-integration/cdi-manually-bound/src/main/java/org/glassfish/jersey/tests/cdi/manuallybound/NoBeanDefiningAnnotationContainerFilter.java @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0, which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +package org.glassfish.jersey.tests.cdi.manuallybound; + +import javax.inject.Inject; +import javax.ws.rs.container.ContainerRequestContext; +import javax.ws.rs.container.ContainerResponseContext; +import javax.ws.rs.container.ContainerResponseFilter; +import java.io.IOException; +import java.util.Collections; + +public class NoBeanDefiningAnnotationContainerFilter implements ContainerResponseFilter { + + @Inject + CdiService service; + + @Override + public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException { + if (service != null) { + StringBuilder sb = new StringBuilder(); + service.doService(sb); + responseContext.getHeaders().put( + NoBeanDefiningAnnotationContainerFilter.class.getSimpleName(), + Collections.singletonList(sb.toString()) + ); + } + } +} diff --git a/tests/integration/cdi-integration/cdi-manually-bound/src/main/java/org/glassfish/jersey/tests/cdi/manuallybound/Resource.java b/tests/integration/cdi-integration/cdi-manually-bound/src/main/java/org/glassfish/jersey/tests/cdi/manuallybound/Resource.java new file mode 100644 index 0000000000..ffec9fab3f --- /dev/null +++ b/tests/integration/cdi-integration/cdi-manually-bound/src/main/java/org/glassfish/jersey/tests/cdi/manuallybound/Resource.java @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0, which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +package org.glassfish.jersey.tests.cdi.manuallybound; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; + +@Path("/") +public class Resource { + @GET + public String get() { + return Resource.class.getSimpleName(); + } +} diff --git a/tests/integration/cdi-integration/cdi-manually-bound/src/main/resources/META-INF/beans.xml b/tests/integration/cdi-integration/cdi-manually-bound/src/main/resources/META-INF/beans.xml new file mode 100644 index 0000000000..1fb9c844d4 --- /dev/null +++ b/tests/integration/cdi-integration/cdi-manually-bound/src/main/resources/META-INF/beans.xml @@ -0,0 +1,26 @@ + + + + + + \ No newline at end of file diff --git a/tests/integration/cdi-integration/cdi-manually-bound/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension b/tests/integration/cdi-integration/cdi-manually-bound/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension new file mode 100644 index 0000000000..d899e9362d --- /dev/null +++ b/tests/integration/cdi-integration/cdi-manually-bound/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension @@ -0,0 +1 @@ +org.glassfish.jersey.tests.cdi.manuallybound.CdiServiceExtension \ No newline at end of file diff --git a/tests/integration/cdi-integration/cdi-manually-bound/src/main/webapp/WEB-INF/web.xml b/tests/integration/cdi-integration/cdi-manually-bound/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000000..daed0f5bb4 --- /dev/null +++ b/tests/integration/cdi-integration/cdi-manually-bound/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,24 @@ + + + + + diff --git a/tests/integration/cdi-integration/cdi-manually-bound/src/test/java/org/glassfish/jersey/tests/cdi/manuallybound/CdiServiceInjectTest.java b/tests/integration/cdi-integration/cdi-manually-bound/src/test/java/org/glassfish/jersey/tests/cdi/manuallybound/CdiServiceInjectTest.java new file mode 100644 index 0000000000..7b6db1fc75 --- /dev/null +++ b/tests/integration/cdi-integration/cdi-manually-bound/src/test/java/org/glassfish/jersey/tests/cdi/manuallybound/CdiServiceInjectTest.java @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0, which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +package org.glassfish.jersey.tests.cdi.manuallybound; + +import org.glassfish.jersey.inject.hk2.Hk2InjectionManagerFactory; +import org.glassfish.jersey.server.ResourceConfig; +import org.glassfish.jersey.test.JerseyTest; +import org.glassfish.jersey.test.external.ExternalTestContainerFactory; +import org.jboss.weld.environment.se.Weld; +import org.junit.Assert; +import org.junit.Assume; +import org.junit.Before; +import org.junit.Test; + +import javax.ws.rs.core.Application; +import javax.ws.rs.core.Response; + +public class CdiServiceInjectTest extends JerseyTest { + private Weld weld; + + @Before + public void setup() { + Assume.assumeTrue(Hk2InjectionManagerFactory.isImmediateStrategy()); + } + + @Override + public void setUp() throws Exception { + if (Hk2InjectionManagerFactory.isImmediateStrategy()) { + if (!ExternalTestContainerFactory.class.isAssignableFrom(getTestContainerFactory().getClass())) { + weld = new Weld(); + weld.initialize(); + } + super.setUp(); + } + } + + @Override + protected Application configure() { + return new ResourceConfig(NoBeanDefiningAnnotationContainerFilter.class, Resource.class); + } + + @Test + public void testServiceIsInjected() { + try (Response response = target().request().get()) { + String header = response.getStringHeaders().getFirst(NoBeanDefiningAnnotationContainerFilter.class.getSimpleName()); + Assert.assertEquals(CdiServiceImpl.class.getSimpleName(), header); + } + } +} diff --git a/tests/integration/cdi-integration/pom.xml b/tests/integration/cdi-integration/pom.xml new file mode 100644 index 0000000000..03d804f58f --- /dev/null +++ b/tests/integration/cdi-integration/pom.xml @@ -0,0 +1,37 @@ + + + + + project + org.glassfish.jersey.tests.integration + 2.30-SNAPSHOT + + 4.0.0 + + org.glassfish.jersey.tests.integration.cdi + cdi-integration-project + cdi-integration-project + + cdi-manually-bound + + pom + + \ No newline at end of file diff --git a/tests/integration/pom.xml b/tests/integration/pom.xml index 3f78da87e8..9d50ef075d 100644 --- a/tests/integration/pom.xml +++ b/tests/integration/pom.xml @@ -38,6 +38,7 @@ cdi-beanvalidation-webapp cdi-ejb-test-webapp cdi-iface-with-non-jaxrs-impl-test-webapp + cdi-integration cdi-log-check cdi-multimodule ejb-multimodule-reload