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

Commit ea4d4ce

Browse files
committed
Removed BufferedWriter from ReaderWriter (too much time spent in buffered writer instance creation)
Resource method dispatch providers get request and validator passed over as method parameters rather than via inefficient HK2 provider mechanism. Change-Id: Id7e2fd742e7fb02760f22ea46b95543f69af3673 Signed-off-by: Jakub Podlesak <[email protected]>
1 parent 0b5cb9f commit ea4d4ce

File tree

10 files changed

+94
-84
lines changed

10 files changed

+94
-84
lines changed

core-common/src/main/java/org/glassfish/jersey/message/internal/ReaderWriter.java

Lines changed: 3 additions & 5 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-2014 Oracle and/or its affiliates. All rights reserved.
4+
* Copyright (c) 2010-2015 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
@@ -39,7 +39,6 @@
3939
*/
4040
package org.glassfish.jersey.message.internal;
4141

42-
import java.io.BufferedWriter;
4342
import java.io.IOException;
4443
import java.io.InputStream;
4544
import java.io.InputStreamReader;
@@ -186,9 +185,8 @@ public static String readFromAsString(Reader reader) throws IOException {
186185
*/
187186
public static void writeToAsString(String s, OutputStream out,
188187
MediaType type) throws IOException {
189-
Writer osw = new BufferedWriter(new OutputStreamWriter(out,
190-
getCharset(type)));
191-
osw.write(s, 0, s.length()); // MUCH faster than BufferedWriter.write(s) on JDK 1.6
188+
Writer osw = new OutputStreamWriter(out, getCharset(type));
189+
osw.write(s, 0, s.length());
192190
osw.flush();
193191
}
194192

core-server/src/main/java/org/glassfish/jersey/server/ApplicationHandler.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@
136136
import jersey.repackaged.com.google.common.collect.Lists;
137137
import jersey.repackaged.com.google.common.collect.Sets;
138138
import jersey.repackaged.com.google.common.util.concurrent.AbstractFuture;
139+
import org.glassfish.jersey.message.MessageBodyWorkers;
139140

140141
/**
141142
* Jersey server-side application handler.
@@ -234,6 +235,7 @@ protected void configure() {
234235
private final ResourceConfig runtimeConfig;
235236
private final ServiceLocator locator;
236237
private ServerRuntime runtime;
238+
private MessageBodyWorkers msgBodyWorkers;
237239

238240

239241
/**
@@ -538,7 +540,6 @@ private void initialize() {
538540
*/
539541
final Stage<RequestProcessingContext> rootStage = Stages
540542
.chain(referencesInitializer)
541-
.to(locator.createAndInitialize(ContainerMessageBodyWorkersInitializer.class))
542543
.to(preMatchRequestFilteringStage)
543544
.to(routingStage)
544545
.to(resourceFilteringStage)
@@ -554,6 +555,8 @@ private void initialize() {
554555
locator.inject(instance);
555556
}
556557

558+
msgBodyWorkers = locator.getService(MessageBodyWorkers.class);
559+
557560
logApplicationInitConfiguration(locator, resourceBag, processingProviders);
558561

559562
if (compositeListener != null) {
@@ -1070,6 +1073,7 @@ protected void interruptTask() {
10701073
* @param request container request context of the current request.
10711074
*/
10721075
public void handle(final ContainerRequest request) {
1076+
request.setWorkers(msgBodyWorkers);
10731077
runtime.process(request);
10741078
}
10751079

core-server/src/main/java/org/glassfish/jersey/server/model/ResourceMethodInvoker.java

Lines changed: 9 additions & 4 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) 2011-2014 Oracle and/or its affiliates. All rights reserved.
4+
* Copyright (c) 2011-2015 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
@@ -77,6 +77,7 @@
7777
import org.glassfish.jersey.server.ContainerResponse;
7878
import org.glassfish.jersey.server.internal.LocalizationMessages;
7979
import org.glassfish.jersey.server.internal.ProcessingProviders;
80+
import org.glassfish.jersey.server.internal.inject.ConfiguredValidator;
8081
import org.glassfish.jersey.server.internal.process.Endpoint;
8182
import org.glassfish.jersey.server.internal.process.RequestProcessingContext;
8283
import org.glassfish.jersey.server.model.internal.ResourceMethodDispatcherFactory;
@@ -128,6 +129,8 @@ public static class Builder {
128129
private ServiceLocator locator;
129130
@Inject
130131
private Configuration globalConfig;
132+
@Inject
133+
private javax.inject.Provider<ConfiguredValidator> validatorProvider;
131134

132135
/**
133136
* Build a new resource method invoker instance.
@@ -146,7 +149,8 @@ public ResourceMethodInvoker build(
146149
method,
147150
processingProviders,
148151
locator,
149-
globalConfig);
152+
globalConfig,
153+
validatorProvider.get());
150154
}
151155
}
152156

@@ -156,13 +160,14 @@ private ResourceMethodInvoker(
156160
final ResourceMethod method,
157161
final ProcessingProviders processingProviders,
158162
ServiceLocator locator,
159-
final Configuration globalConfig) {
163+
final Configuration globalConfig,
164+
final ConfiguredValidator validator) {
160165

161166

162167
this.method = method;
163168
final Invocable invocable = method.getInvocable();
164169
this.dispatcher = dispatcherProvider.create(invocable,
165-
invocationHandlerProvider.create(invocable));
170+
invocationHandlerProvider.create(invocable), validator);
166171

167172
this.resourceMethod = invocable.getHandlingMethod();
168173
this.resourceClass = invocable.getHandler().getHandlerClass();

core-server/src/main/java/org/glassfish/jersey/server/model/internal/AbstractJavaResourceMethodDispatcher.java

Lines changed: 8 additions & 20 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) 2011-2013 Oracle and/or its affiliates. All rights reserved.
4+
* Copyright (c) 2011-2015 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
@@ -47,11 +47,9 @@
4747

4848
import javax.ws.rs.ProcessingException;
4949
import javax.ws.rs.WebApplicationException;
50-
import javax.ws.rs.core.Request;
5150
import javax.ws.rs.core.Response;
5251
import javax.ws.rs.core.SecurityContext;
5352

54-
import javax.inject.Inject;
5553
import javax.validation.ValidationException;
5654

5755
import org.glassfish.jersey.message.internal.TracingLogger;
@@ -73,28 +71,22 @@
7371
*/
7472
abstract class AbstractJavaResourceMethodDispatcher implements ResourceMethodDispatcher {
7573

76-
@Inject
77-
private javax.inject.Provider<ConfiguredValidator> validatorProvider;
78-
79-
@Inject
80-
private javax.inject.Provider<ContainerRequest> request;
81-
8274
private final Method method;
8375
private final InvocationHandler methodHandler;
84-
85-
private Invocable resourceMethod;
76+
private final Invocable resourceMethod;
77+
private final ConfiguredValidator validator;
8678

8779
/**
8880
* Initialize common java resource method dispatcher structures.
8981
*
9082
* @param resourceMethod invocable resource class Java method.
9183
* @param methodHandler method invocation handler.
9284
*/
93-
AbstractJavaResourceMethodDispatcher(Invocable resourceMethod, InvocationHandler methodHandler) {
85+
AbstractJavaResourceMethodDispatcher(Invocable resourceMethod, InvocationHandler methodHandler, ConfiguredValidator validator) {
9486
this.method = resourceMethod.getDefinitionMethod();
9587
this.methodHandler = methodHandler;
96-
9788
this.resourceMethod = resourceMethod;
89+
this.validator = validator;
9890
}
9991

10092
@Override
@@ -118,7 +110,7 @@ public final Response dispatch(Object resource, ContainerRequest request) throws
118110
* @throws ProcessingException in case of a processing error.
119111
* @see ResourceMethodDispatcher#dispatch(Object, org.glassfish.jersey.server.ContainerRequest)
120112
*/
121-
protected abstract Response doDispatch(Object resource, Request request) throws ProcessingException;
113+
protected abstract Response doDispatch(Object resource, ContainerRequest request) throws ProcessingException;
122114

123115
/**
124116
* Use the underlying invocation handler to invoke the underlying Java method
@@ -130,17 +122,13 @@ public final Response dispatch(Object resource, ContainerRequest request) throws
130122
* @throws ProcessingException (possibly {@link MappableException mappable})
131123
* container exception in case the invocation failed.
132124
*/
133-
final Object invoke(final Object resource, final Object... args) throws ProcessingException {
125+
final Object invoke(final ContainerRequest containerRequest, final Object resource, final Object... args) throws ProcessingException {
134126
try {
135-
final ConfiguredValidator validator = validatorProvider.get();
136-
137127
// Validate resource class & method input parameters.
138128
if (validator != null) {
139129
validator.validateResourceAndInputParams(resource, resourceMethod, args);
140130
}
141131

142-
final ContainerRequest containerRequest = request.get();
143-
144132
final PrivilegedAction invokeMethodAction = new PrivilegedAction() {
145133
@Override
146134
public Object run() {
@@ -194,4 +182,4 @@ private static RuntimeException mapTargetToRuntimeEx(Throwable throwable) {
194182
public String toString() {
195183
return method.toString();
196184
}
197-
}
185+
}

core-server/src/main/java/org/glassfish/jersey/server/model/internal/JavaResourceMethodDispatcherProvider.java

Lines changed: 43 additions & 35 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) 2011-2013 Oracle and/or its affiliates. All rights reserved.
4+
* Copyright (c) 2011-2015 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
@@ -45,17 +45,18 @@
4545

4646
import javax.ws.rs.ProcessingException;
4747
import javax.ws.rs.core.GenericEntity;
48-
import javax.ws.rs.core.Request;
4948
import javax.ws.rs.core.Response;
5049

5150
import javax.inject.Inject;
5251

52+
import org.glassfish.jersey.server.ContainerRequest;
5353
import org.glassfish.jersey.server.model.Invocable;
5454
import org.glassfish.jersey.server.spi.internal.ParameterValueHelper;
5555
import org.glassfish.jersey.server.spi.internal.ResourceMethodDispatcher;
5656

5757
import org.glassfish.hk2.api.Factory;
5858
import org.glassfish.hk2.api.ServiceLocator;
59+
import org.glassfish.jersey.server.internal.inject.ConfiguredValidator;
5960

6061
/**
6162
* An implementation of {@link ResourceMethodDispatcher.Provider} that
@@ -70,24 +71,26 @@ class JavaResourceMethodDispatcherProvider implements ResourceMethodDispatcher.P
7071
private ServiceLocator serviceLocator;
7172

7273
@Override
73-
public ResourceMethodDispatcher create(Invocable resourceMethod, InvocationHandler invocationHandler) {
74+
public ResourceMethodDispatcher create(final Invocable resourceMethod,
75+
final InvocationHandler invocationHandler,
76+
final ConfiguredValidator validator) {
7477
final List<Factory<?>> valueProviders = resourceMethod.getValueProviders(serviceLocator);
7578
final Class<?> returnType = resourceMethod.getHandlingMethod().getReturnType();
7679

7780
ResourceMethodDispatcher resourceMethodDispatcher;
7881
if (Response.class.isAssignableFrom(returnType)) {
79-
resourceMethodDispatcher = new ResponseOutInvoker(resourceMethod, invocationHandler, valueProviders);
82+
resourceMethodDispatcher = new ResponseOutInvoker(resourceMethod, invocationHandler, valueProviders, validator);
8083
// TODO should we support JResponse?
8184
// } else if (JResponse.class.isAssignableFrom(returnType)) {
8285
// return new JResponseOutInvoker(resourceMethod, pp, invocationHandler);
8386
} else if (returnType != void.class) {
8487
if (returnType == Object.class || GenericEntity.class.isAssignableFrom(returnType)) {
85-
resourceMethodDispatcher = new ObjectOutInvoker(resourceMethod, invocationHandler, valueProviders);
88+
resourceMethodDispatcher = new ObjectOutInvoker(resourceMethod, invocationHandler, valueProviders, validator);
8689
} else {
87-
resourceMethodDispatcher = new TypeOutInvoker(resourceMethod, invocationHandler, valueProviders);
90+
resourceMethodDispatcher = new TypeOutInvoker(resourceMethod, invocationHandler, valueProviders, validator);
8891
}
8992
} else {
90-
resourceMethodDispatcher = new VoidOutInvoker(resourceMethod, invocationHandler, valueProviders);
93+
resourceMethodDispatcher = new VoidOutInvoker(resourceMethod, invocationHandler, valueProviders, validator);
9194
}
9295

9396
// Inject validator.
@@ -110,10 +113,11 @@ private static abstract class AbstractMethodParamInvoker extends AbstractJavaRes
110113
private final List<Factory<?>> valueProviders;
111114

112115
public AbstractMethodParamInvoker(
113-
Invocable resourceMethod,
114-
InvocationHandler handler,
115-
List<Factory<?>> valueProviders) {
116-
super(resourceMethod, handler);
116+
final Invocable resourceMethod,
117+
final InvocationHandler handler,
118+
final List<Factory<?>> valueProviders,
119+
final ConfiguredValidator validator) {
120+
super(resourceMethod, handler, validator);
117121
this.valueProviders = valueProviders;
118122
}
119123

@@ -125,46 +129,49 @@ final Object[] getParamValues() {
125129
private static final class VoidOutInvoker extends AbstractMethodParamInvoker {
126130

127131
public VoidOutInvoker(
128-
Invocable resourceMethod,
129-
InvocationHandler handler,
130-
List<Factory<?>> valueProviders) {
131-
super(resourceMethod, handler, valueProviders);
132+
final Invocable resourceMethod,
133+
final InvocationHandler handler,
134+
final List<Factory<?>> valueProviders,
135+
final ConfiguredValidator validator) {
136+
super(resourceMethod, handler, valueProviders, validator);
132137
}
133138

134139
@Override
135-
protected Response doDispatch(Object resource, Request request) throws ProcessingException {
136-
invoke(resource, getParamValues());
140+
protected Response doDispatch(final Object resource, final ContainerRequest containerRequest) throws ProcessingException {
141+
invoke(containerRequest, resource, getParamValues());
137142
return Response.noContent().build();
138143
}
139144
}
140145

141146
private static final class ResponseOutInvoker extends AbstractMethodParamInvoker {
142147

143148
public ResponseOutInvoker(
144-
Invocable resourceMethod,
145-
InvocationHandler handler,
146-
List<Factory<?>> valueProviders) {
147-
super(resourceMethod, handler, valueProviders);
149+
final Invocable resourceMethod,
150+
final InvocationHandler handler,
151+
final List<Factory<?>> valueProviders,
152+
final ConfiguredValidator validator) {
153+
super(resourceMethod, handler, valueProviders, validator);
148154
}
149155

150156
@Override
151-
protected Response doDispatch(Object resource, Request request) throws ProcessingException {
152-
return Response.class.cast(invoke(resource, getParamValues()));
157+
protected Response doDispatch(Object resource, final ContainerRequest containerRequest) throws ProcessingException {
158+
return Response.class.cast(invoke(containerRequest, resource, getParamValues()));
153159
}
154160
}
155161

156162
private static final class ObjectOutInvoker extends AbstractMethodParamInvoker {
157163

158164
public ObjectOutInvoker(
159-
Invocable resourceMethod,
160-
InvocationHandler handler,
161-
List<Factory<?>> valueProviders) {
162-
super(resourceMethod, handler, valueProviders);
165+
final Invocable resourceMethod,
166+
final InvocationHandler handler,
167+
final List<Factory<?>> valueProviders,
168+
final ConfiguredValidator validator) {
169+
super(resourceMethod, handler, valueProviders, validator);
163170
}
164171

165172
@Override
166-
protected Response doDispatch(Object resource, Request request) throws ProcessingException {
167-
final Object o = invoke(resource, getParamValues());
173+
protected Response doDispatch(final Object resource, final ContainerRequest containerRequest) throws ProcessingException {
174+
final Object o = invoke(containerRequest, resource, getParamValues());
168175

169176
if (o instanceof Response) {
170177
return Response.class.cast(o);
@@ -183,16 +190,17 @@ private static final class TypeOutInvoker extends AbstractMethodParamInvoker {
183190
private final Type t;
184191

185192
public TypeOutInvoker(
186-
Invocable resourceMethod,
187-
InvocationHandler handler,
188-
List<Factory<?>> valueProviders) {
189-
super(resourceMethod, handler, valueProviders);
193+
final Invocable resourceMethod,
194+
final InvocationHandler handler,
195+
final List<Factory<?>> valueProviders,
196+
final ConfiguredValidator validator) {
197+
super(resourceMethod, handler, valueProviders, validator);
190198
this.t = resourceMethod.getHandlingMethod().getGenericReturnType();
191199
}
192200

193201
@Override
194-
protected Response doDispatch(Object resource, Request request) throws ProcessingException {
195-
final Object o = invoke(resource, getParamValues());
202+
protected Response doDispatch(final Object resource, final ContainerRequest containerRequest) throws ProcessingException {
203+
final Object o = invoke(containerRequest, resource, getParamValues());
196204
if (o != null) {
197205

198206
Response response = Response.ok().entity(o).build();

0 commit comments

Comments
 (0)