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

Commit 4cc704f

Browse files
committed
Temporarily adding back direct support for hk2 binders.
Change-Id: I72f1660e0624b584e48b6cd40671ecb23b8a1790
1 parent bc97030 commit 4cc704f

File tree

6 files changed

+214
-1
lines changed

6 files changed

+214
-1
lines changed

core-common/src/main/java/org/glassfish/jersey/hk2/HK2InjectionManager.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,11 @@ public void register(Binder binder) {
191191
Hk2Helper.bind(locator, binder);
192192
}
193193

194+
@Override
195+
public void register(org.glassfish.hk2.utilities.Binder... binder) {
196+
ServiceLocatorUtilities.bind(locator, binder);
197+
}
198+
194199
@Override
195200
public <U> U createAndInitialize(Class<U> clazz) {
196201
return locator.createAndInitialize(clazz);
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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+
package org.glassfish.jersey.hk2;
41+
42+
import javax.inject.Provider;
43+
44+
import org.glassfish.jersey.internal.util.collection.Ref;
45+
import org.glassfish.jersey.internal.util.collection.Refs;
46+
47+
import org.glassfish.hk2.api.Factory;
48+
49+
/**
50+
* Factory that provides injection of the referenced instance.
51+
*
52+
* @param <T>
53+
* @author Marek Potociar (marek.potociar at oracle.com)
54+
*/
55+
public abstract class Hk2ReferencingFactory<T> implements Factory<T> {
56+
57+
private static class EmptyReferenceFactory<T> implements Factory<Ref<T>> {
58+
59+
@Override
60+
public Ref<T> provide() {
61+
return Refs.emptyRef();
62+
}
63+
64+
@Override
65+
public void dispose(Ref<T> instance) {
66+
//not used
67+
}
68+
}
69+
70+
private static class InitializedReferenceFactory<T> implements Factory<Ref<T>> {
71+
72+
private final T initialValue;
73+
74+
public InitializedReferenceFactory(T initialValue) {
75+
this.initialValue = initialValue;
76+
}
77+
78+
@Override
79+
public Ref<T> provide() {
80+
return Refs.of(initialValue);
81+
}
82+
83+
@Override
84+
public void dispose(Ref<T> instance) {
85+
//not used
86+
}
87+
}
88+
89+
private final Provider<Ref<T>> referenceFactory;
90+
91+
/**
92+
* Create new referencing injection factory.
93+
*
94+
* @param referenceFactory reference provider backing the factory.
95+
*/
96+
public Hk2ReferencingFactory(Provider<Ref<T>> referenceFactory) {
97+
this.referenceFactory = referenceFactory;
98+
}
99+
100+
@Override
101+
public T provide() {
102+
return referenceFactory.get().get();
103+
}
104+
105+
@Override
106+
public void dispose(T instance) {
107+
//not used
108+
}
109+
110+
/**
111+
* Get a reference factory providing an empty reference.
112+
*
113+
* @param <T> reference type.
114+
* @return reference factory providing an empty reference.
115+
*/
116+
public static <T> Factory<Ref<T>> referenceFactory() {
117+
return new EmptyReferenceFactory<T>();
118+
}
119+
120+
/**
121+
* Get a reference factory providing an initialized reference.
122+
*
123+
* @param <T> reference type.
124+
* @param initialValue initial value stored in the reference provided
125+
* by the returned factory.
126+
* @return reference factory providing a reference initialized with an
127+
* {@code initialValue}.
128+
*/
129+
public static <T> Factory<Ref<T>> referenceFactory(T initialValue) {
130+
if (initialValue == null) {
131+
return new EmptyReferenceFactory<T>();
132+
}
133+
134+
return new InitializedReferenceFactory<T>(initialValue);
135+
}
136+
}

core-common/src/main/java/org/glassfish/jersey/internal/inject/InjectionManager.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,19 @@ public interface InjectionManager {
112112
*/
113113
void register(Binder binder);
114114

115+
/**
116+
* Register a HK2 Binder.
117+
*
118+
* For compatibility reasons only, to be removed.
119+
*
120+
* @param binder collection of descriptors.
121+
* @see ClassBinding
122+
* @see InstanceBinding
123+
* @see SupplierClassBinding
124+
* @see SupplierInstanceBinding
125+
*/
126+
void register(org.glassfish.hk2.utilities.Binder... binder);
127+
115128
/**
116129
* This method creates, injects and post-constructs an object with the given class. This is equivalent to calling the
117130
* {@code create-class} method followed by the {@code inject-class} method followed by the {@code post-construct} method.

core-common/src/main/java/org/glassfish/jersey/internal/inject/Providers.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ private static Map<Class<?>, ProviderRuntime> getExternalProviderInterfaces() {
123123
interfaces.putAll(JAX_RS_PROVIDER_INTERFACE_WHITELIST);
124124
interfaces.put(javax.ws.rs.core.Feature.class, ProviderRuntime.BOTH);
125125
interfaces.put(Binder.class, ProviderRuntime.BOTH);
126+
interfaces.put(org.glassfish.hk2.utilities.Binder.class, ProviderRuntime.BOTH);
126127
return interfaces;
127128
}
128129

core-common/src/main/java/org/glassfish/jersey/model/internal/CommonConfig.java

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ public class CommonConfig implements FeatureContext, ExtendedConfig {
9393

9494
private static final Logger LOGGER = Logger.getLogger(CommonConfig.class.getName());
9595
private static final Function<Object, Binder> CAST_TO_BINDER = Binder.class::cast;
96+
private static final Function<Object, org.glassfish.hk2.utilities.Binder> CAST_TO_HK2_BINDER =
97+
org.glassfish.hk2.utilities.Binder.class::cast;
9698

9799
/**
98100
* Configuration runtime type.
@@ -625,7 +627,7 @@ public void configureAutoDiscoverableProviders(final InjectionManager injectionM
625627
* @param injectionManager locator in which the binders and features should be configured.
626628
*/
627629
public void configureMetaProviders(InjectionManager injectionManager) {
628-
// First, configure existing binders
630+
// configure existing binders
629631
Set<Binder> configuredBinders = configureBinders(injectionManager, Collections.emptySet());
630632

631633
// Check whether meta providers have been initialized for a config this config has been loaded from.
@@ -636,6 +638,19 @@ public void configureMetaProviders(InjectionManager injectionManager) {
636638
// At last, configure any new binders added by features
637639
configureBinders(injectionManager, configuredBinders);
638640
}
641+
642+
// Configure hk2 binders
643+
Set<org.glassfish.hk2.utilities.Binder> configuredHk2Binders =
644+
configureHk2Binders(injectionManager, Collections.emptySet());
645+
646+
// Check whether meta providers have been initialized for a config this config has been loaded from.
647+
if (!disableMetaProviderConfiguration) {
648+
injectionManager.register(new ManagedObjectsFinalizerHk2Binder());
649+
// Next, configure all features
650+
configureFeatures(injectionManager, new HashSet<>(), resetRegistrations());
651+
// At last, configure any new binders added by features
652+
configureHk2Binders(injectionManager, configuredHk2Binders);
653+
}
639654
}
640655

641656
public static class ManagedObjectsFinalizerBinder extends AbstractBinder {
@@ -646,6 +661,14 @@ protected void configure() {
646661
}
647662
}
648663

664+
public static class ManagedObjectsFinalizerHk2Binder extends org.glassfish.hk2.utilities.binding.AbstractBinder {
665+
@Override
666+
protected void configure() {
667+
bindAsContract(ManagedObjectsFinalizer.class)
668+
.in(Singleton.class);
669+
}
670+
}
671+
649672
private Set<Binder> configureBinders(InjectionManager injectionManager, Set<Binder> configured) {
650673
Set<Binder> allConfigured = Collections.newSetFromMap(new IdentityHashMap<>());
651674
allConfigured.addAll(configured);
@@ -659,6 +682,22 @@ private Set<Binder> configureBinders(InjectionManager injectionManager, Set<Bind
659682
return allConfigured;
660683
}
661684

685+
private Set<org.glassfish.hk2.utilities.Binder> configureHk2Binders(
686+
InjectionManager injectionManager,
687+
Set<org.glassfish.hk2.utilities.Binder> configured) {
688+
689+
Set<org.glassfish.hk2.utilities.Binder> allConfigured = Collections.newSetFromMap(new IdentityHashMap<>());
690+
allConfigured.addAll(configured);
691+
692+
Collection<org.glassfish.hk2.utilities.Binder> binders = getHk2BeanDescriptors(configured);
693+
if (!binders.isEmpty()) {
694+
injectionManager.register(binders.toArray(new org.glassfish.hk2.utilities.Binder[]{}));
695+
allConfigured.addAll(binders);
696+
}
697+
698+
return allConfigured;
699+
}
700+
662701
private Collection<Binder> getBeanDescriptors(Set<Binder> configured) {
663702
return componentBag.getInstances(ComponentBag.BINDERS_ONLY)
664703
.stream()
@@ -667,6 +706,15 @@ private Collection<Binder> getBeanDescriptors(Set<Binder> configured) {
667706
.collect(Collectors.toList());
668707
}
669708

709+
private Collection<org.glassfish.hk2.utilities.Binder> getHk2BeanDescriptors(
710+
Set<org.glassfish.hk2.utilities.Binder> configured) {
711+
return componentBag.getInstances(ComponentBag.HK2_BINDERS_ONLY)
712+
.stream()
713+
.map(CAST_TO_HK2_BINDER)
714+
.filter(descriptor -> !configured.contains(descriptor))
715+
.collect(Collectors.toList());
716+
}
717+
670718
private void configureFeatures(InjectionManager injectionManager,
671719
Set<FeatureRegistration> processed,
672720
List<FeatureRegistration> unprocessed) {

core-common/src/main/java/org/glassfish/jersey/model/internal/ComponentBag.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,16 @@ public class ComponentBag {
117117
*/
118118
public static final Predicate<ContractProvider> BINDERS_ONLY = model -> model.getContracts().contains(Binder.class);
119119

120+
/**
121+
* A filtering strategy that includes only models that contain HK2 Binder provider contract.
122+
* <p>
123+
* This filter predicate returns {@code true} for all {@link org.glassfish.jersey.model.ContractProvider contract provider models}
124+
* that represent a provider registered to provide {@link Binder} contract.
125+
* </p>
126+
*/
127+
public static final Predicate<ContractProvider> HK2_BINDERS_ONLY =
128+
model -> model.getContracts().contains(org.glassfish.hk2.utilities.Binder.class);
129+
120130
/**
121131
* A filtering strategy that excludes models with no recognized contracts.
122132
* <p>

0 commit comments

Comments
 (0)