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

Commit 364e980

Browse files
author
Petr Bouda
committed
InjectionManager can test and register its specific implementation's objects.
Change-Id: I090e0edff2af611b5c343324e42fae11b834492f
1 parent d22d0c9 commit 364e980

File tree

16 files changed

+423
-109
lines changed

16 files changed

+423
-109
lines changed

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
import java.util.logging.Logger;
4848
import java.util.stream.Collectors;
4949

50-
import org.glassfish.jersey.internal.LocalizationMessages;
5150
import org.glassfish.jersey.internal.inject.Binder;
5251
import org.glassfish.jersey.internal.inject.Binding;
5352
import org.glassfish.jersey.internal.inject.ClassBinding;
@@ -146,7 +145,7 @@ public void initialize(String name, Object parent, Binder... binders) {
146145
ServiceLocatorRuntimeBean serviceLocatorRuntimeBean = locator.getService(ServiceLocatorRuntimeBean.class);
147146
if (serviceLocatorRuntimeBean != null) {
148147
if (LOGGER.isLoggable(Level.FINE)) {
149-
LOGGER.fine(LocalizationMessages.CLEARING_HK_2_CACHE(
148+
LOGGER.fine(LocalizationMessages.HK_2_CLEARING_CACHE(
150149
serviceLocatorRuntimeBean.getServiceCacheSize(),
151150
serviceLocatorRuntimeBean.getReflectionCacheSize()));
152151
}
@@ -162,7 +161,7 @@ public void initialize(String name, Object parent, Binder... binders) {
162161
*/
163162
private static void assertParentLocatorType(Object parent) {
164163
if (parent != null && !(parent instanceof ServiceLocator || parent instanceof HK2InjectionManager)) {
165-
throw new RuntimeException(LocalizationMessages.HK_2_UNKNOWN_PARENT_INSTANCE_MANAGER(
164+
throw new IllegalArgumentException(LocalizationMessages.HK_2_UNKNOWN_PARENT_INJECTION_MANAGER(
166165
parent.getClass().getSimpleName()));
167166
}
168167
}
@@ -192,8 +191,17 @@ public void register(Binder binder) {
192191
}
193192

194193
@Override
195-
public void register(org.glassfish.hk2.utilities.Binder... binder) {
196-
ServiceLocatorUtilities.bind(locator, binder);
194+
public void register(Object provider) {
195+
if (isRegistrable(provider.getClass())) {
196+
ServiceLocatorUtilities.bind(locator, (org.glassfish.hk2.utilities.Binder) provider);
197+
} else {
198+
throw new IllegalArgumentException();
199+
}
200+
}
201+
202+
@Override
203+
public boolean isRegistrable(Class<?> clazz) {
204+
return org.glassfish.hk2.utilities.Binder.class.isAssignableFrom(clazz);
197205
}
198206

199207
@Override
@@ -244,7 +252,8 @@ private ForeignDescriptor createAndTranslateForeignDescriptor(Binding binding) {
244252
} else if (InstanceBinding.class.isAssignableFrom(binding.getClass())) {
245253
activeDescriptor = Hk2Helper.translateToActiveDescriptor((InstanceBinding<?>) binding);
246254
} else {
247-
throw new RuntimeException(LocalizationMessages.UNKNOWN_DESCRIPTOR_TYPE(binding.getClass().getSimpleName()));
255+
throw new RuntimeException(org.glassfish.jersey.internal.LocalizationMessages.UNKNOWN_DESCRIPTOR_TYPE(
256+
binding.getClass().getSimpleName()));
248257
}
249258

250259
return ForeignDescriptor.wrap(activeDescriptor, activeDescriptor::dispose);

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
import javax.inject.Singleton;
4747

4848
import org.glassfish.jersey.internal.Errors;
49-
import org.glassfish.jersey.internal.LocalizationMessages;
5049
import org.glassfish.jersey.internal.inject.AbstractBinder;
5150

5251
import org.glassfish.hk2.api.ErrorInformation;

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

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@
5555
public interface InjectionManager {
5656

5757
/**
58-
* This will initialize the {@code InjectionManager} and underlying DI provider. The method may get the array of binders to
58+
* Initializes {@code InjectionManager} and underlying DI provider. The method may get the array of binders to
5959
* register {@link Binding} them during initialization process. {@code name} and {@code parent} are not required parameters
60-
* and can be null without the initialization exception.
60+
* and can be {@code null} without the initialization exception.
6161
*
6262
* @param name Name of the injection manager.
6363
* @param parent Parent object of the underlying DI provider on which new injection manager should be dependent. A specific
@@ -68,12 +68,36 @@ public interface InjectionManager {
6868
void initialize(String name, Object parent, Binder... binders);
6969

7070
/**
71-
* This will shutdown the entire injection manager and underlying DI provider along with injected executors and schedulers.
71+
* Initializes {@code InjectionManager} and underlying DI provider. The method may get the array of binders to
72+
* register {@link Binding} them during initialization process. {@code parent} is not required parameters and can be
73+
* {@code null} without the initialization exception.
74+
*
75+
* @param parent Parent object of the underlying DI provider on which new injection manager should be dependent. A specific
76+
* DI provider checks whether the parent object is in the proper type of underlying service storage or
77+
* a proper implementation of {@link InjectionManager}.
78+
* @param binders Binders with descriptions to include them during initialization process.
79+
*/
80+
default void initialize(Object parent, Binder... binders) {
81+
initialize(null, parent, binders);
82+
}
83+
84+
/**
85+
* Initializes {@code InjectionManager} and underlying DI provider. The method may get the array of binders to
86+
* register {@link Binding} them during initialization process.
87+
88+
* @param binders Binders with descriptions to include them during initialization process.
89+
*/
90+
default void initialize(Binder... binders) {
91+
initialize(null, null, binders);
92+
}
93+
94+
/**
95+
* Shuts down the entire {@link InjectionManager}and underlying DI provider along with injected executors and schedulers.
7296
*/
7397
void shutdown();
7498

7599
/**
76-
* This will register one bean represented using fields in the provided descriptor. The final bean can be direct bean or
100+
* Registers one bean represented using fields in the provided descriptor. The final bean can be direct bean or
77101
* factory object which will create the bean at the time of injection. {@code InjectionManager} is able to register a bean
78102
* represented by a class or direct instance.
79103
*
@@ -86,7 +110,7 @@ public interface InjectionManager {
86110
void register(Binding binding);
87111

88112
/**
89-
* This will register a collection of beans represented using fields in the provided descriptors. The final bean can be
113+
* Registers a collection of beans represented using fields in the provided descriptors. The final bean can be
90114
* direct bean or factory object which will create the bean at the time of injection. {@code InjectionManager} is able to
91115
* register a bean represented by a class or direct instance.
92116
*
@@ -99,7 +123,7 @@ public interface InjectionManager {
99123
void register(Iterable<Binding> descriptors);
100124

101125
/**
102-
* This will register beans which are included in {@link Binder}. {@code Binder} can contains all descriptors extending
126+
* Registers beans which are included in {@link Binder}. {@code Binder} can contains all descriptors extending
103127
* {@link Binding} or other binders which are installed together in tree-structure. This method will get all descriptors
104128
* bound in the given binder and register them in the order how the binders are installed together. In the tree structure,
105129
* the deeper on the left side will be processed first.
@@ -113,20 +137,26 @@ public interface InjectionManager {
113137
void register(Binder binder);
114138

115139
/**
116-
* Register a HK2 Binder.
140+
* Registers a provider. An implementation of the {@link InjectionManager} should test whether the type of the object can be
141+
* registered using the method {@link #isRegistrable(Class)}. Then a caller has an certainty that the instance of the tested
142+
* class can be registered in {@code InjectionManager}. If {@code InjectionManager} is not able to register the provider
143+
* then {@link IllegalArgumentException} is thrown.
117144
*
118-
* For compatibility reasons only, to be removed.
145+
* @param provider object that can be registered in {@code InjectionManager}.
146+
* @throws IllegalArgumentException provider cannot be registered.
147+
*/
148+
void register(Object provider);
149+
150+
/**
151+
* Tests whether the provided {@code clazz} can be registered by the implementation of the {@link InjectionManager}.
119152
*
120-
* @param binder collection of descriptors.
121-
* @see ClassBinding
122-
* @see InstanceBinding
123-
* @see SupplierClassBinding
124-
* @see SupplierInstanceBinding
153+
* @param clazz type that is tested whether is registrable by the implementation of {@code InjectionManager}.
154+
* @return {@code true} if the {@code InjectionManager} is able to register this type.
125155
*/
126-
void register(org.glassfish.hk2.utilities.Binder... binder);
156+
boolean isRegistrable(Class<?> clazz);
127157

128158
/**
129-
* This method creates, injects and post-constructs an object with the given class. This is equivalent to calling the
159+
* Creates, injects and post-constructs an object with the given class. This is equivalent to calling the
130160
* {@code create-class} method followed by the {@code inject-class} method followed by the {@code post-construct} method.
131161
* <p>
132162
* The object created is not managed by the injection manager.
@@ -234,7 +264,7 @@ public interface InjectionManager {
234264
<T> List<T> getAllInstances(Type contractOrImpl);
235265

236266
/**
237-
* This will analyze the given object and inject into its fields and methods.
267+
* Analyzes the given object and inject into its fields and methods.
238268
* The object injected in this way will not be managed by HK2
239269
*
240270
* @param injectMe The object to be analyzed and injected into
@@ -251,7 +281,7 @@ public interface InjectionManager {
251281
void inject(Object injectMe, String classAnalyzer);
252282

253283
/**
254-
* This will analyze the given object and call the preDestroy method. The object given will not be managed by bean manager.
284+
* Analyzes the given object and call the preDestroy method. The object given will not be managed by bean manager.
255285
*
256286
* @param preDestroyMe The object to preDestroy
257287
*/

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,7 @@ public static void bindProviders(ComponentBag componentBag,
184184
RuntimeType constrainedTo,
185185
Set<Class<?>> registeredClasses,
186186
InjectionManager injectionManager) {
187-
Predicate<ContractProvider> filter =
188-
input -> ComponentBag.EXCLUDE_EMPTY.test(input) && ComponentBag.EXCLUDE_META_PROVIDERS.test(input);
187+
Predicate<ContractProvider> filter = ComponentBag.EXCLUDE_EMPTY.and(ComponentBag.excludeMetaProviders(injectionManager));
189188

190189
/*
191190
* Check the {@code component} whether it is correctly configured for client or server {@link RuntimeType runtime}.

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@ 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);
127126
return interfaces;
128127
}
129128

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

Lines changed: 25 additions & 11 deletions
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) 2012-2016 Oracle and/or its affiliates. All rights reserved.
4+
* Copyright (c) 2012-2017 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
@@ -66,10 +66,11 @@ public final class ContractProvider implements Scoped, NameBound {
6666
/**
6767
* Create new contract provider model builder.
6868
*
69+
* @param implementationClass class which the contracts belong to.
6970
* @return new contract provider builder.
7071
*/
71-
public static Builder builder() {
72-
return new Builder();
72+
public static Builder builder(Class<?> implementationClass) {
73+
return new Builder(implementationClass);
7374
}
7475

7576
/**
@@ -87,21 +88,21 @@ public static Builder builder(final ContractProvider original) {
8788
*/
8889
public static final class Builder {
8990

90-
private static final ContractProvider EMPTY_MODEL = new ContractProvider(
91-
Singleton.class,
92-
Collections.emptyMap(),
93-
NO_PRIORITY,
94-
Collections.emptySet());
91+
private static final ContractProvider EMPTY_MODEL =
92+
new ContractProvider(null, Singleton.class, Collections.emptyMap(), NO_PRIORITY, Collections.emptySet());
9593

94+
private Class<?> implementationClass = null;
9695
private Class<? extends Annotation> scope = null;
9796
private Map<Class<?>, Integer> contracts = new HashMap<>();
9897
private int defaultPriority = NO_PRIORITY;
9998
private Set<Class<? extends Annotation>> nameBindings = Collections.newSetFromMap(new IdentityHashMap<>());
10099

101-
private Builder() {
100+
private Builder(Class<?> implementationClass) {
101+
this.implementationClass = implementationClass;
102102
}
103103

104104
private Builder(final ContractProvider original) {
105+
this.implementationClass = original.implementationClass;
105106
this.scope = original.scope;
106107
this.contracts.putAll(original.contracts);
107108
this.defaultPriority = original.defaultPriority;
@@ -248,25 +249,29 @@ public ContractProvider build() {
248249
final Set<Class<? extends Annotation>> bindings = (nameBindings.isEmpty())
249250
? Collections.emptySet() : Collections.unmodifiableSet(nameBindings);
250251

251-
if (scope == Singleton.class && _contracts.isEmpty() && defaultPriority == NO_PRIORITY && bindings.isEmpty()) {
252+
if (implementationClass == null && scope == Singleton.class && _contracts.isEmpty() && defaultPriority == NO_PRIORITY
253+
&& bindings.isEmpty()) {
252254
return EMPTY_MODEL;
253255
}
254256

255-
return new ContractProvider(scope, _contracts, defaultPriority, bindings);
257+
return new ContractProvider(implementationClass, scope, _contracts, defaultPriority, bindings);
256258
}
257259
}
258260

261+
private final Class<?> implementationClass;
259262
private final Map<Class<?>, Integer> contracts;
260263
private final int defaultPriority;
261264
private final Set<Class<? extends Annotation>> nameBindings;
262265
private final Class<? extends Annotation> scope;
263266

264267
private ContractProvider(
268+
final Class<?> implementationClass,
265269
final Class<? extends Annotation> scope,
266270
final Map<Class<?>, Integer> contracts,
267271
final int defaultPriority,
268272
final Set<Class<? extends Annotation>> nameBindings) {
269273

274+
this.implementationClass = implementationClass;
270275
this.scope = scope;
271276
this.contracts = contracts;
272277
this.defaultPriority = defaultPriority;
@@ -278,6 +283,15 @@ public Class<? extends Annotation> getScope() {
278283
return scope;
279284
}
280285

286+
/**
287+
* Get the implementation class which the contracts belong to.
288+
*
289+
* @return implementation class.
290+
*/
291+
public Class<?> getImplementationClass() {
292+
return implementationClass;
293+
}
294+
281295
/**
282296
* Get provided contracts recognized by Jersey.
283297
*

0 commit comments

Comments
 (0)