Skip to content
This repository was archived by the owner on May 28, 2018. It is now read-only.

Commit b827896

Browse files
japodjerseyrobot
authored andcommitted
JERSEY-2527: Jersey CDI extension registers unnecessary/redundant CDI beans
Change-Id: I4c7bf5c7c94bba83cd08075d7795a0a824e2935f Signed-off-by: Jakub Podlesak <[email protected]>
1 parent cdcfaad commit b827896

File tree

5 files changed

+90
-60
lines changed

5 files changed

+90
-60
lines changed

containers/glassfish/jersey-gf-cdi/src/main/java/org/glassfish/jersey/gf/cdi/internal/CdiComponentProvider.java

Lines changed: 24 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -616,16 +616,6 @@ public boolean isAnnotationPresent(Class<? extends Annotation> annotationType) {
616616
});
617617
}
618618

619-
@SuppressWarnings("unused")
620-
private void processProducerMethod(@Observes final ProcessProducerMethod processProducerMethod) {
621-
typesSeenBeforeValidation.addAll(processProducerMethod.getAnnotatedProducerMethod().getTypeClosure());
622-
}
623-
624-
@SuppressWarnings("unused")
625-
private void processProducerField(@Observes final ProcessProducerField processProducerField) {
626-
typesSeenBeforeValidation.addAll(processProducerField.getAnnotatedProducerField().getTypeClosure());
627-
}
628-
629619
@SuppressWarnings("unused")
630620
private void afterTypeDiscovery(@Observes final AfterTypeDiscovery afterTypeDiscovery) {
631621
final List<Class<?>> interceptors = afterTypeDiscovery.getInterceptors();
@@ -673,11 +663,10 @@ public Object intercept(final InvocationContext ic) throws Exception {
673663
private void processInjectionTarget(@Observes final ProcessInjectionTarget event) {
674664
final InjectionTarget it = event.getInjectionTarget();
675665
final Class<?> componentClass = event.getAnnotatedType().getJavaClass();
676-
typesSeenBeforeValidation.add(event.getAnnotatedType().getBaseType());
677666

678-
final Set<InjectionPoint> filteredInjectionPoints = filterHk2InjectionPointsOut(it.getInjectionPoints());
667+
final Set<InjectionPoint> cdiInjectionPoints = filterHk2InjectionPointsOut(it.getInjectionPoints());
679668

680-
for (final InjectionPoint injectionPoint : filteredInjectionPoints) {
669+
for (final InjectionPoint injectionPoint : cdiInjectionPoints) {
681670
final Member member = injectionPoint.getMember();
682671
if (member instanceof Field) {
683672
addInjecteeToSkip(componentClass, fieldsToSkip, (Field) member);
@@ -702,45 +691,42 @@ public Set getInjectionPoints() {
702691
@Override
703692
public Set getInjectionPoints() {
704693
// Inject CDI beans into JAX-RS resources/providers/application.
705-
return filteredInjectionPoints;
694+
return cdiInjectionPoints;
706695
}
707696
});
708697
}
709698
}
710699

711-
// TODO: refactor the following method
712700
private Set<InjectionPoint> filterHk2InjectionPointsOut(final Set<InjectionPoint> originalInjectionPoints) {
713701
final Set<InjectionPoint> filteredInjectionPoints = new HashSet<InjectionPoint>();
714702
for (InjectionPoint ip : originalInjectionPoints) {
715703
final Type injectedType = ip.getType();
716-
if (injectedType instanceof Class<?>) {
717-
if (!isJerseyOrDependencyType((Class<?>)injectedType)
718-
&& !(customHk2TypesProvider != null && customHk2TypesProvider.getHk2Types().contains(injectedType))) {
719-
filteredInjectionPoints.add(ip);
720-
} else {
721-
//remember the type, we would need to mock it's CDI binding at runtime
722-
hk2ProvidedTypes.add(injectedType);
723-
}
704+
if (customHk2TypesProvider != null && customHk2TypesProvider.getHk2Types().contains(injectedType)) {
705+
//remember the type, we would need to mock it's CDI binding at runtime
706+
hk2ProvidedTypes.add(injectedType);
724707
} else {
725-
if (isInjectionProvider(injectedType)) {
726-
if (!isProviderOfJerseyType((ParameterizedType)injectedType)
727-
&& !(customHk2TypesProvider != null && customHk2TypesProvider.getHk2Types().contains(injectedType))) {
708+
if (injectedType instanceof Class<?>) {
709+
if (isJerseyOrDependencyType((Class<?>)injectedType)) {
710+
//remember the type, we would need to mock it's CDI binding at runtime
711+
hk2ProvidedTypes.add(injectedType);
712+
} else {
728713
filteredInjectionPoints.add(ip);
714+
}
715+
} else { // it is not a class, maybe provider type?:
716+
if (isInjectionProvider(injectedType)
717+
&& (isProviderOfJerseyType((ParameterizedType)injectedType))) {
718+
//remember the type, we would need to mock it's CDI binding at runtime
719+
hk2ProvidedTypes.add(((ParameterizedType)injectedType).getActualTypeArguments()[0]);
729720
} else {
730-
//remember the type, we would need to mock it's CDI binding at runtime
731-
hk2ProvidedTypes.add(((ParameterizedType)injectedType).getActualTypeArguments()[0]);
721+
filteredInjectionPoints.add(ip);
732722
}
733-
} else {
734-
filteredInjectionPoints.add(ip);
735723
}
736724
}
737725
}
738726
return filteredInjectionPoints;
739727
}
740728

741729
private final Set<Type> hk2ProvidedTypes = Collections.synchronizedSet(new HashSet<Type>());
742-
private final Set<Type> potentionalHk2CustomBoundTypes = Collections.synchronizedSet(new HashSet<Type>());
743-
private final Set<Type> typesSeenBeforeValidation = Collections.synchronizedSet(new HashSet<Type>());
744730
private final Set<Type> jerseyVetoedTypes = Collections.synchronizedSet(new HashSet<Type>());
745731

746732
private boolean isInjectionProvider(final Type injectedType) {
@@ -750,7 +736,10 @@ private boolean isInjectionProvider(final Type injectedType) {
750736

751737
private boolean isProviderOfJerseyType(final ParameterizedType provider) {
752738
final Type firstArgumentType = provider.getActualTypeArguments()[0];
753-
return firstArgumentType instanceof Class && isJerseyOrDependencyType((Class<?>)firstArgumentType);
739+
if (firstArgumentType instanceof Class && isJerseyOrDependencyType((Class<?>)firstArgumentType)) {
740+
return true;
741+
}
742+
return (customHk2TypesProvider != null && customHk2TypesProvider.getHk2Types().contains(firstArgumentType));
754743
}
755744

756745
/**
@@ -763,23 +752,7 @@ public static class CdiDefaultAnnotation extends AnnotationLiteral<Default> impl
763752
@SuppressWarnings({ "unused", "unchecked", "rawtypes" })
764753
private void afterDiscoveryObserver(@Observes AfterBeanDiscovery abd) {
765754

766-
if (customHk2TypesProvider == null) {
767-
potentionalHk2CustomBoundTypes.removeAll(typesSeenBeforeValidation);
768-
potentionalHk2CustomBoundTypes.removeAll(_getContracts(typesSeenBeforeValidation));
769-
final boolean configLogEnabled = LOGGER.isLoggable(Level.CONFIG);
770-
final Set<Type> effectiveHk2CustomBoundTypes = configLogEnabled ? new HashSet<Type>() : null;
771-
for (Type t : potentionalHk2CustomBoundTypes) {
772-
if (!isNothingWeWantToMessUpWith(t)) { // need to avoid built-in beans conflict
773-
hk2ProvidedTypes.add(t);
774-
if (configLogEnabled) {
775-
effectiveHk2CustomBoundTypes.add(t);
776-
}
777-
}
778-
}
779-
if (configLogEnabled) {
780-
LOGGER.config(listTypes(new StringBuilder().append("\n"), effectiveHk2CustomBoundTypes).toString());
781-
}
782-
} else {
755+
if (customHk2TypesProvider != null) {
783756
hk2ProvidedTypes.addAll(customHk2TypesProvider.getHk2Types());
784757
}
785758

@@ -901,19 +874,12 @@ private boolean isJerseyOrDependencyType(final Class<?> clazz) {
901874

902875
final String pkgName = pkg.getName();
903876

904-
final boolean result = pkgName.contains("org.glassfish.hk2")
877+
return pkgName.contains("org.glassfish.hk2")
905878
|| pkgName.contains("jersey.repackaged")
906879
|| pkgName.contains("org.jvnet.hk2")
907880
|| (pkgName.startsWith("org.glassfish.jersey")
908881
&& !pkgName.startsWith("org.glassfish.jersey.examples")
909882
&& !pkgName.startsWith("org.glassfish.jersey.tests"));
910-
911-
// TODO: refactor this, it needs to get out of this checking method
912-
if (!result) {
913-
potentionalHk2CustomBoundTypes.add(clazz);
914-
}
915-
916-
return result;
917883
}
918884

919885
private static BeanManager beanManagerFromJndi() {

tests/integration/cdi-with-jersey-injection-custom-cfg-webapp/src/test/java/org/glassfish/jersey/tests/cdi/resources/RequestSensitiveTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,6 @@ public void testCdiInjection() {
9898
@Test
9999
public void testHk2Injection() {
100100
String s = target().path(resource).path("path").path(straight).request().get(String.class);
101-
assertThat(s, equalTo(String.format("/%s/path/%s", resource, straight)));
101+
assertThat(s, equalTo(String.format("%s/path/%s", resource, straight)));
102102
}
103103
}

tests/integration/cdi-with-jersey-injection-webapp/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@
5555
<description>Jersey CDI test web application, this one uses Jersey (non JAX-RS) component injection</description>
5656

5757
<dependencies>
58+
<dependency>
59+
<groupId>org.glassfish.jersey.containers.glassfish</groupId>
60+
<artifactId>jersey-gf-cdi</artifactId>
61+
<scope>provided</scope>
62+
</dependency>
5863
<dependency>
5964
<groupId>org.glassfish.jersey.core</groupId>
6065
<artifactId>jersey-server</artifactId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* The contents of this file are subject to the terms of either the GNU
7+
* General Public License Version 2 only ("GPL") or the Common Development
8+
* and Distribution License("CDDL") (collectively, the "License"). You
9+
* may not use this file except in compliance with the License. You can
10+
* obtain a copy of the License at
11+
* http://glassfish.java.net/public/CDDL+GPL_1_1.html
12+
* or packager/legal/LICENSE.txt. See the License for the specific
13+
* language governing permissions and limitations under the License.
14+
*
15+
* When distributing the software, include this License Header Notice in each
16+
* file and include the License file at packager/legal/LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* Oracle designates this particular file as subject to the "Classpath"
20+
* exception as provided by Oracle in the GPL Version 2 section of the License
21+
* file that accompanied this code.
22+
*
23+
* Modifications:
24+
* If applicable, add the following below the License Header, with the fields
25+
* enclosed by brackets [] replaced by your own identifying information:
26+
* "Portions Copyright [year] [name of copyright owner]"
27+
*
28+
* Contributor(s):
29+
* If you wish your version of this file to be governed by only the CDDL or
30+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31+
* elects to include this software in this distribution under the [CDDL or GPL
32+
* Version 2] license." If you don't indicate a single choice of license, a
33+
* recipient has the option to distribute your version of this file under
34+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35+
* its licensees as provided above. However, if you add GPL Version 2 code
36+
* and therefore, elected the GPL Version 2 license, then the option applies
37+
* only if the new code is made subject to such option by the copyright
38+
* holder.
39+
*/
40+
package org.glassfish.jersey.tests.cdi.resources;
41+
42+
import java.lang.reflect.Type;
43+
import java.util.HashSet;
44+
import java.util.Set;
45+
46+
import org.glassfish.jersey.gf.cdi.spi.Hk2CustomBoundTypesProvider;
47+
48+
/**
49+
* Tell Jersey CDI extension what types should be bridged from HK2 to CDI.
50+
*
51+
* @author Jakub Podlesak (jakub.podlesak at oracle.com)
52+
*/
53+
public class MyHk2TypesProvider implements Hk2CustomBoundTypesProvider {
54+
55+
@Override
56+
public Set<Type> getHk2Types() {
57+
return new HashSet<Type>(){{add(MyApplication.MyInjection.class);}};
58+
}
59+
}

tests/integration/cdi-with-jersey-injection-webapp/src/test/java/org/glassfish/jersey/tests/cdi/resources/RequestSensitiveTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,6 @@ public void testCdiInjection() {
9898
@Test
9999
public void testHk2Injection() {
100100
String s = target().path(resource).path("path").path(straight).request().get(String.class);
101-
assertThat(s, equalTo(String.format("/%s/path/%s", resource, straight)));
101+
assertThat(s, equalTo(String.format("%s/path/%s", resource, straight)));
102102
}
103103
}

0 commit comments

Comments
 (0)