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

Commit 54fce07

Browse files
committed
Fix for JERSEY-2461: CDI extension injection should be done exclusively by CDI container
Change-Id: Ie64097ba58fe528575c853c5b229f6ba7717460c Signed-off-by: Jakub Podlesak <[email protected]>
1 parent 4726653 commit 54fce07

File tree

6 files changed

+249
-9
lines changed

6 files changed

+249
-9
lines changed

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

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ public void preDestroy(final T instance) {
227227

228228
final AnnotatedType<T> annotatedType = beanManager.createAnnotatedType(clazz);
229229
final InjectionTarget<T> injectionTarget = beanManager.createInjectionTarget(annotatedType);
230-
final CreationalContext creationalContext = beanManager.createCreationalContext(null);
230+
final CreationalContext<T> creationalContext = beanManager.createCreationalContext(null);
231231

232232
@Override
233233
public T getInstance(final Class<T> clazz) {
@@ -526,10 +526,10 @@ private Set<InjectionPoint> filterHk2InjectionPointsOut(final Set<InjectionPoint
526526
return filteredInjectionPoints;
527527
}
528528

529-
private Set<Type> hk2ProvidedTypes = Collections.synchronizedSet(new HashSet<Type>());
530-
private Set<Type> potentionalHk2CustomBoundTypes = Collections.synchronizedSet(new HashSet<Type>());
531-
private Set<Type> typesSeenBeforeValidation = Collections.synchronizedSet(new HashSet<Type>());
532-
private Set<Type> jerseyVetoedTypes = Collections.synchronizedSet(new HashSet<Type>());
529+
private final Set<Type> hk2ProvidedTypes = Collections.synchronizedSet(new HashSet<Type>());
530+
private final Set<Type> potentionalHk2CustomBoundTypes = Collections.synchronizedSet(new HashSet<Type>());
531+
private final Set<Type> typesSeenBeforeValidation = Collections.synchronizedSet(new HashSet<Type>());
532+
private final Set<Type> jerseyVetoedTypes = Collections.synchronizedSet(new HashSet<Type>());
533533

534534
private boolean isInjectionProvider(final Type injectedType) {
535535
return injectedType instanceof ParameterizedType
@@ -586,7 +586,7 @@ private Hk2CustomBoundTypesProvider lookupHk2CustomBoundTypesProvider() throws S
586586
return providers.isEmpty() ? null : providers.get(0).getProvider();
587587
}
588588

589-
private Set<Type> _getContracts(Set<Type> types) {
589+
private Set<Type> _getContracts(final Set<Type> types) {
590590
Set<Type> result = new HashSet<Type>();
591591

592592
for (Type t : types) {
@@ -616,7 +616,7 @@ private void _addDerivedContracts(Type t, Set<Type> result) {
616616
}
617617
}
618618

619-
private boolean isNothingWeWantToMessUpWith(Type t) {
619+
private boolean isNothingWeWantToMessUpWith(final Type t) {
620620
if (!(t instanceof Class)) {
621621
return false;
622622
}
@@ -635,7 +635,23 @@ private boolean isNothingWeWantToMessUpWith(Type t) {
635635

636636
final String pkgName = pkg.getName();
637637

638-
return pkgName.startsWith("java.") || pkgName.startsWith("javax.");
638+
if (pkgName.startsWith("java.") || pkgName.startsWith("javax.")) {
639+
return true;
640+
}
641+
642+
// rule out everyting implementing types from the "javax.enterprise.inject.spi" package
643+
final Set<Type> contracts = _getContracts(new HashSet<Type>(){{add(t);}});
644+
for (Type ct : contracts) {
645+
final Class<?> cc = (Class<?>)ct;
646+
if (!cc.isPrimitive() && !cc.isSynthetic()) {
647+
final Package cp = cc.getPackage();
648+
if (cp != null && cp.getName().startsWith("javax.enterprise.inject.spi")) {
649+
return true;
650+
}
651+
}
652+
}
653+
654+
return false;
639655
}
640656

641657
private <T> void addInjecteeToSkip(final Class<?> componentClass, final Map<Class<?>, Set<T>> toSkip, final T member) {
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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 javax.enterprise.context.RequestScoped;
43+
import javax.inject.Inject;
44+
import javax.ws.rs.GET;
45+
import javax.ws.rs.Path;
46+
47+
/**
48+
* Part of JERSEY-2461 reproducer. This one will get injected with a CDI extension.
49+
* HK2 should not mess up with this.
50+
*
51+
* @author Jakub Podlesak (jakub.podlesak at oracle.com)
52+
*/
53+
@RequestScoped
54+
@Path("counter")
55+
public class CounterResource {
56+
57+
final CustomExtension e;
58+
59+
/**
60+
* To make CDI happy... namely to make the bean proxy-able.
61+
*/
62+
public CounterResource() {
63+
this.e = null;
64+
}
65+
66+
/**
67+
* This one will get used at runtime actually.
68+
*
69+
* @param extension current application CDI custom extension.
70+
*/
71+
@Inject
72+
public CounterResource(CustomExtension extension) {
73+
this.e = extension;
74+
}
75+
76+
/**
77+
* Return custom extension counter state.
78+
*
79+
* @return next count.
80+
*/
81+
@GET
82+
public int getCount() {
83+
return e.getCount();
84+
}
85+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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.util.concurrent.atomic.AtomicInteger;
43+
import javax.enterprise.inject.spi.Extension;
44+
45+
/**
46+
* Part of JERSEY-2461 reproducer. We need an extension that we could inject,
47+
* to make sure HK2 custom binding does not attempt to mess up.
48+
*
49+
* @author Jakub Podlesak (jakub.podlesak at oracle.com)
50+
*/
51+
public class CustomExtension implements Extension {
52+
53+
private AtomicInteger counter = new AtomicInteger();
54+
55+
/**
56+
* A made up functionality. Does not really matter. CDI
57+
* would refuse to deploy the application if something went wrong.
58+
*
59+
* @return next count.
60+
*/
61+
public int getCount() {
62+
return counter.incrementAndGet();
63+
}
64+
}

tests/integration/cdi-test-webapp/src/main/java/org/glassfish/jersey/tests/cdi/resources/MyApplication.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
33
*
4-
* Copyright (c) 2010-2013 Oracle and/or its affiliates. All rights reserved.
4+
* Copyright (c) 2010-2014 Oracle and/or its affiliates. All rights reserved.
55
*
66
* The contents of this file are subject to the terms of either the GNU
77
* General Public License Version 2 only ("GPL") or the Common Development
@@ -73,6 +73,7 @@ public Set<Class<?>> getClasses() {
7373
classes.add(StutteringEchoResource.class);
7474
classes.add(StutteringEcho.class);
7575
classes.add(ReversingEchoResource.class);
76+
classes.add(CounterResource.class);
7677
return classes;
7778
}
7879

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
org.glassfish.jersey.tests.cdi.resources.CustomExtension
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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 javax.ws.rs.client.WebTarget;
43+
import javax.ws.rs.core.Response;
44+
import org.junit.Test;
45+
46+
import static org.junit.Assert.assertThat;
47+
import static org.junit.Assert.assertTrue;
48+
import static org.hamcrest.CoreMatchers.is;
49+
50+
/**
51+
* Part of JERSEY-2641 reproducer. Accessing CDI bean that has custom CDI
52+
* extension injected.
53+
*
54+
* @author Jakub Podlesak (jakub.podlesak at oracle.com)
55+
*/
56+
public class CounterTest extends CdiTest {
57+
58+
@Test
59+
public void testGet() {
60+
61+
final WebTarget target = target().path("counter");
62+
63+
final Response firstResponse = target.request().get();
64+
assertThat(firstResponse.getStatus(), is(200));
65+
int firstNumber = Integer.decode(firstResponse.readEntity(String.class));
66+
67+
final Response secondResponse = target.request().get();
68+
assertThat(secondResponse.getStatus(), is(200));
69+
int secondNumber = Integer.decode(secondResponse.readEntity(String.class));
70+
71+
assertTrue("Second request should have greater number!", secondNumber > firstNumber);
72+
}
73+
}

0 commit comments

Comments
 (0)