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

Commit a7b449e

Browse files
author
Petr Janouch
committed
JERSEY-1982: @PreDestroy not invoked on Features
Change-Id: I45048131c989dc9adef7f855ec671345f3c40adf
1 parent 3ba648c commit a7b449e

File tree

4 files changed

+150
-2
lines changed

4 files changed

+150
-2
lines changed

core-common/src/main/java/org/glassfish/jersey/model/internal/CommonConfig.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
import javax.ws.rs.core.FeatureContext;
6363

6464
import javax.annotation.Priority;
65+
import javax.inject.Singleton;
6566

6667
import org.glassfish.jersey.ExtendedConfig;
6768
import org.glassfish.jersey.internal.LocalizationMessages;
@@ -77,6 +78,7 @@
7778
import org.glassfish.hk2.api.DynamicConfiguration;
7879
import org.glassfish.hk2.api.ServiceLocator;
7980
import org.glassfish.hk2.utilities.Binder;
81+
import org.glassfish.hk2.utilities.binding.ScopedBindingBuilder;
8082

8183
import jersey.repackaged.com.google.common.base.Function;
8284
import jersey.repackaged.com.google.common.base.Predicate;
@@ -640,6 +642,8 @@ public void configureMetaProviders(final ServiceLocator locator) {
640642

641643
// Check whether meta providers have been initialized for a config this config has been loaded from.
642644
if (!disableMetaProviderConfiguration) {
645+
646+
registerManagedObjectsFinalizer(locator);
643647
// Next, configure all features
644648
configureFeatures(
645649
locator,
@@ -651,6 +655,15 @@ public void configureMetaProviders(final ServiceLocator locator) {
651655
}
652656
}
653657

658+
private void registerManagedObjectsFinalizer(ServiceLocator locator) {
659+
DynamicConfiguration dc = Injections.getConfiguration(locator);
660+
ScopedBindingBuilder<ManagedObjectsFinalizer> binder = Injections.newBinder(ManagedObjectsFinalizer.class)
661+
.to(ManagedObjectsFinalizer.class)
662+
.in(Singleton.class);
663+
Injections.addBinding(binder, dc);
664+
dc.commit();
665+
}
666+
654667
private Set<Binder> configureBinders(final ServiceLocator locator, final Set<Binder> configured) {
655668
final Set<Binder> allConfigured = Sets.newIdentityHashSet();
656669
allConfigured.addAll(configured);
@@ -684,6 +697,8 @@ private void configureFeatures(final ServiceLocator locator,
684697
final Set<FeatureRegistration> processed,
685698
final List<FeatureRegistration> unprocessed) {
686699

700+
ManagedObjectsFinalizer managedObjectsFinalizer = locator.getService(ManagedObjectsFinalizer.class);
701+
687702
FeatureContextWrapper featureContextWrapper = null;
688703
for (final FeatureRegistration registration : unprocessed) {
689704
if (processed.contains(registration)) {
@@ -694,6 +709,7 @@ private void configureFeatures(final ServiceLocator locator,
694709
Feature feature = registration.getFeature();
695710
if (feature == null) {
696711
feature = locator.createAndInitialize(registration.getFeatureClass());
712+
managedObjectsFinalizer.registerForPreDestroyCall(feature);
697713
} else {
698714
// Disable injection of Feature instances on the client-side. Instances may be registered into multiple
699715
// web-targets which means that injecting anything into these instances is not safe.
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2015 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.model.internal;
42+
43+
import java.util.HashSet;
44+
import java.util.Set;
45+
46+
import javax.annotation.PreDestroy;
47+
import javax.inject.Inject;
48+
import javax.inject.Singleton;
49+
50+
import org.glassfish.hk2.api.ServiceLocator;
51+
52+
/**
53+
* Invokes {@link PreDestroy} methods on all registered objects, when the service locator is shut down.
54+
* <p/>
55+
* Some objects managed by Jersey are created using {@link ServiceLocator#createAndInitialize}. This means
56+
* that such objects are created, dependencies injected and methods annotated with {@link javax.annotation.PostConstruct}
57+
* invoked. Therefore methods annotated with {@link PreDestroy} should be invoked on such objects too, when they are destroyed.
58+
* <p/>
59+
* This service invokes {@link PreDestroy} on all registered objects when {@link ServiceLocator#shutdown()} is invoked
60+
* on the service locator where this service is registered. Therefore only classes with their lifecycle linked
61+
* to the service locator that created them should be registered here.
62+
*
63+
* @author Petr Janouch (petr.janouch at oracle.com)
64+
*/
65+
@Singleton
66+
public class ManagedObjectsFinalizer {
67+
68+
@Inject
69+
private ServiceLocator serviceLocator;
70+
71+
private final Set<Object> managedObjects = new HashSet<Object>();
72+
73+
/**
74+
* Register an object for invocation of its {@link PreDestroy} method.
75+
* It will be invoked when the service locator is shut down.
76+
*
77+
* @param object an object to be registered.
78+
*/
79+
public void registerForPreDestroyCall(Object object) {
80+
managedObjects.add(object);
81+
}
82+
83+
@PreDestroy
84+
public void preDestroy() {
85+
try {
86+
for (Object o : managedObjects) {
87+
serviceLocator.preDestroy(o);
88+
}
89+
90+
} finally {
91+
managedObjects.clear();
92+
}
93+
}
94+
}

tests/e2e/src/test/java/org/glassfish/jersey/tests/e2e/client/ClientDestroyTest.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@
5353
import javax.ws.rs.client.ClientRequestContext;
5454
import javax.ws.rs.client.ClientRequestFilter;
5555
import javax.ws.rs.core.Application;
56+
import javax.ws.rs.core.Feature;
57+
import javax.ws.rs.core.FeatureContext;
5658
import javax.ws.rs.ext.ReaderInterceptor;
5759
import javax.ws.rs.ext.ReaderInterceptorContext;
5860

@@ -86,6 +88,7 @@ public void setUp() throws Exception {
8688
destroyed.clear();
8789
destroyed.put("filter", false);
8890
destroyed.put("reader", false);
91+
destroyed.put("feature", false);
8992

9093
super.setUp();
9194
}
@@ -126,11 +129,25 @@ public void preDestroy() {
126129
}
127130
}
128131

132+
public static class MyFeature implements Feature {
133+
134+
@PreDestroy
135+
public void preDestroy() {
136+
destroyed.put("feature", true);
137+
}
138+
139+
@Override
140+
public boolean configure(final FeatureContext context) {
141+
return true;
142+
}
143+
}
144+
129145
@Test
130146
public void testClientInvokePreDestroyMethodOnProviderClass() throws Exception {
131147
final Client client = ClientBuilder.newClient()
132148
.register(MyFilter.class)
133-
.register(MyReader.class);
149+
.register(MyReader.class)
150+
.register(MyFeature.class);
134151

135152
assertThat(client.target(getBaseUri()).request().get(String.class), is("reader-resource-bar"));
136153

tests/e2e/src/test/java/org/glassfish/jersey/tests/e2e/server/ServerDestroyTest.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@
5151
import javax.ws.rs.container.ContainerResponseContext;
5252
import javax.ws.rs.container.ContainerResponseFilter;
5353
import javax.ws.rs.core.Application;
54+
import javax.ws.rs.core.Feature;
55+
import javax.ws.rs.core.FeatureContext;
5456
import javax.ws.rs.core.Response;
5557
import javax.ws.rs.ext.WriterInterceptor;
5658
import javax.ws.rs.ext.WriterInterceptorContext;
@@ -97,6 +99,7 @@ public void setUp() throws Exception {
9799
destroyed.put("filter", false);
98100
destroyed.put("writer", false);
99101
destroyed.put("singleton-factory", false);
102+
destroyed.put("feature", false);
100103

101104
super.setUp();
102105
}
@@ -153,7 +156,12 @@ public void preDestroy() {
153156

154157
@Override
155158
public Set<Class<?>> getClasses() {
156-
return Sets.<Class<?>>newHashSet(Resource.class, MyFilter.class, MyWriter.class, MyContainerLifecycleListener.class);
159+
return Sets.<Class<?>>newHashSet(
160+
Resource.class,
161+
MyFilter.class,
162+
MyWriter.class,
163+
MyContainerLifecycleListener.class,
164+
MyFeature.class);
157165
}
158166

159167
@Override
@@ -214,6 +222,19 @@ public void onShutdown(final Container container) {
214222
}
215223
}
216224

225+
public static class MyFeature implements Feature {
226+
227+
@PreDestroy
228+
public void preDestroy() {
229+
destroyed.put("feature", true);
230+
}
231+
232+
@Override
233+
public boolean configure(final FeatureContext context) {
234+
return true;
235+
}
236+
}
237+
217238
@Override
218239
protected DeploymentContext configureDeployment() {
219240
reloader = new Reloader();

0 commit comments

Comments
 (0)