Skip to content

Commit

Permalink
Fix Container#reload
Browse files Browse the repository at this point in the history
Signed-off-by: jansupol <jan.supol@oracle.com>
  • Loading branch information
jansupol committed Oct 13, 2022
1 parent f2f95f9 commit 0c2f28a
Show file tree
Hide file tree
Showing 19 changed files with 148 additions and 53 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2022 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -370,7 +370,7 @@ public ResourceConfig getConfiguration() {

@Override
public void reload() {
reload(appHandler.getConfiguration());
reload(new ResourceConfig(appHandler.getConfiguration()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2022 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -201,7 +201,7 @@ public ResourceConfig getConfiguration() {

@Override
public void reload() {
reload(getConfiguration());
reload(new ResourceConfig(getConfiguration()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@ public ResourceConfig getConfiguration() {

@Override
public void reload() {
reload(getConfiguration());
reload(new ResourceConfig(getConfiguration()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2021 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2022 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -417,7 +417,7 @@ public ResourceConfig getConfiguration() {

@Override
public void reload() {
reload(getConfiguration());
reload(new ResourceConfig(getConfiguration()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2022 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -53,7 +53,7 @@ public ApplicationHandler getApplicationHandler() {

@Override
public void reload() {
reload(appHandler.getConfiguration());
reload(new ResourceConfig(appHandler.getConfiguration()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2022 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -407,7 +407,7 @@ public ResourceConfig getConfiguration() {

@Override
public void reload() {
reload(getConfiguration());
reload(new ResourceConfig(getConfiguration()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2021 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2022 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -50,6 +50,12 @@ public interface InjectionManager {
*/
void shutdown();

/**
* Returns {@code true} when the {@link InjectionManager} has been shutdown, {@code false} otherwise.
* @return Whether the {@code InjectionManager} has been shutdown.
*/
boolean isShutdown();

/**
* Registers one bean represented using fields in the provided descriptor. The final bean can be direct bean or
* factory object which will create the bean at the time of injection. {@code InjectionManager} is able to register a bean
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2021 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2022 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -231,43 +231,50 @@ public void process(final ContainerRequest request) {
requestScopeInstance, externalRequestScope.open(injectionManager));
context.initAsyncContext(asyncResponderHolder);

requestScope.runInScope(requestScopeInstance, new Runnable() {
@Override
public void run() {
try {
// set base URI into response builder thread-local variable
// for later resolving of relative location URIs
if (!disableLocationHeaderRelativeUriResolution) {
final URI uriToUse =
rfc7231LocationHeaderRelativeUriResolution ? request.getRequestUri() : request.getBaseUri();
OutboundJaxrsResponse.Builder.setBaseUri(uriToUse);
}
try {
requestScope.runInScope(requestScopeInstance, new Runnable() {
@Override
public void run() {
try {
// set base URI into response builder thread-local variable
// for later resolving of relative location URIs
if (!disableLocationHeaderRelativeUriResolution) {
final URI uriToUse =
rfc7231LocationHeaderRelativeUriResolution ? request.getRequestUri() : request.getBaseUri();
OutboundJaxrsResponse.Builder.setBaseUri(uriToUse);
}

final Ref<Endpoint> endpointRef = Refs.emptyRef();
final RequestProcessingContext data = Stages.process(context, requestProcessingRoot, endpointRef);
final Ref<Endpoint> endpointRef = Refs.emptyRef();
final RequestProcessingContext data = Stages.process(context, requestProcessingRoot, endpointRef);

final Endpoint endpoint = endpointRef.get();
if (endpoint == null) {
// not found
throw new NotFoundException();
}
final Endpoint endpoint = endpointRef.get();
if (endpoint == null) {
// not found
throw new NotFoundException();
}

final ContainerResponse response = endpoint.apply(data);
final ContainerResponse response = endpoint.apply(data);

if (!asyncResponderHolder.isAsync()) {
responder.process(response);
} else {
externalRequestScope.suspend(asyncResponderHolder.externalContext, injectionManager);
if (!asyncResponderHolder.isAsync()) {
responder.process(response);
} else {
externalRequestScope.suspend(asyncResponderHolder.externalContext, injectionManager);
}
} catch (final Throwable throwable) {
responder.process(throwable);
} finally {
asyncResponderHolder.release();
// clear base URI from the thread
OutboundJaxrsResponse.Builder.clearBaseUri();
}
} catch (final Throwable throwable) {
responder.process(throwable);
} finally {
asyncResponderHolder.release();
// clear base URI from the thread
OutboundJaxrsResponse.Builder.clearBaseUri();
}
});
} catch (RuntimeException illegalStateException) {
if (!IllegalStateException.class.isInstance(illegalStateException.getCause()) || !injectionManager.isShutdown()) {
// consume the IllegalStateException: InjectionManager has been closed.
throw illegalStateException;
}
});
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ public InjectionManagerInjectedCdiTarget(InjectionTarget delegate) {
@Override
public void inject(final Object t, final CreationalContext cc) {
InjectionManager injectingManager = getEffectiveInjectionManager();
if (injectingManager == null) {
if (injectingManager == null || /* reload */ injectingManager.isShutdown()) {
injectingManager = effectiveInjectionManager;
threadInjectionManagers.set(injectingManager);
}
Expand Down Expand Up @@ -737,7 +737,7 @@ public boolean isNullable() {
@Override
public Object create(final CreationalContext creationalContext) {
InjectionManager injectionManager = getEffectiveInjectionManager();
if (injectionManager == null) {
if (injectionManager == null || /* reload */ injectionManager.isShutdown()) {
injectionManager = threadInjectionManagers.get();
}

Expand Down
1 change: 1 addition & 0 deletions ext/microprofile/mp-rest-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
<Import-Package>
${cdi.osgi.version},
org.eclipse.microprofile.rest.client.*;version="[1,3)",
org.eclipse.microprofile.config;version="!",
*
</Import-Package>
</instructions>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2022 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -597,6 +597,11 @@ public void shutdown() {
//noop
}

@Override
public boolean isShutdown() {
return false;
}

@Override
public void register(Binding binding) {
BinderRegisterExtension.this.register(runtimeType, binding);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2022 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -333,6 +333,11 @@ public void shutdown() {

}

@Override
public boolean isShutdown() {
return false;
}

protected Binder getBindings() {
return bindings;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2022 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -50,6 +50,11 @@ public void shutdown() {
injectionManager.shutdown();
}

@Override
public boolean isShutdown() {
return injectionManager.isShutdown();
}

@Override
public void register(Binding binding) {
injectionManager.register(binding);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2021 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2022 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -289,6 +289,11 @@ public void shutdown() {
}
}

@Override
public boolean isShutdown() {
return !container.isRunning();
}

@Override
public void inject(Object injectMe, String classAnalyzer) {
// TODO: Used only in legacy CDI integration.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2021 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2022 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -51,6 +51,7 @@ abstract class AbstractHk2InjectionManager implements InjectionManager {
private static final ServiceLocatorFactory factory = ServiceLocatorFactory.getInstance();

private ServiceLocator locator;
private Boolean isShutdown = Boolean.FALSE; // TODO replace by getServiceLocator().isShutDown() in 3.x

/**
* Private constructor.
Expand Down Expand Up @@ -182,6 +183,12 @@ public void shutdown() {
} else {
getServiceLocator().shutdown();
}
isShutdown = Boolean.TRUE;
}

@Override
public boolean isShutdown() {
return isShutdown;
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.2</version>
<version>3.3.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,42 @@

package org.glassfish.jersey.tests.cdi.gf;

import org.glassfish.jersey.CommonProperties;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.spi.Container;
import org.glassfish.jersey.server.spi.ContainerLifecycleListener;

import javax.ws.rs.ApplicationPath;

@ApplicationPath("/test")
public class GFTestApp extends ResourceConfig {
public static final String RELOADER = "RELOADER";
private Reloader reloader = new Reloader();

public GFTestApp() {
super(GFTestResource.class);
register(reloader);

property(CommonProperties.PROVIDER_DEFAULT_DISABLE, "ALL");
property(RELOADER, reloader);
}

static class Reloader implements ContainerLifecycleListener {
Container container;

@Override
public void onStartup(Container container) {
this.container = container;
}

@Override
public void onReload(Container container) {

}

@Override
public void onShutdown(Container container) {

}
}
}
Loading

0 comments on commit 0c2f28a

Please sign in to comment.