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

Commit b9432c8

Browse files
Petr BoudaGerrit Code Review
authored andcommitted
Merge "Splitting RequestScope class into Jersey specific part and HK2 Context implementation for a creation and destroying instances." into 2.x
2 parents 7f878fe + 732916b commit b9432c8

File tree

7 files changed

+227
-62
lines changed

7 files changed

+227
-62
lines changed

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@
5353
import org.glassfish.jersey.internal.inject.ClassBinding;
5454
import org.glassfish.jersey.internal.inject.CompositeBinder;
5555
import org.glassfish.jersey.internal.inject.ForeignDescriptor;
56-
import org.glassfish.jersey.internal.inject.ForeignDescriptorImpl;
5756
import org.glassfish.jersey.internal.inject.InjectionManager;
5857
import org.glassfish.jersey.internal.inject.InstanceBinding;
5958
import org.glassfish.jersey.internal.inject.ServiceHolder;
6059
import org.glassfish.jersey.internal.inject.ServiceHolderImpl;
6160

6261
import org.glassfish.hk2.api.ActiveDescriptor;
62+
import org.glassfish.hk2.api.Descriptor;
6363
import org.glassfish.hk2.api.ServiceLocator;
6464
import org.glassfish.hk2.api.ServiceLocatorFactory;
6565
import org.glassfish.hk2.utilities.ServiceLocatorUtilities;
@@ -233,12 +233,13 @@ public Object getInstance(ForeignDescriptor foreignDescriptor) {
233233
public ForeignDescriptor createForeignDescriptor(Binding binding) {
234234
ForeignDescriptor foreignDescriptor = createAndTranslateForeignDescriptor(binding);
235235
ActiveDescriptor<Object> activeDescriptor = ServiceLocatorUtilities
236-
.addOneDescriptor(locator, (org.glassfish.hk2.api.Descriptor) foreignDescriptor.get(), false);
237-
return new ForeignDescriptorImpl(activeDescriptor);
236+
.addOneDescriptor(locator, (Descriptor) foreignDescriptor.get(), false);
237+
return ForeignDescriptor.wrap(activeDescriptor, activeDescriptor::dispose);
238238
}
239239

240+
@SuppressWarnings("unchecked")
240241
private ForeignDescriptor createAndTranslateForeignDescriptor(Binding binding) {
241-
ActiveDescriptor<?> activeDescriptor;
242+
ActiveDescriptor activeDescriptor;
242243
if (ClassBinding.class.isAssignableFrom(binding.getClass())) {
243244
activeDescriptor = Hk2Helper.translateToActiveDescriptor((ClassBinding<?>) binding);
244245
} else if (InstanceBinding.class.isAssignableFrom(binding.getClass())) {
@@ -247,7 +248,7 @@ private ForeignDescriptor createAndTranslateForeignDescriptor(Binding binding) {
247248
throw new RuntimeException(LocalizationMessages.UNKNOWN_DESCRIPTOR_TYPE(binding.getClass().getSimpleName()));
248249
}
249250

250-
return new ForeignDescriptorImpl(activeDescriptor);
251+
return ForeignDescriptor.wrap(activeDescriptor, activeDescriptor::dispose);
251252
}
252253

253254
@Override

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ protected void configure() {
7272
new HK2InjectionManagerBinder(injectionManager),
7373
// Jersey-like class analyzer that is able to choose the right services' construtor.
7474
new JerseyClassAnalyzer.Binder(injectionManager),
75+
// Activate possibility to start Request Scope.
76+
new RequestContext.Binder(),
7577
// Add support for Context annotation.
7678
new ContextInjectionResolverImpl.Binder(),
7779
// Compose together the initialization binders and bind them as a whole.
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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 java.lang.annotation.Annotation;
44+
45+
import javax.inject.Inject;
46+
import javax.inject.Singleton;
47+
48+
import org.glassfish.jersey.internal.inject.AbstractBinder;
49+
import org.glassfish.jersey.internal.inject.ForeignDescriptor;
50+
import org.glassfish.jersey.process.internal.RequestScope;
51+
import org.glassfish.jersey.process.internal.RequestScoped;
52+
53+
import org.glassfish.hk2.api.ActiveDescriptor;
54+
import org.glassfish.hk2.api.Context;
55+
import org.glassfish.hk2.api.ServiceHandle;
56+
import org.glassfish.hk2.api.TypeLiteral;
57+
58+
/**
59+
* Class is able to communicate with {@link RequestScope} and provide request-scoped descriptors to HK2 DI provider to create or
60+
* destroy instances.
61+
*/
62+
@Singleton
63+
public class RequestContext implements Context<RequestScoped> {
64+
65+
private final RequestScope requestScope;
66+
67+
@Inject
68+
public RequestContext(RequestScope requestScope) {
69+
this.requestScope = requestScope;
70+
}
71+
72+
@Override
73+
public Class<? extends Annotation> getScope() {
74+
return RequestScoped.class;
75+
}
76+
77+
@Override
78+
public <U> U findOrCreate(ActiveDescriptor<U> activeDescriptor, ServiceHandle<?> root) {
79+
final RequestScope.Instance instance = requestScope.current();
80+
81+
U retVal = instance.get(ForeignDescriptor.wrap(activeDescriptor));
82+
if (retVal == null) {
83+
retVal = activeDescriptor.create(root);
84+
instance.put(ForeignDescriptor.wrap(activeDescriptor, obj -> activeDescriptor.dispose((U) obj)), retVal);
85+
}
86+
return retVal;
87+
}
88+
89+
@Override
90+
public boolean containsKey(ActiveDescriptor<?> descriptor) {
91+
RequestScope.Instance instance = requestScope.current();
92+
return instance.contains(ForeignDescriptor.wrap(descriptor));
93+
}
94+
95+
@Override
96+
public boolean supportsNullCreation() {
97+
return true;
98+
}
99+
100+
@Override
101+
public boolean isActive() {
102+
return requestScope.isActive();
103+
}
104+
105+
@Override
106+
public void destroyOne(ActiveDescriptor<?> descriptor) {
107+
RequestScope.Instance instance = requestScope.current();
108+
instance.remove(ForeignDescriptor.wrap(descriptor));
109+
}
110+
111+
@Override
112+
public void shutdown() {
113+
requestScope.shutdown();
114+
}
115+
116+
/**
117+
* Request scope injection binder.
118+
*/
119+
public static class Binder extends AbstractBinder {
120+
121+
@Override
122+
protected void configure() {
123+
bindAsContract(RequestContext.class)
124+
.to(new TypeLiteral<Context<RequestScoped>>() {}.getType())
125+
.in(Singleton.class);
126+
}
127+
}
128+
}

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040

4141
package org.glassfish.jersey.internal.inject;
4242

43+
import java.util.function.Consumer;
44+
4345
/**
4446
* The descriptor holder for an externally provided DI providers. Using this interface DI provider is able to provider his own
4547
* descriptor which can be used and returned to the DI provider in further processing.
@@ -56,4 +58,33 @@ public interface ForeignDescriptor {
5658
*/
5759
Object get();
5860

61+
/**
62+
* Disposes this instance. All the PerLookup objects that were created for this instance will be destroyed after this
63+
* object has been destroyed.
64+
*
65+
* @param instance The instance to destroy.
66+
*/
67+
void dispose(Object instance);
68+
69+
/**
70+
* Wraps incoming descriptor instance and provides a default implementation of {@link ForeignDescriptor}.
71+
*
72+
* @param descriptor incoming foreign descriptor.
73+
* @return wrapped foreign descriptor.
74+
*/
75+
static ForeignDescriptor wrap(Object descriptor) {
76+
return new ForeignDescriptorImpl(descriptor);
77+
}
78+
79+
/**
80+
* Wraps incoming descriptor instance and provides a default implementation of {@link ForeignDescriptor} along with a
81+
* {@link Consumer} for a disposing an instance created using a given descriptor.
82+
*
83+
* @param descriptor incoming foreign descriptor.
84+
* @param disposeInstance consumer which is able to dispose an instance created with the given descriptor.
85+
* @return wrapped foreign descriptor.
86+
*/
87+
static ForeignDescriptor wrap(Object descriptor, Consumer<Object> disposeInstance) {
88+
return new ForeignDescriptorImpl(descriptor, disposeInstance);
89+
}
5990
}

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

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040

4141
package org.glassfish.jersey.internal.inject;
4242

43+
import java.util.Objects;
44+
import java.util.function.Consumer;
45+
4346
/**
4447
* The descriptor holder for an externally provided DI providers. Using this interface DI provider is able to provider his own
4548
* descriptor which can be used and returned to the DI provider in further processing.
@@ -49,19 +52,55 @@
4952
*/
5053
public class ForeignDescriptorImpl implements ForeignDescriptor {
5154

52-
private Object foreignDescriptor;
55+
private static final Consumer<Object> NOOP_DISPOSE_INSTANCE = instance -> {};
56+
57+
private final Object foreignDescriptor;
58+
private final Consumer<Object> disposeInstance;
5359

5460
/**
5561
* Constructor accepts a descriptor of the DI provider and to be able to provide it in further processing.
5662
*
5763
* @param foreignDescriptor DI provider's descriptor.
5864
*/
5965
public ForeignDescriptorImpl(Object foreignDescriptor) {
66+
this(foreignDescriptor, NOOP_DISPOSE_INSTANCE);
67+
}
68+
69+
/**
70+
* Constructor accepts a descriptor of the DI provider and to be able to provide it in further processing along with
71+
* dispose mechanism to destroy the objects corresponding the given {@code foreign key}.
72+
*
73+
* @param foreignDescriptor DI provider's descriptor.
74+
*/
75+
public ForeignDescriptorImpl(Object foreignDescriptor, Consumer<Object> disposeInstance) {
6076
this.foreignDescriptor = foreignDescriptor;
77+
this.disposeInstance = disposeInstance;
6178
}
6279

6380
@Override
6481
public Object get() {
6582
return foreignDescriptor;
6683
}
84+
85+
@Override
86+
public void dispose(Object instance) {
87+
disposeInstance.accept(instance);
88+
}
89+
90+
@Override
91+
public boolean equals(final Object o) {
92+
if (this == o) {
93+
return true;
94+
}
95+
if (!(o instanceof ForeignDescriptorImpl)) {
96+
return false;
97+
}
98+
final ForeignDescriptorImpl that = (ForeignDescriptorImpl) o;
99+
return foreignDescriptor.equals(that.foreignDescriptor);
100+
}
101+
102+
@Override
103+
public int hashCode() {
104+
return foreignDescriptor.hashCode();
105+
}
67106
}

0 commit comments

Comments
 (0)