Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Take Hk2CustomBoundTypesProvider into an account #4298

Merged
merged 1 commit into from
Oct 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,14 @@ private void afterDiscoveryObserver(@Observes final AfterBeanDiscovery abd) {
}
}

/**
* Get the types provided by HK2
* @return Types that HK2 is to inject
*/
/* package */ boolean isHk2ProvidedType(Type type) {
return hk2ProvidedTypes.contains(type);
}

/**
* Gets you fields to skip from a proxied instance.
* <p/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public final class InjecteeSkippingAnalyzer implements ClassAnalyzer {
private final Map<Class<?>, Set<Method>> methodsToSkip;
private final Map<Class<?>, Set<Field>> fieldsToSkip;
private final BeanManager beanManager;
private final CdiComponentProvider cdiComponentProvider;

public InjecteeSkippingAnalyzer(ClassAnalyzer defaultAnalyzer,
Map<Class<?>, Set<Method>> methodsToSkip,
Expand All @@ -54,6 +55,7 @@ public InjecteeSkippingAnalyzer(ClassAnalyzer defaultAnalyzer,
this.methodsToSkip = methodsToSkip;
this.fieldsToSkip = fieldsToSkip;
this.beanManager = beanManager;
this.cdiComponentProvider = beanManager.getExtension(CdiComponentProvider.class);
}

@Override
Expand Down Expand Up @@ -108,7 +110,7 @@ private <M extends Member> Set<M> getMembersToSkip(final Class<?> type, final Ma

private void addCdiInjectedFieldsToSkip(Set<Field> skippedFields, Set<Field> originalFields) {
for (Field field : originalFields) {
if (field.getAnnotation(Inject.class) != null) {
if (field.getAnnotation(Inject.class) != null && !cdiComponentProvider.isHk2ProvidedType(field.getType())) {
skippedFields.add(field);
}
}
Expand Down
9 changes: 6 additions & 3 deletions tests/integration/cdi-integration/cdi-manually-bound/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,30 @@
<dependency>
<groupId>jakarta.ws.rs</groupId>
<artifactId>jakarta.ws.rs-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jakarta.annotation</groupId>
<artifactId>jakarta.annotation-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext.cdi</groupId>
<artifactId>jersey-cdi1x</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.weld.se</groupId>
<artifactId>weld-se-core</artifactId>
<version>3.0.3.Final</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.test-framework</groupId>
<artifactId>jersey-test-framework-util</artifactId>
<scope>provided</scope>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -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 org.glassfish.jersey.internal.inject.AbstractBinder;

import javax.inject.Singleton;

public class HK2Binder extends AbstractBinder {
@Override
protected void configure() {
bindAsContract(HK2ServiceImpl.class).to(HK2Service.class).in(Singleton.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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 javax.ws.rs.core.Configuration;
import java.io.IOException;

public class HK2InjectedFilter implements ContainerResponseFilter {
@Inject
HK2Service service;

@Inject
Configuration configuration;

@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException {
final StringBuilder stringBuilder = new StringBuilder();
if (service != null) {
service.service(stringBuilder);
responseContext.getHeaders().add(HK2InjectedFilter.class.getSimpleName(), stringBuilder.toString());
}
}
}
Original file line number Diff line number Diff line change
@@ -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 HK2Service {
void service(StringBuilder stringBuilder);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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.event.Observes;
import javax.enterprise.inject.literal.InjectLiteral;
import javax.enterprise.inject.spi.AfterTypeDiscovery;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.Extension;
import javax.enterprise.inject.spi.ProcessAnnotatedType;
import javax.enterprise.inject.spi.configurator.AnnotatedTypeConfigurator;

/*
* Replaces bean-discovery-mode="annotated" + @ApplicationScoped on HK2InjectedFilter
*/
public class HK2ServiceExtension implements Extension {
public void observeAfterTypeDiscovery(@Observes AfterTypeDiscovery event, BeanManager beanManager) {
event.addAnnotatedType(HK2InjectedFilter.class, "test-hk2service");
}

public void decorateAnnotatedType(@Observes ProcessAnnotatedType<HK2InjectedFilter> pat, BeanManager beanManager) {
AnnotatedTypeConfigurator<HK2InjectedFilter> annotatedTypeConfigurator = pat.configureAnnotatedType();

annotatedTypeConfigurator.filterFields(service -> service.getBaseType() == HK2Service.class).findFirst().get()
.remove(a -> a.equals(InjectLiteral.INSTANCE));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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 HK2ServiceImpl implements HK2Service {
@Override
public void service(StringBuilder stringBuilder) {
stringBuilder.append(HK2ServiceImpl.class.getSimpleName());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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.ext.cdi1x.spi.Hk2CustomBoundTypesProvider;

import javax.ws.rs.core.Configuration;
import java.lang.reflect.Type;
import java.util.HashSet;
import java.util.Set;

public class Hk2CustomTypesProvider implements Hk2CustomBoundTypesProvider {

@Override
public Set<Type> getHk2Types(){
Set<Type> set = new HashSet<>();
set.add(Configuration.class);
set.add(HK2Service.class);
return set;
}
}
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
org.glassfish.jersey.tests.cdi.manuallybound.CdiServiceExtension
org.glassfish.jersey.tests.cdi.manuallybound.CdiServiceExtension
org.glassfish.jersey.tests.cdi.manuallybound.HK2ServiceExtension
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.glassfish.jersey.tests.cdi.manuallybound.Hk2CustomTypesProvider
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,19 @@ public void setUp() throws Exception {
}
}

@Override
public void tearDown() throws Exception {
weld.shutdown();
super.tearDown();
}

@Override
protected Application configure() {
return new ResourceConfig(NoBeanDefiningAnnotationContainerFilter.class, Resource.class);
}

@Test
public void testServiceIsInjected() {
public void testCdiServiceIsInjected() {
try (Response response = target().request().get()) {
String header = response.getStringHeaders().getFirst(NoBeanDefiningAnnotationContainerFilter.class.getSimpleName());
Assert.assertEquals(CdiServiceImpl.class.getSimpleName(), header);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* 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 HK2ServiceInjectTest 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
public void tearDown() throws Exception {
weld.shutdown();
super.tearDown();
}

@Override
protected Application configure() {
return new ResourceConfig(Resource.class, HK2InjectedFilter.class).register(new HK2Binder());
}

@Test
public void testHK2ServiceIsInjected() {
try (Response response = target().request().get()) {
String header = response.getStringHeaders().getFirst(HK2InjectedFilter.class.getSimpleName());
Assert.assertEquals(HK2ServiceImpl.class.getSimpleName(), header);
}
}
}