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

Commit 9ac3345

Browse files
pavelbucekGerrit Code Review
authored andcommitted
Merge "InjectionManager can test and register its specific implementation's objects." into 2.x
2 parents c4d0f2e + b3f4d87 commit 9ac3345

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-2015 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
@@ -65,10 +65,11 @@ public final class ContractProvider implements Scoped, NameBound {
6565
/**
6666
* Create new contract provider model builder.
6767
*
68+
* @param implementationClass class which the contracts belong to.
6869
* @return new contract provider builder.
6970
*/
70-
public static Builder builder() {
71-
return new Builder();
71+
public static Builder builder(Class<?> implementationClass) {
72+
return new Builder(implementationClass);
7273
}
7374

7475
/**
@@ -86,21 +87,21 @@ public static Builder builder(final ContractProvider original) {
8687
*/
8788
public static final class Builder {
8889

89-
private static final ContractProvider EMPTY_MODEL = new ContractProvider(
90-
Singleton.class,
91-
Collections.<Class<?>, Integer>emptyMap(),
92-
NO_PRIORITY,
93-
Collections.<Class<? extends Annotation>>emptySet());
90+
private static final ContractProvider EMPTY_MODEL =
91+
new ContractProvider(null, Singleton.class, Collections.emptyMap(), NO_PRIORITY, Collections.emptySet());
9492

93+
private Class<?> implementationClass = null;
9594
private Class<? extends Annotation> scope = null;
9695
private Map<Class<?>, Integer> contracts = Maps.newHashMap();
9796
private int defaultPriority = NO_PRIORITY;
9897
private Set<Class<? extends Annotation>> nameBindings = Sets.newIdentityHashSet();
9998

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

103103
private Builder(final ContractProvider original) {
104+
this.implementationClass = original.implementationClass;
104105
this.scope = original.scope;
105106
this.contracts.putAll(original.contracts);
106107
this.defaultPriority = original.defaultPriority;
@@ -245,25 +246,29 @@ public Integer transformEntry(final Class<?> contract, final Integer priority) {
245246
final Set<Class<? extends Annotation>> bindings = (nameBindings.isEmpty())
246247
? Collections.<Class<? extends Annotation>>emptySet() : Collections.unmodifiableSet(nameBindings);
247248

248-
if (scope == Singleton.class && _contracts.isEmpty() && defaultPriority == NO_PRIORITY && bindings.isEmpty()) {
249+
if (implementationClass == null && scope == Singleton.class && _contracts.isEmpty() && defaultPriority == NO_PRIORITY
250+
&& bindings.isEmpty()) {
249251
return EMPTY_MODEL;
250252
}
251253

252-
return new ContractProvider(scope, _contracts, defaultPriority, bindings);
254+
return new ContractProvider(implementationClass, scope, _contracts, defaultPriority, bindings);
253255
}
254256
}
255257

258+
private final Class<?> implementationClass;
256259
private final Map<Class<?>, Integer> contracts;
257260
private final int defaultPriority;
258261
private final Set<Class<? extends Annotation>> nameBindings;
259262
private final Class<? extends Annotation> scope;
260263

261264
private ContractProvider(
265+
final Class<?> implementationClass,
262266
final Class<? extends Annotation> scope,
263267
final Map<Class<?>, Integer> contracts,
264268
final int defaultPriority,
265269
final Set<Class<? extends Annotation>> nameBindings) {
266270

271+
this.implementationClass = implementationClass;
267272
this.scope = scope;
268273
this.contracts = contracts;
269274
this.defaultPriority = defaultPriority;
@@ -275,6 +280,15 @@ public Class<? extends Annotation> getScope() {
275280
return scope;
276281
}
277282

283+
/**
284+
* Get the implementation class which the contracts belong to.
285+
*
286+
* @return implementation class.
287+
*/
288+
public Class<?> getImplementationClass() {
289+
return implementationClass;
290+
}
291+
278292
/**
279293
* Get provided contracts recognized by Jersey.
280294
*

0 commit comments

Comments
 (0)