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

Commit 4aef56d

Browse files
Petr Boudapavelbucek
authored andcommitted
Introducing Injection SPI.
- new InstanceManager SPI - HK2 implementation of Instance SPI and set up as default - all ServiceLocator invocations routed through InstanceManager - Marek’s partial feedback incorporated (along with TODOs) Change-Id: Ied029d19aaba9f22efc0b7965594e4eac96690fd Signed-off-by: Marek Potociar <[email protected]>
1 parent 1837796 commit 4aef56d

File tree

229 files changed

+5199
-3022
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

229 files changed

+5199
-3022
lines changed

containers/glassfish/jersey-gf-ejb/src/main/java/org/glassfish/jersey/gf/ejb/internal/EjbComponentInterceptor.java

Lines changed: 8 additions & 9 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
@@ -43,8 +43,7 @@
4343
import javax.interceptor.InvocationContext;
4444

4545
import org.glassfish.jersey.ext.cdi1x.internal.CdiComponentProvider;
46-
47-
import org.glassfish.hk2.api.ServiceLocator;
46+
import org.glassfish.jersey.spi.inject.InstanceManager;
4847

4948
/**
5049
* EJB interceptor to inject Jersey specific stuff into EJB beans.
@@ -53,22 +52,22 @@
5352
*/
5453
public final class EjbComponentInterceptor {
5554

56-
private final ServiceLocator locator;
55+
private final InstanceManager instanceManager;
5756

5857
/**
59-
* Create new EJB component locator.
58+
* Create new EJB component instance manager.
6059
*
61-
* @param locator HK2 service locator.
60+
* @param instanceManager instance manager.
6261
*/
63-
public EjbComponentInterceptor(final ServiceLocator locator) {
64-
this.locator = locator;
62+
public EjbComponentInterceptor(final InstanceManager instanceManager) {
63+
this.instanceManager = instanceManager;
6564
}
6665

6766
@PostConstruct
6867
private void inject(final InvocationContext context) throws Exception {
6968

7069
final Object beanInstance = context.getTarget();
71-
locator.inject(beanInstance, CdiComponentProvider.CDI_CLASS_ANALYZER);
70+
instanceManager.inject(beanInstance, CdiComponentProvider.CDI_CLASS_ANALYZER);
7271

7372
// Invoke next interceptor in chain
7473
context.proceed();

containers/glassfish/jersey-gf-ejb/src/main/java/org/glassfish/jersey/gf/ejb/internal/EjbComponentProvider.java

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -60,25 +60,23 @@
6060
import java.util.logging.Level;
6161
import java.util.logging.Logger;
6262

63-
import javax.ws.rs.ext.ExceptionMapper;
64-
6563
import javax.annotation.Priority;
6664
import javax.ejb.Local;
6765
import javax.ejb.Remote;
6866
import javax.inject.Singleton;
6967
import javax.naming.InitialContext;
7068
import javax.naming.NamingException;
7169

72-
import org.glassfish.jersey.internal.inject.Injections;
7370
import org.glassfish.jersey.internal.inject.SupplierFactory;
7471
import org.glassfish.jersey.server.ApplicationHandler;
7572
import org.glassfish.jersey.server.model.Invocable;
7673
import org.glassfish.jersey.server.spi.ComponentProvider;
7774
import org.glassfish.jersey.server.spi.internal.ResourceMethodInvocationHandlerProvider;
78-
79-
import org.glassfish.hk2.api.DynamicConfiguration;
80-
import org.glassfish.hk2.api.ServiceLocator;
81-
import org.glassfish.hk2.utilities.binding.ServiceBindingBuilder;
75+
import org.glassfish.jersey.spi.inject.ClassBeanDescriptor;
76+
import org.glassfish.jersey.spi.inject.Descriptor;
77+
import org.glassfish.jersey.spi.inject.Descriptors;
78+
import org.glassfish.jersey.spi.inject.InstanceBeanDescriptor;
79+
import org.glassfish.jersey.spi.inject.InstanceManager;
8280

8381
import org.glassfish.ejb.deployment.descriptor.EjbBundleDescriptorImpl;
8482
import org.glassfish.ejb.deployment.descriptor.EjbDescriptor;
@@ -145,15 +143,16 @@ public EjbFactory(Class<T> rawType, InitialContext ctx, EjbComponentProvider ejb
145143
add("javax.ejb.Singleton");
146144
}});
147145

148-
private ServiceLocator locator = null;
146+
private InstanceManager instanceManager = null;
149147

150148
// ComponentProvider
151149
@Override
152-
public void initialize(final ServiceLocator locator) {
153-
this.locator = locator;
154-
final DynamicConfiguration configuration = Injections.getConfiguration(locator);
155-
Injections.addBinding(Injections.newBinder(this).to(ResourceMethodInvocationHandlerProvider.class), configuration);
156-
configuration.commit();
150+
public void initialize(final InstanceManager instanceManager) {
151+
this.instanceManager = instanceManager;
152+
153+
InstanceBeanDescriptor<EjbComponentProvider> descriptor = Descriptors.service(EjbComponentProvider.this)
154+
.to(ResourceMethodInvocationHandlerProvider.class);
155+
this.instanceManager.register(descriptor);
157156
}
158157

159158
private ApplicationInfo getApplicationInfo(EjbContainerUtil ejbUtil) throws NamingException {
@@ -191,7 +190,7 @@ private ApplicationInfo getApplicationInfo(EjbContainerUtil ejbUtil) throws Nami
191190

192191
private void registerEjbInterceptor() {
193192
try {
194-
final Object interceptor = new EjbComponentInterceptor(locator);
193+
final Object interceptor = new EjbComponentInterceptor(instanceManager);
195194
initialContext = getInitialContext();
196195
final EjbContainerUtil ejbUtil = EjbContainerUtilImpl.getInstance();
197196
final ApplicationInfo appInfo = getApplicationInfo(ejbUtil);
@@ -271,7 +270,7 @@ public boolean bind(Class<?> component, Set<Class<?>> providerContracts) {
271270
LOGGER.fine(LocalizationMessages.EJB_CLASS_BEING_CHECKED(component));
272271
}
273272

274-
if (locator == null) {
273+
if (instanceManager == null) {
275274
throw new IllegalStateException(LocalizationMessages.EJB_COMPONENT_PROVIDER_NOT_INITIALIZED_PROPERLY());
276275
}
277276

@@ -283,18 +282,10 @@ public boolean bind(Class<?> component, Set<Class<?>> providerContracts) {
283282
registerEjbInterceptor();
284283
}
285284

286-
DynamicConfiguration dc = Injections.getConfiguration(locator);
287-
288-
final ServiceBindingBuilder bindingBuilder = Injections.newFactoryBinder(new EjbFactory(component, initialContext, this));
289-
290-
bindingBuilder.to(component);
291-
for (Class contract : providerContracts) {
292-
bindingBuilder.to(contract);
293-
}
294-
295-
Injections.addBinding(bindingBuilder, dc);
296-
297-
dc.commit();
285+
Descriptor descriptor = Descriptors.factory(new EjbFactory(component, initialContext, EjbComponentProvider.this))
286+
.to(component)
287+
.to(providerContracts);
288+
instanceManager.register(descriptor);
298289

299290
if (LOGGER.isLoggable(Level.CONFIG)) {
300291
LOGGER.config(LocalizationMessages.EJB_CLASS_BOUND_WITH_CDI(component));
@@ -309,9 +300,11 @@ public void done() {
309300
}
310301

311302
private void registerEjbExceptionMapper() {
312-
final DynamicConfiguration dc = Injections.getConfiguration(locator);
313-
Injections.addBinding(Injections.newBinder(EjbExceptionMapper.class).to(ExceptionMapper.class).in(Singleton.class), dc);
314-
dc.commit();
303+
ClassBeanDescriptor<EjbExceptionMapper> descriptor =
304+
Descriptors.serviceAsContract(EjbExceptionMapper.class)
305+
.in(Singleton.class);
306+
307+
instanceManager.register(descriptor);
315308
}
316309

317310
private boolean isEjbComponent(Class<?> component) {

containers/grizzly2-http/src/main/java/org/glassfish/jersey/grizzly2/httpserver/GrizzlyHttpContainer.java

Lines changed: 13 additions & 18 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) 2010-2015 Oracle and/or its affiliates. All rights reserved.
4+
* Copyright (c) 2010-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
@@ -37,6 +37,7 @@
3737
* only if the new code is made subject to such option by the copyright
3838
* holder.
3939
*/
40+
4041
package org.glassfish.jersey.grizzly2.httpserver;
4142

4243
import java.io.IOException;
@@ -53,6 +54,7 @@
5354
import java.util.logging.Logger;
5455

5556
import javax.ws.rs.core.Application;
57+
import javax.ws.rs.core.GenericType;
5658
import javax.ws.rs.core.SecurityContext;
5759

5860
import javax.inject.Inject;
@@ -72,11 +74,9 @@
7274
import org.glassfish.jersey.server.internal.ContainerUtils;
7375
import org.glassfish.jersey.server.spi.Container;
7476
import org.glassfish.jersey.server.spi.ContainerResponseWriter;
75-
import org.glassfish.jersey.server.spi.RequestScopedInitializer;
77+
import org.glassfish.jersey.spi.inject.AbstractBinder;
7678

7779
import org.glassfish.hk2.api.ServiceLocator;
78-
import org.glassfish.hk2.api.TypeLiteral;
79-
import org.glassfish.hk2.utilities.binding.AbstractBinder;
8080

8181
import org.glassfish.grizzly.CompletionHandler;
8282
import org.glassfish.grizzly.http.server.HttpHandler;
@@ -95,10 +95,9 @@ public final class GrizzlyHttpContainer extends HttpHandler implements Container
9595
private static final ExtendedLogger logger =
9696
new ExtendedLogger(Logger.getLogger(GrizzlyHttpContainer.class.getName()), Level.FINEST);
9797

98-
private final Type RequestTYPE = (new TypeLiteral<Ref<Request>>() {
99-
}).getType();
100-
private final Type ResponseTYPE = (new TypeLiteral<Ref<Response>>() {
101-
}).getType();
98+
private final Type RequestTYPE = (new GenericType<Ref<Request>>() { }).getType();
99+
private final Type ResponseTYPE = (new GenericType<Ref<Response>>() { }).getType();
100+
102101
/**
103102
* Cached value of configuration property
104103
* {@link org.glassfish.jersey.server.ServerProperties#RESPONSE_SET_STATUS_OVER_SEND_ERROR}.
@@ -151,12 +150,12 @@ static class GrizzlyBinder extends AbstractBinder {
151150
protected void configure() {
152151
bindFactory(GrizzlyRequestReferencingFactory.class).to(Request.class)
153152
.proxy(false).in(RequestScoped.class);
154-
bindFactory(ReferencingFactory.<Request>referenceFactory()).to(new TypeLiteral<Ref<Request>>() {})
153+
bindFactory(ReferencingFactory.<Request>referenceFactory()).to(new GenericType<Ref<Request>>() {})
155154
.in(RequestScoped.class);
156155

157156
bindFactory(GrizzlyResponseReferencingFactory.class).to(Response.class)
158157
.proxy(true).proxyForSameScope(false).in(RequestScoped.class);
159-
bindFactory(ReferencingFactory.<Response>referenceFactory()).to(new TypeLiteral<Ref<Response>>() {})
158+
bindFactory(ReferencingFactory.<Response>referenceFactory()).to(new GenericType<Ref<Response>>() {})
160159
.in(RequestScoped.class);
161160
}
162161
}
@@ -343,7 +342,7 @@ private void rethrow(final Throwable error) {
343342
* Create a new Grizzly HTTP container.
344343
*
345344
* @param application JAX-RS / Jersey application to be deployed on Grizzly HTTP container.
346-
* @param parentLocator parent HK2 service locator.
345+
* @param parentLocator parent instance manager.
347346
*/
348347
/* package */ GrizzlyHttpContainer(final Application application, final ServiceLocator parentLocator) {
349348
this.appHandler = new ApplicationHandler(application, new GrizzlyBinder(), parentLocator);
@@ -373,13 +372,9 @@ public void service(final Request request, final Response response) {
373372
}
374373
requestContext.setWriter(responseWriter);
375374

376-
requestContext.setRequestScopedInitializer(new RequestScopedInitializer() {
377-
378-
@Override
379-
public void initialize(final ServiceLocator locator) {
380-
locator.<Ref<Request>>getService(RequestTYPE).set(request);
381-
locator.<Ref<Response>>getService(ResponseTYPE).set(response);
382-
}
375+
requestContext.setRequestScopedInitializer(instanceManager -> {
376+
instanceManager.<Ref<Request>>getInstance(RequestTYPE).set(request);
377+
instanceManager.<Ref<Response>>getInstance(ResponseTYPE).set(response);
383378
});
384379
appHandler.handle(requestContext);
385380
} finally {

containers/jersey-servlet-core/src/main/java/org/glassfish/jersey/servlet/ServletContainer.java

Lines changed: 3 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) 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
@@ -37,6 +37,7 @@
3737
* only if the new code is made subject to such option by the copyright
3838
* holder.
3939
*/
40+
4041
package org.glassfish.jersey.servlet;
4142

4243
import java.io.IOException;
@@ -644,7 +645,7 @@ private String pickUrlMapping(final String requestUri, final List<String> filter
644645
private FilterUrlMappingsProvider getFilterUrlMappingsProvider() {
645646
FilterUrlMappingsProvider filterUrlMappingsProvider = null;
646647
final Iterator<FilterUrlMappingsProvider> providers = Providers.getAllProviders(
647-
getApplicationHandler().getServiceLocator(), FilterUrlMappingsProvider.class).iterator();
648+
getApplicationHandler().getInstanceManager(), FilterUrlMappingsProvider.class).iterator();
648649
if (providers.hasNext()) {
649650
filterUrlMappingsProvider = providers.next();
650651
}

containers/jersey-servlet-core/src/main/java/org/glassfish/jersey/servlet/WebComponent.java

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
* only if the new code is made subject to such option by the copyright
3838
* holder.
3939
*/
40+
4041
package org.glassfish.jersey.servlet;
4142

4243
import java.io.IOException;
@@ -60,6 +61,7 @@
6061

6162
import javax.ws.rs.RuntimeType;
6263
import javax.ws.rs.core.Form;
64+
import javax.ws.rs.core.GenericType;
6365
import javax.ws.rs.core.MediaType;
6466
import javax.ws.rs.core.MultivaluedMap;
6567
import javax.ws.rs.core.Response;
@@ -105,11 +107,11 @@
105107
import org.glassfish.jersey.servlet.spi.AsyncContextDelegate;
106108
import org.glassfish.jersey.servlet.spi.AsyncContextDelegateProvider;
107109
import org.glassfish.jersey.servlet.spi.FilterUrlMappingsProvider;
110+
import org.glassfish.jersey.spi.inject.AbstractBinder;
111+
import org.glassfish.jersey.spi.inject.InstanceManager;
108112
import org.glassfish.jersey.uri.UriComponent;
109113

110114
import org.glassfish.hk2.api.ServiceLocator;
111-
import org.glassfish.hk2.api.TypeLiteral;
112-
import org.glassfish.hk2.utilities.binding.AbstractBinder;
113115

114116
import jersey.repackaged.com.google.common.base.Predicate;
115117
import jersey.repackaged.com.google.common.collect.Collections2;
@@ -128,8 +130,8 @@ public class WebComponent {
128130

129131
private static final Logger LOGGER = Logger.getLogger(WebComponent.class.getName());
130132

131-
private static final Type REQUEST_TYPE = (new TypeLiteral<Ref<HttpServletRequest>>() {}).getType();
132-
private static final Type RESPONSE_TYPE = (new TypeLiteral<Ref<HttpServletResponse>>() {}).getType();
133+
private static final Type REQUEST_TYPE = (new GenericType<Ref<HttpServletRequest>>() {}).getType();
134+
private static final Type RESPONSE_TYPE = (new GenericType<Ref<HttpServletResponse>>() {}).getType();
133135

134136
private static final AsyncContextDelegate DEFAULT_ASYNC_DELEGATE = new AsyncContextDelegate() {
135137

@@ -147,28 +149,20 @@ public void complete() {
147149
private final boolean requestResponseBindingExternalized;
148150

149151
private static final RequestScopedInitializerProvider DEFAULT_REQUEST_SCOPE_INITIALIZER_PROVIDER =
150-
new RequestScopedInitializerProvider() {
151-
@Override
152-
public RequestScopedInitializer get(final RequestContextProvider context) {
153-
return new RequestScopedInitializer() {
154-
@Override
155-
public void initialize(final ServiceLocator locator) {
156-
locator.<Ref<HttpServletRequest>>getService(REQUEST_TYPE).set(context.getHttpServletRequest());
157-
locator.<Ref<HttpServletResponse>>getService(RESPONSE_TYPE).set(context.getHttpServletResponse());
158-
}
159-
};
160-
}
152+
context -> (RequestScopedInitializer) instanceManager -> {
153+
instanceManager.<Ref<HttpServletRequest>>getInstance(REQUEST_TYPE).set(context.getHttpServletRequest());
154+
instanceManager.<Ref<HttpServletResponse>>getInstance(RESPONSE_TYPE).set(context.getHttpServletResponse());
161155
};
162156

163157
/**
164158
* Return the first found {@link AsyncContextDelegateProvider}
165-
* (via {@link Providers#getAllProviders(org.glassfish.hk2.api.ServiceLocator, Class)}) or {@code #DEFAULT_ASYNC_DELEGATE} if
159+
* (via {@link Providers#getAllProviders(InstanceManager, Class)}) or {@code #DEFAULT_ASYNC_DELEGATE} if
166160
* other delegate cannot be found.
167161
*
168162
* @return a non-null AsyncContextDelegateProvider.
169163
*/
170164
private AsyncContextDelegateProvider getAsyncExtensionDelegate() {
171-
final Iterator<AsyncContextDelegateProvider> providers = Providers.getAllProviders(appHandler.getServiceLocator(),
165+
final Iterator<AsyncContextDelegateProvider> providers = Providers.getAllProviders(appHandler.getInstanceManager(),
172166
AsyncContextDelegateProvider.class).iterator();
173167
if (providers.hasNext()) {
174168
return providers.next();
@@ -226,13 +220,13 @@ protected void configure() {
226220
.proxy(true).proxyForSameScope(false).in(RequestScoped.class);
227221

228222
bindFactory(ReferencingFactory.<HttpServletRequest>referenceFactory())
229-
.to(new TypeLiteral<Ref<HttpServletRequest>>() {}).in(RequestScoped.class);
223+
.to(new GenericType<Ref<HttpServletRequest>>() {}).in(RequestScoped.class);
230224

231225
// response
232226
bindFactory(HttpServletResponseReferencingFactory.class).to(HttpServletResponse.class)
233227
.proxy(true).proxyForSameScope(false).in(RequestScoped.class);
234228
bindFactory(ReferencingFactory.<HttpServletResponse>referenceFactory())
235-
.to(new TypeLiteral<Ref<HttpServletResponse>>() {}).in(RequestScoped.class);
229+
.to(new GenericType<Ref<HttpServletResponse>>() {}).in(RequestScoped.class);
236230
}
237231

238232
bindFactory(new SupplierFactory<ServletContext>() {
@@ -380,8 +374,8 @@ public WebComponent(final WebConfig webConfig, ResourceConfig resourceConfig) th
380374
this.queryParamsAsFormParams = !resourceConfig.isProperty(ServletProperties.QUERY_PARAMS_AS_FORM_PARAMS_DISABLED);
381375
this.configSetStatusOverSendError = ServerProperties.getValue(resourceConfig.getProperties(),
382376
ServerProperties.RESPONSE_SET_STATUS_OVER_SEND_ERROR, false, Boolean.class);
383-
this.backgroundTaskScheduler = appHandler.getServiceLocator()
384-
.getService(ScheduledExecutorService.class, BackgroundSchedulerLiteral.INSTANCE);
377+
this.backgroundTaskScheduler = appHandler.getInstanceManager()
378+
.getInstance(ScheduledExecutorService.class, BackgroundSchedulerLiteral.INSTANCE);
385379
}
386380

387381
/**

0 commit comments

Comments
 (0)