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

Commit 9e553ad

Browse files
Petr Boudapavelbucek
authored andcommitted
Replace HK2 PerThread by Jersey PerThread.
Change-Id: I8c0cb2645e6e5a0f469006fddd84a953b1aa35ea
1 parent 853cc83 commit 9e553ad

File tree

7 files changed

+80
-17
lines changed

7 files changed

+80
-17
lines changed

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import org.glassfish.jersey.internal.inject.InjectionResolverBinding;
5656
import org.glassfish.jersey.internal.inject.InstanceBinding;
5757
import org.glassfish.jersey.internal.inject.PerLookup;
58+
import org.glassfish.jersey.internal.inject.PerThread;
5859
import org.glassfish.jersey.internal.inject.SupplierClassBinding;
5960
import org.glassfish.jersey.internal.inject.SupplierInstanceBinding;
6061

@@ -233,7 +234,7 @@ private static void bindSupplierClassBinding(ServiceLocator locator, SupplierCla
233234
}
234235
});
235236
binding.getQualifiers().forEach(supplierBuilder::qualifiedBy);
236-
supplierBuilder.in(replaceScope(binding.getSupplierScope()));
237+
supplierBuilder.in(transformScope(binding.getSupplierScope()));
237238
binder.bind(supplierBuilder);
238239

239240
// Register wrapper for factory functionality, wrapper automatically call service locator which is able to retrieve
@@ -258,7 +259,7 @@ private static void setupSupplierFactoryBridge(Binding<?, ?> binding, ServiceBin
258259
builder.named(binding.getName());
259260
binding.getContracts().forEach(builder::to);
260261
binding.getQualifiers().forEach(builder::qualifiedBy);
261-
builder.in(replaceScope(binding.getScope()));
262+
builder.in(transformScope(binding.getScope()));
262263

263264
if (binding.getRank() != null) {
264265
builder.ranked(binding.getRank());
@@ -278,7 +279,7 @@ static ActiveDescriptor<?> translateToActiveDescriptor(ClassBinding<?> desc) {
278279
.analyzeWith(desc.getAnalyzer());
279280

280281
if (desc.getScope() != null) {
281-
binding.in(replaceScope(desc.getScope()));
282+
binding.in(transformScope(desc.getScope()));
282283
}
283284

284285
if (desc.getRank() != null) {
@@ -404,9 +405,17 @@ protected void configure() {
404405
};
405406
}
406407

407-
private static Class<? extends Annotation> replaceScope(Class<? extends Annotation> scope) {
408+
/**
409+
* Transforms Jersey scopes/annotations to HK2 equivalents.
410+
*
411+
* @param scope Jersey scope/annotation.
412+
* @return HK2 equivalent scope/annotation.
413+
*/
414+
private static Class<? extends Annotation> transformScope(Class<? extends Annotation> scope) {
408415
if (scope == PerLookup.class) {
409416
return org.glassfish.hk2.api.PerLookup.class;
417+
} else if (scope == PerThread.class) {
418+
return org.glassfish.hk2.api.PerThread.class;
410419
}
411420
return scope;
412421
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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.internal.inject;
42+
43+
import java.lang.annotation.Documented;
44+
import java.lang.annotation.Retention;
45+
import java.lang.annotation.Target;
46+
47+
import javax.inject.Scope;
48+
49+
import static java.lang.annotation.ElementType.METHOD;
50+
import static java.lang.annotation.ElementType.TYPE;
51+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
52+
53+
/**
54+
* PerThread is a scope that operates like {@link javax.inject.Singleton} scope, except on a per-thread basis. The lifecycle of
55+
* the service is determined by the thread it is on. On a single thread only one of the service will be created, but a new
56+
* service will be created for each thread.
57+
*
58+
* @author John Wells (john.wells at oracle.com)
59+
*/
60+
@Documented
61+
@Retention(RUNTIME)
62+
@Scope
63+
@Target({ TYPE, METHOD })
64+
public @interface PerThread {
65+
}

media/jaxb/src/main/java/org/glassfish/jersey/jaxb/internal/DocumentBuilderFactoryInjectionProvider.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@
4444
import javax.inject.Inject;
4545
import javax.xml.parsers.DocumentBuilderFactory;
4646

47-
import org.glassfish.hk2.api.PerThread;
48-
4947
/**
5048
* Thread-scoped injection provider of {@link DocumentBuilderFactory document
5149
* builder factories}.
@@ -67,7 +65,6 @@ public DocumentBuilderFactoryInjectionProvider(final Configuration config) {
6765
}
6866

6967
@Override
70-
@PerThread
7168
public DocumentBuilderFactory get() {
7269
DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
7370

media/jaxb/src/main/java/org/glassfish/jersey/jaxb/internal/JaxbMessagingBinder.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@
5050
import javax.xml.transform.TransformerFactory;
5151

5252
import org.glassfish.jersey.internal.inject.AbstractBinder;
53-
54-
import org.glassfish.hk2.api.PerThread;
53+
import org.glassfish.jersey.internal.inject.PerThread;
5554

5655
/**
5756
* Binder for JAX-B message body workers.

media/jaxb/src/main/java/org/glassfish/jersey/jaxb/internal/SaxParserFactoryInjectionProvider.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@
4444
import javax.inject.Inject;
4545
import javax.xml.parsers.SAXParserFactory;
4646

47-
import org.glassfish.hk2.api.PerThread;
48-
4947
/**
5048
* Thread-scoped injection provider of {@link SAXParserFactory SAX parser factories}.
5149
*
@@ -67,7 +65,6 @@ public SaxParserFactoryInjectionProvider(final Configuration config) {
6765
}
6866

6967
@Override
70-
@PerThread
7168
public SAXParserFactory get() {
7269
SAXParserFactory factory = SAXParserFactory.newInstance();
7370

media/jaxb/src/main/java/org/glassfish/jersey/jaxb/internal/XmlInputFactoryInjectionProvider.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@
4444
import javax.inject.Inject;
4545
import javax.xml.stream.XMLInputFactory;
4646

47-
import org.glassfish.hk2.api.PerThread;
48-
4947
/**
5048
* Thread-scoped injection provider of {@link XMLInputFactory transformer factories}.
5149
*
@@ -66,7 +64,6 @@ public XmlInputFactoryInjectionProvider(final Configuration config) {
6664
}
6765

6866
@Override
69-
@PerThread
7067
public XMLInputFactory get() {
7168
XMLInputFactory factory = XMLInputFactory.newInstance();
7269

media/jaxb/src/test/java/org/glassfish/jersey/jaxb/internal/SaxParserFactoryInjectionProviderTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@
6363
import org.glassfish.jersey.internal.inject.Binder;
6464
import org.glassfish.jersey.internal.inject.InjectionManager;
6565
import org.glassfish.jersey.internal.inject.Injections;
66-
67-
import org.glassfish.hk2.api.PerThread;
66+
import org.glassfish.jersey.internal.inject.PerThread;
6867

6968
import org.junit.Before;
7069
import org.junit.Test;

0 commit comments

Comments
 (0)