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

Commit b729302

Browse files
Petr Boudapavelbucek
authored andcommitted
#3556: Supplier Bridge does not take into account @nAmed qualifier.
Change-Id: Idbefc45ff68508e715b252684d6ae4e5df6bcce7
1 parent 617996b commit b729302

File tree

5 files changed

+142
-7
lines changed

5 files changed

+142
-7
lines changed

inject/hk2/src/main/java/org/glassfish/jersey/inject/hk2/Hk2Helper.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ private static void bindSupplierInstanceBinding(ServiceLocator locator, Supplier
199199
}
200200
});
201201
// Always call SupplierFactoryBridge.
202+
supplierBuilder.setName(binding.getName());
202203
binding.getQualifiers().forEach(supplierBuilder::addQualifierAnnotation);
203204
binder.bind(supplierBuilder);
204205

@@ -234,6 +235,7 @@ private static void bindSupplierClassBinding(ServiceLocator locator, SupplierCla
234235
}
235236
});
236237
binding.getQualifiers().forEach(supplierBuilder::qualifiedBy);
238+
supplierBuilder.named(binding.getName());
237239
supplierBuilder.in(transformScope(binding.getSupplierScope()));
238240
binder.bind(supplierBuilder);
239241

@@ -245,7 +247,8 @@ private static void bindSupplierClassBinding(ServiceLocator locator, SupplierCla
245247
contract = binding.getContracts().iterator().next();
246248
}
247249

248-
ServiceBindingBuilder<?> builder = binder.bindFactory(new SupplierFactoryBridge<>(locator, contract, disposable));
250+
ServiceBindingBuilder<?> builder = binder.bindFactory(
251+
new SupplierFactoryBridge<>(locator, contract, binding.getName(), disposable));
249252
setupSupplierFactoryBridge(binding, builder);
250253
if (binding.getImplementationType() != null) {
251254
builder.asType(binding.getImplementationType());

inject/hk2/src/main/java/org/glassfish/jersey/inject/hk2/SupplierFactoryBridge.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@
6767
public class SupplierFactoryBridge<T> implements Factory<T> {
6868

6969
private ServiceLocator locator;
70-
private ParameterizedType type;
70+
private ParameterizedType beanType;
71+
private String beanName;
7172
private boolean disposable;
7273

7374
// This bridge can create multiple instances using the method 'provide' therefore must map created suppliers because of
@@ -79,21 +80,23 @@ public class SupplierFactoryBridge<T> implements Factory<T> {
7980
* Constructor for a new bridge.
8081
*
8182
* @param locator currently used locator, all factory invocations will be delegated to this locator.
82-
* @param type generic type of a {@link Supplier} which is looked for in locator and on which the creation of
83+
* @param beanType generic type of a {@link Supplier} which is looked for in locator and on which the creation of
8384
* the new instance is delegated.
85+
* @param beanName name of the bean that is provided by supplier.
8486
* @param disposable flag whether the bridge is set up for disposing the created object.
8587
*/
86-
SupplierFactoryBridge(ServiceLocator locator, Type type, boolean disposable) {
88+
SupplierFactoryBridge(ServiceLocator locator, Type beanType, String beanName, boolean disposable) {
8789
this.locator = locator;
88-
this.type = new ParameterizedTypeImpl(Supplier.class, type);
90+
this.beanType = new ParameterizedTypeImpl(Supplier.class, beanType);
91+
this.beanName = beanName;
8992
this.disposable = disposable;
9093
}
9194

9295
@Override
9396
@SuppressWarnings("unchecked")
9497
public T provide() {
95-
if (type != null) {
96-
Supplier<T> supplier = locator.getService(type);
98+
if (beanType != null) {
99+
Supplier<T> supplier = locator.getService(beanType, beanName);
97100
T instance = supplier.get();
98101
if (disposable) {
99102
disposableSuppliers.put(instance, (DisposableSupplier<T>) supplier);

inject/hk2/src/test/java/org/glassfish/jersey/inject/hk2/SupplierClassBindingTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,4 +172,28 @@ public void testSupplierSingletonInstanceSingleton() {
172172
assertSame(supplier1, supplier2);
173173
assertSame(supplier2, supplier3);
174174
}
175+
176+
@Test
177+
public void testSupplierBeanNamed() {
178+
BindingTestHelper.bind(injectionManager, binder -> {
179+
binder.bindFactory(TestSuppliers.TestSupplier.class).named(TestSuppliers.TEST).to(String.class);
180+
binder.bindFactory(TestSuppliers.OtherTestSupplier.class).named(TestSuppliers.OTHER_TEST).to(String.class);
181+
binder.bindAsContract(TestSuppliers.TargetSupplierBean.class);
182+
});
183+
184+
TestSuppliers.TargetSupplierBean instance = injectionManager.getInstance(TestSuppliers.TargetSupplierBean.class);
185+
assertEquals(TestSuppliers.OTHER_TEST, instance.obj);
186+
}
187+
188+
@Test
189+
public void testSupplierNamed() {
190+
BindingTestHelper.bind(injectionManager, binder -> {
191+
binder.bindFactory(TestSuppliers.TestSupplier.class).named(TestSuppliers.TEST).to(String.class);
192+
binder.bindFactory(TestSuppliers.OtherTestSupplier.class).named(TestSuppliers.OTHER_TEST).to(String.class);
193+
binder.bindAsContract(TestSuppliers.TargetSupplier.class);
194+
});
195+
196+
TestSuppliers.TargetSupplier instance = injectionManager.getInstance(TestSuppliers.TargetSupplier.class);
197+
assertEquals(TestSuppliers.OTHER_TEST, instance.supplier.get());
198+
}
175199
}

inject/hk2/src/test/java/org/glassfish/jersey/inject/hk2/SupplierInstanceBindingTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,4 +152,28 @@ public void testSupplierSingletonInstanceSingleton() {
152152
assertSame(supplier1, supplier2);
153153
assertSame(supplier2, supplier3);
154154
}
155+
156+
@Test
157+
public void testSupplierBeanNamed() {
158+
BindingTestHelper.bind(injectionManager, binder -> {
159+
binder.bindFactory(new TestSuppliers.TestSupplier()).named(TestSuppliers.TEST).to(String.class);
160+
binder.bindFactory(new TestSuppliers.OtherTestSupplier()).named(TestSuppliers.OTHER_TEST).to(String.class);
161+
binder.bindAsContract(TestSuppliers.TargetSupplierBean.class);
162+
});
163+
164+
TestSuppliers.TargetSupplierBean instance = injectionManager.getInstance(TestSuppliers.TargetSupplierBean.class);
165+
assertEquals(TestSuppliers.OTHER_TEST, instance.obj);
166+
}
167+
168+
@Test
169+
public void testSupplierNamed() {
170+
BindingTestHelper.bind(injectionManager, binder -> {
171+
binder.bindFactory(new TestSuppliers.TestSupplier()).named(TestSuppliers.TEST).to(String.class);
172+
binder.bindFactory(new TestSuppliers.OtherTestSupplier()).named(TestSuppliers.OTHER_TEST).to(String.class);
173+
binder.bindAsContract(TestSuppliers.TargetSupplier.class);
174+
});
175+
176+
TestSuppliers.TargetSupplier instance = injectionManager.getInstance(TestSuppliers.TargetSupplier.class);
177+
assertEquals(TestSuppliers.OTHER_TEST, instance.supplier.get());
178+
}
155179
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2017 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+
41+
package org.glassfish.jersey.inject.hk2;
42+
43+
import java.util.function.Supplier;
44+
45+
import javax.inject.Inject;
46+
import javax.inject.Named;
47+
48+
/**
49+
* Set of suppliers to inject.
50+
*/
51+
public class TestSuppliers {
52+
53+
static final String TEST = "Test";
54+
static final String OTHER_TEST = "OtherTest";
55+
56+
public static class TargetSupplierBean {
57+
@Inject
58+
@Named(OTHER_TEST)
59+
public String obj;
60+
}
61+
62+
public static class TargetSupplier {
63+
@Inject
64+
@Named(OTHER_TEST)
65+
public Supplier<String> supplier;
66+
}
67+
68+
public static class TestSupplier implements Supplier<String> {
69+
@Override
70+
public String get() {
71+
return TEST;
72+
}
73+
}
74+
75+
public static class OtherTestSupplier implements Supplier<String> {
76+
@Override
77+
public String get() {
78+
return OTHER_TEST;
79+
}
80+
}
81+
}

0 commit comments

Comments
 (0)