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

Commit 7f878fe

Browse files
Petr BoudaGerrit Code Review
authored andcommitted
Merge "Remove HK2 classes: MultiException, JerseyErrorService, Singleton Immediate annotation, Unqualified annotation." into 2.x
2 parents a13528b + 8c7a598 commit 7f878fe

File tree

16 files changed

+155
-174
lines changed

16 files changed

+155
-174
lines changed

core-client/src/main/java/org/glassfish/jersey/client/ClientBinder.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454

5555
import org.glassfish.jersey.internal.ContextResolverFactory;
5656
import org.glassfish.jersey.internal.JaxrsProviders;
57-
import org.glassfish.jersey.internal.JerseyErrorService;
5857
import org.glassfish.jersey.internal.PropertiesDelegate;
5958
import org.glassfish.jersey.internal.ServiceFinderBinder;
6059
import org.glassfish.jersey.internal.inject.AbstractBinder;
@@ -113,7 +112,6 @@ public PropertiesDelegate get() {
113112
@Override
114113
protected void configure() {
115114
install(new RequestScope.Binder(), // must go first as it registers the request scope instance.
116-
new JerseyErrorService.Binder(),
117115
new MessagingBinders.MessageBodyProviders(clientRuntimeProperties, RuntimeType.CLIENT),
118116
new MessagingBinders.HeaderDelegateProviders(),
119117
new MessageBodyFactory.Binder(),

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

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
import org.glassfish.jersey.internal.LocalizationMessages;
5151
import org.glassfish.jersey.internal.inject.Binder;
5252
import org.glassfish.jersey.internal.inject.Binding;
53-
import org.glassfish.jersey.internal.inject.Bindings;
5453
import org.glassfish.jersey.internal.inject.ClassBinding;
5554
import org.glassfish.jersey.internal.inject.CompositeBinder;
5655
import org.glassfish.jersey.internal.inject.ForeignDescriptor;
@@ -143,16 +142,9 @@ public ServiceLocator getServiceLocator() {
143142
@Override
144143
public void initialize(String name, InjectionManager parent, Binder... binders) {
145144
this.locator = createLocator(name, parent);
146-
ServiceLocatorUtilities.bind(locator, new JerseyClassAnalyzer.Binder(locator));
147145

148-
// First service the current BeanManager to be able to inject itself into other services.
149-
Hk2Helper.bind(locator, Bindings.service(this).to(InjectionManager.class));
150-
151-
// Add support for Context annotation.
152-
Hk2Helper.bind(locator, new ContextInjectionResolverImpl.Binder());
153-
154-
// Compose together the initialization binders and bind them as a whole.
155-
Hk2Helper.bind(locator, CompositeBinder.wrap(binders));
146+
// Register all components needed for proper HK2 locator bootstrap
147+
Hk2Helper.bind(locator, new Hk2BootstrapBinder(this, CompositeBinder.wrap(binders)));
156148

157149
this.locator.setDefaultClassAnalyzerName(JerseyClassAnalyzer.NAME);
158150

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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.hk2;
42+
43+
import org.glassfish.jersey.internal.inject.AbstractBinder;
44+
import org.glassfish.jersey.internal.inject.InjectionManager;
45+
46+
/**
47+
* {@link AbstractBinder} that registers all components needed for a proper bootstrap of Jersey based on HK2 framework.
48+
*
49+
* @author Petr Bouda (petr.bouda at oracle.com)
50+
*/
51+
public class Hk2BootstrapBinder extends AbstractBinder {
52+
53+
private final AbstractBinder externalBinder;
54+
55+
private final InjectionManager injectionManager;
56+
57+
/**
58+
* Create a bootstrap which is specific for HK2 module and automatically install {@code externalBinder}.
59+
*
60+
* @param injectionManager HK2 service locator.
61+
* @param externalBinder externally provided binder (particularly Jersey specific services).
62+
*/
63+
public Hk2BootstrapBinder(InjectionManager injectionManager, AbstractBinder externalBinder) {
64+
this.injectionManager = injectionManager;
65+
this.externalBinder = externalBinder;
66+
}
67+
68+
@Override
69+
protected void configure() {
70+
install(
71+
// First service the current injection manager to be able to inject itself into other services.
72+
new HK2InjectionManagerBinder(injectionManager),
73+
// Jersey-like class analyzer that is able to choose the right services' construtor.
74+
new JerseyClassAnalyzer.Binder(injectionManager),
75+
// Add support for Context annotation.
76+
new ContextInjectionResolverImpl.Binder(),
77+
// Compose together the initialization binders and bind them as a whole.
78+
externalBinder,
79+
// Improved HK2 Error reporting.
80+
new JerseyErrorService.Binder());
81+
}
82+
83+
/**
84+
* Binder that registers a provided injection manager.
85+
*/
86+
private static class HK2InjectionManagerBinder extends AbstractBinder {
87+
88+
private final InjectionManager injectionManager;
89+
90+
/**
91+
* Constructor for a creation injection manager binder.
92+
*
93+
* @param injectionManager current injection manager.
94+
*/
95+
private HK2InjectionManagerBinder(InjectionManager injectionManager) {
96+
this.injectionManager = injectionManager;
97+
}
98+
99+
@Override
100+
protected void configure() {
101+
bind(injectionManager).to(InjectionManager.class);
102+
}
103+
}
104+
}

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@
5757

5858
import org.glassfish.jersey.internal.Errors;
5959
import org.glassfish.jersey.internal.LocalizationMessages;
60+
import org.glassfish.jersey.internal.inject.AbstractBinder;
61+
import org.glassfish.jersey.internal.inject.InjectionManager;
6062
import org.glassfish.jersey.internal.inject.InjectionResolver;
6163
import org.glassfish.jersey.internal.util.collection.ImmutableCollectors;
6264
import org.glassfish.jersey.internal.util.collection.LazyValue;
@@ -65,8 +67,6 @@
6567

6668
import org.glassfish.hk2.api.ClassAnalyzer;
6769
import org.glassfish.hk2.api.MultiException;
68-
import org.glassfish.hk2.api.ServiceLocator;
69-
import org.glassfish.hk2.utilities.binding.AbstractBinder;
7070

7171
/**
7272
* Implementation of the {@link ClassAnalyzer} that supports selection
@@ -90,18 +90,23 @@ public final class JerseyClassAnalyzer implements ClassAnalyzer {
9090
*/
9191
public static final class Binder extends AbstractBinder {
9292

93-
private final ServiceLocator serviceLocator;
93+
private final InjectionManager injectionManager;
9494

95-
public Binder(ServiceLocator serviceLocator) {
96-
this.serviceLocator = serviceLocator;
95+
/**
96+
* Constructor for {@code JerseyClassAnalyzer}.
97+
*
98+
* @param injectionManager current injection manager.
99+
*/
100+
public Binder(InjectionManager injectionManager) {
101+
this.injectionManager = injectionManager;
97102
}
98103

99104
@Override
100105
protected void configure() {
101106
ClassAnalyzer defaultAnalyzer =
102-
serviceLocator.getService(ClassAnalyzer.class, ClassAnalyzer.DEFAULT_IMPLEMENTATION_NAME);
107+
injectionManager.getInstance(ClassAnalyzer.class, ClassAnalyzer.DEFAULT_IMPLEMENTATION_NAME);
103108

104-
Supplier<List<InjectionResolver>> resolvers = () -> serviceLocator.getAllServices(InjectionResolver.class);
109+
Supplier<List<InjectionResolver>> resolvers = () -> injectionManager.getAllInstances(InjectionResolver.class);
105110

106111
bind(new JerseyClassAnalyzer(defaultAnalyzer, resolvers))
107112
.analyzeWith(ClassAnalyzer.DEFAULT_IMPLEMENTATION_NAME)

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

Lines changed: 4 additions & 2 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) 2013-2017 Oracle and/or its affiliates. All rights reserved.
4+
* Copyright (c) 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
@@ -38,13 +38,15 @@
3838
* holder.
3939
*/
4040

41-
package org.glassfish.jersey.internal;
41+
package org.glassfish.jersey.hk2;
4242

4343
import java.io.PrintWriter;
4444
import java.io.StringWriter;
4545

4646
import javax.inject.Singleton;
4747

48+
import org.glassfish.jersey.internal.Errors;
49+
import org.glassfish.jersey.internal.LocalizationMessages;
4850
import org.glassfish.jersey.internal.inject.AbstractBinder;
4951

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

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ private CompositeBinder(Collection<Binder> installed) {
7070
* @param binders provided binder to install as a collection.
7171
* @return composite binder.
7272
*/
73-
public static Binder wrap(Collection<Binder> binders) {
73+
public static AbstractBinder wrap(Collection<Binder> binders) {
7474
return new CompositeBinder(binders);
7575
}
7676

@@ -80,7 +80,7 @@ public static Binder wrap(Collection<Binder> binders) {
8080
* @param binders provided binder to install as an array.
8181
* @return composite binder.
8282
*/
83-
public static Binder wrap(Binder... binders) {
83+
public static AbstractBinder wrap(Binder... binders) {
8484
return new CompositeBinder(Arrays.asList(binders));
8585
}
8686

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

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@
4949

5050
import org.glassfish.jersey.hk2.HK2InjectionManager;
5151

52-
import org.glassfish.hk2.api.MultiException;
53-
5452
/**
5553
* Injection binding utility methods.
5654
*
@@ -132,28 +130,25 @@ private static InjectionManager _injectionManager(String name, InjectionManager
132130
/**
133131
* Get the class by contract or create and inject a new instance.
134132
*
135-
* @param <T> instance type.
133+
* @param <T> instance type.
136134
* @param injectionManager DI injection manager.
137-
* @param clazz class of the instance to be provider.
135+
* @param clazz class of the instance to be provider.
138136
* @return instance of the class either provided as a service or created and injected by HK2.
139137
*/
140138
public static <T> T getOrCreate(InjectionManager injectionManager, final Class<T> clazz) {
141139
try {
142140
final T component = injectionManager.getInstance(clazz);
143141
return component == null ? injectionManager.createAndInitialize(clazz) : component;
144-
// TODO: not really MultiException.
145-
} catch (final MultiException e) {
146-
142+
} catch (final RuntimeException e) {
147143
// Look for WebApplicationException and return it if found. MultiException is thrown when *Param field is
148144
// annotated and value cannot be provided (for example fromString(String) method can throw unchecked
149145
// exception.
150146
//
151147
// see InvalidParamTest
152148
// see JERSEY-1117
153-
for (final Throwable t : e.getErrors()) {
154-
if (WebApplicationException.class.isAssignableFrom(t.getClass())) {
155-
throw (WebApplicationException) t;
156-
}
149+
Throwable throwable = e.getCause();
150+
if (throwable != null && WebApplicationException.class.isAssignableFrom(throwable.getClass())) {
151+
throw (WebApplicationException) throwable;
157152
}
158153

159154
throw e;

core-common/src/test/java/org/glassfish/jersey/internal/TestBinder.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ public static void initProviders(final InjectionManager injectionManager,
8282
protected void configure() {
8383
install(
8484
new RequestScope.Binder(),
85-
new JerseyErrorService.Binder(),
8685
new MessagingBinders.MessageBodyProviders(null, RuntimeType.SERVER),
8786
new MessageBodyFactory.Binder(),
8887
new ExceptionMapperFactory.Binder(),

0 commit comments

Comments
 (0)